SPR-5682:

* polishing for ConfigurationClassApplicationContext & tests
* added ConfigurationClassWebApplicationContext & tests
* next: refactoring out duplications between ConfigurationClassApplicationContext & ConfigurationClassWebApplicationContext
This commit is contained in:
Chris Beams
2009-10-05 05:27:30 +00:00
parent 772a74a636
commit fd8935ba0b
5 changed files with 275 additions and 50 deletions

View File

@@ -20,30 +20,28 @@ import static java.lang.String.format;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.matchers.JUnitMatchers.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
public class ConfigurationClassApplicationContextTests {
@Test(expected=IllegalStateException.class)
public void emptyConstructorRequiresManualRefresh() {
ConfigurationClassApplicationContext context = new ConfigurationClassApplicationContext();
context.getBean("foo");
}
@Test
public void classesMissingConfigurationAnnotationAddedToContextAreDisallowed() {
ConfigurationClassApplicationContext ctx =
new ConfigurationClassApplicationContext(Config.class);
// should be fine
ctx.addConfigurationClass(ConfigWithCustomName.class);
// should cause immediate failure (no refresh necessary)
try {
ctx.addConfigurationClass(ConfigMissingAnnotation.class);
@@ -54,19 +52,18 @@ public class ConfigurationClassApplicationContextTests {
"is not annotated with @Configuration"));
}
}
@Test(expected=IllegalArgumentException.class)
public void classesMissingConfigurationAnnotationSuppliedToConstructorAreDisallowed() {
new ConfigurationClassApplicationContext(ConfigMissingAnnotation.class);
}
@Test(expected=IllegalArgumentException.class)
public void nullGetBeanParameterIsDisallowed() {
ConfigurationClassApplicationContext context = new ConfigurationClassApplicationContext(Config.class);
context.getBean((Class<?>)null);
}
@Test
public void addConfigurationClass() {
ConfigurationClassApplicationContext context = new ConfigurationClassApplicationContext();
@@ -77,7 +74,7 @@ public class ConfigurationClassApplicationContextTests {
context.refresh();
context.getBean("name");
}
@Test
public void getBeanByType() {
ConfigurationClassApplicationContext context = new ConfigurationClassApplicationContext(Config.class);
@@ -85,7 +82,7 @@ public class ConfigurationClassApplicationContextTests {
assertNotNull("getBean() should not return null", testBean);
assertThat(testBean.name, equalTo("foo"));
}
/**
* Tests that Configuration classes are registered according to convention
* @see org.springframework.beans.factory.support.DefaultBeanNameGenerator#generateBeanName
@@ -93,12 +90,12 @@ public class ConfigurationClassApplicationContextTests {
@Test
public void defaultConfigClassBeanNameIsGeneratedProperly() {
ConfigurationClassApplicationContext context = new ConfigurationClassApplicationContext(Config.class);
// attempt to retrieve the instance by its generated bean name
Config configObject = (Config) context.getBean(Config.class.getName() + "#0");
assertNotNull(configObject);
}
/**
* Tests that specifying @Configuration(value="foo") results in registering
* the configuration class with bean name 'foo'.
@@ -107,17 +104,17 @@ public class ConfigurationClassApplicationContextTests {
public void explicitConfigClassBeanNameIsRespected() {
ConfigurationClassApplicationContext context =
new ConfigurationClassApplicationContext(ConfigWithCustomName.class);
// attempt to retrieve the instance by its specified name
ConfigWithCustomName configObject =
(ConfigWithCustomName) context.getBean("customConfigBeanName");
assertNotNull(configObject);
}
@Test
public void getBeanByTypeRaisesNoSuchBeanDefinitionException() {
ConfigurationClassApplicationContext context = new ConfigurationClassApplicationContext(Config.class);
// attempt to retrieve a bean that does not exist
Class<?> targetType = java.util.regex.Pattern.class;
try {
@@ -128,12 +125,12 @@ public class ConfigurationClassApplicationContextTests {
format("No unique bean of type [%s] is defined", targetType.getName())));
}
}
@SuppressWarnings("unchecked")
@Test
public void getBeanByTypeAmbiguityRaisesException() {
ConfigurationClassApplicationContext context = new ConfigurationClassApplicationContext(TwoTestBeanConfig.class);
try {
context.getBean(TestBean.class);
} catch (RuntimeException ex) {
@@ -148,14 +145,14 @@ public class ConfigurationClassApplicationContextTests {
);
}
}
@Test
public void autowiringIsEnabledByDefault() {
ConfigurationClassApplicationContext context = new ConfigurationClassApplicationContext(AutowiredConfig.class);
assertThat(context.getBean(TestBean.class).name, equalTo("foo"));
}
@Configuration
static class Config {
@Bean
@@ -165,7 +162,7 @@ public class ConfigurationClassApplicationContextTests {
return testBean;
}
}
@Configuration("customConfigBeanName")
static class ConfigWithCustomName {
@Bean
@@ -173,37 +170,37 @@ public class ConfigurationClassApplicationContextTests {
return new TestBean();
}
}
static class ConfigMissingAnnotation {
@Bean
public TestBean testBean() {
return new TestBean();
}
}
@Configuration
static class TwoTestBeanConfig {
@Bean TestBean tb1() { return new TestBean(); }
@Bean TestBean tb2() { return new TestBean(); }
}
@Configuration
static class NameConfig {
@Bean String name() { return "foo"; }
}
@Configuration
@Import(NameConfig.class)
static class AutowiredConfig {
@Autowired String autowiredName;
@Bean TestBean testBean() {
TestBean testBean = new TestBean();
testBean.name = autowiredName;
return testBean;
}
}
}
class TestBean {
@@ -233,6 +230,5 @@ class TestBean {
return false;
return true;
}
}
}