Skip to content

Commit b9bdbd1

Browse files
committed
Modified 2 tests to ensure local cache behavior
Local cache is used for nested select even if `localCacheScope` is `STATEMENT`. This should prevent regression caused by changes like #3299
1 parent 372319a commit b9bdbd1

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/test/java/org/apache/ibatis/submitted/associationtype/AssociationTypeTest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121

2222
import org.apache.ibatis.BaseDataTest;
2323
import org.apache.ibatis.io.Resources;
24+
import org.apache.ibatis.session.LocalCacheScope;
2425
import org.apache.ibatis.session.SqlSession;
2526
import org.apache.ibatis.session.SqlSessionFactory;
2627
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
2728
import org.junit.jupiter.api.Assertions;
2829
import org.junit.jupiter.api.BeforeAll;
29-
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.EnumSource;
3032

3133
class AssociationTypeTest {
3234

@@ -45,14 +47,22 @@ static void setUp() throws Exception {
4547
"org/apache/ibatis/submitted/associationtype/CreateDB.sql");
4648
}
4749

48-
@Test
49-
void shouldGetAUser() {
50+
@ParameterizedTest
51+
@EnumSource
52+
void shouldGetAUser(LocalCacheScope localCacheScope) {
53+
sqlSessionFactory.getConfiguration().setLocalCacheScope(localCacheScope);
5054
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
51-
List<Map> results = sqlSession.selectList("getUser");
52-
for (Map r : results) {
53-
Assertions.assertEquals(String.class, r.get("a1").getClass());
54-
Assertions.assertEquals(String.class, r.get("a2").getClass());
55+
List<Map<String, ?>> results = sqlSession.selectList("getUser");
56+
for (Map<String, ?> r : results) {
57+
Object a1 = r.get("a1");
58+
Object a2 = r.get("a2");
59+
Assertions.assertEquals(String.class, a1.getClass());
60+
Assertions.assertEquals(String.class, a2.getClass());
61+
Assertions.assertSame(a1, a2, "The result should be put into local cache regardless of localCacheScope setting.");
5562
}
63+
} finally {
64+
// Reset the scope for other tests
65+
sqlSessionFactory.getConfiguration().setLocalCacheScope(LocalCacheScope.SESSION);
5666
}
5767
}
5868

src/test/java/org/apache/ibatis/submitted/permissions/PermissionsTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020

2121
import org.apache.ibatis.BaseDataTest;
2222
import org.apache.ibatis.io.Resources;
23+
import org.apache.ibatis.session.LocalCacheScope;
2324
import org.apache.ibatis.session.SqlSession;
2425
import org.apache.ibatis.session.SqlSessionFactory;
2526
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
2627
import org.junit.jupiter.api.Assertions;
2728
import org.junit.jupiter.api.BeforeAll;
2829
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.EnumSource;
2932

3033
class PermissionsTest {
3134

@@ -66,8 +69,10 @@ void checkNestedResultMapLoop() {
6669
}
6770
}
6871

69-
@Test
70-
void checkNestedSelectLoop() {
72+
@ParameterizedTest
73+
@EnumSource
74+
void checkNestedSelectLoop(LocalCacheScope localCacheScope) {
75+
sqlSessionFactory.getConfiguration().setLocalCacheScope(localCacheScope);
7176
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
7277
final PermissionsMapper mapper = sqlSession.getMapper(PermissionsMapper.class);
7378

@@ -93,6 +98,9 @@ void checkNestedSelectLoop() {
9398
if (!readFound) {
9499
Assertions.fail();
95100
}
101+
} finally {
102+
// Reset the scope for other tests
103+
sqlSessionFactory.getConfiguration().setLocalCacheScope(LocalCacheScope.SESSION);
96104
}
97105
}
98106

0 commit comments

Comments
 (0)