Browse Source

Unit tests for java.util.Optional with autowired collections on @Autowired fields/methods and @Bean method parameters

Issue: SPR-9132
pull/692/head
Juergen Hoeller 10 years ago
parent
commit
7d2231541b
  1. 82
      spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java
  2. 60
      spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java

82
spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java

@ -630,6 +630,62 @@ public class InjectAnnotationBeanPostProcessorTests { @@ -630,6 +630,62 @@ public class InjectAnnotationBeanPostProcessorTests {
bf.destroySingletons();
}
@Test
public void testOptionalListFieldInjectionWithBeanAvailable() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalListFieldInjectionBean.class));
bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
OptionalListFieldInjectionBean bean = (OptionalListFieldInjectionBean) bf.getBean("annotatedBean");
assertTrue(bean.getTestBean().isPresent());
assertSame(bf.getBean("testBean"), bean.getTestBean().get().get(0));
bf.destroySingletons();
}
@Test
public void testOptionalListFieldInjectionWithBeanNotAvailable() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalListFieldInjectionBean.class));
OptionalListFieldInjectionBean bean = (OptionalListFieldInjectionBean) bf.getBean("annotatedBean");
assertFalse(bean.getTestBean().isPresent());
bf.destroySingletons();
}
@Test
public void testOptionalListMethodInjectionWithBeanAvailable() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalListMethodInjectionBean.class));
bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
OptionalListMethodInjectionBean bean = (OptionalListMethodInjectionBean) bf.getBean("annotatedBean");
assertTrue(bean.getTestBean().isPresent());
assertSame(bf.getBean("testBean"), bean.getTestBean().get().get(0));
bf.destroySingletons();
}
@Test
public void testOptionalListMethodInjectionWithBeanNotAvailable() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalListMethodInjectionBean.class));
OptionalListMethodInjectionBean bean = (OptionalListMethodInjectionBean) bf.getBean("annotatedBean");
assertFalse(bean.getTestBean().isPresent());
bf.destroySingletons();
}
@Test
public void testProviderOfOptionalFieldInjectionWithBeanAvailable() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
@ -1239,6 +1295,32 @@ public class InjectAnnotationBeanPostProcessorTests { @@ -1239,6 +1295,32 @@ public class InjectAnnotationBeanPostProcessorTests {
}
public static class OptionalListFieldInjectionBean {
@Inject
private Optional<List<TestBean>> testBean;
public Optional<List<TestBean>> getTestBean() {
return this.testBean;
}
}
public static class OptionalListMethodInjectionBean {
private Optional<List<TestBean>> testBean;
@Inject
public void setTestBean(Optional<List<TestBean>> testBeanFactory) {
this.testBean = testBeanFactory;
}
public Optional<List<TestBean>> getTestBean() {
return this.testBean;
}
}
public static class ProviderOfOptionalFieldInjectionBean {
@Inject

60
spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java

@ -16,6 +16,8 @@ @@ -16,6 +16,8 @@
package org.springframework.context.annotation.configuration;
import java.util.List;
import java.util.Optional;
import javax.inject.Provider;
import org.junit.Test;
@ -58,6 +60,33 @@ public class AutowiredConfigurationTests { @@ -58,6 +60,33 @@ public class AutowiredConfigurationTests {
assertThat(factory.getBean("testBean", TestBean.class).getName(), equalTo(Colour.RED.toString()));
}
@Test
public void testAutowiredConfigurationMethodDependencies() {
AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext(
AutowiredMethodConfig.class, ColorConfig.class);
assertThat(factory.getBean(Colour.class), equalTo(Colour.RED));
assertThat(factory.getBean(TestBean.class).getName(), equalTo("RED-RED"));
}
@Test
public void testAutowiredConfigurationMethodDependenciesWithOptionalAndAvailable() {
AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext(
OptionalAutowiredMethodConfig.class, ColorConfig.class);
assertThat(factory.getBean(Colour.class), equalTo(Colour.RED));
assertThat(factory.getBean(TestBean.class).getName(), equalTo("RED-RED"));
}
@Test
public void testAutowiredConfigurationMethodDependenciesWithOptionalAndNotAvailable() {
AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext(
OptionalAutowiredMethodConfig.class);
assertTrue(factory.getBeansOfType(Colour.class).isEmpty());
assertThat(factory.getBean(TestBean.class).getName(), equalTo(""));
}
/**
* {@link Autowired} constructors are not supported on {@link Configuration} classes
* due to CGLIB constraints
@ -151,11 +180,26 @@ public class AutowiredConfigurationTests { @@ -151,11 +180,26 @@ public class AutowiredConfigurationTests {
@Configuration
static class ColorConfig {
static class AutowiredMethodConfig {
@Bean
public Colour colour() {
return Colour.RED;
public TestBean testBean(Colour colour, List<Colour> colours) {
return new TestBean(colour.toString() + "-" + colours.get(0).toString());
}
}
@Configuration
static class OptionalAutowiredMethodConfig {
@Bean
public TestBean testBean(Optional<Colour> colour, Optional<List<Colour>> colours) {
if (!colour.isPresent() && !colours.isPresent()) {
return new TestBean("");
}
else {
return new TestBean(colour.get().toString() + "-" + colours.get().get(0).toString());
}
}
}
@ -172,6 +216,16 @@ public class AutowiredConfigurationTests { @@ -172,6 +216,16 @@ public class AutowiredConfigurationTests {
}
@Configuration
static class ColorConfig {
@Bean
public Colour colour() {
return Colour.RED;
}
}
@Configuration
static class ValueConfig {

Loading…
Cancel
Save