Skip to content

Commit ce4ec96

Browse files
author
Fabrice Bibonne
committed
fix(step name unicity WITHIN A FLOW)
1 parent a021d1d commit ce4ec96

File tree

7 files changed

+32
-45
lines changed

7 files changed

+32
-45
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -374,20 +374,6 @@ public final void execute(JobExecution execution) {
374374

375375
protected abstract void checkStepNamesUnicity() throws AlreadyUsedStepNameException ;
376376

377-
private Optional<String> findFirstDoubleElementInList(List<String> strings) {
378-
if (strings==null){
379-
return Optional.empty();
380-
}
381-
Set<String> alreadyChecked=new HashSet<>();
382-
for (String value:strings){
383-
if (alreadyChecked.contains(value)){
384-
return Optional.of(value);
385-
}
386-
alreadyChecked.add(value);
387-
}
388-
return Optional.empty();
389-
}
390-
391377
private void stopObservation(JobExecution execution, Observation observation) {
392378
List<Throwable> throwables = execution.getFailureExceptions();
393379
if (!throwables.isEmpty()) {

spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleJob.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.springframework.batch.core.StartLimitExceededException;
2626
import org.springframework.batch.core.Step;
2727
import org.springframework.batch.core.StepExecution;
28-
import org.springframework.batch.core.job.builder.AlreadyUsedStepNameException;
2928
import org.springframework.batch.core.repository.JobRestartException;
3029
import org.springframework.batch.core.step.StepLocator;
3130

@@ -145,9 +144,8 @@ protected void doExecute(JobExecution execution)
145144
}
146145

147146
@Override
148-
protected void checkStepNamesUnicity() throws AlreadyUsedStepNameException {
149-
Map<String, Step> map = new HashMap<>();
150-
steps.forEach(step->{addToMapCheckingUnicity(map, step, step.getName());});
147+
protected void checkStepNamesUnicity() {
148+
//noop : steps of SimpleJob can share the same name
151149
}
152150

153151
}

spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowJob.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,20 @@ public Step getStep(String stepName) {
8585
*/
8686
private void init() {
8787
if (!initialized) {
88-
findStepsThrowingIfNameNotUnique(flow, stepMap);
88+
findStepsThrowingIfNameNotUnique(flow);
8989
initialized = true;
9090
}
9191
}
9292

93-
private void findStepsThrowingIfNameNotUnique(Flow flow, Map<String, Step> map) {
93+
private void findStepsThrowingIfNameNotUnique(Flow flow) {
9494

9595
for (State state : flow.getStates()) {
9696
if (state instanceof StepLocator locator) {
9797
for (String name : locator.getStepNames()) {
98-
addToMapCheckingUnicity(map, locator.getStep(name), name);
98+
addToMapCheckingUnicity(this.stepMap, locator.getStep(name), name);
9999
}
100100
}
101-
//TODO remove this else bock ? not executed during tests : the only State wich implements StepHolder is StepState which implements also StepLocator
101+
//TODO remove this else bock ? not executed during tests : the only State which implements StepHolder is StepState which already implements StepLocator
102102
/*
103103
Tests Coverage
104104
Hits : 30
@@ -108,11 +108,11 @@ private void findStepsThrowingIfNameNotUnique(Flow flow, Map<String, Step> map)
108108
*/
109109
else if (state instanceof StepHolder stepHolder) {
110110
Step step = stepHolder.getStep();
111-
addToMapCheckingUnicity(map, step, step.getName());
111+
addToMapCheckingUnicity(this.stepMap, step, step.getName());
112112
}
113113
else if (state instanceof FlowHolder flowHolder) {
114114
for (Flow subflow : flowHolder.getFlows()) {
115-
findStepsThrowingIfNameNotUnique(subflow, map);
115+
findStepsThrowingIfNameNotUnique(subflow);
116116
}
117117
}
118118
}

spring-batch-core/src/test/java/org/springframework/batch/core/job/ExtendedAbstractJobTests.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,20 @@
1717

1818
import org.junit.jupiter.api.BeforeEach;
1919
import org.junit.jupiter.api.Test;
20-
import org.springframework.batch.core.BatchStatus;
21-
import org.springframework.batch.core.JobExecution;
22-
import org.springframework.batch.core.JobExecutionException;
23-
import org.springframework.batch.core.JobInterruptedException;
24-
import org.springframework.batch.core.JobParameters;
25-
import org.springframework.batch.core.JobParametersInvalidException;
26-
import org.springframework.batch.core.Step;
27-
import org.springframework.batch.core.StepExecution;
28-
import org.springframework.batch.core.job.builder.AlreadyUsedStepNameException;
20+
import org.springframework.batch.core.*;
2921
import org.springframework.batch.core.repository.JobRepository;
3022
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
3123
import org.springframework.batch.core.step.StepSupport;
32-
import org.springframework.jdbc.support.JdbcTransactionManager;
3324
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
3425
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
26+
import org.springframework.jdbc.support.JdbcTransactionManager;
3527
import org.springframework.lang.Nullable;
3628

3729
import java.time.LocalDateTime;
3830
import java.util.Collection;
3931
import java.util.Collections;
40-
import java.util.List;
4132

42-
import static org.junit.jupiter.api.Assertions.assertEquals;
43-
import static org.junit.jupiter.api.Assertions.assertFalse;
44-
import static org.junit.jupiter.api.Assertions.assertNull;
45-
import static org.junit.jupiter.api.Assertions.assertThrows;
46-
import static org.junit.jupiter.api.Assertions.assertTrue;
33+
import static org.junit.jupiter.api.Assertions.*;
4734

4835
/**
4936
* @author Dave Syer
@@ -218,8 +205,7 @@ protected void doExecute(JobExecution execution) throws JobExecutionException {
218205
}
219206

220207
@Override
221-
protected void checkStepNamesUnicity() throws AlreadyUsedStepNameException {
222-
208+
protected void checkStepNamesUnicity(){
223209
}
224210

225211
@Override

spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,25 @@ void testGetMultipleJobParameters() throws Exception {
513513

514514
}
515515

516+
@Test
517+
public void testMultipleStepsWithSameName(){
518+
job.setName("MultipleStepsWithSameName");
519+
String sharedName="stepName";
520+
final List<String> executionsCallbacks=new ArrayList<>();
521+
StubStep sharedNameStep1=new StubStep(sharedName, jobRepository);
522+
sharedNameStep1.setCallback(()->executionsCallbacks.add("step1"));
523+
job.addStep(sharedNameStep1);
524+
StubStep sharedNameStep2=new StubStep(sharedName, jobRepository);
525+
sharedNameStep2.setCallback(()->executionsCallbacks.add("step2"));
526+
job.addStep(sharedNameStep2);
527+
StubStep sharedNameStep3=new StubStep(sharedName, jobRepository);
528+
sharedNameStep3.setCallback(()->executionsCallbacks.add("step3"));
529+
job.addStep(sharedNameStep3);
530+
job.execute(jobExecution);
531+
assertEquals(List.of("step1", "step2", "step3"), executionsCallbacks);
532+
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
533+
}
534+
516535
/*
517536
* Check JobRepository to ensure status is being saved.
518537
*/

spring-batch-core/src/test/java/org/springframework/batch/core/job/builder/FlowJobBuilderTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555

5656
import static org.junit.Assert.assertTrue;
5757
import static org.junit.jupiter.api.Assertions.assertEquals;
58-
import static org.junit.jupiter.api.Assertions.assertThrows;
5958

6059
/**
6160
* @author Dave Syer

spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/SimpleJobOperatorTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.springframework.batch.core.explore.JobExplorer;
4444
import org.springframework.batch.core.job.AbstractJob;
4545
import org.springframework.batch.core.job.JobSupport;
46-
import org.springframework.batch.core.job.builder.AlreadyUsedStepNameException;
4746
import org.springframework.batch.core.launch.JobInstanceAlreadyExistsException;
4847
import org.springframework.batch.core.launch.NoSuchJobException;
4948
import org.springframework.batch.core.launch.NoSuchJobExecutionException;
@@ -457,7 +456,7 @@ protected void doExecute(JobExecution execution) throws JobExecutionException {
457456
}
458457

459458
@Override
460-
protected void checkStepNamesUnicity() throws AlreadyUsedStepNameException {
459+
protected void checkStepNamesUnicity() {
461460
}
462461

463462
}

0 commit comments

Comments
 (0)