|
| 1 | +/** |
| 2 | + * Copyright 2009-2015 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 org.apache.ibatis.BaseDataTest; |
| 19 | +import org.apache.ibatis.domain.blog.Author; |
| 20 | +import org.apache.ibatis.domain.blog.Post; |
| 21 | +import org.apache.ibatis.domain.blog.Section; |
| 22 | +import org.apache.ibatis.executor.BatchResult; |
| 23 | +import org.apache.ibatis.mapping.Environment; |
| 24 | +import org.apache.ibatis.session.*; |
| 25 | +import org.apache.ibatis.transaction.TransactionFactory; |
| 26 | +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; |
| 27 | +import org.junit.BeforeClass; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import javax.sql.DataSource; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +import static org.hamcrest.core.Is.is; |
| 35 | +import static org.junit.Assert.assertNotNull; |
| 36 | +import static org.junit.Assert.assertThat; |
| 37 | + |
| 38 | +public class FlushTest { |
| 39 | + private static SqlSessionFactory sqlSessionFactory; |
| 40 | + |
| 41 | + @BeforeClass |
| 42 | + public 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 | + public void invokeFlushStatementsViaMapper() { |
| 56 | + |
| 57 | + SqlSession session = sqlSessionFactory.openSession(); |
| 58 | + |
| 59 | + try { |
| 60 | + |
| 61 | + BoundAuthorMapper mapper = session.getMapper(BoundAuthorMapper.class); |
| 62 | + Author author = new Author(- 1, "cbegin", "******", "[email protected]", "N/A", Section. NEWS); |
| 63 | + List<Integer> ids = new ArrayList<Integer>(); |
| 64 | + mapper.insertAuthor(author); |
| 65 | + ids.add(author.getId()); |
| 66 | + mapper.insertAuthor(author); |
| 67 | + ids.add(author.getId()); |
| 68 | + mapper.insertAuthor(author); |
| 69 | + ids.add(author.getId()); |
| 70 | + mapper.insertAuthor(author); |
| 71 | + ids.add(author.getId()); |
| 72 | + mapper.insertAuthor(author); |
| 73 | + ids.add(author.getId()); |
| 74 | + |
| 75 | + // test |
| 76 | + List<BatchResult> results = mapper.flush(); |
| 77 | + |
| 78 | + assertThat(results.size(), is(1)); |
| 79 | + assertThat(results.get(0).getUpdateCounts().length, is(ids.size())); |
| 80 | + |
| 81 | + for (int id : ids) { |
| 82 | + Author selectedAuthor = mapper.selectAuthor(id); |
| 83 | + assertNotNull(id + " is not found.", selectedAuthor); |
| 84 | + } |
| 85 | + |
| 86 | + session.rollback(); |
| 87 | + } finally { |
| 88 | + session.close(); |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments