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.binding;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.junit.jupiter.api.Assertions.assertNotNull;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.sql.DataSource;
25  
26  import org.apache.ibatis.BaseDataTest;
27  import org.apache.ibatis.domain.blog.Author;
28  import org.apache.ibatis.domain.blog.Post;
29  import org.apache.ibatis.domain.blog.Section;
30  import org.apache.ibatis.executor.BatchResult;
31  import org.apache.ibatis.mapping.Environment;
32  import org.apache.ibatis.session.*;
33  import org.apache.ibatis.transaction.TransactionFactory;
34  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
35  import org.junit.jupiter.api.BeforeAll;
36  import org.junit.jupiter.api.Test;
37  
38  class FlushTest {
39      private static SqlSessionFactory sqlSessionFactory;
40  
41      @BeforeAll
42      static void setup() throws Exception {
43          DataSource dataSource = BaseDataTest.createBlogDataSource();
44          TransactionFactory transactionFactory = new JdbcTransactionFactory();
45          Environment environment = new Environment("Production", transactionFactory, dataSource);
46          Configuration configuration = new Configuration(environment);
47          configuration.setDefaultExecutorType(ExecutorType.BATCH);
48          configuration.getTypeAliasRegistry().registerAlias(Post.class);
49          configuration.getTypeAliasRegistry().registerAlias(Author.class);
50          configuration.addMapper(BoundAuthorMapper.class);
51          sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
52      }
53  
54      @Test
55      void invokeFlushStatementsViaMapper() {
56          try (SqlSession session = sqlSessionFactory.openSession()) {
57  
58              BoundAuthorMapper mapper = session.getMapper(BoundAuthorMapper.class);
59              Author author = new Author(-1, "cbegin", "******", "cbegin@nowhere.com", "N/A", Section.NEWS);
60              List<Integer> ids = new ArrayList<>();
61              mapper.insertAuthor(author);
62              ids.add(author.getId());
63              mapper.insertAuthor(author);
64              ids.add(author.getId());
65              mapper.insertAuthor(author);
66              ids.add(author.getId());
67              mapper.insertAuthor(author);
68              ids.add(author.getId());
69              mapper.insertAuthor(author);
70              ids.add(author.getId());
71  
72              // test
73              List<BatchResult> results = mapper.flush();
74  
75              assertThat(results.size()).isEqualTo(1);
76              assertThat(results.get(0).getUpdateCounts().length).isEqualTo(ids.size());
77  
78              for (int id : ids) {
79                  Author selectedAuthor = mapper.selectAuthor(id);
80                  assertNotNull(selectedAuthor, id + " is not found.");
81              }
82  
83              session.rollback();
84          }
85  
86      }
87  
88  }