BATCH-1473: Refactored context creation components

This commit is contained in:
dsyer
2010-01-02 21:55:52 +00:00
parent 4911ea71f1
commit 891ff55bf0
20 changed files with 749 additions and 364 deletions

View File

@@ -21,7 +21,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ClassPathXmlJobLoaderContextTests {
public class AutomaticJobRegistrarContextTests {
@Autowired
private JobRegistry registry;

View File

@@ -1,16 +1,18 @@
package org.springframework.batch.core.configuration.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -20,18 +22,21 @@ import org.springframework.core.io.Resource;
/**
*
*
* @author Dave Syer
* @author Lucas Ward
*
*/
public class ClassPathXmlJobLoaderTests {
public class AutomaticJobRegistrarTests {
private AutomaticJobRegistrar registrar = new AutomaticJobRegistrar();
private ClassPathXmlJobLoader loader = new ClassPathXmlJobLoader();
private MapJobRegistry registry = new MapJobRegistry();
@Before
public void setUp() {
loader.setJobRegistry(registry);
DefaultJobLoader jobLoader = new DefaultJobLoader();
jobLoader.setJobRegistry(registry);
registrar.setJobLoader(jobLoader);
}
@Test
@@ -41,11 +46,11 @@ public class ClassPathXmlJobLoaderTests {
new ClassPathResource("org/springframework/batch/core/launch/support/job.xml"),
new ClassPathResource("org/springframework/batch/core/launch/support/job2.xml") };
loader.setJobPaths(jobPaths);
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
loader.setApplicationContext(applicationContext);
loader.initialize();
setUpApplicationContextFactories(jobPaths, applicationContext);
registrar.setApplicationContext(applicationContext);
registrar.start();
Collection<String> names = registry.getJobNames();
assertEquals(2, names.size());
@@ -58,16 +63,16 @@ public class ClassPathXmlJobLoaderTests {
assertEquals("test-job2", job.getName());
}
@Test(expected = NoSuchJobException.class)
@Test
public void testNoJobFound() throws Exception {
Resource[] jobPaths = new Resource[] { new ClassPathResource(
"org/springframework/batch/core/launch/support/test-environment.xml") };
loader.setJobPaths(jobPaths);
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
loader.setApplicationContext(applicationContext);
loader.initialize();
setUpApplicationContextFactories(jobPaths, applicationContext);
registrar.setApplicationContext(applicationContext);
registrar.start();
}
@Test
@@ -75,11 +80,11 @@ public class ClassPathXmlJobLoaderTests {
Resource[] jobPaths = new Resource[] { new ClassPathResource(
"org/springframework/batch/core/launch/support/2jobs.xml") };
loader.setJobPaths(jobPaths);
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
loader.setApplicationContext(applicationContext);
loader.initialize();
setUpApplicationContextFactories(jobPaths, applicationContext);
registrar.setApplicationContext(applicationContext);
registrar.start();
assertEquals(2, registry.getJobNames().size());
}
@@ -88,10 +93,11 @@ public class ClassPathXmlJobLoaderTests {
Resource[] jobPaths = new Resource[] { new ClassPathResource(
"org/springframework/batch/core/launch/support/2jobs.xml") };
loader.setApplicationContext(new ClassPathXmlApplicationContext(
"/org/springframework/batch/core/launch/support/test-environment-with-registry-and-auto-register.xml"));
loader.setJobPaths(jobPaths);
loader.initialize();
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"/org/springframework/batch/core/launch/support/test-environment-with-registry-and-auto-register.xml");
registrar.setApplicationContext(applicationContext);
setUpApplicationContextFactories(jobPaths, applicationContext);
registrar.start();
assertEquals(2, registry.getJobNames().size());
}
@@ -101,9 +107,9 @@ public class ClassPathXmlJobLoaderTests {
Resource[] jobPaths = new Resource[] {
new ClassPathResource("org/springframework/batch/core/launch/support/2jobs.xml"),
new ClassPathResource("org/springframework/batch/core/launch/support/error.xml") };
loader.setJobPaths(jobPaths);
setUpApplicationContextFactories(jobPaths, null);
try {
loader.initialize();
registrar.start();
fail("Expected BeanCreationException");
}
catch (BeanCreationException e) {
@@ -112,47 +118,74 @@ public class ClassPathXmlJobLoaderTests {
}
@Test
public void testDestroy() throws Exception {
public void testClear() throws Exception {
Resource[] jobPaths = new Resource[] { new ClassPathResource(
"org/springframework/batch/core/launch/support/2jobs.xml") };
loader.setJobPaths(jobPaths);
loader.initialize();
setUpApplicationContextFactories(jobPaths, null);
registrar.start();
assertEquals(2, registry.getJobNames().size());
loader.clear();
registrar.stop();
assertEquals(0, registry.getJobNames().size());
}
@Test
public void testStartStopRunning() throws Exception {
Resource[] jobPaths = new Resource[] { new ClassPathResource(
"org/springframework/batch/core/launch/support/2jobs.xml") };
setUpApplicationContextFactories(jobPaths, null);
registrar.start();
assertTrue(registrar.isRunning());
registrar.start();
assertEquals(2, registry.getJobNames().size());
registrar.stop();
assertFalse(registrar.isRunning());
}
@Test
public void testInitCalledOnContextRefreshed() throws Exception {
Resource[] jobPaths = new Resource[] { new ClassPathResource(
"org/springframework/batch/core/launch/support/2jobs.xml") };
loader.setApplicationContext(new ClassPathXmlApplicationContext(
registrar.setApplicationContext(new ClassPathXmlApplicationContext(
"/org/springframework/batch/core/launch/support/test-environment-with-registry-and-auto-register.xml"));
loader.setJobPaths(jobPaths);
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
loader.setApplicationContext(applicationContext);
loader.onApplicationEvent(new ContextRefreshedEvent(applicationContext));
setUpApplicationContextFactories(jobPaths, applicationContext);
registrar.setApplicationContext(applicationContext);
registrar.onApplicationEvent(new ContextRefreshedEvent(applicationContext));
assertEquals(2, registry.getJobNames().size());
}
@Test
public void testDestroyCalledOnContextClosed() throws Exception {
public void testClearCalledOnContextClosed() throws Exception {
Resource[] jobPaths = new Resource[] { new ClassPathResource(
"org/springframework/batch/core/launch/support/2jobs.xml") };
loader.setJobPaths(jobPaths);
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
loader.setApplicationContext(applicationContext);
loader.initialize();
setUpApplicationContextFactories(jobPaths, applicationContext);
registrar.setApplicationContext(applicationContext);
registrar.start();
assertEquals(2, registry.getJobNames().size());
loader.onApplicationEvent(new ContextClosedEvent(applicationContext));
registrar.onApplicationEvent(new ContextClosedEvent(applicationContext));
assertEquals(0, registry.getJobNames().size());
}
private void setUpApplicationContextFactories(Resource[] jobPaths, ApplicationContext parent) {
Collection<ApplicationContextFactory> applicationContextFactories = new ArrayList<ApplicationContextFactory>();
for (Resource resource : jobPaths) {
ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory();
factory.setResource(resource);
factory.setApplicationContext(parent);
applicationContextFactories.add(factory);
}
registrar.setApplicationContextFactories(applicationContextFactories
.toArray(new ApplicationContextFactory[jobPaths.length]));
}
}

View File

@@ -36,20 +36,20 @@ public class ClassPathXmlApplicationContextFactoryTests {
@Test
public void testCreateJob() {
factory.setPath(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "trivial-context.xml")));
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "trivial-context.xml")));
assertNotNull(factory.createApplicationContext());
}
@Test
public void testGetJobName() {
factory.setPath(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "trivial-context.xml")));
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "trivial-context.xml")));
assertEquals("test-job", factory.createApplicationContext().getBeanNamesForType(Job.class)[0]);
}
@Test
public void testParentConfigurationInherited() {
factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(getClass(), "parent-context.xml")));
factory.setPath(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml")));
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml")));
ConfigurableApplicationContext context = factory.createApplicationContext();
assertEquals("test-job", context.getBeanNamesForType(Job.class)[0]);
assertEquals("bar", ((Job) context.getBean("test-job", Job.class)).getName());
@@ -59,7 +59,7 @@ public class ClassPathXmlApplicationContextFactoryTests {
@Test
public void testBeanFactoryPostProcessorsNotCopied() {
factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(getClass(), "parent-context.xml")));
factory.setPath(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml")));
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml")));
@SuppressWarnings("unchecked")
Class<? extends BeanFactoryPostProcessor>[] classes = (Class<? extends BeanFactoryPostProcessor>[]) new Class<?>[0];
factory.setBeanFactoryPostProcessorClasses(classes);
@@ -72,7 +72,7 @@ public class ClassPathXmlApplicationContextFactoryTests {
@Test
public void testBeanFactoryConfigurationNotCopied() {
factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(getClass(), "parent-context.xml")));
factory.setPath(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml")));
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml")));
factory.setCopyConfiguration(false);
ConfigurableApplicationContext context = factory.createApplicationContext();
assertEquals("test-job", context.getBeanNamesForType(Job.class)[0]);

View File

@@ -30,7 +30,6 @@ import org.springframework.util.ClassUtils;
* @author Dave Syer
*
*/
@SuppressWarnings("deprecation")
public class OsgiBundleXmlApplicationContextFactoryTests {
private OsgiBundleXmlApplicationContextFactory factory = new OsgiBundleXmlApplicationContextFactory();

View File

@@ -48,6 +48,14 @@ public class JobRegistryBackgroundJobRunnerTests {
assertEquals(0, JobRegistryBackgroundJobRunner.getErrors().size());
}
@Test
public void testMainWithJobLoader() throws Exception {
JobRegistryBackgroundJobRunner.main(
ClassUtils.addResourcePathToPackagePath(getClass(), "test-environment-with-loader.xml"), ClassUtils
.addResourcePathToPackagePath(getClass(), "job.xml"));
assertEquals(0, JobRegistryBackgroundJobRunner.getErrors().size());
}
@Before
public void setUp() throws Exception {
JobRegistryBackgroundJobRunner.getErrors().clear();