View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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.*;
19  
20  import java.io.Serializable;
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  
24  import javassist.util.proxy.Proxy;
25  
26  import org.apache.ibatis.domain.blog.Author;
27  import org.apache.ibatis.executor.ExecutorException;
28  import org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory;
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 JavassistProxyTest extends SerializableProxyTest {
36  
37    @BeforeAll
38    static void createProxyFactory() {
39      proxyFactory = new JavassistProxyFactory();
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 author2 = (Author) deserialize(serialize((Serializable) proxy));
48      assertTrue(author2 instanceof Proxy);
49    }
50  
51    @Test
52    void shouldFailCallingAnUnloadedProperty() {
53      // yes, it must go in uppercase
54      HashMap<String, ResultLoaderMap.LoadPair> unloadedProperties = new HashMap<> ();
55      unloadedProperties.put("ID", null);
56      Author author2 = (Author) ((JavassistProxyFactory)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 author2 = (Author) ((JavassistProxyFactory)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 = ((JavassistProxyFactory)proxyFactory).createDeserializationProxy(author, new HashMap<> (), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
69      Author author2 = (Author) deserialize(serialize((Serializable) proxy));
70      assertEquals(author, author2);
71      assertNotEquals(author.getClass(), author2.getClass());
72    }
73  
74  }