Skip to content

BATCH-2711: Fix JobParameterBuilder not to overwrite new parameters with old ones #630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* @author Michael Minella
* @author Glenn Renfro
* @author Mahmoud Ben Hassine
* @author Minhyeok Jeong
* @since 1.0
* @see JobParameters
* @see JobParameter
Expand Down Expand Up @@ -224,15 +225,29 @@ public JobParametersBuilder addParameter(String key, JobParameter jobParameter)
}

/**
* Copy job parameters into the current state.
* @param jobParameters parameters to copy in
* Add job parameters into the current parameter map. If the map previously
* contained a mapping for the key, the old value is replaced.
*
* @param jobParameters parameters to add to
* @return a reference to this object.
*/
public JobParametersBuilder addJobParameters(JobParameters jobParameters) {
Assert.notNull(jobParameters, "jobParameters must not be null");

this.parameterMap.putAll(jobParameters.getParameters());
return this;
}

/**
* Add job parameters into the current parameter map. If the map previously
* contained a mapping for the key, the new value is ignored.
*
* @param jobParameters parameters to add to
* @return a reference to this object.
*/
public JobParametersBuilder addJobParametersIfAbsent(JobParameters jobParameters) {
Assert.notNull(jobParameters, "jobParameters must not be null");
jobParameters.getParameters().forEach(
(key, value) -> this.parameterMap.putIfAbsent(key, value));
return this;
}

Expand Down Expand Up @@ -282,7 +297,7 @@ else if (incrementer != null) {
}
}

this.parameterMap = addJobParameters(nextParameters)
this.parameterMap = addJobParametersIfAbsent(nextParameters)
.toJobParameters()
.getParameters();
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand All @@ -40,7 +41,7 @@
* @author Michael Minella
* @author Glenn Renfro
* @author Mahmoud Ben Hassine
*
* @author Minhyeok Jeong
*/
public class JobParametersBuilderTests {

Expand Down Expand Up @@ -82,9 +83,31 @@ public void testAddingExistingJobParameters() {
.addJobParameters(params2)
.toJobParameters();

assertEquals(finalParams.getString("foo"), "baz");
assertEquals(finalParams.getString("bar"), "baz");
assertEquals(finalParams.getString("baz"), "quix");
assertEquals("baz", finalParams.getString("foo"));
assertEquals("baz", finalParams.getString("bar"));
assertEquals("quix", finalParams.getString("baz"));
}

@Test
public void testAddingExistingJobParametersIfAbsent() {
JobParameters params1 = new JobParametersBuilder()
.addString("foo", "bar")
.addString("bar", "baz")
.toJobParameters();

JobParameters params2 = new JobParametersBuilder()
.addString("foo", "baz")
.toJobParameters();

JobParameters finalParams = new JobParametersBuilder()
.addString("baz", "quix")
.addJobParametersIfAbsent(params1)
.addJobParametersIfAbsent(params2)
.toJobParameters();

assertEquals("bar", finalParams.getString("foo"));
assertEquals("baz", finalParams.getString("bar"));
assertEquals("quix", finalParams.getString("baz"));
}

@Test
Expand Down Expand Up @@ -265,11 +288,16 @@ private void baseJobParametersVerify(JobParameters parameters, int paramCount) {
}

private JobExecution getJobExecution(JobInstance jobInstance, BatchStatus batchStatus) {
JobExecution jobExecution = new JobExecution(jobInstance, 1L, null, "TestConfig");
Map<String, JobParameter> parameters = new LinkedHashMap<>();
parameters.put("STRING", new JobParameter("previous value"));
JobParameters jobParameters = new JobParameters(parameters);

JobExecution jobExecution = new JobExecution(jobInstance, 1L, jobParameters, "TestConfig");

if(batchStatus != null) {
jobExecution.setStatus(batchStatus);
}
return jobExecution;

return jobExecution;
}
}