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