1717
1818import java .util .Properties ;
1919
20+ import org .junit .jupiter .api .Assertions ;
2021import org .junit .jupiter .api .BeforeEach ;
2122import org .junit .jupiter .api .Test ;
2223import org .mockito .Mockito ;
2324
25+ import org .springframework .batch .core .BatchStatus ;
2426import org .springframework .batch .core .configuration .JobRegistry ;
2527import org .springframework .batch .core .converter .JobParametersConverter ;
2628import org .springframework .batch .core .job .Job ;
3032import org .springframework .batch .core .repository .JobRepository ;
3133
3234import static org .mockito .Mockito .mock ;
35+ import static org .springframework .batch .core .launch .support .ExitCodeMapper .JVM_EXITCODE_GENERIC_ERROR ;
3336
3437/**
3538 * Tests for {@link CommandLineJobOperator}.
3639 *
3740 * @author Mahmoud Ben Hassine
3841 * @author Yejeong Ham
42+ * @author Cheolhwan Ihn
3943 */
4044class CommandLineJobOperatorTests {
4145
@@ -111,6 +115,7 @@ void restart() throws Exception {
111115 // given
112116 long jobExecutionId = 1 ;
113117 JobExecution jobExecution = mock ();
118+ Mockito .when (jobExecution .getStatus ()).thenReturn (BatchStatus .FAILED );
114119
115120 // when
116121 Mockito .when (jobRepository .getJobExecution (jobExecutionId )).thenReturn (jobExecution );
@@ -121,19 +126,67 @@ void restart() throws Exception {
121126 }
122127
123128 @ Test
124- void abandon () throws Exception {
129+ void restartJobExecutionStopped () throws Exception {
125130 // given
126131 long jobExecutionId = 1 ;
127132 JobExecution jobExecution = mock ();
133+ Mockito .when (jobExecution .getStatus ()).thenReturn (BatchStatus .STOPPED );
134+ Mockito .when (jobRepository .getJobExecution (jobExecutionId )).thenReturn (jobExecution );
128135
129136 // when
137+ this .commandLineJobOperator .restart (jobExecutionId );
138+
139+ // then
140+ Mockito .verify (jobOperator ).restart (jobExecution );
141+ }
142+
143+ @ Test
144+ void restartJobExecutionNotFailed () throws Exception {
145+ // given
146+ long jobExecutionId = 1 ;
147+ JobExecution jobExecution = mock ();
148+ Mockito .when (jobExecution .getStatus ()).thenReturn (BatchStatus .COMPLETED );
130149 Mockito .when (jobRepository .getJobExecution (jobExecutionId )).thenReturn (jobExecution );
150+
151+ // when
152+ int exitCode = this .commandLineJobOperator .restart (jobExecutionId );
153+
154+ // then
155+ Assertions .assertEquals (JVM_EXITCODE_GENERIC_ERROR , exitCode );
156+ Mockito .verify (jobOperator , Mockito .never ()).restart (jobExecution );
157+ }
158+
159+ @ Test
160+ void abandon () throws Exception {
161+ // given
162+ long jobExecutionId = 1 ;
163+ JobExecution jobExecution = mock ();
164+ Mockito .when (jobExecution .getStatus ()).thenReturn (BatchStatus .STOPPED );
165+ Mockito .when (jobRepository .getJobExecution (jobExecutionId )).thenReturn (jobExecution );
166+
167+ // when
131168 this .commandLineJobOperator .abandon (jobExecutionId );
132169
133170 // then
134171 Mockito .verify (jobOperator ).abandon (jobExecution );
135172 }
136173
174+ @ Test
175+ void abandonJobExecutionNotStopped () throws Exception {
176+ // given
177+ long jobExecutionId = 1 ;
178+ JobExecution jobExecution = mock ();
179+ Mockito .when (jobExecution .getStatus ()).thenReturn (BatchStatus .COMPLETED );
180+ Mockito .when (jobRepository .getJobExecution (jobExecutionId )).thenReturn (jobExecution );
181+
182+ // when
183+ int exitCode = this .commandLineJobOperator .abandon (jobExecutionId );
184+
185+ // then
186+ Assertions .assertEquals (ExitCodeMapper .JVM_EXITCODE_GENERIC_ERROR , exitCode ); // JVM_EXITCODE_GENERIC_ERROR
187+ Mockito .verify (jobOperator , Mockito .never ()).abandon (jobExecution );
188+ }
189+
137190 @ Test
138191 void recover () {
139192 // given
@@ -148,4 +201,4 @@ void recover() {
148201 Mockito .verify (jobOperator ).recover (jobExecution );
149202 }
150203
151- }
204+ }
0 commit comments