RESOLVED - issue BATCH-1411: Allow a Job to specify its required JobParameters
This commit is contained in:
@@ -20,6 +20,7 @@ import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
@@ -28,6 +29,7 @@ import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecutionListener;
|
||||
import org.springframework.batch.core.job.AbstractJob;
|
||||
import org.springframework.batch.core.job.DefaultJobParametersValidator;
|
||||
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.repository.support.SimpleJobRepository;
|
||||
@@ -182,6 +184,18 @@ public class JobParserTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParametersValidator() {
|
||||
ApplicationContext ctx = jobParserParentAttributeTestsCtx;
|
||||
Job job = (Job) ctx.getBean("jobWithParametersValidator");
|
||||
assertTrue(job instanceof AbstractJob);
|
||||
Object validator = ReflectionTestUtils.getField(job, "jobParametersValidator");
|
||||
assertTrue(validator instanceof DefaultJobParametersValidator);
|
||||
@SuppressWarnings("unchecked")
|
||||
Collection<String> keys = (Collection<String>) ReflectionTestUtils.getField(validator, "requiredKeys");
|
||||
assertEquals(2, keys.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListenerClearingJob() throws Exception {
|
||||
assertEquals(0, getListeners("listenerClearingJob", jobParserParentAttributeTestsCtx).size());
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.springframework.batch.core.job;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.JobParametersInvalidException;
|
||||
|
||||
public class DefaultJobParametersValidatorTests {
|
||||
|
||||
private DefaultJobParametersValidator validator = new DefaultJobParametersValidator();
|
||||
|
||||
@Test(expected = JobParametersInvalidException.class)
|
||||
public void testValidateNull() throws Exception {
|
||||
validator.validate(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateRequiredValues() throws Exception {
|
||||
validator.setRequiredKeys(new String[] { "name", "value" });
|
||||
validator
|
||||
.validate(new JobParametersBuilder().addString("name", "foo").addLong("value", 111L).toJobParameters());
|
||||
}
|
||||
|
||||
@Test(expected = JobParametersInvalidException.class)
|
||||
public void testValidateRequiredValuesMissing() throws Exception {
|
||||
validator.setRequiredKeys(new String[] { "name", "value" });
|
||||
validator.validate(new JobParameters());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateOptionalValues() throws Exception {
|
||||
validator.setOptionalKeys(new String[] { "name", "value" });
|
||||
validator.validate(new JobParameters());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testOptionalValuesAlsoRequired() throws Exception {
|
||||
validator.setOptionalKeys(new String[] { "name", "value" });
|
||||
validator.setRequiredKeys(new String[] { "foo", "value" });
|
||||
validator.afterPropertiesSet();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,6 +31,7 @@ import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobExecutionException;
|
||||
import org.springframework.batch.core.JobInterruptedException;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersInvalidException;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
@@ -42,7 +43,7 @@ import org.springframework.batch.support.transaction.ResourcelessTransactionMana
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class AbstractJobTests {
|
||||
public class ExtendedAbstractJobTests {
|
||||
|
||||
AbstractJob job = new StubJob("job");
|
||||
|
||||
@@ -109,6 +110,28 @@ public class AbstractJobTests {
|
||||
assertTrue(e.getMessage().contains("JobRepository"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected=JobParametersInvalidException.class)
|
||||
public void testValidatorWithNullParameters() throws Exception {
|
||||
job.validate(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidatorWithNotNullParameters() throws Exception {
|
||||
job.validate(new JobParameters());
|
||||
// Should be free of side effects
|
||||
}
|
||||
|
||||
@Test(expected=JobParametersInvalidException.class)
|
||||
public void testSetValidator() throws Exception {
|
||||
job.setJobParametersValidator(new DefaultJobParametersValidator() {
|
||||
@Override
|
||||
public void validate(JobParameters parameters) throws JobParametersInvalidException {
|
||||
throw new JobParametersInvalidException("Expected");
|
||||
}
|
||||
});
|
||||
job.validate(new JobParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the step and persists job execution context.
|
||||
@@ -21,7 +21,9 @@ 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;
|
||||
@@ -132,6 +134,13 @@ public class JobSupport implements BeanNameAware, Job {
|
||||
public boolean isRestartable() {
|
||||
return restartable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Job#validate(JobParameters)
|
||||
*/
|
||||
public void validate(JobParameters parameters) throws JobParametersInvalidException {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
|
||||
@@ -17,10 +17,7 @@ import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.job.SimpleJob;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
|
||||
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.repository.JobRestartException;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -308,8 +305,7 @@ public class FaultTolerantExceptionClassesTests implements ApplicationContextAwa
|
||||
assertEquals("[1, 1, 1, 1]", tasklet.getCommitted().toString());
|
||||
}
|
||||
|
||||
private StepExecution launchStep(String stepName) throws JobExecutionAlreadyRunningException, JobRestartException,
|
||||
JobInstanceAlreadyCompleteException {
|
||||
private StepExecution launchStep(String stepName) throws Exception {
|
||||
SimpleJob job = new SimpleJob();
|
||||
job.setName("job");
|
||||
job.setJobRepository(jobRepository);
|
||||
|
||||
Reference in New Issue
Block a user