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