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.empty_row;
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.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.Reader;
24  import java.util.Map;
25  
26  import org.apache.ibatis.BaseDataTest;
27  import org.apache.ibatis.io.Resources;
28  import org.apache.ibatis.session.SqlSession;
29  import org.apache.ibatis.session.SqlSessionFactory;
30  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31  import org.junit.jupiter.api.BeforeAll;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  
35  class ReturnInstanceForEmptyRowTest {
36  
37    private static SqlSessionFactory sqlSessionFactory;
38  
39    @BeforeAll
40    static void setUp() throws Exception {
41      // create an SqlSessionFactory
42      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/empty_row/mybatis-config.xml")) {
43        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
44      }
45  
46      // populate in-memory database
47      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
48          "org/apache/ibatis/submitted/empty_row/CreateDB.sql");
49    }
50  
51    @BeforeEach
52    void resetCallSettersOnNulls() {
53      sqlSessionFactory.getConfiguration().setCallSettersOnNulls(false);
54    }
55  
56    @Test
57    void shouldSimpleTypeBeNull() {
58      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
59        Mapper mapper = sqlSession.getMapper(Mapper.class);
60        String result = mapper.getString();
61        assertNull(result);
62      }
63    }
64  
65    @Test
66    void shouldObjectTypeNotBeNull() {
67      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
68        Mapper mapper = sqlSession.getMapper(Mapper.class);
69        Parent parent = mapper.getBean(1);
70        assertNotNull(parent);
71      }
72    }
73  
74    @Test
75    void shouldMapBeEmpty() {
76      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
77        Mapper mapper = sqlSession.getMapper(Mapper.class);
78        Map<String, String> map = mapper.getMap(1);
79        assertNotNull(map);
80        assertTrue(map.isEmpty());
81      }
82    }
83  
84    @Test
85    void shouldMapHaveColumnNamesIfCallSettersOnNullsEnabled() {
86      sqlSessionFactory.getConfiguration().setCallSettersOnNulls(true);
87      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
88        Mapper mapper = sqlSession.getMapper(Mapper.class);
89        Map<String, String> map = mapper.getMap(1);
90        assertEquals(2, map.size());
91        assertTrue(map.containsKey("COL1"));
92        assertTrue(map.containsKey("COL2"));
93      }
94    }
95  
96    @Test
97    void shouldAssociationNotBeNull() {
98      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
99        Mapper mapper = sqlSession.getMapper(Mapper.class);
100       Parent parent = mapper.getAssociation(1);
101       assertNotNull(parent.getChild());
102     }
103   }
104 
105   @Test
106   void shouldAssociationBeNullIfNotNullColumnSpecified() {
107     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
108       Mapper mapper = sqlSession.getMapper(Mapper.class);
109       Parent parent = mapper.getAssociationWithNotNullColumn(1);
110       assertNotNull(parent);
111       assertNull(parent.getChild());
112     }
113   }
114 
115   @Test
116   void shouldNestedAssociationNotBeNull() {
117     // #420
118     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
119       Mapper mapper = sqlSession.getMapper(Mapper.class);
120       Parent parent = mapper.getNestedAssociation();
121       assertNotNull(parent.getChild().getGrandchild());
122     }
123   }
124 
125   @Test
126   void testCollection() {
127     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
128       Mapper mapper = sqlSession.getMapper(Mapper.class);
129       Parent parent = mapper.getCollection(1);
130       assertEquals(1, parent.getChildren().size());
131       assertNotNull(parent.getChildren().get(0));
132     }
133   }
134 
135   @Test
136   void shouldSquashMultipleEmptyResults() {
137     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
138       Mapper mapper = sqlSession.getMapper(Mapper.class);
139       Parent parent = mapper.getTwoCollections(2);
140       assertEquals(1, parent.getPets().size());
141       assertNotNull(parent.getPets().get(0));
142     }
143   }
144 
145   @Test
146   void testConstructorAutomapping() {
147     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
148       Mapper mapper = sqlSession.getMapper(Mapper.class);
149       ImmutableParent parent = mapper.selectImmutable(1);
150       assertNotNull(parent);
151     }
152   }
153 
154   @Test
155   void testArgNameBasedConstructorAutomapping() {
156     sqlSessionFactory.getConfiguration().setArgNameBasedConstructorAutoMapping(true);
157     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
158       Mapper mapper = sqlSession.getMapper(Mapper.class);
159       ImmutableParent parent = mapper.selectImmutable(1);
160       assertNotNull(parent);
161     }
162   }
163 }