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 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 = CursorReaderBatchConfiguration.class)
42 class SpringBatchCursorTest {
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(numberOfRowsProcessed(execution)).isEqualTo(2);
58
59
60 assertThat(upperCaseRowCount()).isEqualTo(2);
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 long upperCaseRowCount() {
75 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
76 PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
77
78 SelectStatementProvider selectStatement = SelectDSL.select(count())
79 .from(person)
80 .where(lastName, isEqualTo("FLINTSTONE"))
81 .build()
82 .render(RenderingStrategies.MYBATIS3);
83
84 return personMapper.count(selectStatement);
85 }
86 }
87 }