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.selectkey;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNotNull;
20  
21  import java.io.Reader;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.ibatis.BaseDataTest;
26  import org.apache.ibatis.exceptions.PersistenceException;
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.Assertions;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Disabled;
34  import org.junit.jupiter.api.Test;
35  
36  class SelectKeyTest {
37  
38    protected static SqlSessionFactory sqlSessionFactory;
39  
40    @BeforeEach
41    void setUp() throws Exception {
42      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/selectkey/MapperConfig.xml")) {
43        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
44        sqlSessionFactory.getConfiguration().addMapper(AnnotatedMapper.class);
45      }
46  
47      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
48          "org/apache/ibatis/submitted/selectkey/CreateDB.sql");
49    }
50  
51    @Test
52    void testSelectKey() throws Exception {
53      // this test checks to make sure that we can have select keys with the same
54      // insert id in different namespaces
55      String resource = "org/apache/ibatis/submitted/selectkey/MapperConfig.xml";
56      Reader reader = Resources.getResourceAsReader(resource);
57      SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
58      SqlSessionFactory sqlMapper = builder.build(reader);
59      assertNotNull(sqlMapper);
60    }
61  
62    @Test
63    void testInsertTable1() {
64      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
65        Map<String, Object> parms = new HashMap<>();
66        parms.put("name", "Fred");
67        int rows = sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table1.insert", parms);
68        assertEquals(1, rows);
69        assertEquals(11, parms.get("id"));
70      }
71    }
72  
73    @Test
74    void testInsertTable2() {
75      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
76        Map<String, Object> parms = new HashMap<>();
77        parms.put("name", "Fred");
78        int rows = sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insert", parms);
79        assertEquals(1, rows);
80        assertEquals(22, parms.get("id"));
81      }
82    }
83  
84    @Test
85    void testSeleckKeyReturnsNoData() {
86      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
87        Map<String, String> parms = new HashMap<>();
88        parms.put("name", "Fred");
89        Assertions.assertThrows(PersistenceException.class,
90            () -> sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insertNoValuesInSelectKey", parms));
91      }
92    }
93  
94    @Test
95    void testSeleckKeyReturnsTooManyData() {
96      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
97        Map<String, String> parms = new HashMap<>();
98        parms.put("name", "Fred");
99        sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insertTooManyValuesInSelectKey", parms);
100       Assertions.assertThrows(PersistenceException.class, () -> sqlSession
101           .insert("org.apache.ibatis.submitted.selectkey.Table2.insertTooManyValuesInSelectKey", parms));
102     }
103   }
104 
105   @Test
106   void testAnnotatedInsertTable2() {
107     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
108       Name name = new Name();
109       name.setName("barney");
110       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
111       int rows = mapper.insertTable2(name);
112       assertEquals(1, rows);
113       assertEquals(22, name.getNameId());
114     }
115   }
116 
117   @Test
118   void testAnnotatedInsertTable2WithGeneratedKey() {
119     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
120       Name name = new Name();
121       name.setName("barney");
122       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
123       int rows = mapper.insertTable2WithGeneratedKey(name);
124       assertEquals(1, rows);
125       assertEquals(22, name.getNameId());
126       assertEquals("barney_fred", name.getGeneratedName());
127     }
128   }
129 
130   @Test
131   @Disabled("HSQLDB is not returning the generated column after the update")
132   void testAnnotatedUpdateTable2WithGeneratedKey() {
133     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
134       Name name = new Name();
135       name.setName("barney");
136       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
137       int rows = mapper.insertTable2WithGeneratedKey(name);
138       assertEquals(1, rows);
139       assertEquals(22, name.getNameId());
140       assertEquals("barney_fred", name.getGeneratedName());
141 
142       name.setName("Wilma");
143       rows = mapper.updateTable2WithGeneratedKey(name);
144       assertEquals(1, rows);
145       assertEquals(22, name.getNameId());
146       assertEquals("Wilma_fred", name.getGeneratedName());
147     }
148   }
149 
150   @Test
151   @Disabled("HSQLDB is not returning the generated column after the update")
152   void testAnnotatedUpdateTable2WithGeneratedKeyXml() {
153     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
154       Name name = new Name();
155       name.setName("barney");
156       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
157       int rows = mapper.insertTable2WithGeneratedKeyXml(name);
158       assertEquals(1, rows);
159       assertEquals(22, name.getNameId());
160       assertEquals("barney_fred", name.getGeneratedName());
161 
162       name.setName("Wilma");
163       rows = mapper.updateTable2WithGeneratedKeyXml(name);
164       assertEquals(1, rows);
165       assertEquals(22, name.getNameId());
166       assertEquals("Wilma_fred", name.getGeneratedName());
167     }
168   }
169 
170   @Test
171   void testAnnotatedInsertTable2WithGeneratedKeyXml() {
172     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
173       Name name = new Name();
174       name.setName("barney");
175       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
176       int rows = mapper.insertTable2WithGeneratedKeyXml(name);
177       assertEquals(1, rows);
178       assertEquals(22, name.getNameId());
179       assertEquals("barney_fred", name.getGeneratedName());
180     }
181   }
182 
183   @Test
184   void testAnnotatedInsertTable2WithSelectKeyWithKeyMap() {
185     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
186       Name name = new Name();
187       name.setName("barney");
188       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
189       int rows = mapper.insertTable2WithSelectKeyWithKeyMap(name);
190       assertEquals(1, rows);
191       assertEquals(22, name.getNameId());
192       assertEquals("barney_fred", name.getGeneratedName());
193     }
194   }
195 
196   @Test
197   void testAnnotatedUpdateTable2WithSelectKeyWithKeyMap() {
198     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
199       Name name = new Name();
200       name.setName("barney");
201       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
202       int rows = mapper.insertTable2WithSelectKeyWithKeyMap(name);
203       assertEquals(1, rows);
204       assertEquals(22, name.getNameId());
205       assertEquals("barney_fred", name.getGeneratedName());
206 
207       name.setName("Wilma");
208       rows = mapper.updateTable2WithSelectKeyWithKeyMap(name);
209       assertEquals(1, rows);
210       assertEquals(22, name.getNameId());
211       assertEquals("Wilma_fred", name.getGeneratedName());
212     }
213   }
214 
215   @Test
216   void testAnnotatedInsertTable2WithSelectKeyWithKeyMapXml() {
217     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
218       Name name = new Name();
219       name.setName("barney");
220       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
221       int rows = mapper.insertTable2WithSelectKeyWithKeyMapXml(name);
222       assertEquals(1, rows);
223       assertEquals(22, name.getNameId());
224       assertEquals("barney_fred", name.getGeneratedName());
225     }
226   }
227 
228   @Test
229   void testAnnotatedUpdateTable2WithSelectKeyWithKeyMapXml() {
230     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
231       Name name = new Name();
232       name.setName("barney");
233       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
234       int rows = mapper.insertTable2WithSelectKeyWithKeyMapXml(name);
235       assertEquals(1, rows);
236       assertEquals(22, name.getNameId());
237       assertEquals("barney_fred", name.getGeneratedName());
238 
239       name.setName("Wilma");
240       rows = mapper.updateTable2WithSelectKeyWithKeyMapXml(name);
241       assertEquals(1, rows);
242       assertEquals(22, name.getNameId());
243       assertEquals("Wilma_fred", name.getGeneratedName());
244     }
245   }
246 
247   @Test
248   void testAnnotatedInsertTable2WithSelectKeyWithKeyObject() {
249     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
250       Name name = new Name();
251       name.setName("barney");
252       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
253       int rows = mapper.insertTable2WithSelectKeyWithKeyObject(name);
254       assertEquals(1, rows);
255       assertEquals(22, name.getNameId());
256       assertEquals("barney_fred", name.getGeneratedName());
257     }
258   }
259 
260   @Test
261   void testAnnotatedUpdateTable2WithSelectKeyWithKeyObject() {
262     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
263       Name name = new Name();
264       name.setName("barney");
265       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
266       int rows = mapper.insertTable2WithSelectKeyWithKeyObject(name);
267       assertEquals(1, rows);
268       assertEquals(22, name.getNameId());
269       assertEquals("barney_fred", name.getGeneratedName());
270 
271       name.setName("Wilma");
272       rows = mapper.updateTable2WithSelectKeyWithKeyObject(name);
273       assertEquals(1, rows);
274       assertEquals(22, name.getNameId());
275       assertEquals("Wilma_fred", name.getGeneratedName());
276     }
277   }
278 
279   @Test
280   void testAnnotatedUpdateTable2WithSelectKeyWithKeyObjectXml() {
281     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
282       Name name = new Name();
283       name.setName("barney");
284       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
285       int rows = mapper.insertTable2WithSelectKeyWithKeyObjectXml(name);
286       assertEquals(1, rows);
287       assertEquals(22, name.getNameId());
288       assertEquals("barney_fred", name.getGeneratedName());
289 
290       name.setName("Wilma");
291       rows = mapper.updateTable2WithSelectKeyWithKeyObjectXml(name);
292       assertEquals(1, rows);
293       assertEquals(22, name.getNameId());
294       assertEquals("Wilma_fred", name.getGeneratedName());
295     }
296   }
297 
298   @Test
299   void testAnnotatedInsertTable2WithSelectKeyWithKeyObjectXml() {
300     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
301       Name name = new Name();
302       name.setName("barney");
303       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
304       int rows = mapper.insertTable2WithSelectKeyWithKeyObjectXml(name);
305       assertEquals(1, rows);
306       assertEquals(22, name.getNameId());
307       assertEquals("barney_fred", name.getGeneratedName());
308     }
309   }
310 
311   @Test
312   void testAnnotatedInsertTable3() {
313     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
314       Name name = new Name();
315       name.setName("barney");
316       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
317       int rows = mapper.insertTable3(name);
318       assertEquals(1, rows);
319       assertEquals(33, name.getNameId());
320     }
321   }
322 
323   @Test
324   void testAnnotatedInsertTable3_2() {
325     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
326       Name name = new Name();
327       name.setName("barney");
328       AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
329       int rows = mapper.insertTable3_2(name);
330       assertEquals(1, rows);
331       assertEquals(33, name.getNameId());
332     }
333   }
334 
335   @Test
336   void testSeleckKeyWithWrongKeyProperty() {
337     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
338       Name name = new Name();
339       name.setName("Kyoto");
340       Assertions.assertThrows(PersistenceException.class,
341           () -> sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insertWrongKeyProperty", name));
342     }
343   }
344 }