View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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.*;
19  
20  import java.io.Serializable;
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  
24  import net.sf.cglib.proxy.Factory;
25  
26  import org.apache.ibatis.domain.blog.Author;
27  import org.apache.ibatis.executor.ExecutorException;
28  import org.apache.ibatis.executor.loader.cglib.CglibProxyFactory;
29  import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
30  import org.apache.ibatis.session.Configuration;
31  import org.junit.jupiter.api.Assertions;
32  import org.junit.jupiter.api.BeforeAll;
33  import org.junit.jupiter.api.Tag;
34  import org.junit.jupiter.api.Test;
35  
36  @Tag("RequireIllegalAccess")
37  class CglibProxyTest extends SerializableProxyTest {
38  
39    @BeforeAll
40    static void createProxyFactory() {
41      proxyFactory = new CglibProxyFactory();
42    }
43  
44    @Test
45    void shouldCreateAProxyForAPartiallyLoadedBean() throws Exception {
46      ResultLoaderMap loader = new ResultLoaderMap();
47      loader.addLoader("id", null, null);
48      Object proxy = proxyFactory.createProxy(author, loader, new Configuration(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
49      Author author2 = (Author) deserialize(serialize((Serializable) proxy));
50      assertTrue(author2 instanceof Factory);
51    }
52  
53    @Test
54    void shouldFailCallingAnUnloadedProperty() {
55      // yes, it must go in uppercase
56      HashMap<String, ResultLoaderMap.LoadPair> unloadedProperties = new HashMap<>();
57      unloadedProperties.put("ID", null);
58      Author author2 = (Author) ((CglibProxyFactory)proxyFactory).createDeserializationProxy(author, unloadedProperties, new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
59      Assertions.assertThrows(ExecutorException.class, author2::getId);
60    }
61  
62    @Test
63    void shouldLetCallALoadedProperty() {
64      Author author2 = (Author) ((CglibProxyFactory)proxyFactory).createDeserializationProxy(author, new HashMap<>(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
65      assertEquals(999, author2.getId());
66    }
67  
68    @Test
69    void shouldSerizalizeADeserlizaliedProxy() throws Exception {
70      Object proxy = ((CglibProxyFactory)proxyFactory).createDeserializationProxy(author, new HashMap<>(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
71      Author author2 = (Author) deserialize(serialize((Serializable) proxy));
72      assertEquals(author, author2);
73      assertNotEquals(author.getClass(), author2.getClass());
74    }
75  
76  }