View Javadoc
1   /*
2    *    Copyright 2016-2025 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 examples.springbatch.paging;
17  
18  import static examples.springbatch.mapper.PersonDynamicSqlSupport.*;
19  import static org.assertj.core.api.Assertions.assertThat;
20  import static org.mybatis.dynamic.sql.SqlBuilder.count;
21  import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
22  
23  import org.apache.ibatis.session.SqlSession;
24  import org.apache.ibatis.session.SqlSessionFactory;
25  import org.junit.jupiter.api.Test;
26  import org.mybatis.dynamic.sql.render.RenderingStrategies;
27  import org.mybatis.dynamic.sql.select.SelectDSL;
28  import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
29  import org.springframework.batch.core.ExitStatus;
30  import org.springframework.batch.core.JobExecution;
31  import org.springframework.batch.core.StepExecution;
32  import org.springframework.batch.item.ExecutionContext;
33  import org.springframework.batch.test.JobLauncherTestUtils;
34  import org.springframework.batch.test.context.SpringBatchTest;
35  import org.springframework.beans.factory.annotation.Autowired;
36  import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
37  
38  import examples.springbatch.mapper.PersonMapper;
39  
40  @SpringBatchTest
41  @SpringJUnitConfig(classes = PagingReaderBatchConfiguration.class)
42  class SpringBatchPagingTest {
43  
44      @Autowired
45      private JobLauncherTestUtils jobLauncherTestUtils;
46  
47      @Autowired
48      private SqlSessionFactory sqlSessionFactory;
49  
50      @Test
51      void testThatRowsAreTransformedToUpperCase() throws Exception {
52          // starting condition
53          assertThat(upperCaseRowCount()).isZero();
54  
55          JobExecution execution = jobLauncherTestUtils.launchJob();
56          assertThat(execution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED);
57          assertThat(numberOfChunks(execution)).isEqualTo(14);
58          assertThat(numberOfRowsProcessed(execution)).isEqualTo(93);
59  
60          // ending condition
61          assertThat(upperCaseRowCount()).isEqualTo(93);
62      }
63  
64      private int numberOfRowsProcessed(JobExecution jobExecution) {
65          return jobExecution.getStepExecutions().stream()
66                  .map(StepExecution::getExecutionContext)
67                  .mapToInt(this::getRowCount)
68                  .sum();
69      }
70  
71      private int getRowCount(ExecutionContext executionContext) {
72          return executionContext.getInt("row_count", 0);
73      }
74  
75      private int numberOfChunks(JobExecution jobExecution) {
76          return jobExecution.getStepExecutions().stream()
77                  .map(StepExecution::getExecutionContext)
78                  .mapToInt(this::getChunkCount)
79                  .sum();
80      }
81  
82      private int getChunkCount(ExecutionContext executionContext) {
83          return executionContext.getInt("chunk_count", 0);
84      }
85  
86      private long upperCaseRowCount() {
87          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
88              PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
89  
90              SelectStatementProvider selectStatement = SelectDSL.select(count())
91                      .from(person)
92                      .where(lastName, isEqualTo("SMITH"))
93                      .build()
94                      .render(RenderingStrategies.MYBATIS3);
95  
96              return personMapper.count(selectStatement);
97          }
98      }
99  }