1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.lazy_properties;
17
18 import static org.junit.jupiter.api.Assertions.*;
19
20 import java.io.Reader;
21 import java.util.Collections;
22 import java.util.HashSet;
23
24 import org.apache.ibatis.BaseDataTest;
25 import org.apache.ibatis.executor.loader.ProxyFactory;
26 import org.apache.ibatis.executor.loader.cglib.CglibProxyFactory;
27 import org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory;
28 import org.apache.ibatis.io.Resources;
29 import org.apache.ibatis.session.Configuration;
30 import org.apache.ibatis.session.SqlSession;
31 import org.apache.ibatis.session.SqlSessionFactory;
32 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35
36 class LazyPropertiesTest {
37
38 private static SqlSessionFactory sqlSessionFactory;
39
40 @BeforeEach
41 void setUp() throws Exception {
42
43 try (Reader reader = Resources
44 .getResourceAsReader("org/apache/ibatis/submitted/lazy_properties/mybatis-config.xml")) {
45 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
46 }
47
48
49 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
50 "org/apache/ibatis/submitted/lazy_properties/CreateDB.sql");
51 }
52
53 @Test
54 void shouldLoadOnlyTheInvokedLazyProperty() {
55 sqlSessionFactory.getConfiguration().setAggressiveLazyLoading(false);
56 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
57 Mapper mapper = sqlSession.getMapper(Mapper.class);
58 User user = mapper.getUser(1);
59 assertEquals(0, user.setterCounter);
60 assertNotNull(user.getLazy1());
61 assertEquals(1, user.setterCounter, "Should NOT load other lazy properties.");
62 }
63 }
64
65 @Test
66 void verifyAggressiveLazyLoadingBehavior() {
67 sqlSessionFactory.getConfiguration().setAggressiveLazyLoading(true);
68 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
69 Mapper mapper = sqlSession.getMapper(Mapper.class);
70 User user = mapper.getUser(1);
71
72 assertEquals(3, user.setterCounter, "Should load all lazy properties.");
73 }
74 }
75
76 @Test
77 void shouldToStringTriggerLazyLoading() {
78 sqlSessionFactory.getConfiguration().setAggressiveLazyLoading(false);
79 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
80 Mapper mapper = sqlSession.getMapper(Mapper.class);
81 User user = mapper.getUser(1);
82 user.toString();
83 assertEquals(3, user.setterCounter);
84 }
85 }
86
87 @Test
88 void shouldHashCodeTriggerLazyLoading() {
89 sqlSessionFactory.getConfiguration().setAggressiveLazyLoading(false);
90 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
91 Mapper mapper = sqlSession.getMapper(Mapper.class);
92 User user = mapper.getUser(1);
93 user.hashCode();
94 assertEquals(3, user.setterCounter);
95 }
96 }
97
98 @Test
99 void shouldEqualsTriggerLazyLoading() {
100 sqlSessionFactory.getConfiguration().setAggressiveLazyLoading(false);
101 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
102 Mapper mapper = sqlSession.getMapper(Mapper.class);
103 User user = mapper.getUser(1);
104 user.equals(null);
105 assertEquals(3, user.setterCounter);
106 }
107 }
108
109 @Test
110 void shouldCloneTriggerLazyLoading() {
111 sqlSessionFactory.getConfiguration().setAggressiveLazyLoading(false);
112 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
113 Mapper mapper = sqlSession.getMapper(Mapper.class);
114 User user = mapper.getUser(1);
115 user.clone();
116 assertEquals(3, user.setterCounter);
117 }
118 }
119
120 @Test
121 void verifyEmptyLazyLoadTriggerMethods() {
122 Configuration configuration = sqlSessionFactory.getConfiguration();
123 configuration.setAggressiveLazyLoading(false);
124 configuration.setLazyLoadTriggerMethods(new HashSet<>());
125 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
126 Mapper mapper = sqlSession.getMapper(Mapper.class);
127 User user = mapper.getUser(1);
128 user.toString();
129 user.hashCode();
130 user.equals(null);
131 user.clone();
132 assertEquals(0, user.setterCounter);
133 }
134 }
135
136 @Test
137 void verifyCustomLazyLoadTriggerMethods() {
138 Configuration configuration = sqlSessionFactory.getConfiguration();
139 configuration.setAggressiveLazyLoading(false);
140 configuration
141 .setLazyLoadTriggerMethods(new HashSet<>(Collections.singleton("trigger")));
142 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
143 Mapper mapper = sqlSession.getMapper(Mapper.class);
144 User user = mapper.getUser(1);
145 user.toString();
146 user.hashCode();
147 user.equals(null);
148 user.clone();
149 assertEquals(0, user.setterCounter);
150 user.trigger();
151 assertEquals(3, user.setterCounter);
152 }
153 }
154
155 @Test
156 void shouldInvokingSetterInvalidateLazyLoading_Javassist() {
157 shoulInvokingSetterInvalidateLazyLoading(new JavassistProxyFactory());
158 }
159
160 @Test
161 void shouldInvokingSetterInvalidateLazyLoading_Cglib() {
162 shoulInvokingSetterInvalidateLazyLoading(new CglibProxyFactory());
163 }
164
165 private void shoulInvokingSetterInvalidateLazyLoading(ProxyFactory proxyFactory) {
166 Configuration config = sqlSessionFactory.getConfiguration();
167 config.setProxyFactory(proxyFactory);
168 config.setAggressiveLazyLoading(false);
169 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
170 Mapper mapper = sqlSession.getMapper(Mapper.class);
171 User user = mapper.getUser(1);
172 Userubmitted/lazy_properties/User.html#User">User u2 = new User();
173 u2.setId(99);
174 user.setLazy1(u2);
175 assertEquals(1, user.setterCounter);
176 assertEquals(Integer.valueOf(99), user.getLazy1().getId());
177 assertEquals(1, user.setterCounter);
178 }
179 }
180 }