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