1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.nested_query_cache;
17
18 import static org.assertj.core.api.Assertions.assertThat;
19 import static org.junit.jupiter.api.Assertions.assertNotNull;
20
21 import java.io.Reader;
22
23 import org.apache.ibatis.BaseDataTest;
24 import org.apache.ibatis.domain.blog.Author;
25 import org.apache.ibatis.io.Resources;
26 import org.apache.ibatis.session.SqlSession;
27 import org.apache.ibatis.session.SqlSessionFactory;
28 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29 import org.junit.jupiter.api.BeforeAll;
30 import org.junit.jupiter.api.Test;
31
32 class NestedQueryCacheTest extends BaseDataTest {
33
34 private static SqlSessionFactory sqlSessionFactory;
35
36 @BeforeAll
37 static void setUp() throws Exception {
38
39 try (Reader reader = Resources
40 .getResourceAsReader("org/apache/ibatis/submitted/nested_query_cache/MapperConfig.xml")) {
41 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42 }
43
44 createBlogDataSource();
45 }
46
47 @Test
48 void testThatNestedQueryItemsAreRetrievedFromCache() {
49 final Author author;
50 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
51 final AuthorMapper authorMapper = sqlSession.getMapper(AuthorMapper.class);
52 author = authorMapper.selectAuthor(101);
53
54
55 final Author cachedAuthor = authorMapper.selectAuthor(101);
56 assertThat(author).isSameAs(cachedAuthor);
57 }
58
59
60 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
61 final BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
62
63
64 assertThat(blogMapper.selectBlog(1).getAuthor()).isSameAs(author);
65 assertThat(blogMapper.selectBlogUsingConstructor(1).getAuthor()).isSameAs(author);
66 }
67 }
68
69 @Test
70 void testThatNestedQueryItemsAreRetrievedIfNotInCache() {
71 Author author;
72 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
73 final BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
74 author = blogMapper.selectBlog(1).getAuthor();
75
76
77 assertNotNull(blogMapper.selectBlog(1).getAuthor(), "blog author");
78 assertNotNull(blogMapper.selectBlogUsingConstructor(1).getAuthor(), "blog author");
79 }
80
81
82 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
83 final AuthorMapper authorMapper = sqlSession.getMapper(AuthorMapper.class);
84 Author cachedAuthor = authorMapper.selectAuthor(101);
85
86
87 assertThat(cachedAuthor).isSameAs(author);
88 }
89
90 }
91 }