View Javadoc
1   /**
2    *    Copyright 2009-2020 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    *       http://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.*;
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.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        // ensure that author is cached
54        final Author cachedAuthor = authorMapper.selectAuthor(101);
55        assertThat(author).isSameAs(cachedAuthor);
56      }
57  
58      // open a new session
59      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
60        final BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
61  
62        // ensure that nested author within blog is cached
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        // ensure that nested author within blog is cached
76        assertNotNull(blogMapper.selectBlog(1).getAuthor(), "blog author");
77        assertNotNull(blogMapper.selectBlogUsingConstructor(1).getAuthor(), "blog author");
78      }
79  
80      // open a new session
81      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
82        final AuthorMapper authorMapper = sqlSession.getMapper(AuthorMapper.class);
83        Author cachedAuthor = authorMapper.selectAuthor(101);
84  
85        // ensure that nested author within blog is cached
86        assertThat(cachedAuthor).isSameAs(author);
87      }
88  
89    }
90  }