1
1
/*
2
- * Copyright 2012-2022 the original author or authors.
2
+ * Copyright 2012-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
20
20
import org .junit .jupiter .api .Test ;
21
21
22
+ import org .springframework .batch .core .BatchStatus ;
22
23
import org .springframework .batch .core .ExitStatus ;
23
24
import org .springframework .batch .core .JobExecution ;
24
25
import org .springframework .batch .core .JobInterruptedException ;
34
35
import org .springframework .batch .core .step .StepSupport ;
35
36
36
37
import static org .junit .jupiter .api .Assertions .assertEquals ;
38
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
37
39
38
40
/**
39
41
* @author Dave Syer
40
42
* @author Michael Minella
41
43
* @author Mahmoud Ben Hassine
44
+ * @author Injae Kim
42
45
*
43
46
*/
44
47
class FlowBuilderTests {
45
48
46
49
@ Test
47
- void test () throws Exception {
50
+ void testNext () throws Exception {
48
51
FlowBuilder <Flow > builder = new FlowBuilder <>("flow" );
49
52
JobRepository jobRepository = new JobRepositorySupport ();
50
53
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 ());
57
111
}
58
112
59
113
@ Test
@@ -66,7 +120,7 @@ void testTransitionOrdering() throws Exception {
66
120
@ Override
67
121
public void execute (StepExecution stepExecution )
68
122
throws JobInterruptedException , UnexpectedJobExecutionException {
69
- stepExecution .setExitStatus (new ExitStatus ( " FAILED" ) );
123
+ stepExecution .setExitStatus (ExitStatus . FAILED );
70
124
}
71
125
};
72
126
@@ -94,10 +148,20 @@ public void execute(StepExecution stepExecution)
94
148
.start (new JobFlowExecutor (jobRepository , new SimpleStepHandler (jobRepository ), execution ));
95
149
96
150
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
+ };
101
165
}
102
166
103
167
}
0 commit comments