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 javassist.util.proxy.Proxy;
27
28 import org.apache.ibatis.domain.blog.Author;
29 import org.apache.ibatis.executor.ExecutorException;
30 import org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory;
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.Test;
36
37 class JavassistProxyTest extends SerializableProxyTest {
38
39 @BeforeAll
40 static void createProxyFactory() {
41 proxyFactory = new JavassistProxyFactory();
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(),
49 new ArrayList<>(), new ArrayList<>());
50 Author author2 = (Author) deserialize(serialize((Serializable) proxy));
51 assertTrue(author2 instanceof Proxy);
52 }
53
54 @Test
55 void shouldFailCallingAnUnloadedProperty() {
56
57 HashMap<String, ResultLoaderMap.LoadPair> unloadedProperties = new HashMap<>();
58 unloadedProperties.put("ID", null);
59 Author author2 = (Author) ((JavassistProxyFactory) proxyFactory).createDeserializationProxy(author,
60 unloadedProperties, new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
61 Assertions.assertThrows(ExecutorException.class, author2::getId);
62 }
63
64 @Test
65 void shouldLetCallALoadedProperty() {
66 Author author2 = (Author) ((JavassistProxyFactory) proxyFactory).createDeserializationProxy(author, new HashMap<>(),
67 new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
68 assertEquals(999, author2.getId());
69 }
70
71 @Test
72 void shouldSerizalizeADeserlizaliedProxy() throws Exception {
73 Object proxy = ((JavassistProxyFactory) proxyFactory).createDeserializationProxy(author, new HashMap<>(),
74 new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
75 Author author2 = (Author) deserialize(serialize((Serializable) proxy));
76 assertEquals(author, author2);
77 assertNotEquals(author.getClass(), author2.getClass());
78 }
79
80 }