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.Test;
34
35 class CglibProxyTest extends SerializableProxyTest {
36
37 @BeforeAll
38 static void createProxyFactory() {
39 proxyFactory = new CglibProxyFactory();
40 }
41
42 @Test
43 void shouldCreateAProxyForAPartiallyLoadedBean() throws Exception {
44 ResultLoaderMap loader = new ResultLoaderMap();
45 loader.addLoader("id", null, null);
46 Object proxy = proxyFactory.createProxy(author, loader, new Configuration(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
47 Author./../../../../org/apache/ibatis/domain/blog/Author.html#Author">Author author2 = (Author) deserialize(serialize((Serializable) proxy));
48 assertTrue(author2 instanceof Factory);
49 }
50
51 @Test
52 void shouldFailCallingAnUnloadedProperty() {
53
54 HashMap<String, ResultLoaderMap.LoadPair> unloadedProperties = new HashMap<>();
55 unloadedProperties.put("ID", null);
56 Author./../../../../org/apache/ibatis/domain/blog/Author.html#Author">Author author2 = (Author) ((CglibProxyFactory)proxyFactory).createDeserializationProxy(author, unloadedProperties, new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
57 Assertions.assertThrows(ExecutorException.class, author2::getId);
58 }
59
60 @Test
61 void shouldLetCallALoadedProperty() {
62 Author./../../../../org/apache/ibatis/domain/blog/Author.html#Author">Author author2 = (Author) ((CglibProxyFactory)proxyFactory).createDeserializationProxy(author, new HashMap<>(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
63 assertEquals(999, author2.getId());
64 }
65
66 @Test
67 void shouldSerizalizeADeserlizaliedProxy() throws Exception {
68 Object proxy = ((CglibProxyFactory)proxyFactory).createDeserializationProxy(author, new HashMap<>(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
69 Author./../../../../org/apache/ibatis/domain/blog/Author.html#Author">Author author2 = (Author) deserialize(serialize((Serializable) proxy));
70 assertEquals(author, author2);
71 assertNotEquals(author.getClass(), author2.getClass());
72 }
73
74 }