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.keycolumn;
17  
18  import static org.junit.jupiter.api.Assertions.*;
19  
20  import java.util.List;
21  
22  import org.apache.ibatis.BaseDataTest;
23  import org.apache.ibatis.executor.BatchResult;
24  import org.apache.ibatis.mapping.Environment;
25  import org.apache.ibatis.session.Configuration;
26  import org.apache.ibatis.session.ExecutorType;
27  import org.apache.ibatis.session.SqlSession;
28  import org.apache.ibatis.session.SqlSessionFactory;
29  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30  import org.apache.ibatis.testcontainers.PgContainer;
31  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
32  import org.junit.jupiter.api.BeforeAll;
33  import org.junit.jupiter.api.Tag;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * @author Jeff Butler
38   */
39  @Tag("TestcontainersTests")
40  class InsertTest {
41  
42    private static SqlSessionFactory sqlSessionFactory;
43  
44    @BeforeAll
45    static void setUp() throws Exception {
46      Configuration configuration = new Configuration();
47      Environment environment = new Environment("development", new JdbcTransactionFactory(),
48          PgContainer.getUnpooledDataSource());
49      configuration.setEnvironment(environment);
50      configuration.addMapper(InsertMapper.class);
51      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
52  
53      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
54          "org/apache/ibatis/submitted/keycolumn/CreateDB.sql");
55    }
56  
57    @Test
58    void testInsertAnnotated() {
59      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
60        InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
61        Name name = new Name();
62        name.setFirstName("Fred");
63        name.setLastName("Flintstone");
64  
65        int rows = mapper.insertNameAnnotated(name);
66  
67        assertNotNull(name.getId());
68        assertEquals(1, rows);
69      }
70    }
71  
72    @Test
73    void testInsertMapped() {
74      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
75        InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
76        Name name = new Name();
77        name.setFirstName("Fred");
78        name.setLastName("Flintstone");
79  
80        int rows = mapper.insertNameMapped(name);
81  
82        assertNotNull(name.getId());
83        assertEquals(1, rows);
84      }
85    }
86  
87    @Test
88    void testInsertMappedBatch() {
89      try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
90        InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
91        Name name = new Name();
92        name.setFirstName("Fred");
93        name.setLastName("Flintstone");
94        mapper.insertNameMapped(name);
95        Name name2 = new Name();
96        name2.setFirstName("Wilma");
97        name2.setLastName("Flintstone");
98        mapper.insertNameMapped(name2);
99        List<BatchResult> batchResults = sqlSession.flushStatements();
100       assertNotNull(name.getId());
101       assertNotNull(name2.getId());
102       assertEquals(1, batchResults.size());
103     }
104   }
105 
106 }