1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.automapping;
17
18 import java.io.Reader;
19 import java.util.List;
20
21 import org.apache.ibatis.BaseDataTest;
22 import org.apache.ibatis.io.Resources;
23 import org.apache.ibatis.session.AutoMappingBehavior;
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 AutomappingTest {
32
33 private static SqlSessionFactory sqlSessionFactory;
34
35 @BeforeAll
36 static void setUp() throws Exception {
37
38 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/automapping/mybatis-config.xml")) {
39 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40 }
41
42
43 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
44 "org/apache/ibatis/submitted/automapping/CreateDB.sql");
45 }
46
47 @Test
48 void shouldGetAUser() {
49 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
50 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
51 Mapper mapper = sqlSession.getMapper(Mapper.class);
52 User user = mapper.getUser(1);
53 Assertions.assertEquals("User1", user.getName());
54 }
55 }
56
57 @Test
58 void shouldGetAUserWhithPhoneNumber() {
59 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
60 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
61 Mapper mapper = sqlSession.getMapper(Mapper.class);
62 User user = mapper.getUserWithPhoneNumber(1);
63 Assertions.assertEquals("User1", user.getName());
64 Assertions.assertEquals(Long.valueOf(12345678901L), user.getPhone());
65 }
66 }
67
68 @Test
69 void shouldNotInheritAutoMappingInherited_InlineNestedResultMap() {
70 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
71 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72 Mapper mapper = sqlSession.getMapper(Mapper.class);
73 User user = mapper.getUserWithPets_Inline(2);
74 Assertions.assertEquals(Integer.valueOf(2), user.getId());
75 Assertions.assertEquals("User2", user.getName());
76 Assertions.assertNull(user.getPets().get(0).getPetName(), "should not inherit auto-mapping");
77 Assertions.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
78 }
79 }
80
81 @Test
82 void shouldNotInheritAutoMappingInherited_ExternalNestedResultMap() {
83 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
84 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
85 Mapper mapper = sqlSession.getMapper(Mapper.class);
86 User user = mapper.getUserWithPets_External(2);
87 Assertions.assertEquals(Integer.valueOf(2), user.getId());
88 Assertions.assertEquals("User2", user.getName());
89 Assertions.assertNull(user.getPets().get(0).getPetName(), "should not inherit auto-mapping");
90 Assertions.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
91 }
92 }
93
94 @Test
95 void shouldIgnorePartialAutoMappingBehavior_InlineNestedResultMap() {
96
97 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
98 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
99 Mapper mapper = sqlSession.getMapper(Mapper.class);
100 User user = mapper.getUserWithPets_Inline(2);
101 Assertions.assertEquals(Integer.valueOf(2), user.getId());
102 Assertions.assertEquals("User2", user.getName());
103 Assertions.assertNull(user.getPets().get(0).getPetName(), "should not inherit auto-mapping");
104 Assertions.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
105 }
106 }
107
108 @Test
109 void shouldRespectFullAutoMappingBehavior_InlineNestedResultMap() {
110 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.FULL);
111 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
112 Mapper mapper = sqlSession.getMapper(Mapper.class);
113 User user = mapper.getUserWithPets_Inline(2);
114 Assertions.assertEquals(Integer.valueOf(2), user.getId());
115 Assertions.assertEquals("User2", user.getName());
116 Assertions.assertEquals("Chien", user.getPets().get(0).getPetName());
117 Assertions.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
118 }
119 }
120
121 @Test
122 void shouldIgnorePartialAutoMappingBehavior_ExternalNestedResultMap() {
123
124 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
125 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
126 Mapper mapper = sqlSession.getMapper(Mapper.class);
127 User user = mapper.getUserWithPets_External(2);
128 Assertions.assertEquals(Integer.valueOf(2), user.getId());
129 Assertions.assertEquals("User2", user.getName());
130 Assertions.assertNull(user.getPets().get(0).getPetName(), "should not inherit auto-mapping");
131 Assertions.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
132 }
133 }
134
135 @Test
136 void shouldRespectFullAutoMappingBehavior_ExternalNestedResultMap() {
137 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.FULL);
138 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
139 Mapper mapper = sqlSession.getMapper(Mapper.class);
140 User user = mapper.getUserWithPets_External(2);
141 Assertions.assertEquals(Integer.valueOf(2), user.getId());
142 Assertions.assertEquals("User2", user.getName());
143 Assertions.assertEquals("Chien", user.getPets().get(0).getPetName());
144 Assertions.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
145 }
146 }
147
148 @Test
149 void shouldGetBooks() {
150
151 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
152 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
153 Mapper mapper = sqlSession.getMapper(Mapper.class);
154
155 List<Book> books = mapper.getBooks();
156 Assertions.assertTrue(!books.isEmpty(), "should return results,no errors throw");
157 }
158 }
159
160 @Test
161 void shouldUpdateFinalField() {
162
163 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
164 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
165 Mapper mapper = sqlSession.getMapper(Mapper.class);
166 Article article = mapper.getArticle();
167
168
169
170 Assertions.assertTrue(article.version > 0, "should update version in mapping");
171 }
172 }
173 }