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