forked from spring-projects/spring-batch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlowJobBuilderTests.java
318 lines (281 loc) · 12.4 KB
/
FlowJobBuilderTests.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
/*
* 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.job.builder;
import java.util.Arrays;
import javax.sql.DataSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInterruptedException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.UnexpectedJobExecutionException;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.JobScope;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.core.step.StepSupport;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.lang.Nullable;
/**
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
class FlowJobBuilderTests {
private JobRepository jobRepository;
private JobExecution execution;
private final StepSupport step1 = new StepSupport("step1") {
@Override
public void execute(StepExecution stepExecution)
throws JobInterruptedException, UnexpectedJobExecutionException {
stepExecution.upgradeStatus(BatchStatus.COMPLETED);
stepExecution.setExitStatus(ExitStatus.COMPLETED);
jobRepository.update(stepExecution);
}
};
private final StepSupport fails = new StepSupport("fails") {
@Override
public void execute(StepExecution stepExecution)
throws JobInterruptedException, UnexpectedJobExecutionException {
stepExecution.upgradeStatus(BatchStatus.FAILED);
stepExecution.setExitStatus(ExitStatus.FAILED);
jobRepository.update(stepExecution);
}
};
private final StepSupport step2 = new StepSupport("step2") {
@Override
public void execute(StepExecution stepExecution)
throws JobInterruptedException, UnexpectedJobExecutionException {
stepExecution.upgradeStatus(BatchStatus.COMPLETED);
stepExecution.setExitStatus(ExitStatus.COMPLETED);
jobRepository.update(stepExecution);
}
};
private final StepSupport step3 = new StepSupport("step3") {
@Override
public void execute(StepExecution stepExecution)
throws JobInterruptedException, UnexpectedJobExecutionException {
stepExecution.upgradeStatus(BatchStatus.COMPLETED);
stepExecution.setExitStatus(ExitStatus.COMPLETED);
jobRepository.update(stepExecution);
}
};
@BeforeEach
void init() throws Exception {
EmbeddedDatabase embeddedDatabase = new EmbeddedDatabaseBuilder()
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").build();
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(embeddedDatabase);
factory.setTransactionManager(new JdbcTransactionManager(embeddedDatabase));
factory.afterPropertiesSet();
jobRepository = factory.getObject();
execution = jobRepository.createJobExecution("flow", new JobParameters());
}
@Test
void testBuildOnOneLine() {
FlowJobBuilder builder = new JobBuilder("flow").repository(jobRepository).start(step1).on("COMPLETED").to(step2)
.end().preventRestart();
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(2, execution.getStepExecutions().size());
}
@Test
void testBuildSingleFlow() {
Flow flow = new FlowBuilder<Flow>("subflow").from(step1).next(step2).build();
FlowJobBuilder builder = new JobBuilder("flow").repository(jobRepository).start(flow).end().preventRestart();
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(2, execution.getStepExecutions().size());
}
@Test
void testBuildOverTwoLines() {
FlowJobBuilder builder = new JobBuilder("flow").repository(jobRepository).start(step1).on("COMPLETED").to(step2)
.end();
builder.preventRestart();
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(2, execution.getStepExecutions().size());
}
@Test
void testBuildSubflow() {
Flow flow = new FlowBuilder<Flow>("subflow").from(step1).end();
JobFlowBuilder builder = new JobBuilder("flow").repository(jobRepository).start(flow);
builder.on("COMPLETED").to(step2);
builder.end().preventRestart().build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(2, execution.getStepExecutions().size());
}
@Test
void testBuildSplit() {
Flow flow = new FlowBuilder<Flow>("subflow").from(step1).end();
SimpleJobBuilder builder = new JobBuilder("flow").repository(jobRepository).start(step2);
builder.split(new SimpleAsyncTaskExecutor()).add(flow).end();
builder.preventRestart().build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(2, execution.getStepExecutions().size());
}
@Test
void testBuildSplitUsingStartAndAdd_BATCH_2346() {
Flow subflow1 = new FlowBuilder<Flow>("subflow1").from(step2).end();
Flow subflow2 = new FlowBuilder<Flow>("subflow2").from(step3).end();
Flow splitflow = new FlowBuilder<Flow>("splitflow").start(subflow1).split(new SimpleAsyncTaskExecutor())
.add(subflow2).build();
FlowJobBuilder builder = new JobBuilder("flow").repository(jobRepository).start(splitflow).end();
builder.preventRestart().build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(2, execution.getStepExecutions().size());
}
@Test
void testBuildSplit_BATCH_2282() {
Flow flow1 = new FlowBuilder<Flow>("subflow1").from(step1).end();
Flow flow2 = new FlowBuilder<Flow>("subflow2").from(step2).end();
Flow splitFlow = new FlowBuilder<Flow>("splitflow").split(new SimpleAsyncTaskExecutor()).add(flow1, flow2)
.build();
FlowJobBuilder builder = new JobBuilder("flow").repository(jobRepository).start(splitFlow).end();
builder.preventRestart().build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(2, execution.getStepExecutions().size());
}
@Test
void testBuildDecision() {
JobExecutionDecider decider = new JobExecutionDecider() {
private int count = 0;
@Override
public FlowExecutionStatus decide(JobExecution jobExecution, @Nullable StepExecution stepExecution) {
count++;
return count < 2 ? new FlowExecutionStatus("ONGOING") : FlowExecutionStatus.COMPLETED;
}
};
step1.setAllowStartIfComplete(true);
SimpleJobBuilder builder = new JobBuilder("flow").repository(jobRepository).start(step1);
builder.next(decider).on("COMPLETED").end().from(decider).on("*").to(step1).end();
builder.preventRestart().build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(2, execution.getStepExecutions().size());
}
@Test
void testBuildWithIntermediateSimpleJob() {
SimpleJobBuilder builder = new JobBuilder("flow").repository(jobRepository).start(step1);
builder.on("COMPLETED").to(step2).end();
builder.preventRestart();
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(2, execution.getStepExecutions().size());
}
@Test
void testBuildWithIntermediateSimpleJobTwoSteps() {
SimpleJobBuilder builder = new JobBuilder("flow").repository(jobRepository).start(step1).next(step2);
builder.on("FAILED").to(step3).end();
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(2, execution.getStepExecutions().size());
}
@Test
void testBuildWithCustomEndState() {
SimpleJobBuilder builder = new JobBuilder("flow").repository(jobRepository).start(step1);
builder.on("COMPLETED").end("FOO");
builder.preventRestart();
builder.build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals("FOO", execution.getExitStatus().getExitCode());
assertEquals(1, execution.getStepExecutions().size());
}
@Test
void testBuildWithStop() {
SimpleJobBuilder builder = new JobBuilder("flow").repository(jobRepository).start(step1);
builder.on("COMPLETED").stop();
builder.preventRestart();
builder.build().execute(execution);
assertEquals(BatchStatus.STOPPED, execution.getStatus());
assertEquals("STOPPED", execution.getExitStatus().getExitCode());
assertEquals(1, execution.getStepExecutions().size());
}
@Test
void testBuildWithStopAndRestart() throws Exception {
SimpleJobBuilder builder = new JobBuilder("flow").repository(jobRepository).start(fails);
builder.on("FAILED").stopAndRestart(step2);
Job job = builder.build();
job.execute(execution);
assertEquals(BatchStatus.STOPPED, execution.getStatus());
assertEquals(1, execution.getStepExecutions().size());
execution = jobRepository.createJobExecution("flow", new JobParameters());
job.execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(1, execution.getStepExecutions().size());
assertEquals("step2", execution.getStepExecutions().iterator().next().getStepName());
}
@Test
void testBuildWithJobScopedStep() throws Exception {
// given
ApplicationContext context = new AnnotationConfigApplicationContext(JobConfiguration.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
JobParameters jobParameters = new JobParametersBuilder().addLong("chunkSize", 2L).toJobParameters();
// when
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
// then
assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
}
@EnableBatchProcessing
@Configuration
static class JobConfiguration {
@Bean
@JobScope
public Step step(StepBuilderFactory stepBuilderFactory,
@Value("#{jobParameters['chunkSize']}") Integer chunkSize) {
return stepBuilderFactory.get("step").<Integer, Integer>chunk(chunkSize)
.reader(new ListItemReader<>(Arrays.asList(1, 2, 3, 4))).writer(new ItemWriter<Integer>() {
@Override
public void write(Chunk<? extends Integer> chunk) throws Exception {
}
}).build();
}
@Bean
public Job job(JobBuilderFactory jobBuilderFactory) {
return jobBuilderFactory.get("job").flow(step(null, null)).build().build();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").generateUniqueName(true).build();
}
}
}