1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }