RESOLVED - issue BATCH-1483: Make the JobParametersValidator instance accessible from the Job interface

This commit is contained in:
dsyer
2010-01-12 09:49:37 +00:00
parent b551563ebe
commit 860e596cba
8 changed files with 107 additions and 22 deletions

View File

@@ -22,6 +22,7 @@ import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersIncrementer;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.job.JobParametersValidator;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
@@ -88,6 +89,10 @@ public class DefaultJobLoaderTests {
return false;
}
public JobParametersValidator getJobParametersValidator() {
return null;
}
}
}

View File

@@ -46,6 +46,8 @@ public class JobSupport implements BeanNameAware, Job {
private int startLimit = Integer.MAX_VALUE;
private DefaultJobParametersValidator jobParametersValidator = new DefaultJobParametersValidator();
/**
* Default constructor.
*/
@@ -97,6 +99,13 @@ public class JobSupport implements BeanNameAware, Job {
public String getName() {
return name;
}
/**
* @param jobParametersValidator the jobParametersValidator to set
*/
public void setJobParametersValidator(DefaultJobParametersValidator jobParametersValidator) {
this.jobParametersValidator = jobParametersValidator;
}
public void setSteps(List<Step> steps) {
this.steps.clear();
@@ -157,4 +166,9 @@ public class JobSupport implements BeanNameAware, Job {
public JobParametersIncrementer getJobParametersIncrementer() {
return null;
}
public JobParametersValidator getJobParametersValidator() {
return jobParametersValidator;
}
}

View File

@@ -33,10 +33,11 @@ import org.junit.Before;
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.JobParametersInvalidException;
import org.springframework.batch.core.job.DefaultJobParametersValidator;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
@@ -52,7 +53,7 @@ public class SimpleJobLauncherTests {
private SimpleJobLauncher jobLauncher;
private Job job = new JobSupport("foo") {
private JobSupport job = new JobSupport("foo") {
@Override
public void execute(JobExecution execution) {
execution.setExitStatus(ExitStatus.COMPLETED);
@@ -78,11 +79,13 @@ public class SimpleJobLauncherTests {
run(ExitStatus.COMPLETED);
}
private void run(ExitStatus exitStatus) throws Exception {
JobExecution jobExecution = new JobExecution(null, null);
@Test(expected=JobParametersInvalidException.class)
public void testRunWithValidator() throws Exception {
job.setJobParametersValidator(new DefaultJobParametersValidator(new String[] { "missing-and-required" },
new String[0]));
expect(jobRepository.getLastJobExecution(job.getName(), jobParameters)).andReturn(null);
expect(jobRepository.createJobExecution(job.getName(), jobParameters)).andReturn(jobExecution);
replay(jobRepository);
jobLauncher.afterPropertiesSet();
@@ -90,9 +93,9 @@ public class SimpleJobLauncherTests {
jobLauncher.run(job, jobParameters);
}
finally {
assertEquals(exitStatus, jobExecution.getExitStatus());
verify(jobRepository);
}
}
/*
@@ -228,6 +231,23 @@ public class SimpleJobLauncherTests {
jobLauncher.setJobRepository(jobRepository);
jobLauncher.afterPropertiesSet(); // no error
}
private void run(ExitStatus exitStatus) throws Exception {
JobExecution jobExecution = new JobExecution(null, null);
expect(jobRepository.getLastJobExecution(job.getName(), jobParameters)).andReturn(null);
expect(jobRepository.createJobExecution(job.getName(), jobParameters)).andReturn(jobExecution);
replay(jobRepository);
jobLauncher.afterPropertiesSet();
try {
jobLauncher.run(job, jobParameters);
}
finally {
assertEquals(exitStatus, jobExecution.getExitStatus());
verify(jobRepository);
}
}
private boolean contains(String str, String searchStr) {
return str.indexOf(searchStr) != -1;