11/*
2- * Copyright 2012-2022 the original author or authors.
2+ * Copyright 2012-2023 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1919
2020import org .junit .jupiter .api .Test ;
2121
22+ import org .springframework .batch .core .BatchStatus ;
2223import org .springframework .batch .core .ExitStatus ;
2324import org .springframework .batch .core .JobExecution ;
2425import org .springframework .batch .core .JobInterruptedException ;
3435import org .springframework .batch .core .step .StepSupport ;
3536
3637import static org .junit .jupiter .api .Assertions .assertEquals ;
38+ import static org .junit .jupiter .api .Assertions .assertFalse ;
3739
3840/**
3941 * @author Dave Syer
4042 * @author Michael Minella
4143 * @author Mahmoud Ben Hassine
44+ * @author Injae Kim
4245 *
4346 */
4447class FlowBuilderTests {
4548
4649 @ Test
47- void test () throws Exception {
50+ void testNext () throws Exception {
4851 FlowBuilder <Flow > builder = new FlowBuilder <>("flow" );
4952 JobRepository jobRepository = new JobRepositorySupport ();
5053 JobExecution execution = jobRepository .createJobExecution ("foo" , new JobParameters ());
51- builder .start (new StepSupport ("step" ) {
52- @ Override
53- public void execute (StepExecution stepExecution )
54- throws JobInterruptedException , UnexpectedJobExecutionException {
55- }
56- }).end ().start (new JobFlowExecutor (jobRepository , new SimpleStepHandler (jobRepository ), execution ));
54+
55+ builder .next (createCompleteStep ("stepA" ))
56+ .end ()
57+ .start (new JobFlowExecutor (jobRepository , new SimpleStepHandler (jobRepository ), execution ));
58+
59+ Iterator <StepExecution > stepExecutions = execution .getStepExecutions ().iterator ();
60+ assertEquals (stepExecutions .next ().getStepName (), "stepA" );
61+ assertFalse (stepExecutions .hasNext ());
62+ }
63+
64+ @ Test
65+ void testMultipleNext () throws Exception {
66+ FlowBuilder <Flow > builder = new FlowBuilder <>("flow" );
67+ JobRepository jobRepository = new JobRepositorySupport ();
68+ JobExecution execution = jobRepository .createJobExecution ("foo" , new JobParameters ());
69+
70+ builder .next (createCompleteStep ("stepA" ))
71+ .next (createCompleteStep ("stepB" ))
72+ .next (createCompleteStep ("stepC" ))
73+ .end ()
74+ .start (new JobFlowExecutor (jobRepository , new SimpleStepHandler (jobRepository ), execution ));
75+
76+ Iterator <StepExecution > stepExecutions = execution .getStepExecutions ().iterator ();
77+ assertEquals (stepExecutions .next ().getStepName (), "stepA" );
78+ assertEquals (stepExecutions .next ().getStepName (), "stepB" );
79+ assertEquals (stepExecutions .next ().getStepName (), "stepC" );
80+ assertFalse (stepExecutions .hasNext ());
81+ }
82+
83+ @ Test
84+ void testStart () throws Exception {
85+ FlowBuilder <Flow > builder = new FlowBuilder <>("flow" );
86+ JobRepository jobRepository = new JobRepositorySupport ();
87+ JobExecution execution = jobRepository .createJobExecution ("foo" , new JobParameters ());
88+
89+ builder .start (createCompleteStep ("stepA" ))
90+ .end ()
91+ .start (new JobFlowExecutor (jobRepository , new SimpleStepHandler (jobRepository ), execution ));
92+
93+ Iterator <StepExecution > stepExecutions = execution .getStepExecutions ().iterator ();
94+ assertEquals (stepExecutions .next ().getStepName (), "stepA" );
95+ assertFalse (stepExecutions .hasNext ());
96+ }
97+
98+ @ Test
99+ void testFrom () throws Exception {
100+ FlowBuilder <Flow > builder = new FlowBuilder <>("flow" );
101+ JobRepository jobRepository = new JobRepositorySupport ();
102+ JobExecution execution = jobRepository .createJobExecution ("foo" , new JobParameters ());
103+
104+ builder .from (createCompleteStep ("stepA" ))
105+ .end ()
106+ .start (new JobFlowExecutor (jobRepository , new SimpleStepHandler (jobRepository ), execution ));
107+
108+ Iterator <StepExecution > stepExecutions = execution .getStepExecutions ().iterator ();
109+ assertEquals (stepExecutions .next ().getStepName (), "stepA" );
110+ assertFalse (stepExecutions .hasNext ());
57111 }
58112
59113 @ Test
@@ -66,7 +120,7 @@ void testTransitionOrdering() throws Exception {
66120 @ Override
67121 public void execute (StepExecution stepExecution )
68122 throws JobInterruptedException , UnexpectedJobExecutionException {
69- stepExecution .setExitStatus (new ExitStatus ( " FAILED" ) );
123+ stepExecution .setExitStatus (ExitStatus . FAILED );
70124 }
71125 };
72126
@@ -94,10 +148,20 @@ public void execute(StepExecution stepExecution)
94148 .start (new JobFlowExecutor (jobRepository , new SimpleStepHandler (jobRepository ), execution ));
95149
96150 Iterator <StepExecution > stepExecutions = execution .getStepExecutions ().iterator ();
97- StepExecution stepExecutionA = stepExecutions .next ();
98- assertEquals (stepExecutionA .getStepName (), "stepA" );
99- StepExecution stepExecutionC = stepExecutions .next ();
100- assertEquals (stepExecutionC .getStepName (), "stepC" );
151+ assertEquals (stepExecutions .next ().getStepName (), "stepA" );
152+ assertEquals (stepExecutions .next ().getStepName (), "stepC" );
153+ assertFalse (stepExecutions .hasNext ());
154+ }
155+
156+ private static StepSupport createCompleteStep (String name ) {
157+ return new StepSupport (name ) {
158+ @ Override
159+ public void execute (StepExecution stepExecution )
160+ throws JobInterruptedException , UnexpectedJobExecutionException {
161+ stepExecution .upgradeStatus (BatchStatus .COMPLETED );
162+ stepExecution .setExitStatus (ExitStatus .COMPLETED );
163+ }
164+ };
101165 }
102166
103167}
0 commit comments