Skip to content

Commit b8628d3

Browse files
author
Fabrice Bibonne
committed
test(check unicity of job name in a flow) failing test
1 parent bf3f00d commit b8628d3

File tree

2 files changed

+76
-37
lines changed

2 files changed

+76
-37
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2006-2024 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.job.builder;
18+
19+
/**
20+
* Exception to indicate the name of a step is already used by a different step in the same flow.
21+
* Step names must be unique within a flow definition because the search of the next step to find
22+
* relies on the step name
23+
*
24+
* @author Fabrice Bibonne
25+
*
26+
*/
27+
public class AlreadyUsedStepNameException extends RuntimeException{
28+
29+
public AlreadyUsedStepNameException(String name){
30+
super("the name "+name+" is already used");
31+
}
32+
33+
}

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

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,9 @@
1515
*/
1616
package org.springframework.batch.core.job.builder;
1717

18-
import java.util.Arrays;
19-
20-
import javax.sql.DataSource;
21-
22-
import static org.junit.jupiter.api.Assertions.assertEquals;
23-
2418
import org.junit.jupiter.api.BeforeEach;
2519
import org.junit.jupiter.api.Test;
26-
import org.springframework.batch.core.BatchStatus;
27-
import org.springframework.batch.core.ExitStatus;
28-
import org.springframework.batch.core.Job;
29-
import org.springframework.batch.core.JobExecution;
30-
import org.springframework.batch.core.JobInterruptedException;
31-
import org.springframework.batch.core.JobParameters;
32-
import org.springframework.batch.core.JobParametersBuilder;
33-
import org.springframework.batch.core.Step;
34-
import org.springframework.batch.core.StepExecution;
35-
import org.springframework.batch.core.UnexpectedJobExecutionException;
20+
import org.springframework.batch.core.*;
3621
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
3722
import org.springframework.batch.core.configuration.annotation.JobScope;
3823
import org.springframework.batch.core.job.flow.Flow;
@@ -51,12 +36,18 @@
5136
import org.springframework.context.annotation.Bean;
5237
import org.springframework.context.annotation.Configuration;
5338
import org.springframework.core.task.SimpleAsyncTaskExecutor;
54-
import org.springframework.jdbc.support.JdbcTransactionManager;
5539
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
5640
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
41+
import org.springframework.jdbc.support.JdbcTransactionManager;
5742
import org.springframework.lang.Nullable;
5843
import org.springframework.transaction.PlatformTransactionManager;
5944

45+
import javax.sql.DataSource;
46+
import java.util.Arrays;
47+
48+
import static org.junit.jupiter.api.Assertions.assertEquals;
49+
import static org.junit.jupiter.api.Assertions.assertThrows;
50+
6051
/**
6152
* @author Dave Syer
6253
* @author Mahmoud Ben Hassine
@@ -262,26 +253,6 @@ public FlowExecutionStatus decide(JobExecution jobExecution, @Nullable StepExecu
262253
assertEquals(1, execution.getStepExecutions().size());
263254
}
264255

265-
@Test
266-
void testBuildWithDeciderPriorityOnWildcardCount() {
267-
JobExecutionDecider decider = (jobExecution, stepExecution) -> new FlowExecutionStatus("COMPLETED_PARTIALLY");
268-
JobFlowBuilder builder = new JobBuilder("flow_priority", jobRepository).start(decider);
269-
builder.on("**").end();
270-
builder.on("*").fail();
271-
builder.build().preventRestart().build().execute(execution);
272-
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
273-
}
274-
275-
@Test
276-
void testBuildWithDeciderPriorityWithEqualWildcard() {
277-
JobExecutionDecider decider = (jobExecution, stepExecution) -> new FlowExecutionStatus("COMPLETED_PARTIALLY");
278-
JobFlowBuilder builder = new JobBuilder("flow_priority", jobRepository).start(decider);
279-
builder.on("COMPLETED*").end();
280-
builder.on("*").fail();
281-
builder.build().preventRestart().build().execute(execution);
282-
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
283-
}
284-
285256
@Test
286257
void testBuildWithDeciderPriority() {
287258
JobExecutionDecider decider = (jobExecution, stepExecution) -> new FlowExecutionStatus("COMPLETED_PARTIALLY");
@@ -383,6 +354,41 @@ void testBuildWithJobScopedStep() throws Exception {
383354
assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
384355
}
385356

357+
//https://github.com/spring-projects/spring-batch/issues/3757#issuecomment-1821593539
358+
@Test
359+
void testStepNamesMustBeUniqueWithinFlowDefinition() {
360+
Step conditionalStep = new StepSupport("conditionalStep") {
361+
@Override
362+
public void execute(StepExecution stepExecution) {
363+
stepExecution.upgradeStatus(BatchStatus.COMPLETED);
364+
stepExecution.setExitStatus(ExitStatus.COMPLETED);
365+
String exitStatus = (System.currentTimeMillis() % 2 == 0) ? "EVEN" : "ODD";
366+
stepExecution.setExitStatus(new ExitStatus(exitStatus));
367+
jobRepository.update(stepExecution);
368+
}
369+
};
370+
371+
StepSupport misnamedStep = new StepSupport(step3Name) {
372+
@Override
373+
public void execute(StepExecution stepExecution)
374+
throws UnexpectedJobExecutionException {
375+
376+
stepExecution.upgradeStatus(BatchStatus.COMPLETED);
377+
stepExecution.setExitStatus(ExitStatus.COMPLETED);
378+
jobRepository.update(stepExecution);
379+
}
380+
};
381+
382+
JobBuilder jobBuilder = new JobBuilder("flow", jobRepository);
383+
FlowBuilder<FlowJobBuilder> flowBuilder = jobBuilder.start(conditionalStep)
384+
.on("ODD").to(step2)
385+
.from(conditionalStep).on("EVEN").to(step3)
386+
.from(step3);
387+
assertThrows(AlreadyUsedStepNameException.class, () -> flowBuilder.next(misnamedStep));
388+
flowBuilder.end().build();
389+
}
390+
391+
386392
@EnableBatchProcessing
387393
@Configuration
388394
static class JobConfiguration {

0 commit comments

Comments
 (0)