Skip to content

Commit 46ceda4

Browse files
committed
Add AbstractTaskletStepBuilder copy constructor and testcode
This commit includes tests for the copy constructor of AbstractTaskletStepBuilder and for the faultTolerant method, specifically after taskExecutor has been set.
1 parent 5712b8a commit 46ceda4

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/AbstractTaskletStepBuilder.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* @author Dave Syer
5050
* @author Michael Minella
5151
* @author Mahmoud Ben Hassine
52+
* @author Ilpyo Yang
5253
* @since 2.2
5354
* @param <B> the type of builder represented
5455
*/
@@ -74,6 +75,23 @@ public AbstractTaskletStepBuilder(StepBuilderHelper<?> parent) {
7475
super(parent);
7576
}
7677

78+
/**
79+
* Create a new builder initialized with any properties in the parent. The parent is
80+
* copied, so it can be re-used.
81+
* @param parent a parent helper containing common step properties
82+
*/
83+
protected AbstractTaskletStepBuilder(AbstractTaskletStepBuilder<?> parent) {
84+
super(parent);
85+
this.chunkListeners = parent.chunkListeners;
86+
this.stepOperations = parent.stepOperations;
87+
this.transactionManager = parent.transactionManager;
88+
this.transactionAttribute = parent.transactionAttribute;
89+
this.streams.addAll(parent.streams);
90+
this.exceptionHandler = parent.exceptionHandler;
91+
this.throttleLimit = parent.throttleLimit;
92+
this.taskExecutor = parent.taskExecutor;
93+
}
94+
7795
protected abstract Tasklet createTasklet();
7896

7997
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2020-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.batch.core.step.builder;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.mockito.Mockito.mock;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.platform.commons.util.ReflectionUtils;
24+
import org.springframework.batch.core.repository.JobRepository;
25+
import org.springframework.batch.core.step.tasklet.TaskletStep;
26+
import org.springframework.batch.item.ItemProcessor;
27+
import org.springframework.batch.item.ItemReader;
28+
import org.springframework.batch.item.ItemWriter;
29+
import org.springframework.batch.repeat.support.RepeatTemplate;
30+
import org.springframework.core.task.SimpleAsyncTaskExecutor;
31+
import org.springframework.transaction.PlatformTransactionManager;
32+
33+
/**
34+
* Test cases for verifying the {@link AbstractTaskletStepBuilder} and faultTolerant() functionality.
35+
*
36+
* @author Jérôme Navez
37+
* @author Ilpyo Yang
38+
*/
39+
40+
class AbstractTaskletStepBuilderTests {
41+
private final PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class);
42+
private final JobRepository jobRepository = mock(JobRepository.class);
43+
private final ItemReader itemReader = mock(ItemReader.class);
44+
private final ItemProcessor itemProcessor = mock(ItemProcessor.class);
45+
private final ItemWriter itemWriter = mock(ItemWriter.class);
46+
47+
private final SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
48+
49+
@Test
50+
void testCopyConstractorFaultTolerant() {
51+
TaskletStep step1 = new StepBuilder("step-name", jobRepository)
52+
.chunk(10, transactionManager)
53+
.reader(itemReader)
54+
.processor(itemProcessor)
55+
.writer(itemWriter)
56+
.faultTolerant()
57+
.taskExecutor(taskExecutor)
58+
.build();
59+
60+
TaskletStep step2 = new StepBuilder("step-name", jobRepository)
61+
.chunk(10, transactionManager)
62+
.taskExecutor(taskExecutor)
63+
.reader(itemReader)
64+
.processor(itemProcessor)
65+
.writer(itemWriter)
66+
.faultTolerant()
67+
.build();
68+
69+
RepeatTemplate stepOperations1 = (RepeatTemplate) ReflectionUtils
70+
.readFieldValue(TaskletStep.class, "stepOperations", step1).get();
71+
RepeatTemplate stepOperations2 = (RepeatTemplate) ReflectionUtils
72+
.readFieldValue(TaskletStep.class, "stepOperations", step2).get();
73+
74+
assertEquals(stepOperations1.getClass(), stepOperations2.getClass());
75+
}
76+
}

0 commit comments

Comments
 (0)