View Javadoc
1   /*
2    *    Copyright 2009-2021 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.cglib_lazy_error;
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.Assertions;
26  import org.junit.jupiter.api.BeforeAll;
27  import org.junit.jupiter.api.Test;
28  
29  class CglibNPELazyTest {
30  
31    private static SqlSessionFactory sqlSessionFactory;
32  
33    @BeforeAll
34    static void initDatabase() throws Exception {
35      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/cglib_lazy_error/ibatisConfigLazy.xml")) {
36        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
37        sqlSessionFactory.getConfiguration().setLazyLoadingEnabled(true);
38        sqlSessionFactory.getConfiguration().setAggressiveLazyLoading(false);
39      }
40  
41      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
42              "org/apache/ibatis/submitted/cglib_lazy_error/CreateDB.sql");
43    }
44  
45    @Test
46    void testNoParent() {
47      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
48        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
49        Person person = personMapper.selectById(1);
50        Assertions.assertNotNull(person, "Persons must not be null");
51        Person parent = person.getParent();
52        Assertions.assertNull(parent, "Parent must be null");
53      }
54    }
55  
56    @Test
57    void testAncestorSelf() {
58      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
59        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
60        Person person = personMapper.selectById(1);
61        Assertions.assertNotNull(person, "Persons must not be null");
62        Person ancestor = person.getAncestor();
63        Assertions.assertEquals(person, ancestor, "Ancestor must be John Smith sr.");
64      }
65    }
66  
67    @Test
68    void testAncestorAfterQueryingParents() {
69      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
70        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
71        Person expectedAncestor = personMapper.selectById(1);
72        Person person = personMapper.selectById(3);
73        // Load ancestor indirectly.
74        Assertions.assertNotNull(person, "Persons must not be null");
75        Assertions.assertNotNull(person.getParent(), "Parent must not be null");
76        Assertions.assertNotNull(person.getParent().getParent(), "Grandparent must not be null");
77        Assertions.assertEquals(expectedAncestor, person.getAncestor(), "Ancestor must be John Smith sr.");
78      }
79    }
80  
81    @Test
82    void testGrandParent() {
83      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
84        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
85        Person expectedParent = personMapper.selectById(2);
86        Person expectedGrandParent = personMapper.selectById(1);
87        Person person = personMapper.selectById(3);
88        Assertions.assertNotNull(person, "Persons must not be null");
89        final Person actualParent = person.getParent();
90        final Person actualGrandParent = person.getParent().getParent();
91        Assertions.assertEquals(expectedParent, actualParent);
92        Assertions.assertEquals(expectedGrandParent, actualGrandParent);
93      }
94    }
95  
96    @Test
97    void testAncestor() {
98      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
99        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
100       Person expectedAncestor = personMapper.selectById(1);
101       Person person = personMapper.selectById(3);
102       Assertions.assertNotNull(person, "Persons must not be null");
103       final Person actualAncestor = person.getAncestor();
104       Assertions.assertEquals(expectedAncestor, actualAncestor);
105     }
106   }
107 
108 }