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