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.*;
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
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 }