View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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      // create a SqlSessionFactory
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        // ensure that author is cached
55        final Author cachedAuthor = authorMapper.selectAuthor(101);
56        assertThat(author).isSameAs(cachedAuthor);
57      }
58  
59      // open a new session
60      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
61        final BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
62  
63        // ensure that nested author within blog is cached
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        // ensure that nested author within blog is cached
77        assertNotNull(blogMapper.selectBlog(1).getAuthor(), "blog author");
78        assertNotNull(blogMapper.selectBlogUsingConstructor(1).getAuthor(), "blog author");
79      }
80  
81      // open a new session
82      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
83        final AuthorMapper authorMapper = sqlSession.getMapper(AuthorMapper.class);
84        Author cachedAuthor = authorMapper.selectAuthor(101);
85  
86        // ensure that nested author within blog is cached
87        assertThat(cachedAuthor).isSameAs(author);
88      }
89  
90    }
91  }