11/*
2- * Copyright 2008-2024 the original author or authors.
2+ * Copyright 2008-2025 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.
3030import org .springframework .batch .core .job .parameters .JobParameter ;
3131import org .springframework .batch .core .job .parameters .JobParameters ;
3232import org .springframework .batch .core .job .parameters .JobParametersBuilder ;
33- import org .springframework .batch .core .repository .explore .JobExplorer ;
34- import org .springframework .batch .core .launch .support .RunIdIncrementer ;
3533
3634import static org .assertj .core .api .Assertions .assertThat ;
3735import static org .junit .jupiter .api .Assertions .assertEquals ;
3836import static org .junit .jupiter .api .Assertions .assertFalse ;
3937import static org .junit .jupiter .api .Assertions .assertThrows ;
40- import static org .mockito .ArgumentMatchers .any ;
41- import static org .mockito .Mockito .mock ;
42- import static org .mockito .Mockito .when ;
4338
4439/**
4540 * @author Lucas Ward
@@ -54,8 +49,6 @@ class JobParametersBuilderTests {
5449
5550 private SimpleJob job ;
5651
57- private JobExplorer jobExplorer ;
58-
5952 private List <JobInstance > jobInstanceList ;
6053
6154 private List <JobExecution > jobExecutionList ;
@@ -65,10 +58,9 @@ class JobParametersBuilderTests {
6558 @ BeforeEach
6659 void initialize () {
6760 this .job = new SimpleJob ("simpleJob" );
68- this .jobExplorer = mock ();
6961 this .jobInstanceList = new ArrayList <>(1 );
7062 this .jobExecutionList = new ArrayList <>(1 );
71- this .parametersBuilder = new JobParametersBuilder (this . jobExplorer );
63+ this .parametersBuilder = new JobParametersBuilder ();
7264 }
7365
7466 @ Test
@@ -162,90 +154,4 @@ void testAddJobParameter() {
162154 assertEquals ("bar" , parameters .get ("foo" ).getValue ());
163155 }
164156
165- @ Test
166- void testGetNextJobParametersFirstRun () {
167- job .setJobParametersIncrementer (new RunIdIncrementer ());
168- initializeForNextJobParameters ();
169- this .parametersBuilder .getNextJobParameters (this .job );
170- defaultNextJobParametersVerify (this .parametersBuilder .toJobParameters (), 4 );
171- }
172-
173- @ Test
174- void testGetNextJobParametersNoIncrementer () {
175- initializeForNextJobParameters ();
176- final Exception expectedException = assertThrows (IllegalArgumentException .class ,
177- () -> this .parametersBuilder .getNextJobParameters (this .job ));
178- assertEquals ("No job parameters incrementer found for job=simpleJob" , expectedException .getMessage ());
179- }
180-
181- @ Test
182- void testGetNextJobParameters () {
183- this .job .setJobParametersIncrementer (new RunIdIncrementer ());
184- this .jobInstanceList .add (new JobInstance (1L , "simpleJobInstance" ));
185- this .jobExecutionList .add (getJobExecution (this .jobInstanceList .get (0 ), null ));
186- when (this .jobExplorer .getJobInstances ("simpleJob" , 0 , 1 )).thenReturn (this .jobInstanceList );
187- when (this .jobExplorer .getJobExecutions (any ())).thenReturn (this .jobExecutionList );
188- initializeForNextJobParameters ();
189- this .parametersBuilder .getNextJobParameters (this .job );
190- defaultNextJobParametersVerify (this .parametersBuilder .toJobParameters (), 4 );
191- }
192-
193- @ Test
194- void testGetNextJobParametersRestartable () {
195- this .job .setRestartable (true );
196- this .job .setJobParametersIncrementer (new RunIdIncrementer ());
197- this .jobInstanceList .add (new JobInstance (1L , "simpleJobInstance" ));
198- this .jobExecutionList .add (getJobExecution (this .jobInstanceList .get (0 ), BatchStatus .FAILED ));
199- when (this .jobExplorer .getJobInstances ("simpleJob" , 0 , 1 )).thenReturn (this .jobInstanceList );
200- when (this .jobExplorer .getJobExecutions (any ())).thenReturn (this .jobExecutionList );
201- initializeForNextJobParameters ();
202- this .parametersBuilder .addLong ("NON_IDENTIFYING_LONG" , 1L , false );
203- this .parametersBuilder .getNextJobParameters (this .job );
204- baseJobParametersVerify (this .parametersBuilder .toJobParameters (), 5 );
205- }
206-
207- @ Test
208- void testGetNextJobParametersNoPreviousExecution () {
209- this .job .setJobParametersIncrementer (new RunIdIncrementer ());
210- this .jobInstanceList .add (new JobInstance (1L , "simpleJobInstance" ));
211- when (this .jobExplorer .getJobInstances ("simpleJob" , 0 , 1 )).thenReturn (this .jobInstanceList );
212- when (this .jobExplorer .getJobExecutions (any ())).thenReturn (this .jobExecutionList );
213- initializeForNextJobParameters ();
214- this .parametersBuilder .getNextJobParameters (this .job );
215- baseJobParametersVerify (this .parametersBuilder .toJobParameters (), 4 );
216- }
217-
218- @ Test
219- void testMissingJobExplorer () {
220- this .parametersBuilder = new JobParametersBuilder ();
221- assertThrows (IllegalStateException .class , () -> this .parametersBuilder .getNextJobParameters (this .job ));
222- }
223-
224- private void initializeForNextJobParameters () {
225- this .parametersBuilder .addDate ("SCHEDULE_DATE" , date );
226- this .parametersBuilder .addLong ("LONG" , 1L );
227- this .parametersBuilder .addString ("STRING" , "string value" );
228- }
229-
230- private void defaultNextJobParametersVerify (JobParameters parameters , int paramCount ) {
231- baseJobParametersVerify (parameters , paramCount );
232- assertEquals (1 , parameters .getLong ("run.id" ));
233- }
234-
235- private void baseJobParametersVerify (JobParameters parameters , int paramCount ) {
236- assertEquals (date , parameters .getDate ("SCHEDULE_DATE" ));
237- assertEquals (1L , parameters .getLong ("LONG" ).longValue ());
238- assertEquals ("string value" , parameters .getString ("STRING" ));
239- assertEquals (paramCount , parameters .getParameters ().size ());
240- }
241-
242- private JobExecution getJobExecution (JobInstance jobInstance , BatchStatus batchStatus ) {
243- JobExecution jobExecution = new JobExecution (jobInstance , 1L , null );
244- if (batchStatus != null ) {
245- jobExecution .setStatus (batchStatus );
246- }
247- return jobExecution ;
248-
249- }
250-
251157}
0 commit comments