From e37a402b2e102b97b1fb0c647968266740db8a20 Mon Sep 17 00:00:00 2001 From: dsyer Date: Mon, 8 Nov 2010 12:42:47 +0000 Subject: [PATCH] Try CommandLineJobRunner with params from stdin --- .../launch/support/CommandLineJobRunner.java | 38 ++++++++++++-- .../support/CommandLineJobRunnerTests.java | 52 ++++++++++++++----- 2 files changed, 73 insertions(+), 17 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java index 6a4540af7..54099bc27 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java @@ -15,14 +15,37 @@ */ package org.springframework.batch.core.launch.support; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.batch.core.*; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobInstance; +import org.springframework.batch.core.JobParameter; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersIncrementer; import org.springframework.batch.core.configuration.JobLocator; import org.springframework.batch.core.converter.DefaultJobParametersConverter; import org.springframework.batch.core.converter.JobParametersConverter; import org.springframework.batch.core.explore.JobExplorer; -import org.springframework.batch.core.launch.*; +import org.springframework.batch.core.launch.JobExecutionNotFailedException; +import org.springframework.batch.core.launch.JobExecutionNotRunningException; +import org.springframework.batch.core.launch.JobExecutionNotStoppedException; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.launch.JobParametersNotFoundException; import org.springframework.batch.core.repository.JobRepository; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; @@ -31,8 +54,6 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.Assert; import org.springframework.util.StringUtils; -import java.util.*; - /** *

* Basic launcher for starting jobs from the command line. In general, it is @@ -482,7 +503,7 @@ public class CommandLineJobRunner { * command line. *

*/ - public static void main(String[] args) { + public static void main(String[] args) throws Exception { CommandLineJobRunner command = new CommandLineJobRunner(); @@ -520,6 +541,13 @@ public class CommandLineJobRunner { command.exit(1); } + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + String line = reader.readLine(); + while (line != null) { + params.add(line); + line = reader.readLine(); + } + String[] parameters = params.toArray(new String[params.size()]); int result = command.start(jobPath, jobIdentifier, parameters, opts); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java index c2c04ae3d..98b332307 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java @@ -18,6 +18,7 @@ package org.springframework.batch.core.launch.support; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -63,23 +64,37 @@ public class CommandLineJobRunnerTests { private String[] args = new String[] { jobPath, jobName, jobKey, scheduleDate, vendorId }; + private InputStream stdin; + @Before public void setUp() throws Exception { JobExecution jobExecution = new JobExecution(null, new Long(1)); ExitStatus exitStatus = ExitStatus.COMPLETED; jobExecution.setExitStatus(exitStatus); StubJobLauncher.jobExecution = jobExecution; + stdin = System.in; + System.setIn(new InputStream() { + public int read() { + return -1; + } + }); + } + + @After + public void tearDown() throws Exception { + System.setIn(stdin); + StubJobLauncher.tearDown(); } @Test - public void testMain() { + public void testMain() throws Exception { CommandLineJobRunner.main(args); assertTrue("Injected JobParametersConverter not used instead of default", StubJobParametersConverter.called); assertEquals(0, StubSystemExiter.getStatus()); } @Test - public void testWithJobLocator() { + public void testWithJobLocator() throws Exception { jobPath = ClassUtils.addResourcePathToPackagePath(CommandLineJobRunnerTests.class, "launcher-with-locator.xml"); CommandLineJobRunner.main(new String[] { jobPath, jobName, jobKey }); assertTrue("Injected JobParametersConverter not used instead of default", StubJobParametersConverter.called); @@ -94,7 +109,7 @@ public class CommandLineJobRunnerTests { } @Test - public void testInvalidArgs() { + public void testInvalidArgs() throws Exception { String[] args = new String[] {}; CommandLineJobRunner.presetSystemExiter(new StubSystemExiter()); CommandLineJobRunner.main(args); @@ -104,7 +119,7 @@ public class CommandLineJobRunnerTests { } @Test - public void testWrongJobName() { + public void testWrongJobName() throws Exception { String[] args = new String[] { jobPath, "no-such-job" }; CommandLineJobRunner.main(args); assertEquals(1, StubSystemExiter.status); @@ -121,6 +136,23 @@ public class CommandLineJobRunnerTests { assertEquals(new JobParameters(), StubJobLauncher.jobParameters); } + @Test + public void testWithStdinParameters() throws Throwable { + String[] args = new String[] { jobPath, jobName }; + System.setIn(new InputStream() { + char[] input = ("foo=bar\nspam=bucket").toCharArray(); + + int index = 0; + + public int read() { + return index