1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
47 @Test
48 void serializeAndDeserializeObjectsWithoutAggressiveLazyLoadingWithoutPreloadingAttribute()
49 throws Exception {
50 try (SqlSession sqlSession = createSessionWithoutAggressiveLazyLoading()) {
51
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
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 }