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