OPEN - issue BATCH-1336: Add -restart and -next options to CommandLineJobRunner
Added -restart and -next. Need some more tests.
This commit is contained in:
@@ -18,17 +18,28 @@ package org.springframework.batch.core.launch.support;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
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.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
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.JobLauncher;
|
||||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -78,12 +89,20 @@ public class CommandLineJobRunnerTests {
|
||||
assertEquals(1, StubSystemExiter.status);
|
||||
}
|
||||
|
||||
// can't test because it will cause the system to exit.
|
||||
// public void testInvalidArgs(){
|
||||
//
|
||||
// String[] args = new String[]{jobPath, jobName};
|
||||
// CommandLineJobRunner.main(args);
|
||||
// }
|
||||
@Test
|
||||
@Ignore
|
||||
public void testInvalidArgs() {
|
||||
String[] args = new String[] {};
|
||||
CommandLineJobRunner.main(args);
|
||||
assertEquals(1, StubSystemExiter.status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrongJobName() {
|
||||
String[] args = new String[] { jobPath, "no-such-job" };
|
||||
CommandLineJobRunner.main(args);
|
||||
assertEquals(1, StubSystemExiter.status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithNoParameters() throws Throwable {
|
||||
@@ -93,6 +112,27 @@ public class CommandLineJobRunnerTests {
|
||||
assertEquals(new JobParameters(), StubJobLauncher.jobParameters);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRestart() throws Throwable {
|
||||
String[] args = new String[] { jobPath, "-restart", jobName };
|
||||
JobParameters jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters();
|
||||
StubJobExplorer.jobInstances = Arrays.asList(new JobInstance(0L, jobParameters, jobName));
|
||||
CommandLineJobRunner.main(args);
|
||||
assertEquals(0, StubSystemExiter.status);
|
||||
assertEquals(jobParameters, StubJobLauncher.jobParameters);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNext() throws Throwable {
|
||||
String[] args = new String[] { jobPath, "-next", jobName };
|
||||
JobParameters jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters();
|
||||
StubJobExplorer.jobInstances = Arrays.asList(new JobInstance(1L, jobParameters, jobName));
|
||||
CommandLineJobRunner.main(args);
|
||||
assertEquals(0, StubSystemExiter.status);
|
||||
jobParameters = new JobParametersBuilder().addString("foo", "spam").toJobParameters();
|
||||
assertEquals(jobParameters, StubJobLauncher.jobParameters);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDestroyCallback() throws Throwable {
|
||||
String[] args = new String[] { jobPath, jobName };
|
||||
@@ -118,7 +158,7 @@ public class CommandLineJobRunnerTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static class StubJobLauncher implements JobLauncher {
|
||||
public static class StubJobLauncher implements JobLauncher {
|
||||
|
||||
public static JobExecution jobExecution;
|
||||
|
||||
@@ -151,7 +191,48 @@ public class CommandLineJobRunnerTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static class StubJobParametersConverter implements JobParametersConverter {
|
||||
public static class StubJobExplorer implements JobExplorer {
|
||||
|
||||
static List<JobInstance> jobInstances = new ArrayList<JobInstance>();
|
||||
|
||||
public Set<JobExecution> findRunningJobExecutions(String jobName) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public JobExecution getJobExecution(Long executionId) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public List<JobExecution> getJobExecutions(JobInstance jobInstance) {
|
||||
if (jobInstance.getId() == 0) {
|
||||
return Arrays.asList(createJobInstance(jobInstance, BatchStatus.FAILED));
|
||||
}
|
||||
return Arrays.asList(createJobInstance(jobInstance, BatchStatus.COMPLETED));
|
||||
}
|
||||
|
||||
private JobExecution createJobInstance(JobInstance jobInstance, BatchStatus status) {
|
||||
JobExecution jobExecution = new JobExecution(jobInstance, 1L);
|
||||
jobExecution.setStatus(status);
|
||||
jobExecution.setStartTime(new Date());
|
||||
jobExecution.setEndTime(new Date());
|
||||
return jobExecution;
|
||||
}
|
||||
|
||||
public JobInstance getJobInstance(Long instanceId) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public List<JobInstance> getJobInstances(String jobName, int start, int count) {
|
||||
return jobInstances;
|
||||
}
|
||||
|
||||
public StepExecution getStepExecution(Long jobExecutionId, Long stepExecutionId) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class StubJobParametersConverter implements JobParametersConverter {
|
||||
|
||||
JobParametersConverter delegate = new DefaultJobParametersConverter();
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.springframework.batch.core.launch.support;
|
||||
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.JobParametersIncrementer;
|
||||
|
||||
public class TestJobParametersIncrementer implements JobParametersIncrementer {
|
||||
|
||||
public JobParameters getNext(JobParameters parameters) {
|
||||
return new JobParametersBuilder().addString("foo", "spam").toJobParameters();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
|
||||
|
||||
<bean id="test-job"
|
||||
class="org.springframework.batch.core.job.SimpleJob">
|
||||
<bean id="test-job" class="org.springframework.batch.core.job.SimpleJob">
|
||||
<property name="jobRepository">
|
||||
<bean class="org.springframework.batch.core.step.JobRepositorySupport" />
|
||||
</property>
|
||||
<property name="jobParametersIncrementer">
|
||||
<bean
|
||||
class="org.springframework.batch.core.launch.support.TestJobParametersIncrementer" />
|
||||
</property>
|
||||
<property name="steps">
|
||||
<bean id="step1"
|
||||
class="org.springframework.batch.core.step.item.SimpleStepFactoryBean">
|
||||
<property name="itemReader">
|
||||
<bean
|
||||
class="org.springframework.batch.item.support.ListItemReader">
|
||||
<bean class="org.springframework.batch.item.support.ListItemReader">
|
||||
<constructor-arg value="foo,bar,spam" />
|
||||
</bean>
|
||||
</property>
|
||||
<property name="itemWriter">
|
||||
<bean
|
||||
class="org.springframework.batch.core.launch.EmptyItemWriter" />
|
||||
<bean class="org.springframework.batch.core.launch.EmptyItemWriter" />
|
||||
</property>
|
||||
<property name="jobRepository">
|
||||
<bean
|
||||
class="org.springframework.batch.core.step.JobRepositorySupport" />
|
||||
<bean class="org.springframework.batch.core.step.JobRepositorySupport" />
|
||||
</property>
|
||||
<property name="transactionManager">
|
||||
<bean
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
|
||||
<bean class="org.springframework.batch.core.launch.support.SimpleJvmExitCodeMapper" />
|
||||
|
||||
<bean class="org.springframework.batch.core.launch.support.CommandLineJobRunnerTests$StubJobExplorer" />
|
||||
|
||||
<bean class="org.springframework.batch.core.launch.support.CommandLineJobRunnerTests$StubSystemExiter" />
|
||||
|
||||
<bean class="org.springframework.batch.core.launch.support.CommandLineJobRunnerTests$StubJobParametersConverter" />
|
||||
|
||||
Reference in New Issue
Block a user