Browse Source

Unit tests for configuration classes implementing the JSR-330 Provider interface

Issue: SPR-11830
pull/551/head
Juergen Hoeller 11 years ago
parent
commit
31f2f20307
  1. 88
      spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java

88
spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,11 +17,12 @@
package org.springframework.context.annotation.configuration; package org.springframework.context.annotation.configuration;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
import javax.inject.Provider;
import org.junit.Test; import org.junit.Test;
import org.springframework.tests.sample.beans.ITestBean;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
@ -49,6 +50,8 @@ import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.tests.sample.beans.ITestBean;
import org.springframework.tests.sample.beans.TestBean;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -115,7 +118,28 @@ public class ConfigurationClassProcessingTests {
try { try {
factory.getBean("methodName"); factory.getBean("methodName");
fail("bean should not have been registered with 'methodName'"); fail("bean should not have been registered with 'methodName'");
} catch (NoSuchBeanDefinitionException ex) { /* expected */ } }
catch (NoSuchBeanDefinitionException ex) {
// expected
}
}
@Test // SPR-11830
public void configWithBeanWithProviderImplementation() {
GenericApplicationContext ac = new GenericApplicationContext();
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
ac.registerBeanDefinition("config", new RootBeanDefinition(ConfigWithBeanWithProviderImplementation.class));
ac.refresh();
assertSame(ac.getBean("customName"), ConfigWithBeanWithProviderImplementation.testBean);
}
@Test // SPR-11830
public void configWithSetWithProviderImplementation() {
GenericApplicationContext ac = new GenericApplicationContext();
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
ac.registerBeanDefinition("config", new RootBeanDefinition(ConfigWithSetWithProviderImplementation.class));
ac.refresh();
assertSame(ac.getBean("customName"), ConfigWithSetWithProviderImplementation.set);
} }
@Test(expected=BeanDefinitionParsingException.class) @Test(expected=BeanDefinitionParsingException.class)
@ -124,7 +148,7 @@ public class ConfigurationClassProcessingTests {
} }
@Test @Test
public void simplestPossibleConfiguration() { public void simplestPossibleConfig() {
BeanFactory factory = initBeanFactory(SimplestPossibleConfig.class); BeanFactory factory = initBeanFactory(SimplestPossibleConfig.class);
String stringBean = factory.getBean("stringBean", String.class); String stringBean = factory.getBean("stringBean", String.class);
assertEquals(stringBean, "foo"); assertEquals(stringBean, "foo");
@ -202,7 +226,9 @@ public class ConfigurationClassProcessingTests {
@Configuration @Configuration
static class ConfigWithBeanWithCustomName { static class ConfigWithBeanWithCustomName {
static TestBean testBean = new TestBean(); static TestBean testBean = new TestBean();
@Bean(name="customName") @Bean(name="customName")
public TestBean methodName() { public TestBean methodName() {
return testBean; return testBean;
@ -210,8 +236,45 @@ public class ConfigurationClassProcessingTests {
} }
@Configuration
static class ConfigWithBeanWithAliases {
static TestBean testBean = new TestBean();
@Bean(name={"name1", "alias1", "alias2", "alias3"})
public TestBean methodName() {
return testBean;
}
}
@Configuration
static class ConfigWithBeanWithProviderImplementation implements Provider<TestBean> {
static TestBean testBean = new TestBean();
@Bean(name="customName")
public TestBean get() {
return testBean;
}
}
@Configuration
static class ConfigWithSetWithProviderImplementation implements Provider<Set<String>> {
static Set<String> set = Collections.singleton("value");
@Bean(name="customName")
public Set<String> get() {
return set;
}
}
@Configuration @Configuration
static class ConfigWithFinalBean { static class ConfigWithFinalBean {
public final @Bean TestBean testBean() { public final @Bean TestBean testBean() {
return new TestBean(); return new TestBean();
} }
@ -220,6 +283,7 @@ public class ConfigurationClassProcessingTests {
@Configuration @Configuration
static class SimplestPossibleConfig { static class SimplestPossibleConfig {
public @Bean String stringBean() { public @Bean String stringBean() {
return "foo"; return "foo";
} }
@ -228,9 +292,11 @@ public class ConfigurationClassProcessingTests {
@Configuration @Configuration
static class ConfigWithNonSpecificReturnTypes { static class ConfigWithNonSpecificReturnTypes {
public @Bean Object stringBean() { public @Bean Object stringBean() {
return "foo"; return "foo";
} }
public @Bean FactoryBean<?> factoryBean() { public @Bean FactoryBean<?> factoryBean() {
ListFactoryBean fb = new ListFactoryBean(); ListFactoryBean fb = new ListFactoryBean();
fb.setSourceList(Arrays.asList("element1", "element2")); fb.setSourceList(Arrays.asList("element1", "element2"));
@ -239,18 +305,6 @@ public class ConfigurationClassProcessingTests {
} }
@Configuration
static class ConfigWithBeanWithAliases {
static TestBean testBean = new TestBean();
@Bean(name={"name1", "alias1", "alias2", "alias3"})
public TestBean methodName() {
return testBean;
}
}
@Configuration @Configuration
static class ConfigWithPrototypeBean { static class ConfigWithPrototypeBean {

Loading…
Cancel
Save