Add appending capabilities to the SimpleCommandLineArgsProvider
Adds the ability to add a list of command line arguments to the existing ones. Resolves #279
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.cloud.task.batch.partition;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
@@ -22,7 +23,8 @@ import org.springframework.cloud.task.repository.TaskExecution;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Returns any command line arguments used with the {@link TaskExecution} provided.
|
||||
* Returns any command line arguments used with the {@link TaskExecution} provided
|
||||
* appended with any additional arguments configured.
|
||||
*
|
||||
* @author Michael Minella
|
||||
* @since 1.1.0
|
||||
@@ -31,14 +33,38 @@ public class SimpleCommandLineArgsProvider implements CommandLineArgsProvider {
|
||||
|
||||
private final TaskExecution taskExecution;
|
||||
|
||||
private List<String> appendedArgs;
|
||||
|
||||
public SimpleCommandLineArgsProvider(TaskExecution taskExecution) {
|
||||
Assert.notNull(taskExecution, "A taskExecution is required");
|
||||
|
||||
this.taskExecution = taskExecution;
|
||||
}
|
||||
|
||||
/**
|
||||
* Additional command line args to be appended.
|
||||
*
|
||||
* @param appendedArgs list of arguments
|
||||
* @since 1.2
|
||||
*/
|
||||
public void setAppendedArgs(List<String> appendedArgs) {
|
||||
this.appendedArgs = appendedArgs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getCommandLineArgs(ExecutionContext executionContext) {
|
||||
return this.taskExecution.getArguments();
|
||||
|
||||
int listSize = this.taskExecution.getArguments().size() +
|
||||
(this.appendedArgs != null ? this.appendedArgs.size() : 0);
|
||||
|
||||
List<String> args = new ArrayList<>(listSize);
|
||||
|
||||
args.addAll(this.taskExecution.getArguments());
|
||||
|
||||
if(this.appendedArgs != null) {
|
||||
args.addAll(this.appendedArgs);
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.cloud.task.batch.partition;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@@ -47,4 +48,44 @@ public class SimpleCommandLineArgsProviderTests {
|
||||
assertEquals("bar", commandLineArgs.get(1));
|
||||
assertEquals("baz", commandLineArgs.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAppending() {
|
||||
List<String> appendedValues = new ArrayList<>(3);
|
||||
appendedValues.add("one");
|
||||
appendedValues.add("two");
|
||||
appendedValues.add("three");
|
||||
|
||||
TaskExecution taskExecution = new TaskExecution();
|
||||
taskExecution.setArguments(Arrays.asList("foo", "bar", "baz"));
|
||||
|
||||
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(taskExecution);
|
||||
provider.setAppendedArgs(appendedValues);
|
||||
|
||||
List<String> commandLineArgs = provider.getCommandLineArgs(null);
|
||||
|
||||
assertEquals("foo", commandLineArgs.get(0));
|
||||
assertEquals("bar", commandLineArgs.get(1));
|
||||
assertEquals("baz", commandLineArgs.get(2));
|
||||
assertEquals("one", commandLineArgs.get(3));
|
||||
assertEquals("two", commandLineArgs.get(4));
|
||||
assertEquals("three", commandLineArgs.get(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAppendingNull() {
|
||||
|
||||
TaskExecution taskExecution = new TaskExecution();
|
||||
taskExecution.setArguments(Arrays.asList("foo", "bar", "baz"));
|
||||
|
||||
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(taskExecution);
|
||||
provider.setAppendedArgs(null);
|
||||
|
||||
List<String> commandLineArgs = provider.getCommandLineArgs(null);
|
||||
|
||||
assertEquals(3, commandLineArgs.size());
|
||||
assertEquals("foo", commandLineArgs.get(0));
|
||||
assertEquals("bar", commandLineArgs.get(1));
|
||||
assertEquals("baz", commandLineArgs.get(2));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user