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.*;
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.getResourceAsReader("org/apache/ibatis/submitted/nested_query_cache/MapperConfig.xml")) {
40 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41 }
42
43 createBlogDataSource();
44 }
45
46 @Test
47 void testThatNestedQueryItemsAreRetrievedFromCache() {
48 final Author author;
49 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
50 final AuthorMapper authorMapper = sqlSession.getMapper(AuthorMapper.class);
51 author = authorMapper.selectAuthor(101);
52
53
54 final Author cachedAuthor = authorMapper.selectAuthor(101);
55 assertThat(author).isSameAs(cachedAuthor);
56 }
57
58
59 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
60 final BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
61
62
63 assertThat(blogMapper.selectBlog(1).getAuthor()).isSameAs(author);
64 assertThat(blogMapper.selectBlogUsingConstructor(1).getAuthor()).isSameAs(author);
65 }
66 }
67
68 @Test
69 void testThatNestedQueryItemsAreRetrievedIfNotInCache() {
70 Author author;
71 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72 final BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
73 author = blogMapper.selectBlog(1).getAuthor();
74
75
76 assertNotNull(blogMapper.selectBlog(1).getAuthor(), "blog author");
77 assertNotNull(blogMapper.selectBlogUsingConstructor(1).getAuthor(), "blog author");
78 }
79
80
81 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
82 final AuthorMapper authorMapper = sqlSession.getMapper(AuthorMapper.class);
83 Author cachedAuthor = authorMapper.selectAuthor(101);
84
85
86 assertThat(cachedAuthor).isSameAs(author);
87 }
88
89 }
90 }