RESOLVED - BATCH-628: SimpleJob doesn't verify that the JobRepository has been set
added the check to AbstractJob, as that's where jobRepository property is set
This commit is contained in:
@@ -25,6 +25,8 @@ import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.listener.CompositeExecutionJobListener;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
@@ -36,7 +38,7 @@ import org.springframework.util.ClassUtils;
|
||||
* @author Lucas Ward
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public abstract class AbstractJob implements BeanNameAware, Job {
|
||||
public abstract class AbstractJob implements Job, BeanNameAware, InitializingBean {
|
||||
|
||||
private List steps = new ArrayList();
|
||||
|
||||
@@ -161,6 +163,10 @@ public abstract class AbstractJob implements BeanNameAware, Job {
|
||||
public void setJobRepository(JobRepository jobRepository) {
|
||||
this.jobRepository = jobRepository;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(getJobRepository(), "JobRepository must be set");
|
||||
}
|
||||
|
||||
protected JobRepository getJobRepository() {
|
||||
return jobRepository;
|
||||
|
||||
@@ -19,6 +19,8 @@ import java.util.Collections;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobExecutionException;
|
||||
import org.springframework.batch.core.step.StepSupport;
|
||||
|
||||
/**
|
||||
@@ -27,13 +29,21 @@ import org.springframework.batch.core.step.StepSupport;
|
||||
*/
|
||||
public class AbstractJobTests extends TestCase {
|
||||
|
||||
JobSupport job = new JobSupport("job");
|
||||
AbstractJob job = new AbstractJob("job") {
|
||||
public void execute(JobExecution execution) throws JobExecutionException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.job.AbstractJob#getName()}.
|
||||
*/
|
||||
public void testGetName() {
|
||||
job = new JobSupport();
|
||||
job = new AbstractJob(){
|
||||
public void execute(JobExecution execution) throws JobExecutionException {
|
||||
// No-op
|
||||
}
|
||||
};
|
||||
assertNull(job.getName());
|
||||
}
|
||||
|
||||
@@ -49,7 +59,11 @@ public class AbstractJobTests extends TestCase {
|
||||
* Test method for {@link org.springframework.batch.core.job.AbstractJob#setBeanName(java.lang.String)}.
|
||||
*/
|
||||
public void testSetBeanNameWithNullName() {
|
||||
job = new JobSupport(null);
|
||||
job = new AbstractJob(null) {
|
||||
public void execute(JobExecution execution) throws JobExecutionException {
|
||||
// NO-OP
|
||||
}
|
||||
};
|
||||
assertEquals(null, job.getName());
|
||||
job.setBeanName("foo");
|
||||
assertEquals("foo", job.getName());
|
||||
@@ -72,15 +86,6 @@ public class AbstractJobTests extends TestCase {
|
||||
assertEquals(1, job.getSteps().size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.job.JobSupport#setStartLimit(int)}.
|
||||
*/
|
||||
public void testSetStartLimit() {
|
||||
assertEquals(Integer.MAX_VALUE, job.getStartLimit());
|
||||
job.setStartLimit(10);
|
||||
assertEquals(10, job.getStartLimit());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.job.AbstractJob#setRestartable(boolean)}.
|
||||
*/
|
||||
@@ -94,18 +99,20 @@ public class AbstractJobTests extends TestCase {
|
||||
String value = job.toString();
|
||||
assertTrue("Should contain name: " + value, value.indexOf("name=") >= 0);
|
||||
}
|
||||
|
||||
public void testRunNotSupported() throws Exception {
|
||||
|
||||
public void testAfterPropertiesSet() throws Exception {
|
||||
AbstractJob job = new AbstractJob() {
|
||||
public void execute(JobExecution execution) throws JobExecutionException {
|
||||
}
|
||||
};
|
||||
job.setJobRepository(null);
|
||||
try {
|
||||
job.execute(null);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
String message = e.getMessage();
|
||||
assertTrue("Message should contain JobSupport: " + message, contains(message, "JobSupport"));
|
||||
job.afterPropertiesSet();
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertTrue(e.getMessage().contains("JobRepository"));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean contains(String str, String searchStr) {
|
||||
return str.indexOf(searchStr) != -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
|
||||
<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="steps">
|
||||
<bean id="step1"
|
||||
class="org.springframework.batch.core.step.item.SimpleStepFactoryBean">
|
||||
|
||||
Reference in New Issue
Block a user