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 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
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
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 }