1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.call_setters_on_nulls;
17
18 import java.io.Reader;
19 import java.util.List;
20 import java.util.Map;
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.Assertions;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.Test;
30
31 class CallSettersOnNullsTest {
32
33 private static SqlSessionFactory sqlSessionFactory;
34
35 @BeforeAll
36 static void setUp() throws Exception {
37
38 try (Reader reader = Resources
39 .getResourceAsReader("org/apache/ibatis/submitted/call_setters_on_nulls/mybatis-config.xml")) {
40 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41 }
42
43
44 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
45 "org/apache/ibatis/submitted/call_setters_on_nulls/CreateDB.sql");
46 }
47
48 @Test
49 void shouldCallNullOnMappedProperty() {
50 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
51 Mapper mapper = sqlSession.getMapper(Mapper.class);
52 User user = mapper.getUserMapped(1);
53 Assertions.assertTrue(user.nullReceived);
54 }
55 }
56
57 @Test
58 void shouldCallNullOnAutomaticMapping() {
59 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
60 Mapper mapper = sqlSession.getMapper(Mapper.class);
61 User user = mapper.getUserUnmapped(1);
62 Assertions.assertTrue(user.nullReceived);
63 }
64 }
65
66 @Test
67 void shouldCallNullOnMap() {
68 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
69 Mapper mapper = sqlSession.getMapper(Mapper.class);
70 Map user = mapper.getUserInMap(1);
71 Assertions.assertTrue(user.containsKey("NAME"));
72 }
73 }
74
75 @Test
76 void shouldCallNullOnMapForSingleColumn() {
77 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
78 Mapper mapper = sqlSession.getMapper(Mapper.class);
79 List<Map<String, Object>> oneColumns = mapper.getNameOnly();
80
81
82
83 Assertions.assertNull(oneColumns.get(1));
84 }
85 }
86
87 @Test
88 void shouldCallNullOnMapForSingleColumnWithResultMap() {
89 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
90 Mapper mapper = sqlSession.getMapper(Mapper.class);
91 List<Map<String, Object>> oneColumns = mapper.getNameOnlyMapped();
92
93
94
95
96
97 Assertions.assertNull(oneColumns.get(1));
98 }
99 }
100
101 }