View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.executor.loader;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNotEquals;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  
26  import net.sf.cglib.proxy.Factory;
27  
28  import org.apache.ibatis.domain.blog.Author;
29  import org.apache.ibatis.executor.ExecutorException;
30  import org.apache.ibatis.executor.loader.cglib.CglibProxyFactory;
31  import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
32  import org.apache.ibatis.session.Configuration;
33  import org.junit.jupiter.api.Assertions;
34  import org.junit.jupiter.api.BeforeAll;
35  import org.junit.jupiter.api.Tag;
36  import org.junit.jupiter.api.Test;
37  
38  @Tag("RequireIllegalAccess")
39  class CglibProxyTest extends SerializableProxyTest {
40  
41    @BeforeAll
42    static void createProxyFactory() {
43      proxyFactory = new CglibProxyFactory();
44    }
45  
46    @Test
47    void shouldCreateAProxyForAPartiallyLoadedBean() throws Exception {
48      ResultLoaderMap loader = new ResultLoaderMap();
49      loader.addLoader("id", null, null);
50      Object proxy = proxyFactory.createProxy(author, loader, new Configuration(), new DefaultObjectFactory(),
51          new ArrayList<>(), new ArrayList<>());
52      Author author2 = (Author) deserialize(serialize((Serializable) proxy));
53      assertTrue(author2 instanceof Factory);
54    }
55  
56    @Test
57    void shouldFailCallingAnUnloadedProperty() {
58      // yes, it must go in uppercase
59      HashMap<String, ResultLoaderMap.LoadPair> unloadedProperties = new HashMap<>();
60      unloadedProperties.put("ID", null);
61      Author author2 = (Author) ((CglibProxyFactory) proxyFactory).createDeserializationProxy(author, unloadedProperties,
62          new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
63      Assertions.assertThrows(ExecutorException.class, author2::getId);
64    }
65  
66    @Test
67    void shouldLetCallALoadedProperty() {
68      Author author2 = (Author) ((CglibProxyFactory) proxyFactory).createDeserializationProxy(author, new HashMap<>(),
69          new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
70      assertEquals(999, author2.getId());
71    }
72  
73    @Test
74    void shouldSerizalizeADeserlizaliedProxy() throws Exception {
75      Object proxy = ((CglibProxyFactory) proxyFactory).createDeserializationProxy(author, new HashMap<>(),
76          new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
77      Author author2 = (Author) deserialize(serialize((Serializable) proxy));
78      assertEquals(author, author2);
79      assertNotEquals(author.getClass(), author2.getClass());
80    }
81  
82  }