-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
StepBuilderTests.java
388 lines (337 loc) · 14 KB
/
StepBuilderTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.step.builder;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.annotation.AfterChunk;
import org.springframework.batch.core.annotation.AfterChunkError;
import org.springframework.batch.core.annotation.AfterProcess;
import org.springframework.batch.core.annotation.AfterRead;
import org.springframework.batch.core.annotation.AfterStep;
import org.springframework.batch.core.annotation.AfterWrite;
import org.springframework.batch.core.annotation.BeforeChunk;
import org.springframework.batch.core.annotation.BeforeProcess;
import org.springframework.batch.core.annotation.BeforeRead;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.core.annotation.BeforeWrite;
import org.springframework.batch.core.configuration.xml.DummyItemReader;
import org.springframework.batch.core.configuration.xml.DummyItemWriter;
import org.springframework.batch.core.job.SimpleJob;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.item.support.ListItemWriter;
import org.springframework.batch.item.support.PassThroughItemProcessor;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.lang.Nullable;
import org.springframework.transaction.PlatformTransactionManager;
import static org.junit.Assert.assertEquals;
/**
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Parikshit Dutta
*
*/
public class StepBuilderTests {
private JobRepository jobRepository;
@Before
public void setUp() throws Exception {
EmbeddedDatabase embeddedDatabase = new EmbeddedDatabaseBuilder()
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.build();
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(embeddedDatabase);
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(embeddedDatabase);
factory.setTransactionManager(transactionManager);
factory.afterPropertiesSet();
this.jobRepository = factory.getObject();
}
@Test
public void test() throws Exception {
StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution(
"step");
jobRepository.add(execution);
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
TaskletStepBuilder builder = new StepBuilder("step").repository(jobRepository)
.transactionManager(transactionManager).tasklet((contribution, chunkContext) -> null);
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
}
@Test
public void testListeners() throws Exception {
StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution("step");
jobRepository.add(execution);
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
TaskletStepBuilder builder = new StepBuilder("step")
.repository(jobRepository)
.transactionManager(transactionManager)
.listener(new InterfaceBasedStepExecutionListener())
.listener(new AnnotationBasedStepExecutionListener())
.tasklet((contribution, chunkContext) -> null);
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(1, InterfaceBasedStepExecutionListener.beforeStepCount);
assertEquals(1, InterfaceBasedStepExecutionListener.afterStepCount);
assertEquals(1, AnnotationBasedStepExecutionListener.beforeStepCount);
assertEquals(1, AnnotationBasedStepExecutionListener.afterStepCount);
assertEquals(1, AnnotationBasedStepExecutionListener.beforeChunkCount);
assertEquals(1, AnnotationBasedStepExecutionListener.afterChunkCount);
}
@Test
public void testAnnotationBasedChunkListenerForTaskletStep() throws Exception {
StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution("step");
jobRepository.add(execution);
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
TaskletStepBuilder builder = new StepBuilder("step")
.repository(jobRepository)
.transactionManager(transactionManager)
.tasklet((contribution, chunkContext) -> null)
.listener(new AnnotationBasedChunkListener());
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(1, AnnotationBasedChunkListener.beforeChunkCount);
assertEquals(1, AnnotationBasedChunkListener.afterChunkCount);
}
@Test
public void testAnnotationBasedChunkListenerForSimpleTaskletStep() throws Exception {
StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution("step");
jobRepository.add(execution);
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
SimpleStepBuilder<Object, Object> builder = new StepBuilder("step")
.repository(jobRepository)
.transactionManager(transactionManager)
.chunk(5)
.reader(new DummyItemReader())
.writer(new DummyItemWriter())
.listener(new AnnotationBasedChunkListener());
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(1, AnnotationBasedChunkListener.beforeChunkCount);
assertEquals(1, AnnotationBasedChunkListener.afterChunkCount);
}
@Test
public void testAnnotationBasedChunkListenerForFaultTolerantTaskletStep() throws Exception {
StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution("step");
jobRepository.add(execution);
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
SimpleStepBuilder<Object, Object> builder = new StepBuilder("step")
.repository(jobRepository)
.transactionManager(transactionManager)
.chunk(5)
.reader(new DummyItemReader())
.writer(new DummyItemWriter())
.faultTolerant()
.listener(new AnnotationBasedChunkListener()); // TODO should this return FaultTolerantStepBuilder?
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(1, AnnotationBasedChunkListener.beforeChunkCount);
assertEquals(1, AnnotationBasedChunkListener.afterChunkCount);
}
@Test
public void testAnnotationBasedChunkListenerForJobStepBuilder() throws Exception {
StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution("step");
jobRepository.add(execution);
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
SimpleJob job = new SimpleJob("job");
job.setJobRepository(jobRepository);
JobStepBuilder builder = new StepBuilder("step")
.repository(jobRepository)
.transactionManager(transactionManager)
.job(job)
.listener(new AnnotationBasedChunkListener());
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
// it makes no sense to register a ChunkListener on a step which is not of type tasklet, so it should not be invoked
assertEquals(0, AnnotationBasedChunkListener.beforeChunkCount);
assertEquals(0, AnnotationBasedChunkListener.afterChunkCount);
}
@Test
public void testItemListeners() throws Exception {
StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution("step");
jobRepository.add(execution);
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
List<String> items = Arrays.asList("1", "2", "3");
ItemReader<String> reader = new ListItemReader<>(items);
SimpleStepBuilder<String, String> builder = new StepBuilder("step")
.repository(jobRepository)
.transactionManager(transactionManager)
.<String, String>chunk(3)
.reader(reader)
.processor(new PassThroughItemProcessor<>())
.writer(new DummyItemWriter())
.listener(new AnnotationBasedStepExecutionListener());
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(1, AnnotationBasedStepExecutionListener.beforeStepCount);
assertEquals(1, AnnotationBasedStepExecutionListener.afterStepCount);
assertEquals(4, AnnotationBasedStepExecutionListener.beforeReadCount);
assertEquals(3, AnnotationBasedStepExecutionListener.afterReadCount);
assertEquals(3, AnnotationBasedStepExecutionListener.beforeProcessCount);
assertEquals(3, AnnotationBasedStepExecutionListener.afterProcessCount);
assertEquals(1, AnnotationBasedStepExecutionListener.beforeWriteCount);
assertEquals(1, AnnotationBasedStepExecutionListener.afterWriteCount);
assertEquals(2, AnnotationBasedStepExecutionListener.beforeChunkCount);
assertEquals(2, AnnotationBasedStepExecutionListener.afterChunkCount);
}
@Test
public void testFunctions() throws Exception {
assertStepFunctions(false);
}
@Test
public void testFunctionsWithFaultTolerantStep() throws Exception {
assertStepFunctions(true);
}
private void assertStepFunctions(boolean faultTolerantStep) throws Exception {
StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution("step");
jobRepository.add(execution);
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
List<Long> items = Arrays.asList(1L, 2L, 3L);
ItemReader<Long> reader = new ListItemReader<>(items);
ListItemWriter<String> itemWriter = new ListItemWriter<>();
SimpleStepBuilder<Object, String> builder = new StepBuilder("step")
.repository(jobRepository)
.transactionManager(transactionManager)
.<Object, String>chunk(3)
.reader(reader)
.processor(Object::toString)
.writer(itemWriter)
.listener(new AnnotationBasedStepExecutionListener());
if (faultTolerantStep) {
builder = builder.faultTolerant();
}
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
List<? extends String> writtenItems = itemWriter.getWrittenItems();
assertEquals("1", writtenItems.get(0));
assertEquals("2", writtenItems.get(1));
assertEquals("3", writtenItems.get(2));
}
public static class InterfaceBasedStepExecutionListener implements StepExecutionListener {
static int beforeStepCount = 0;
static int afterStepCount = 0;
@Override
public void beforeStep(StepExecution stepExecution) {
beforeStepCount++;
}
@Nullable
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
afterStepCount++;
return stepExecution.getExitStatus();
}
}
@SuppressWarnings("unused")
public static class AnnotationBasedStepExecutionListener {
static int beforeStepCount = 0;
static int afterStepCount = 0;
static int beforeReadCount = 0;
static int afterReadCount = 0;
static int beforeProcessCount = 0;
static int afterProcessCount = 0;
static int beforeWriteCount = 0;
static int afterWriteCount = 0;
static int beforeChunkCount = 0;
static int afterChunkCount = 0;
public AnnotationBasedStepExecutionListener() {
beforeStepCount = 0;
afterStepCount = 0;
beforeReadCount = 0;
afterReadCount = 0;
beforeProcessCount = 0;
afterProcessCount = 0;
beforeWriteCount = 0;
afterWriteCount = 0;
beforeChunkCount = 0;
afterChunkCount = 0;
}
@BeforeStep
public void beforeStep() {
beforeStepCount++;
}
@AfterStep
public ExitStatus afterStep(StepExecution stepExecution) {
afterStepCount++;
return stepExecution.getExitStatus();
}
@BeforeRead
public void beforeRead() {
beforeReadCount++;
}
@AfterRead
public void afterRead() {
afterReadCount++;
}
@BeforeProcess
public void beforeProcess() {
beforeProcessCount++;
}
@AfterProcess
public void afterProcess() {
afterProcessCount++;
}
@BeforeWrite
public void beforeWrite() {
beforeWriteCount++;
}
@AfterWrite
public void setAfterWrite() {
afterWriteCount++;
}
@BeforeChunk
public void beforeChunk() {
beforeChunkCount++;
}
@AfterChunk
public void afterChunk() {
afterChunkCount++;
}
}
public static class AnnotationBasedChunkListener {
static int beforeChunkCount = 0;
static int afterChunkCount = 0;
static int afterChunkErrorCount = 0;
public AnnotationBasedChunkListener() {
beforeChunkCount = 0;
afterChunkCount = 0;
afterChunkErrorCount = 0;
}
@BeforeChunk
public void beforeChunk() {
beforeChunkCount++;
}
@AfterChunk
public void afterChunk() {
afterChunkCount++;
}
@AfterChunkError
public void afterChunkError() {
afterChunkErrorCount++;
}
}
}