|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2009 the original author or authors. |
| 2 | + * Copyright 2002-2012 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.
|
|
23 | 23 | import javax.inject.Named;
|
24 | 24 | import javax.inject.Provider;
|
25 | 25 |
|
26 |
| -import static org.junit.Assert.*; |
27 | 26 | import org.junit.Test;
|
28 | 27 | import test.beans.ITestBean;
|
29 | 28 | import test.beans.IndexedTestBean;
|
|
40 | 39 | import org.springframework.beans.factory.support.RootBeanDefinition;
|
41 | 40 | import org.springframework.util.SerializationTestUtils;
|
42 | 41 |
|
| 42 | +import static org.junit.Assert.*; |
| 43 | + |
43 | 44 | /**
|
44 | 45 | * Unit tests for {@link org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor}
|
45 | 46 | * processing the JSR-303 {@link javax.inject.Inject} annotation.
|
@@ -206,8 +207,8 @@ public void testConstructorResourceInjectionWithMultipleCandidatesAsCollection()
|
206 | 207 | AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
|
207 | 208 | bpp.setBeanFactory(bf);
|
208 | 209 | bf.addBeanPostProcessor(bpp);
|
209 |
| - bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition( |
210 |
| - ConstructorsCollectionResourceInjectionBean.class)); |
| 210 | + bf.registerBeanDefinition("annotatedBean", |
| 211 | + new RootBeanDefinition(ConstructorsCollectionResourceInjectionBean.class)); |
211 | 212 | TestBean tb = new TestBean();
|
212 | 213 | bf.registerSingleton("testBean", tb);
|
213 | 214 | NestedTestBean ntb1 = new NestedTestBean();
|
@@ -415,6 +416,74 @@ public void testObjectFactorySerialization() throws Exception {
|
415 | 416 | bf.destroySingletons();
|
416 | 417 | }
|
417 | 418 |
|
| 419 | + @Test |
| 420 | + public void testObjectFactoryWithTypedListField() throws Exception { |
| 421 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 422 | + AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); |
| 423 | + bpp.setBeanFactory(bf); |
| 424 | + bf.addBeanPostProcessor(bpp); |
| 425 | + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryListFieldInjectionBean.class)); |
| 426 | + bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); |
| 427 | + bf.setSerializationId("test"); |
| 428 | + |
| 429 | + ObjectFactoryListFieldInjectionBean bean = (ObjectFactoryListFieldInjectionBean) bf.getBean("annotatedBean"); |
| 430 | + assertSame(bf.getBean("testBean"), bean.getTestBean()); |
| 431 | + bean = (ObjectFactoryListFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); |
| 432 | + assertSame(bf.getBean("testBean"), bean.getTestBean()); |
| 433 | + bf.destroySingletons(); |
| 434 | + } |
| 435 | + |
| 436 | + @Test |
| 437 | + public void testObjectFactoryWithTypedListMethod() throws Exception { |
| 438 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 439 | + AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); |
| 440 | + bpp.setBeanFactory(bf); |
| 441 | + bf.addBeanPostProcessor(bpp); |
| 442 | + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryListMethodInjectionBean.class)); |
| 443 | + bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); |
| 444 | + bf.setSerializationId("test"); |
| 445 | + |
| 446 | + ObjectFactoryListMethodInjectionBean bean = (ObjectFactoryListMethodInjectionBean) bf.getBean("annotatedBean"); |
| 447 | + assertSame(bf.getBean("testBean"), bean.getTestBean()); |
| 448 | + bean = (ObjectFactoryListMethodInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); |
| 449 | + assertSame(bf.getBean("testBean"), bean.getTestBean()); |
| 450 | + bf.destroySingletons(); |
| 451 | + } |
| 452 | + |
| 453 | + @Test |
| 454 | + public void testObjectFactoryWithTypedMapField() throws Exception { |
| 455 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 456 | + AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); |
| 457 | + bpp.setBeanFactory(bf); |
| 458 | + bf.addBeanPostProcessor(bpp); |
| 459 | + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryMapFieldInjectionBean.class)); |
| 460 | + bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); |
| 461 | + bf.setSerializationId("test"); |
| 462 | + |
| 463 | + ObjectFactoryMapFieldInjectionBean bean = (ObjectFactoryMapFieldInjectionBean) bf.getBean("annotatedBean"); |
| 464 | + assertSame(bf.getBean("testBean"), bean.getTestBean()); |
| 465 | + bean = (ObjectFactoryMapFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); |
| 466 | + assertSame(bf.getBean("testBean"), bean.getTestBean()); |
| 467 | + bf.destroySingletons(); |
| 468 | + } |
| 469 | + |
| 470 | + @Test |
| 471 | + public void testObjectFactoryWithTypedMapMethod() throws Exception { |
| 472 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 473 | + AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); |
| 474 | + bpp.setBeanFactory(bf); |
| 475 | + bf.addBeanPostProcessor(bpp); |
| 476 | + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryMapMethodInjectionBean.class)); |
| 477 | + bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); |
| 478 | + bf.setSerializationId("test"); |
| 479 | + |
| 480 | + ObjectFactoryMapMethodInjectionBean bean = (ObjectFactoryMapMethodInjectionBean) bf.getBean("annotatedBean"); |
| 481 | + assertSame(bf.getBean("testBean"), bean.getTestBean()); |
| 482 | + bean = (ObjectFactoryMapMethodInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); |
| 483 | + assertSame(bf.getBean("testBean"), bean.getTestBean()); |
| 484 | + bf.destroySingletons(); |
| 485 | + } |
| 486 | + |
418 | 487 | /**
|
419 | 488 | * Verifies that a dependency on a {@link org.springframework.beans.factory.FactoryBean} can be autowired via
|
420 | 489 | * {@link org.springframework.beans.factory.annotation.Autowired @Inject}, specifically addressing the JIRA issue
|
@@ -835,6 +904,66 @@ public TestBean getTestBean() {
|
835 | 904 | }
|
836 | 905 |
|
837 | 906 |
|
| 907 | + public static class ObjectFactoryListFieldInjectionBean implements Serializable { |
| 908 | + |
| 909 | + @Inject |
| 910 | + private Provider<List<TestBean>> testBeanFactory; |
| 911 | + |
| 912 | + public void setTestBeanFactory(Provider<List<TestBean>> testBeanFactory) { |
| 913 | + this.testBeanFactory = testBeanFactory; |
| 914 | + } |
| 915 | + |
| 916 | + public TestBean getTestBean() { |
| 917 | + return this.testBeanFactory.get().get(0); |
| 918 | + } |
| 919 | + } |
| 920 | + |
| 921 | + |
| 922 | + public static class ObjectFactoryListMethodInjectionBean implements Serializable { |
| 923 | + |
| 924 | + private Provider<List<TestBean>> testBeanFactory; |
| 925 | + |
| 926 | + @Inject |
| 927 | + public void setTestBeanFactory(Provider<List<TestBean>> testBeanFactory) { |
| 928 | + this.testBeanFactory = testBeanFactory; |
| 929 | + } |
| 930 | + |
| 931 | + public TestBean getTestBean() { |
| 932 | + return this.testBeanFactory.get().get(0); |
| 933 | + } |
| 934 | + } |
| 935 | + |
| 936 | + |
| 937 | + public static class ObjectFactoryMapFieldInjectionBean implements Serializable { |
| 938 | + |
| 939 | + @Inject |
| 940 | + private Provider<Map<String, TestBean>> testBeanFactory; |
| 941 | + |
| 942 | + public void setTestBeanFactory(Provider<Map<String, TestBean>> testBeanFactory) { |
| 943 | + this.testBeanFactory = testBeanFactory; |
| 944 | + } |
| 945 | + |
| 946 | + public TestBean getTestBean() { |
| 947 | + return this.testBeanFactory.get().values().iterator().next(); |
| 948 | + } |
| 949 | + } |
| 950 | + |
| 951 | + |
| 952 | + public static class ObjectFactoryMapMethodInjectionBean implements Serializable { |
| 953 | + |
| 954 | + private Provider<Map<String, TestBean>> testBeanFactory; |
| 955 | + |
| 956 | + @Inject |
| 957 | + public void setTestBeanFactory(Provider<Map<String, TestBean>> testBeanFactory) { |
| 958 | + this.testBeanFactory = testBeanFactory; |
| 959 | + } |
| 960 | + |
| 961 | + public TestBean getTestBean() { |
| 962 | + return this.testBeanFactory.get().values().iterator().next(); |
| 963 | + } |
| 964 | + } |
| 965 | + |
| 966 | + |
838 | 967 | /**
|
839 | 968 | * Bean with a dependency on a {@link org.springframework.beans.factory.FactoryBean}.
|
840 | 969 | */
|
|
0 commit comments