View Javadoc
1   /**
2    *    Copyright 2009-2019 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.lazyload_common_property;
17  
18  import java.io.Reader;
19  
20  import org.apache.ibatis.BaseDataTest;
21  import org.apache.ibatis.io.Resources;
22  import org.apache.ibatis.session.SqlSession;
23  import org.apache.ibatis.session.SqlSessionFactory;
24  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
25  import org.junit.jupiter.api.BeforeAll;
26  import org.junit.jupiter.api.Test;
27  
28  class CommonPropertyLazyLoadTest {
29  
30      private static SqlSessionFactory sqlSessionFactory;
31  
32      @BeforeAll
33      static void initDatabase() throws Exception {
34          try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/lazyload_common_property/ibatisConfig.xml")) {
35              sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
36          }
37  
38          BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
39                  "org/apache/ibatis/submitted/lazyload_common_property/CreateDB.sql");
40      }
41  
42      @Test
43      void testLazyLoadWithNoAncestor() {
44          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
45              ChildMapper childMapper = sqlSession.getMapper(ChildMapper.class);
46  
47              childMapper.selectById(1);
48          }
49      }
50      @Test
51      void testLazyLoadWithFirstAncestor() {
52          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
53              FatherMapper fatherMapper = sqlSession.getMapper(FatherMapper.class);
54              ChildMapper childMapper = sqlSession.getMapper(ChildMapper.class);
55  
56              fatherMapper.selectById(1);
57              childMapper.selectById(1);
58          }
59      }
60      @Test
61      void testLazyLoadWithAllAncestors() {
62          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
63              GrandFatherMapper grandFatherMapper = sqlSession.getMapper(GrandFatherMapper.class);
64              FatherMapper fatherMapper = sqlSession.getMapper(FatherMapper.class);
65              ChildMapper childMapper = sqlSession.getMapper(ChildMapper.class);
66  
67              grandFatherMapper.selectById(1);
68              fatherMapper.selectById(1);
69              childMapper.selectById(1);
70          }
71      }
72      @Test
73      void testLazyLoadSkipFirstAncestor() {
74          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
75              GrandFatherMapper grandFatherMapper = sqlSession.getMapper(GrandFatherMapper.class);
76              ChildMapper childMapper = sqlSession.getMapper(ChildMapper.class);
77  
78              grandFatherMapper.selectById(1);
79              childMapper.selectById(1);
80          }
81      }
82  }