From 7d2231541ba3629b9a51773fa4a3ddfc50485e29 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 31 Oct 2014 15:28:49 +0100 Subject: [PATCH] Unit tests for java.util.Optional with autowired collections on @Autowired fields/methods and @Bean method parameters Issue: SPR-9132 --- ...njectAnnotationBeanPostProcessorTests.java | 82 +++++++++++++++++++ .../AutowiredConfigurationTests.java | 60 +++++++++++++- 2 files changed, 139 insertions(+), 3 deletions(-) diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java index 3e210ece59..f1bea66f2b 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java @@ -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 { } + public static class OptionalListFieldInjectionBean { + + @Inject + private Optional> testBean; + + public Optional> getTestBean() { + return this.testBean; + } + } + + + public static class OptionalListMethodInjectionBean { + + private Optional> testBean; + + @Inject + public void setTestBean(Optional> testBeanFactory) { + this.testBean = testBeanFactory; + } + + public Optional> getTestBean() { + return this.testBean; + } + } + + public static class ProviderOfOptionalFieldInjectionBean { @Inject diff --git a/spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java b/spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java index 91a9d488c2..345c6e9f5f 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java @@ -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 { 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 { @Configuration - static class ColorConfig { + static class AutowiredMethodConfig { @Bean - public Colour colour() { - return Colour.RED; + public TestBean testBean(Colour colour, List colours) { + return new TestBean(colour.toString() + "-" + colours.get(0).toString()); + } + } + + + @Configuration + static class OptionalAutowiredMethodConfig { + + @Bean + public TestBean testBean(Optional colour, Optional> 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 { } + @Configuration + static class ColorConfig { + + @Bean + public Colour colour() { + return Colour.RED; + } + } + + @Configuration static class ValueConfig {