Skip to content

Commit

Permalink
Modified 2 tests to ensure local cache behavior
Browse files Browse the repository at this point in the history
Local cache is used for nested select even if `localCacheScope` is `STATEMENT`.
This should prevent regression caused by changes like #3299
  • Loading branch information
harawata committed Nov 19, 2024
1 parent 372319a commit b9bdbd1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.LocalCacheScope;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

class AssociationTypeTest {

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

@Test
void shouldGetAUser() {
@ParameterizedTest
@EnumSource
void shouldGetAUser(LocalCacheScope localCacheScope) {
sqlSessionFactory.getConfiguration().setLocalCacheScope(localCacheScope);
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
List<Map> results = sqlSession.selectList("getUser");
for (Map r : results) {
Assertions.assertEquals(String.class, r.get("a1").getClass());
Assertions.assertEquals(String.class, r.get("a2").getClass());
List<Map<String, ?>> results = sqlSession.selectList("getUser");
for (Map<String, ?> r : results) {
Object a1 = r.get("a1");
Object a2 = r.get("a2");
Assertions.assertEquals(String.class, a1.getClass());
Assertions.assertEquals(String.class, a2.getClass());
Assertions.assertSame(a1, a2, "The result should be put into local cache regardless of localCacheScope setting.");
}
} finally {
// Reset the scope for other tests
sqlSessionFactory.getConfiguration().setLocalCacheScope(LocalCacheScope.SESSION);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.LocalCacheScope;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

class PermissionsTest {

Expand Down Expand Up @@ -66,8 +69,10 @@ void checkNestedResultMapLoop() {
}
}

@Test
void checkNestedSelectLoop() {
@ParameterizedTest
@EnumSource
void checkNestedSelectLoop(LocalCacheScope localCacheScope) {
sqlSessionFactory.getConfiguration().setLocalCacheScope(localCacheScope);
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
final PermissionsMapper mapper = sqlSession.getMapper(PermissionsMapper.class);

Expand All @@ -93,6 +98,9 @@ void checkNestedSelectLoop() {
if (!readFound) {
Assertions.fail();
}
} finally {
// Reset the scope for other tests
sqlSessionFactory.getConfiguration().setLocalCacheScope(LocalCacheScope.SESSION);
}
}

Expand Down

0 comments on commit b9bdbd1

Please sign in to comment.