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 CglibNPETest {
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/ibatisConfig.xml")) {
37        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
38      }
39  
40      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
41          "org/apache/ibatis/submitted/cglib_lazy_error/CreateDB.sql");
42    }
43  
44    @Test
45    void testNoParent() {
46      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
47        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
48        Person person = personMapper.selectById(1);
49        Assertions.assertNotNull(person, "Persons must not be null");
50        Person parent = person.getParent();
51        Assertions.assertNull(parent, "Parent must be null");
52      }
53    }
54  
55    @Test
56    void testAncestorSelf() {
57      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
58        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
59        Person person = personMapper.selectById(1);
60        Assertions.assertNotNull(person, "Persons must not be null");
61        Person ancestor = person.getAncestor();
62        Assertions.assertEquals(person, ancestor, "Ancestor must be John Smith sr.");
63      }
64    }
65  
66    @Test
67    void testGrandParent() {
68      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
69        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
70        Person expectedParent = personMapper.selectById(2);
71        Person expectedGrandParent = personMapper.selectById(1);
72        Person person = personMapper.selectById(3);
73        Assertions.assertNotNull(person, "Persons must not be null");
74        Assertions.assertEquals(expectedParent, person.getParent(), "Parent must be John Smith");
75        Assertions.assertEquals(expectedGrandParent, person.getParent().getParent(), "Parent must be John Smith sr.");
76      }
77    }
78  
79    @Test
80    void testAncestor() {
81      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
82        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
83        Person expectedAncestor = personMapper.selectById(1);
84        Person person = personMapper.selectById(3);
85        Assertions.assertNotNull(person, "Persons must not be null");
86        Assertions.assertEquals(expectedAncestor, person.getAncestor(), "Ancestor must be John Smith sr.");
87      }
88    }
89  
90    @Test
91    void testAncestorAfterQueryingParents() {
92      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
93        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
94        Person expectedAncestor = personMapper.selectById(1);
95        Person person = personMapper.selectById(3);
96        // Load ancestor indirectly.
97        Assertions.assertNotNull(person, "Persons must not be null");
98        Assertions.assertNotNull(person.getParent(), "Parent must not be null");
99        Assertions.assertNotNull(person.getParent().getParent(), "Grandparent must not be null");
100       Assertions.assertEquals(expectedAncestor, person.getAncestor(), "Ancestor must be John Smith sr.");
101     }
102   }
103 
104   @Test
105   void testInsertBetweenTwoSelects() {
106     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
107       PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
108       Person selected1 = personMapper.selectById(1);
109       Person selected2 = personMapper.selectById(2);
110       Person selected3 = personMapper.selectById(3);
111       selected1.setId(4L);
112       int rows = personMapper.insertPerson(selected1);
113       Assertions.assertEquals(1, rows);
114       selected1 = personMapper.selectById(1);
115       selected2 = personMapper.selectById(2);
116       selected3 = personMapper.selectById(3);
117       Person selected4 = personMapper.selectById(4);
118       Assertions.assertEquals(1, selected1.getId().longValue());
119       Assertions.assertEquals(2, selected2.getId().longValue());
120       Assertions.assertEquals(3, selected3.getId().longValue());
121       Assertions.assertEquals(4, selected4.getId().longValue());
122     }
123   }
124 
125   @Test
126   void testSelectWithStringSQLInjection() {
127     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
128       PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
129       Person selected1 = personMapper.selectByStringId("1");
130       Assertions.assertEquals(1, selected1.getId().longValue());
131     }
132   }
133 
134 }