StepRegistry is now a first-class (optional) component:

- MapJobRegistry no longer implements StepRegistry
- DefaultJobLoader updates the stepRegistry when it is available
- Added more tests to cover the cases where the step registry is not available
This commit is contained in:
Stéphane Nicoll
2012-12-15 10:30:28 +01:00
committed by Michael Minella
parent 2870628c85
commit 8abd2ec69f
4 changed files with 100 additions and 52 deletions

View File

@@ -78,7 +78,7 @@ public class DefaultJobLoaderTests {
}
@Test
public void createWithJobRegistryWhichIsAStepRegistry() {
public void createWithOnlyJobRegistry() {
final DefaultJobLoader loader = new DefaultJobLoader();
loader.setJobRegistry(jobRegistry);
@@ -132,6 +132,43 @@ public class DefaultJobLoaderTests {
assertStepDoNotExist("job2", "step11", "step12");
}
@Test
public void testNoStepRegistryAvailable() throws DuplicateJobException {
final JobLoader loader = new DefaultJobLoader(jobRegistry);
ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory(
new ClassPathResource("job-context-with-steps.xml", getClass()));
loader.load(factory);
// No step registry available so just registering the jobs
assertEquals(2, jobRegistry.getJobNames().size());
}
@Test
public void testLoadWithJobThatIsNotAStepLocator() throws DuplicateJobException {
ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory(
new ByteArrayResource(BASIC_JOB_XML.getBytes()));
try {
jobLoader.load(factory);
fail("Should have failed with a ["+UnsupportedOperationException.class.getName()+"] as job does not" +
"implement StepLocator.");
} catch (UnsupportedOperationException e) {
// Job is not a step locator, can't register steps
}
}
@Test
public void testLoadWithJobThatIsNotAStepLocatorNoStepRegistry() throws DuplicateJobException {
final JobLoader loader = new DefaultJobLoader(jobRegistry);
ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory(
new ByteArrayResource(BASIC_JOB_XML.getBytes()));
try {
loader.load(factory);
} catch (UnsupportedOperationException e) {
fail("Should not have failed with a [" + UnsupportedOperationException.class.getName() + "] as " +
"stepRegistry is not available for this JobLoader instance.");
}
}
@Test
public void testReload() throws Exception {
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(new ClassPathResource(
@@ -182,13 +219,19 @@ public class DefaultJobLoaderTests {
}
}
private static final String BASIC_JOB_XML = String
.format(
"<beans xmlns='http://www.springframework.org/schema/beans' 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.5.xsd'><bean class='%s$BasicStubJob'/></beans>",
DefaultJobLoaderTests.class.getName());
private static final String JOB_XML = String
.format(
"<beans xmlns='http://www.springframework.org/schema/beans' 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.5.xsd'><bean class='%s$StubJob'/></beans>",
DefaultJobLoaderTests.class.getName());
public static class StubJob implements Job, StepLocator {
public static class BasicStubJob implements Job {
@Override
public void execute(JobExecution execution) {
@@ -213,6 +256,9 @@ public class DefaultJobLoaderTests {
public JobParametersValidator getJobParametersValidator() {
return null;
}
}
public static class StubJob extends BasicStubJob implements StepLocator {
@Override
public Collection<String> getStepNames() {

View File

@@ -21,8 +21,6 @@ import junit.framework.TestCase;
import org.springframework.batch.core.configuration.DuplicateJobException;
import org.springframework.batch.core.configuration.JobFactory;
import org.springframework.batch.core.configuration.support.MapJobRegistry;
import org.springframework.batch.core.configuration.support.ReferenceJobFactory;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.batch.core.launch.NoSuchJobException;