Added tests to address more situations as well as cleaned up logic a bit

This commit is contained in:
Michael Minella
2014-05-21 14:41:58 -05:00
parent 66f2033793
commit 0337cd8d74
2 changed files with 85 additions and 22 deletions

View File

@@ -16,11 +16,6 @@
package org.springframework.batch.core.configuration.support;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -33,6 +28,11 @@ import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* {@link ApplicationContextFactory} implementation that takes a parent context and a path to the context to create.
* When createApplicationContext method is called, the child {@link ApplicationContext} will be returned. The child
@@ -45,6 +45,8 @@ public class GenericApplicationContextFactory extends AbstractApplicationContext
/**
* Create an application context factory for the resource specified. The resource can be an actual {@link Resource},
* in which case it will be interpreted as an XML file, or it can be a @Configuration class, or a package name.
* All types must be the same (mixing XML with a java package for example is not allowed and will result in an
* {@link java.lang.IllegalArgumentException}).
*
* @param resources some resources (XML configuration files, @Configuration classes or java packages to scan)
*/
@@ -58,21 +60,24 @@ public class GenericApplicationContextFactory extends AbstractApplicationContext
@Override
protected ConfigurableApplicationContext createApplicationContext(ConfigurableApplicationContext parent,
Object... resources) {
ConfigurableApplicationContext context;
if (allObjectsOfType(resources, Resource.class)) {
return new ResourceXmlApplicationContext(parent, resources);
context = new ResourceXmlApplicationContext(parent, resources);
} else if (allObjectsOfType(resources, Class.class)) {
context = new ResourceAnnotationApplicationContext(parent, resources);
} else if (allObjectsOfType(resources, String.class)) {
context = new ResourceAnnotationApplicationContext(parent, resources);
} else {
List<Class<?>> types = new ArrayList<Class<?>>();
for (Object resource : resources) {
types.add(resource.getClass());
}
throw new IllegalArgumentException("No application context could be created for resource types: "
+ Arrays.toString(types.toArray()));
}
if (allObjectsOfType(resources, Class.class)) {
return new ResourceAnnotationApplicationContext(parent, resources);
}
if (allObjectsOfType(resources, String.class)) {
return new ResourceAnnotationApplicationContext(parent, resources);
}
List<Class<?>> types = new ArrayList<Class<?>>();
for (Object resource : resources) {
types.add(resource.getClass());
}
throw new IllegalArgumentException("No application context could be created for resource types: "
+ Arrays.toString(types.toArray()));
return context;
}
private boolean allObjectsOfType(Object[] objects, Class<?> type) {

View File

@@ -15,10 +15,6 @@
*/
package org.springframework.batch.core.configuration.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.job.JobSupport;
@@ -27,11 +23,17 @@ import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.AbstractBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
*
@@ -164,6 +166,36 @@ public class GenericApplicationContextFactoryTests {
assertTrue(autowiredFound);
}
@Test(expected = IllegalArgumentException.class)
public void testDifferentResourceTypes() throws Exception {
Resource resource1 = new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
"abstract-context.xml"));
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource1, Configuration1.class);
factory.createApplicationContext();
}
@Test
public void testPackageScanning() throws Exception {
GenericApplicationContextFactory factory = new GenericApplicationContextFactory("org.springframework.batch.core.configuration.support");
ConfigurableApplicationContext context = factory.createApplicationContext();
assertEquals(context.getBean("bean1"), "bean1");
assertEquals(context.getBean("bean2"), "bean2");
assertEquals(context.getBean("bean3"), "bean3");
assertEquals(context.getBean("bean4"), "bean4");
}
@Test
public void testMultipleConfigurationClasses() throws Exception {
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(Configuration1.class, Configuration2.class);
ConfigurableApplicationContext context = factory.createApplicationContext();
assertEquals(context.getBean("bean1"), "bean1");
assertEquals(context.getBean("bean2"), "bean2");
assertEquals(context.getBean("bean3"), "bean3");
assertEquals(context.getBean("bean4"), "bean4");
}
public static class Foo {
private double[] values;
@@ -172,4 +204,30 @@ public class GenericApplicationContextFactoryTests {
}
}
@Configuration
public static class Configuration1 {
@Bean
public String bean1() {
return "bean1";
}
@Bean
public String bean2() {
return "bean2";
}
}
@Configuration
public static class Configuration2 {
@Bean
public String bean3() {
return "bean3";
}
@Bean
public String bean4() {
return "bean4";
}
}
}