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