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.not_null_column;
17  
18  import static org.junit.jupiter.api.Assertions.*;
19  
20  import java.io.Reader;
21  
22  import org.apache.ibatis.BaseDataTest;
23  import org.apache.ibatis.io.Resources;
24  import org.apache.ibatis.session.SqlSession;
25  import org.apache.ibatis.session.SqlSessionFactory;
26  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27  import org.junit.jupiter.api.BeforeAll;
28  import org.junit.jupiter.api.Test;
29  
30  class NotNullColumnTest {
31  
32    private static SqlSessionFactory sqlSessionFactory;
33  
34    @BeforeAll
35    static void initDatabase() throws Exception {
36      try (
37          Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/not_null_column/ibatisConfig.xml")) {
38        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39      }
40  
41      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
42          "org/apache/ibatis/submitted/not_null_column/CreateDB.sql");
43    }
44  
45    @Test
46    void testNotNullColumnWithChildrenNoFid() {
47      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
48        FatherMapper fatherMapper = sqlSession.getMapper(FatherMapper.class);
49  
50        Father test = fatherMapper.selectByIdNoFid(1);
51        assertNotNull(test);
52        assertNotNull(test.getChildren());
53        assertEquals(2, test.getChildren().size());
54      }
55    }
56  
57    @Test
58    void testNotNullColumnWithoutChildrenNoFid() {
59      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
60        FatherMapper fatherMapper = sqlSession.getMapper(FatherMapper.class);
61  
62        Father test = fatherMapper.selectByIdNoFid(2);
63        assertNotNull(test);
64        assertNotNull(test.getChildren());
65        assertTrue(test.getChildren().isEmpty());
66      }
67    }
68  
69    @Test
70    void testNotNullColumnWithoutChildrenFid() {
71      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72        FatherMapper fatherMapper = sqlSession.getMapper(FatherMapper.class);
73  
74        Father test = fatherMapper.selectByIdFid(2);
75        assertNotNull(test);
76        assertNotNull(test.getChildren());
77        assertTrue(test.getChildren().isEmpty());
78      }
79    }
80  
81    @Test
82    void testNotNullColumnWithoutChildrenWithInternalResultMap() {
83      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
84        FatherMapper fatherMapper = sqlSession.getMapper(FatherMapper.class);
85  
86        Father test = fatherMapper.selectByIdWithInternalResultMap(2);
87        assertNotNull(test);
88        assertNotNull(test.getChildren());
89        assertTrue(test.getChildren().isEmpty());
90      }
91    }
92  
93    @Test
94    void testNotNullColumnWithoutChildrenWithRefResultMap() {
95      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
96        FatherMapper fatherMapper = sqlSession.getMapper(FatherMapper.class);
97  
98        Father test = fatherMapper.selectByIdWithRefResultMap(2);
99        assertNotNull(test);
100       assertNotNull(test.getChildren());
101       assertTrue(test.getChildren().isEmpty());
102     }
103   }
104 
105   @Test
106   void testNotNullColumnWithoutChildrenFidMultipleNullColumns() {
107     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
108       FatherMapper fatherMapper = sqlSession.getMapper(FatherMapper.class);
109 
110       Father test = fatherMapper.selectByIdFidMultipleNullColumns(2);
111       assertNotNull(test);
112       assertNotNull(test.getChildren());
113       assertTrue(test.getChildren().isEmpty());
114     }
115   }
116 
117   @Test
118   void testNotNullColumnWithoutChildrenFidMultipleNullColumnsAndBrackets() {
119     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
120       FatherMapper fatherMapper = sqlSession.getMapper(FatherMapper.class);
121 
122       Father test = fatherMapper.selectByIdFidMultipleNullColumnsAndBrackets(2);
123       assertNotNull(test);
124       assertNotNull(test.getChildren());
125       assertTrue(test.getChildren().isEmpty());
126     }
127   }
128 
129   @Test
130   void testNotNullColumnWithoutChildrenFidWorkaround() {
131     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
132       FatherMapper fatherMapper = sqlSession.getMapper(FatherMapper.class);
133 
134       Father test = fatherMapper.selectByIdFidWorkaround(2);
135       assertNotNull(test);
136       assertNotNull(test.getChildren());
137       assertTrue(test.getChildren().isEmpty());
138     }
139   }
140 }