|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2013 the original author or authors. |
| 2 | + * Copyright 2002-2014 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.io.Serializable;
|
20 | 20 | import java.util.List;
|
21 | 21 | import java.util.Map;
|
| 22 | +import java.util.Optional; |
22 | 23 | import javax.inject.Inject;
|
23 | 24 | import javax.inject.Named;
|
24 | 25 | import javax.inject.Provider;
|
@@ -573,6 +574,62 @@ public void testBeanAutowiredWithFactoryBean() {
|
573 | 574 | bf.destroySingletons();
|
574 | 575 | }
|
575 | 576 |
|
| 577 | + @Test |
| 578 | + public void testOptionalFieldInjectionWithBeanAvailable() { |
| 579 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 580 | + AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); |
| 581 | + bpp.setBeanFactory(bf); |
| 582 | + bf.addBeanPostProcessor(bpp); |
| 583 | + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalFieldInjectionBean.class)); |
| 584 | + bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); |
| 585 | + |
| 586 | + OptionalFieldInjectionBean bean = (OptionalFieldInjectionBean) bf.getBean("annotatedBean"); |
| 587 | + assertTrue(bean.getTestBean().isPresent()); |
| 588 | + assertSame(bf.getBean("testBean"), bean.getTestBean().get()); |
| 589 | + bf.destroySingletons(); |
| 590 | + } |
| 591 | + |
| 592 | + @Test |
| 593 | + public void testOptionalFieldInjectionWithBeanNotAvailable() { |
| 594 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 595 | + AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); |
| 596 | + bpp.setBeanFactory(bf); |
| 597 | + bf.addBeanPostProcessor(bpp); |
| 598 | + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalFieldInjectionBean.class)); |
| 599 | + |
| 600 | + OptionalFieldInjectionBean bean = (OptionalFieldInjectionBean) bf.getBean("annotatedBean"); |
| 601 | + assertFalse(bean.getTestBean().isPresent()); |
| 602 | + bf.destroySingletons(); |
| 603 | + } |
| 604 | + |
| 605 | + @Test |
| 606 | + public void testOptionalMethodInjectionWithBeanAvailable() { |
| 607 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 608 | + AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); |
| 609 | + bpp.setBeanFactory(bf); |
| 610 | + bf.addBeanPostProcessor(bpp); |
| 611 | + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalMethodInjectionBean.class)); |
| 612 | + bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); |
| 613 | + |
| 614 | + OptionalMethodInjectionBean bean = (OptionalMethodInjectionBean) bf.getBean("annotatedBean"); |
| 615 | + assertTrue(bean.getTestBean().isPresent()); |
| 616 | + assertSame(bf.getBean("testBean"), bean.getTestBean().get()); |
| 617 | + bf.destroySingletons(); |
| 618 | + } |
| 619 | + |
| 620 | + @Test |
| 621 | + public void testOptionalMethodInjectionWithBeanNotAvailable() { |
| 622 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 623 | + AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); |
| 624 | + bpp.setBeanFactory(bf); |
| 625 | + bf.addBeanPostProcessor(bpp); |
| 626 | + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalMethodInjectionBean.class)); |
| 627 | + |
| 628 | + OptionalMethodInjectionBean bean = (OptionalMethodInjectionBean) bf.getBean("annotatedBean"); |
| 629 | + assertFalse(bean.getTestBean().isPresent()); |
| 630 | + bf.destroySingletons(); |
| 631 | + } |
| 632 | + |
576 | 633 |
|
577 | 634 | public static class ResourceInjectionBean {
|
578 | 635 |
|
@@ -1099,4 +1156,30 @@ public boolean isSingleton() {
|
1099 | 1156 | }
|
1100 | 1157 | }
|
1101 | 1158 |
|
| 1159 | + |
| 1160 | + public static class OptionalFieldInjectionBean { |
| 1161 | + |
| 1162 | + @Inject |
| 1163 | + private Optional<TestBean> testBean; |
| 1164 | + |
| 1165 | + public Optional<TestBean> getTestBean() { |
| 1166 | + return this.testBean; |
| 1167 | + } |
| 1168 | + } |
| 1169 | + |
| 1170 | + |
| 1171 | + public static class OptionalMethodInjectionBean { |
| 1172 | + |
| 1173 | + private Optional<TestBean> testBean; |
| 1174 | + |
| 1175 | + @Inject |
| 1176 | + public void setTestBean(Optional<TestBean> testBeanFactory) { |
| 1177 | + this.testBean = testBeanFactory; |
| 1178 | + } |
| 1179 | + |
| 1180 | + public Optional<TestBean> getTestBean() { |
| 1181 | + return this.testBean; |
| 1182 | + } |
| 1183 | + } |
| 1184 | + |
1102 | 1185 | }
|
0 commit comments