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