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