RESOLVED - issue BATCH-1411: Allow a Job to specify its required JobParameters
Move validation check to AbstractJob (and out of Job)
This commit is contained in:
@@ -50,7 +50,7 @@ public class SplitInterruptedJobParserTests extends AbstractJobParserTests {
|
||||
Thread.sleep(200L);
|
||||
|
||||
assertEquals(BatchStatus.STOPPED, jobExecution.getStatus());
|
||||
assertEquals(ExitStatus.STOPPED, jobExecution.getExitStatus());
|
||||
assertEquals(ExitStatus.STOPPED.getExitCode(), jobExecution.getExitStatus().getExitCode());
|
||||
|
||||
assertTrue(stepNamesList.contains("stop"));
|
||||
|
||||
|
||||
@@ -14,6 +14,11 @@ public class DefaultJobParametersValidatorTests {
|
||||
validator.validate(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateNoRequiredValues() throws Exception {
|
||||
validator.validate(new JobParametersBuilder().addString("name", "foo").toJobParameters());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateRequiredValues() throws Exception {
|
||||
validator.setRequiredKeys(new String[] { "name", "value" });
|
||||
@@ -33,6 +38,19 @@ public class DefaultJobParametersValidatorTests {
|
||||
validator.validate(new JobParameters());
|
||||
}
|
||||
|
||||
@Test(expected = JobParametersInvalidException.class)
|
||||
public void testValidateOptionalWithImplicitRequiredKey() throws Exception {
|
||||
validator.setOptionalKeys(new String[] { "name", "value" });
|
||||
validator.validate(new JobParametersBuilder().addString("foo", "bar").toJobParameters());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateOptionalWithExplicitRequiredKey() throws Exception {
|
||||
validator.setOptionalKeys(new String[] { "name", "value" });
|
||||
validator.setRequiredKeys(new String[] { "foo" });
|
||||
validator.validate(new JobParametersBuilder().addString("foo", "bar").toJobParameters());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testOptionalValuesAlsoRequired() throws Exception {
|
||||
validator.setOptionalKeys(new String[] { "name", "value" });
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
@@ -45,7 +46,16 @@ import org.springframework.batch.support.transaction.ResourcelessTransactionMana
|
||||
*/
|
||||
public class ExtendedAbstractJobTests {
|
||||
|
||||
AbstractJob job = new StubJob("job");
|
||||
private AbstractJob job;
|
||||
private JobRepository jobRepository;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean();
|
||||
factory.setTransactionManager(new ResourcelessTransactionManager());
|
||||
jobRepository = (JobRepository) factory.getObject();
|
||||
job = new StubJob("job", jobRepository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
@@ -75,7 +85,7 @@ public class ExtendedAbstractJobTests {
|
||||
*/
|
||||
@Test
|
||||
public void testSetBeanNameWithNullName() {
|
||||
job = new StubJob(null);
|
||||
job = new StubJob(null, null);
|
||||
assertEquals(null, job.getName());
|
||||
job.setBeanName("foo");
|
||||
assertEquals("foo", job.getName());
|
||||
@@ -111,26 +121,27 @@ public class ExtendedAbstractJobTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected=JobParametersInvalidException.class)
|
||||
public void testValidatorWithNullParameters() throws Exception {
|
||||
job.validate(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidatorWithNotNullParameters() throws Exception {
|
||||
job.validate(new JobParameters());
|
||||
JobExecution execution = jobRepository.createJobExecution("job", new JobParameters());
|
||||
job.execute(execution);
|
||||
// Should be free of side effects
|
||||
}
|
||||
|
||||
@Test(expected=JobParametersInvalidException.class)
|
||||
@Test
|
||||
public void testSetValidator() throws Exception {
|
||||
job.setJobParametersValidator(new DefaultJobParametersValidator() {
|
||||
@Override
|
||||
public void validate(JobParameters parameters) throws JobParametersInvalidException {
|
||||
throw new JobParametersInvalidException("Expected");
|
||||
throw new JobParametersInvalidException("FOO");
|
||||
}
|
||||
});
|
||||
job.validate(new JobParameters());
|
||||
JobExecution execution = jobRepository.createJobExecution("job", new JobParameters());
|
||||
job.execute(execution);
|
||||
assertEquals(BatchStatus.FAILED, execution.getStatus());
|
||||
assertEquals("FOO", execution.getFailureExceptions().get(0).getMessage());
|
||||
String description = execution.getExitStatus().getExitDescription();
|
||||
assertTrue("Wrong description: "+description, description.contains("FOO"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,9 +194,16 @@ public class ExtendedAbstractJobTests {
|
||||
private static class StubJob extends AbstractJob {
|
||||
/**
|
||||
* @param name
|
||||
* @param jobRepository
|
||||
*/
|
||||
private StubJob(String name) {
|
||||
private StubJob(String name, JobRepository jobRepository) {
|
||||
super(name);
|
||||
try {
|
||||
setJobRepository(jobRepository);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,9 +21,7 @@ import java.util.List;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersIncrementer;
|
||||
import org.springframework.batch.core.JobParametersInvalidException;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.UnexpectedJobExecutionException;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
@@ -135,13 +133,6 @@ public class JobSupport implements BeanNameAware, Job {
|
||||
return restartable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Job#validate(JobParameters)
|
||||
*/
|
||||
public void validate(JobParameters parameters) throws JobParametersInvalidException {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
|
||||
@@ -72,7 +72,10 @@ public class SimpleJobLauncherTests {
|
||||
|
||||
@Test
|
||||
public void testRun() throws Exception {
|
||||
run(ExitStatus.COMPLETED);
|
||||
}
|
||||
|
||||
private void run(ExitStatus exitStatus) throws Exception {
|
||||
JobExecution jobExecution = new JobExecution(null, null);
|
||||
|
||||
expect(jobRepository.getLastJobExecution(job.getName(), jobParameters)).andReturn(null);
|
||||
@@ -80,10 +83,13 @@ public class SimpleJobLauncherTests {
|
||||
replay(jobRepository);
|
||||
|
||||
jobLauncher.afterPropertiesSet();
|
||||
jobLauncher.run(job, jobParameters);
|
||||
assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
|
||||
|
||||
verify(jobRepository);
|
||||
try {
|
||||
jobLauncher.run(job, jobParameters);
|
||||
}
|
||||
finally {
|
||||
assertEquals(exitStatus, jobExecution.getExitStatus());
|
||||
verify(jobRepository);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -142,7 +148,7 @@ public class SimpleJobLauncherTests {
|
||||
}
|
||||
};
|
||||
try {
|
||||
testRun();
|
||||
run(ExitStatus.FAILED);
|
||||
fail("Expected RuntimeException");
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
@@ -159,7 +165,7 @@ public class SimpleJobLauncherTests {
|
||||
}
|
||||
};
|
||||
try {
|
||||
testRun();
|
||||
run(ExitStatus.FAILED);
|
||||
fail("Expected Error");
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
|
||||
@@ -697,9 +697,7 @@ public class TaskletStepTests {
|
||||
|
||||
step.execute(stepExecution);
|
||||
assertEquals(BatchStatus.UNKNOWN, stepExecution.getStatus());
|
||||
String msg = stepExecution.getExitStatus().getExitDescription();
|
||||
Throwable ex = stepExecution.getFailureExceptions().get(0);
|
||||
msg = ex.getMessage();
|
||||
// The original rollback failed because of this one:
|
||||
assertEquals("Bar", ex.getMessage());
|
||||
}
|
||||
@@ -890,16 +888,10 @@ public class TaskletStepTests {
|
||||
|
||||
private boolean restoreFromCalled = false;
|
||||
|
||||
private boolean restoreFromCalledWithSomeContext = false;
|
||||
|
||||
public String read() throws Exception {
|
||||
return "item";
|
||||
}
|
||||
|
||||
public boolean isRestoreFromCalledWithSomeContext() {
|
||||
return restoreFromCalledWithSomeContext;
|
||||
}
|
||||
|
||||
public void update(ExecutionContext executionContext) {
|
||||
getExecutionAttributesCalled = true;
|
||||
executionContext.putString("spam", "bucket");
|
||||
@@ -920,10 +912,6 @@ public class TaskletStepTests {
|
||||
public void beforeStep(StepExecution stepExecution) {
|
||||
}
|
||||
|
||||
public ExitStatus onErrorInStep(StepExecution stepExecution, Throwable e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user