View Javadoc
1   /*
2    *    Copyright 2009-2022 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.submitted.serializecircular;
17  
18  import java.io.Reader;
19  
20  import org.apache.ibatis.BaseDataTest;
21  import org.apache.ibatis.io.Resources;
22  import org.apache.ibatis.session.SqlSession;
23  import org.apache.ibatis.session.SqlSessionFactory;
24  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
25  import org.junit.jupiter.api.Test;
26  
27  class SerializeCircularTest {
28  
29    @Test
30    void serializeAndDeserializeObjectsWithAggressiveLazyLoadingWithoutPreloadingAttribute() throws Exception {
31      try (SqlSession sqlSession = createSessionWithAggressiveLazyLoading()) {
32        testSerializeWithoutPreloadingAttribute(sqlSession);
33      }
34    }
35  
36    @Test
37    void serializeAndDeserializeObjectsWithAggressiveLazyLoadingWithPreloadingAttribute() throws Exception {
38      try (SqlSession sqlSession = createSessionWithAggressiveLazyLoading()) {
39        testSerializeWithPreloadingAttribute(sqlSession);
40      }
41    }
42  
43    @Test
44    void serializeAndDeserializeObjectsWithoutAggressiveLazyLoadingWithoutPreloadingAttribute() throws Exception {
45      try (SqlSession sqlSession = createSessionWithoutAggressiveLazyLoading()) {
46        // expected problem with deserializing
47        testSerializeWithoutPreloadingAttribute(sqlSession);
48      }
49    }
50  
51    @Test
52    void serializeAndDeserializeObjectsWithoutAggressiveLazyLoadingWithPreloadingAttribute() throws Exception {
53      try (SqlSession sqlSession = createSessionWithoutAggressiveLazyLoading()) {
54        testSerializeWithPreloadingAttribute(sqlSession);
55      }
56    }
57  
58    private SqlSession createSessionWithoutAggressiveLazyLoading() throws Exception {
59      return createSession(false);
60    }
61  
62    private SqlSession createSessionWithAggressiveLazyLoading() throws Exception {
63      return createSession(true);
64    }
65  
66    private SqlSession createSession(boolean anAggressiveLazyLoading) throws Exception {
67      String xmlConfig = anAggressiveLazyLoading
68          ? "org/apache/ibatis/submitted/serializecircular/MapperConfigWithAggressiveLazyLoading.xml"
69          : "org/apache/ibatis/submitted/serializecircular/MapperConfigWithoutAggressiveLazyLoading.xml";
70      SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig);
71      return sqlSessionFactory.openSession();
72    }
73  
74    private void testSerializeWithPreloadingAttribute(SqlSession sqlSession) {
75      testSerialize(sqlSession, true);
76    }
77  
78    private void testSerializeWithoutPreloadingAttribute(SqlSession sqlSession) {
79      testSerialize(sqlSession, false);
80    }
81  
82    private void testSerialize(SqlSession sqlSession, boolean aPreloadAttribute) {
83      DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
84      Department department = departmentMapper.getById(1);
85      if (aPreloadAttribute) {
86        department.getAttribute();
87      }
88  
89      serializeAndDeserializeObject(department);
90  
91      // This call results in problems when deserializing department
92      department.getPerson();
93      serializeAndDeserializeObject(department);
94    }
95  
96    void serializeAndDeserializeObject(Object anObject) {
97      UtilityTester.serializeAndDeserializeObject(anObject);
98    }
99  
100   private SqlSessionFactory getSqlSessionFactoryXmlConfig(String resource) throws Exception {
101     try (Reader configReader = Resources.getResourceAsReader(resource)) {
102       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
103 
104       BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
105           "org/apache/ibatis/submitted/serializecircular/CreateDB.sql");
106 
107       return sqlSessionFactory;
108     }
109   }
110 
111 }