View Javadoc
1   /*
2    *    Copyright 2009-2023 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    *       https://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.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      // yes, it must go in uppercase
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  }