Polish
This commit is contained in:
@@ -16,9 +16,6 @@
|
||||
|
||||
package org.springframework.batch.core;
|
||||
|
||||
import org.springframework.batch.core.explore.JobExplorer;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -26,6 +23,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.batch.core.explore.JobExplorer;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Helper class for creating {@link JobParameters}. Useful because all
|
||||
* {@link JobParameter} objects are immutable, and must be instantiated separately
|
||||
@@ -48,11 +48,20 @@ public class JobParametersBuilder {
|
||||
|
||||
private Map<String, JobParameter> parameterMap;
|
||||
|
||||
private JobExplorer jobExplorer;
|
||||
|
||||
/**
|
||||
* Default constructor. Initializes the builder with empty parameters.
|
||||
*/
|
||||
public JobParametersBuilder() {
|
||||
this.parameterMap = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param jobExplorer {@link JobExplorer} used for looking up previous job parameter information
|
||||
*/
|
||||
public JobParametersBuilder(JobExplorer jobExplorer) {
|
||||
this.jobExplorer = jobExplorer;
|
||||
this.parameterMap = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
@@ -61,7 +70,7 @@ public class JobParametersBuilder {
|
||||
* @param jobParameters {@link JobParameters} instance used to initialize the builder.
|
||||
*/
|
||||
public JobParametersBuilder(JobParameters jobParameters) {
|
||||
this.parameterMap = new LinkedHashMap<>(jobParameters.getParameters());
|
||||
this(jobParameters, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,6 +89,16 @@ public class JobParametersBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy constructor. Initializes the builder with the supplied parameters.
|
||||
* @param jobParameters {@link JobParameters} instance used to initialize the builder.
|
||||
* @param jobExplorer {@link JobExplorer} used for looking up previous job parameter information
|
||||
*/
|
||||
public JobParametersBuilder(JobParameters jobParameters, JobExplorer jobExplorer) {
|
||||
this.jobExplorer = jobExplorer;
|
||||
this.parameterMap = new LinkedHashMap<>(jobParameters.getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new identifying String parameter for the given key.
|
||||
*
|
||||
@@ -208,15 +227,18 @@ public class JobParametersBuilder {
|
||||
* should be called after all parameters have been entered into the builder.
|
||||
*
|
||||
* @param job the job for which the {@link JobParameters} are being constructed.
|
||||
* @param jobExplorer instance to a {@link JobExplorer}
|
||||
* @return a reference to this object.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public JobParametersBuilder getNextJobParameters(Job job, JobExplorer jobExplorer) {
|
||||
public JobParametersBuilder getNextJobParameters(Job job) {
|
||||
if(this.jobExplorer == null) {
|
||||
throw new IllegalStateException("A JobExplore is required to get next job parameters");
|
||||
}
|
||||
|
||||
String name = job.getName();
|
||||
JobParameters nextParameters = new JobParameters();
|
||||
List<JobInstance> lastInstances = jobExplorer.getJobInstances(name, 0, 1);
|
||||
List<JobInstance> lastInstances = this.jobExplorer.getJobInstances(name, 0, 1);
|
||||
JobParametersIncrementer incrementer = job.getJobParametersIncrementer();
|
||||
if (lastInstances.isEmpty()) {
|
||||
// Start from a completely clean sheet
|
||||
@@ -225,7 +247,7 @@ public class JobParametersBuilder {
|
||||
}
|
||||
}
|
||||
else {
|
||||
List<JobExecution> previousExecutions = jobExplorer
|
||||
List<JobExecution> previousExecutions = this.jobExplorer
|
||||
.getJobExecutions(lastInstances.get(0));
|
||||
JobExecution previousExecution = previousExecutions.get(0);
|
||||
if (previousExecution == null) {
|
||||
|
||||
@@ -57,11 +57,11 @@ public class JobParametersBuilderTests {
|
||||
|
||||
@Before
|
||||
public void initialize() {
|
||||
this.parametersBuilder = new JobParametersBuilder();
|
||||
this.job = new SimpleJob("simpleJob");
|
||||
this.jobExplorer = mock(JobExplorer.class);
|
||||
this.jobInstanceList = new ArrayList<>(1);
|
||||
this.jobExecutionList = new ArrayList<>(1);
|
||||
this.parametersBuilder = new JobParametersBuilder(this.jobExplorer);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -169,14 +169,14 @@ public class JobParametersBuilderTests {
|
||||
public void testGetNextJobParametersFirstRun(){
|
||||
job.setJobParametersIncrementer(new RunIdIncrementer());
|
||||
initializeForNextJobParameters();
|
||||
this.parametersBuilder.getNextJobParameters(this.job, this.jobExplorer);
|
||||
this.parametersBuilder.getNextJobParameters(this.job);
|
||||
defaultNextJobParametersVerify(this.parametersBuilder.toJobParameters(), 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextJobParametersNoIncrementer(){
|
||||
initializeForNextJobParameters();
|
||||
this.parametersBuilder.getNextJobParameters(this.job, this.jobExplorer);
|
||||
this.parametersBuilder.getNextJobParameters(this.job);
|
||||
baseJobParametersVerify(this.parametersBuilder.toJobParameters(), 3);
|
||||
}
|
||||
|
||||
@@ -188,7 +188,7 @@ public class JobParametersBuilderTests {
|
||||
when(this.jobExplorer.getJobInstances("simpleJob",0,1)).thenReturn(this.jobInstanceList);
|
||||
when(this.jobExplorer.getJobExecutions(any())).thenReturn(this.jobExecutionList);
|
||||
initializeForNextJobParameters();
|
||||
this.parametersBuilder.getNextJobParameters(this.job, this.jobExplorer);
|
||||
this.parametersBuilder.getNextJobParameters(this.job);
|
||||
defaultNextJobParametersVerify(this.parametersBuilder.toJobParameters(), 4);
|
||||
}
|
||||
|
||||
@@ -202,7 +202,7 @@ public class JobParametersBuilderTests {
|
||||
when(this.jobExplorer.getJobExecutions(any())).thenReturn(this.jobExecutionList);
|
||||
initializeForNextJobParameters();
|
||||
this.parametersBuilder.addLong("NON_IDENTIFYING_LONG", new Long(1), false);
|
||||
this.parametersBuilder.getNextJobParameters(this.job, this.jobExplorer);
|
||||
this.parametersBuilder.getNextJobParameters(this.job);
|
||||
baseJobParametersVerify(this.parametersBuilder.toJobParameters(), 3);
|
||||
}
|
||||
|
||||
@@ -214,10 +214,16 @@ public class JobParametersBuilderTests {
|
||||
when(this.jobExplorer.getJobInstances("simpleJob",0,1)).thenReturn(this.jobInstanceList);
|
||||
when(this.jobExplorer.getJobExecutions(any())).thenReturn(this.jobExecutionList);
|
||||
initializeForNextJobParameters();
|
||||
this.parametersBuilder.getNextJobParameters(this.job, this.jobExplorer);
|
||||
this.parametersBuilder.getNextJobParameters(this.job);
|
||||
baseJobParametersVerify(this.parametersBuilder.toJobParameters(), 4);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testMissingJobExplorer() {
|
||||
this.parametersBuilder = new JobParametersBuilder();
|
||||
this.parametersBuilder.getNextJobParameters(this.job);
|
||||
}
|
||||
|
||||
private void initializeForNextJobParameters() {
|
||||
this.parametersBuilder.addDate("SCHEDULE_DATE", date);
|
||||
this.parametersBuilder.addLong("LONG", new Long(1));
|
||||
|
||||
Reference in New Issue
Block a user