View Javadoc
1   /*
2    *    Copyright 2009-2024 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.assertFalse;
20  import static org.junit.jupiter.api.Assertions.fail;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.ObjectInputStream;
25  import java.io.ObjectOutputStream;
26  import java.io.ObjectStreamException;
27  import java.io.Serializable;
28  import java.util.ArrayList;
29  
30  import org.apache.ibatis.domain.blog.Author;
31  import org.apache.ibatis.domain.blog.Section;
32  import org.apache.ibatis.executor.ExecutorException;
33  import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
34  import org.apache.ibatis.session.Configuration;
35  import org.junit.jupiter.api.Assertions;
36  import org.junit.jupiter.api.Test;
37  
38  public abstract class SerializableProxyTest {
39  
40    protected Author author = new Author(999, "someone", "!@#@!#!@#", "someone@somewhere.com", "blah", Section.NEWS);
41  
42    static ProxyFactory proxyFactory;
43  
44    @Test
45    void shouldKeepGenericTypes() {
46      for (int i = 0; i < 10000; i++) {
47        Author pc = new Author();
48        Author proxy = (Author) proxyFactory.createProxy(pc, new ResultLoaderMap(), new Configuration(),
49            new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
50        proxy.getBio();
51      }
52    }
53  
54    @Test
55    void shouldSerializeAProxyForABeanWithDefaultConstructor() throws Exception {
56      Object proxy = proxyFactory.createProxy(author, new ResultLoaderMap(), new Configuration(),
57          new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
58      Object proxy2 = deserialize(serialize((Serializable) proxy));
59      assertEquals(author, proxy2);
60    }
61  
62    @Test
63    void shouldSerializeAProxyForABeanWithoutDefaultConstructor() throws Exception {
64      AuthorWithoutDefaultConstructor author = new AuthorWithoutDefaultConstructor(999, "someone", "!@#@!#!@#",
65          "someone@somewhere.com", "blah", Section.NEWS);
66      ArrayList<Class<?>> argTypes = new ArrayList<>();
67      argTypes.add(Integer.class);
68      argTypes.add(String.class);
69      argTypes.add(String.class);
70      argTypes.add(String.class);
71      argTypes.add(String.class);
72      argTypes.add(Section.class);
73      ArrayList<Object> argValues = new ArrayList<>();
74      argValues.add(999);
75      argValues.add("someone");
76      argValues.add("!@#@!#!@#");
77      argValues.add("someone@somewhere.com");
78      argValues.add("blah");
79      argValues.add(Section.NEWS);
80      Object proxy = proxyFactory.createProxy(author, new ResultLoaderMap(), new Configuration(),
81          new DefaultObjectFactory(), argTypes, argValues);
82      Object proxy2 = deserialize(serialize((Serializable) proxy));
83      assertEquals(author, proxy2);
84    }
85  
86    @Test
87    void shouldSerializeAProxyForABeanWithoutDefaultConstructorAndUnloadedProperties() throws Exception {
88      AuthorWithoutDefaultConstructor author = new AuthorWithoutDefaultConstructor(999, "someone", "!@#@!#!@#",
89          "someone@somewhere.com", "blah", Section.NEWS);
90      ArrayList<Class<?>> argTypes = new ArrayList<>();
91      argTypes.add(Integer.class);
92      argTypes.add(String.class);
93      argTypes.add(String.class);
94      argTypes.add(String.class);
95      argTypes.add(String.class);
96      argTypes.add(Section.class);
97      ArrayList<Object> argValues = new ArrayList<>();
98      argValues.add(999);
99      argValues.add("someone");
100     argValues.add("!@#@!#!@#");
101     argValues.add("someone@somewhere.com");
102     argValues.add("blah");
103     argValues.add(Section.NEWS);
104     ResultLoaderMap loader = new ResultLoaderMap();
105     loader.addLoader("id", null, null);
106     Object proxy = proxyFactory.createProxy(author, loader, new Configuration(), new DefaultObjectFactory(), argTypes,
107         argValues);
108     Object proxy2 = deserialize(serialize((Serializable) proxy));
109     assertEquals(author, proxy2);
110   }
111 
112   @Test
113   void shouldSerizaliceAFullLoadedObjectToOriginalClass() throws Exception {
114     Object proxy = proxyFactory.createProxy(author, new ResultLoaderMap(), new Configuration(),
115         new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
116     Object proxy2 = deserialize(serialize((Serializable) proxy));
117     assertEquals(author.getClass(), proxy2.getClass());
118   }
119 
120   @Test
121   void shouldGenerateWriteReplace() throws Exception {
122     try {
123       author.getClass().getDeclaredMethod("writeReplace");
124       fail("Author should not have a writeReplace method");
125     } catch (NoSuchMethodException e) {
126       // ok
127     }
128     Object proxy = proxyFactory.createProxy(author, new ResultLoaderMap(), new Configuration(),
129         new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
130     proxy.getClass().getDeclaredMethod("writeReplace");
131   }
132 
133   @Test
134   void shouldNotGenerateWriteReplaceItThereIsAlreadyOne() {
135     AuthorWithWriteReplaceMethod beanWithWriteReplace = new AuthorWithWriteReplaceMethod(999, "someone", "!@#@!#!@#",
136         "someone@somewhere.com", "blah", Section.NEWS);
137     Assertions.assertDoesNotThrow(() -> {
138       beanWithWriteReplace.getClass().getDeclaredMethod("writeReplace");
139     }, "Bean should declare a writeReplace method");
140     Object proxy = proxyFactory.createProxy(beanWithWriteReplace, new ResultLoaderMap(), new Configuration(),
141         new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
142     Class<?>[] interfaces = proxy.getClass().getInterfaces();
143     boolean ownInterfaceFound = false;
144     for (Class<?> i : interfaces) {
145       if (i.equals(WriteReplaceInterface.class)) {
146         ownInterfaceFound = true;
147         break;
148       }
149     }
150     assertFalse(ownInterfaceFound);
151   }
152 
153   @Test
154   void shouldNotCreateAProxyForAFullyLoadedBean() throws Exception {
155     Object proxy = proxyFactory.createProxy(author, new ResultLoaderMap(), new Configuration(),
156         new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
157     Author author2 = (Author) deserialize(serialize((Serializable) proxy));
158     assertEquals(author.getClass(), author2.getClass());
159   }
160 
161   @Test
162   void shouldNotLetReadUnloadedPropertyAfterSerialization() throws Exception {
163     ResultLoaderMap loader = new ResultLoaderMap();
164     loader.addLoader("id", null, null);
165     Object proxy = proxyFactory.createProxy(author, loader, new Configuration(), new DefaultObjectFactory(),
166         new ArrayList<>(), new ArrayList<>());
167     Author author2 = (Author) deserialize(serialize((Serializable) proxy));
168     Assertions.assertThrows(ExecutorException.class, author2::getId);
169   }
170 
171   @Test
172   void shouldNotLetReadUnloadedPropertyAfterTwoSerializations() throws Exception {
173     ResultLoaderMap loader = new ResultLoaderMap();
174     loader.addLoader("id", null, null);
175     Object proxy = proxyFactory.createProxy(author, loader, new Configuration(), new DefaultObjectFactory(),
176         new ArrayList<>(), new ArrayList<>());
177     Author author2 = (Author) deserialize(serialize(deserialize(serialize((Serializable) proxy))));
178     Assertions.assertThrows(ExecutorException.class, author2::getId);
179   }
180 
181   @Test
182   void shouldLetReadALoadedPropertyAfterSerialization() throws Exception {
183     Object proxy = proxyFactory.createProxy(author, new ResultLoaderMap(), new Configuration(),
184         new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>());
185     byte[] ser = serialize((Serializable) proxy);
186     Author author2 = (Author) deserialize(ser);
187     assertEquals(999, author2.getId());
188   }
189 
190   byte[] serialize(Serializable value) throws Exception {
191     try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
192         ObjectOutputStream oos = new ObjectOutputStream(bos)) {
193       oos.writeObject(value);
194       oos.flush();
195       return bos.toByteArray();
196     }
197   }
198 
199   Serializable deserialize(byte[] value) throws Exception {
200     try (ByteArrayInputStream bis = new ByteArrayInputStream(value);
201         ObjectInputStream ois = new ObjectInputStream(bis)) {
202       return (Serializable) ois.readObject();
203     }
204   }
205 
206   public static class AuthorWithWriteReplaceMethod extends Author {
207 
208     public AuthorWithWriteReplaceMethod() {
209     }
210 
211     AuthorWithWriteReplaceMethod(Integer id, String username, String password, String email, String bio,
212         Section section) {
213       super(id, username, password, email, bio, section);
214     }
215 
216     Object writeReplace() throws ObjectStreamException {
217       return this;
218     }
219   }
220 
221   public static class AuthorWithoutDefaultConstructor extends Author {
222 
223     AuthorWithoutDefaultConstructor(Integer id, String username, String password, String email, String bio,
224         Section section) {
225       super(id, username, password, email, bio, section);
226     }
227 
228     protected Object writeReplace() throws ObjectStreamException {
229       return this;
230     }
231   }
232 
233 }