OPEN - issue BATCH-1395: Remove or deprecate old application context creation pattern

Revert some recent changes and focus more on factory patterns
This commit is contained in:
dsyer
2009-09-09 08:12:15 +00:00
parent 831b1c2534
commit 6d20540c01
17 changed files with 299 additions and 283 deletions

View File

@@ -5,6 +5,8 @@ import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
@@ -18,10 +20,10 @@ public class ApplicationContextJobFactoryTests {
}
@Test
public void testGroupName() throws Exception {
ApplicationContextJobFactory factory = new ApplicationContextJobFactory("jobs", "job",
new StubApplicationContextFactory());
assertEquals("jobs.job", factory.getJobName());
public void testPostProcessing() throws Exception {
ApplicationContextJobFactory factory = new ApplicationContextJobFactory("job",
new PostProcessingApplicationContextFactory());
assertEquals("bar", factory.getJobName());
}
private static class StubApplicationContextFactory implements ApplicationContextFactory {
@@ -33,4 +35,30 @@ public class ApplicationContextJobFactoryTests {
}
private static class PostProcessingApplicationContextFactory implements ApplicationContextFactory {
public ConfigurableApplicationContext createApplicationContext() {
StaticApplicationContext context = new StaticApplicationContext();
context.registerSingleton("job", JobSupport.class);
context.registerSingleton("postProcessor", TestBeanPostProcessor.class);
context.refresh();
return context;
}
}
private static class TestBeanPostProcessor implements BeanPostProcessor {
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof JobSupport) {
((JobSupport) bean).setName("bar");
}
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
}

View File

@@ -2,12 +2,14 @@ package org.springframework.batch.core.configuration.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.Collection;
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.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
@@ -33,7 +35,7 @@ public class ClassPathXmlJobRegistryTests {
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
registry.setApplicationContext(applicationContext);
registry.afterPropertiesSet();
registry.initialize();
Collection<String> names = registry.getJobNames();
assertEquals(2, names.size());
@@ -55,7 +57,7 @@ public class ClassPathXmlJobRegistryTests {
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
registry.setApplicationContext(applicationContext);
registry.afterPropertiesSet();
registry.initialize();
}
@Test
@@ -67,17 +69,33 @@ public class ClassPathXmlJobRegistryTests {
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
registry.setApplicationContext(applicationContext);
registry.afterPropertiesSet();
registry.initialize();
assertEquals(2, registry.getJobNames().size());
}
@Test
public void testErrorInContext() throws Exception {
Resource[] jobPaths = new Resource[] {
new ClassPathResource("org/springframework/batch/core/launch/support/2jobs.xml"),
new ClassPathResource("org/springframework/batch/core/launch/support/error.xml") };
registry.setJobPaths(jobPaths);
try {
registry.initialize();
fail("Expected BeanCreationException");
}
catch (BeanCreationException e) {
}
}
@Test
public void testDestroy() throws Exception {
Resource[] jobPaths = new Resource[] { new ClassPathResource(
"org/springframework/batch/core/launch/support/2jobs.xml") };
registry.setJobPaths(jobPaths);
registry.afterPropertiesSet();
registry.initialize();
assertEquals(2, registry.getJobNames().size());
registry.destroy();
assertEquals(0, registry.getJobNames().size());

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.batch.core.configuration.support;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.batch.core.Job;
@@ -23,34 +23,22 @@ import org.springframework.batch.core.job.JobSupport;
/**
* @author Dave Syer
*
*
*/
public class GroupAwareJobFactoryTests {
public class GroupAwareJobTests {
private Job job = new JobSupport("foo");
@Test
public void testCreateJob() {
AbstractGroupAwareJobFactory factory = new AbstractGroupAwareJobFactory(job.getName()) {
@Override
protected Job doCreateJob(String jobName) {
return job;
}
};
assertEquals(factory.getJobName(), factory.createJob().getName());
assertEquals(job, factory.createJob());
GroupAwareJob result = new GroupAwareJob(job);
assertEquals("foo", result.getName());
}
@Test
public void testGetJobName() {
AbstractGroupAwareJobFactory factory = new AbstractGroupAwareJobFactory("jobs", job.getName()) {
@Override
protected Job doCreateJob(String jobName) {
return job;
}
};
assertEquals(factory.getJobName(), factory.createJob().getName());
assertEquals("jobs.foo", factory.getJobName());
GroupAwareJob result = new GroupAwareJob("jobs", job);
assertEquals("jobs.foo", result.getName());
}
}

View File

@@ -25,7 +25,6 @@ import java.util.Collection;
import org.junit.Test;
import org.springframework.batch.core.configuration.DuplicateJobException;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.beans.FatalBeanException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -67,8 +66,8 @@ public class JobRegistryBeanPostProcessorTests {
processor.setJobRegistry(registry);
JobSupport job = new JobSupport();
job.setBeanName("foo");
assertEquals(job, processor.postProcessAfterInitialization(job, "bar"));
assertEquals(job.getName(), registry.getJob("foo").getName());
assertNotNull(processor.postProcessAfterInitialization(job, "bar"));
assertEquals("[foo]", registry.getJobNames().toString());
}
@Test
@@ -78,7 +77,7 @@ public class JobRegistryBeanPostProcessorTests {
processor.setGroupName("jobs");
JobSupport job = new JobSupport();
job.setBeanName("foo");
assertEquals(job, processor.postProcessAfterInitialization(job, "bar"));
assertNotNull(processor.postProcessAfterInitialization(job, "bar"));
assertEquals("[jobs.foo]", registry.getJobNames().toString());
}
@@ -105,15 +104,9 @@ public class JobRegistryBeanPostProcessorTests {
processor.setJobRegistry(registry);
JobSupport job = new JobSupport();
job.setBeanName("foo");
assertEquals(job, processor.postProcessAfterInitialization(job, "bar"));
assertNotNull(processor.postProcessAfterInitialization(job, "bar"));
processor.destroy();
try {
assertEquals(null, registry.getJob("foo"));
fail("Expected NoSuchJobConfigurationException");
}
catch (NoSuchJobException e) {
// expected
}
assertEquals("[]", registry.getJobNames().toString());
}
@Test

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.batch.core.configuration.support;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.batch.core.job.JobSupport;
@@ -28,8 +28,8 @@ public class ReferenceJobFactoryTests {
@Test
public void testGroupName() throws Exception {
ReferenceJobFactory factory = new ReferenceJobFactory("jobs", new JobSupport("foo"));
assertEquals("jobs.foo", factory.getJobName());
ReferenceJobFactory factory = new ReferenceJobFactory(new JobSupport("foo"));
assertEquals("foo", factory.getJobName());
}
}