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.cursor;
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 = CursorReaderBatchConfiguration.class)
41  class SpringBatchCursorTest {
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(numberOfRowsProcessed(execution)).isEqualTo(2);
57  
58          // ending condition
59          assertThat(upperCaseRowCount()).isEqualTo(2);
60      }
61  
62      private int numberOfRowsProcessed(JobExecution jobExecution) {
63          return jobExecution.getStepExecutions().stream()
64                  .map(StepExecution::getExecutionContext)
65                  .mapToInt(this::getRowCount)
66                  .sum();
67      }
68  
69      private int getRowCount(ExecutionContext executionContext) {
70          return executionContext.getInt("row_count", 0);
71      }
72  
73      private long upperCaseRowCount() {
74          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
75              PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
76  
77              SelectStatementProvider selectStatement = SelectDSL.select(count())
78                      .from(person)
79                      .where(lastName, isEqualTo("FLINTSTONE"))
80                      .build()
81                      .render(RenderingStrategies.MYBATIS3);
82  
83              return personMapper.count(selectStatement);
84          }
85      }
86  }