1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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 }