16
16
17
17
package org .springframework .beans .factory .support ;
18
18
19
- import static org .hamcrest .CoreMatchers .*;
20
- import static org .junit .Assert .*;
21
-
19
+ import java .util .Arrays ;
22
20
import java .util .Map ;
23
21
22
+ import org .junit .Before ;
24
23
import org .junit .Test ;
25
24
26
- import org .springframework .beans .BeansException ;
27
25
import org .springframework .beans .factory .FactoryBean ;
26
+ import org .springframework .beans .factory .config .InstantiationAwareBeanPostProcessor ;
28
27
import org .springframework .beans .factory .config .InstantiationAwareBeanPostProcessorAdapter ;
29
- import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
30
- import org .springframework .beans .factory .support .RootBeanDefinition ;
28
+
29
+ import static org .hamcrest .CoreMatchers .*;
30
+ import static org .junit .Assert .*;
31
31
32
32
/**
33
33
* Unit tests for SPR-8954, in which a custom {@link InstantiationAwareBeanPostProcessor}
34
34
* forces the predicted type of a FactoryBean, effectively preventing retrieval of the
35
35
* bean from calls to #getBeansOfType(FactoryBean.class). The implementation of
36
- * {@link AbstractBeanFactory#isFactoryBean(String, RootBeanDefinition)} now ensures
37
- * that not only the predicted bean type is considered, but also the original bean
38
- * definition's beanClass.
36
+ * {@link AbstractBeanFactory#isFactoryBean(String, RootBeanDefinition)} now ensures that
37
+ * not only the predicted bean type is considered, but also the original bean definition's
38
+ * beanClass.
39
39
*
40
40
* @author Chris Beams
41
41
* @author Oliver Gierke
42
42
*/
43
43
public class Spr8954Tests {
44
44
45
- @ Test
46
- public void repro () {
47
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
45
+ private DefaultListableBeanFactory bf ;
46
+
47
+ @ Before
48
+ public void setUp () {
49
+ bf = new DefaultListableBeanFactory ();
48
50
bf .registerBeanDefinition ("foo" , new RootBeanDefinition (FooFactoryBean .class ));
49
51
bf .addBeanPostProcessor (new PredictingBPP ());
52
+ }
50
53
54
+ @ Test
55
+ public void repro () {
51
56
assertThat (bf .getBean ("foo" ), instanceOf (Foo .class ));
52
57
assertThat (bf .getBean ("&foo" ), instanceOf (FooFactoryBean .class ));
53
-
54
58
assertThat (bf .isTypeMatch ("&foo" , FactoryBean .class ), is (true ));
55
59
56
60
@ SuppressWarnings ("rawtypes" )
57
61
Map <String , FactoryBean > fbBeans = bf .getBeansOfType (FactoryBean .class );
58
- assertThat (1 , equalTo ( fbBeans .size ()));
59
- assertThat ("&foo" , equalTo ( fbBeans .keySet (). iterator (). next () ));
62
+ assertThat (fbBeans .size (), is ( 1 ));
63
+ assertThat (fbBeans .keySet (), hasItem ( "&foo" ));
60
64
61
65
Map <String , AnInterface > aiBeans = bf .getBeansOfType (AnInterface .class );
62
- assertThat (1 , equalTo ( aiBeans .size ()));
63
- assertThat ("&foo" , equalTo ( aiBeans .keySet (). iterator (). next () ));
66
+ assertThat (aiBeans .size (), is ( 1 ));
67
+ assertThat (aiBeans .keySet (), hasItem ( "&foo" ));
64
68
}
65
69
66
70
@ Test
67
71
public void findsBeansByTypeIfNotInstantiated () {
68
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
69
- bf .registerBeanDefinition ("foo" , new RootBeanDefinition (FooFactoryBean .class ));
70
- bf .addBeanPostProcessor (new PredictingBPP ());
71
-
72
72
assertThat (bf .isTypeMatch ("&foo" , FactoryBean .class ), is (true ));
73
73
74
74
@ SuppressWarnings ("rawtypes" )
@@ -77,8 +77,21 @@ public void findsBeansByTypeIfNotInstantiated() {
77
77
assertThat ("&foo" , equalTo (fbBeans .keySet ().iterator ().next ()));
78
78
79
79
Map <String , AnInterface > aiBeans = bf .getBeansOfType (AnInterface .class );
80
- assertThat (1 , equalTo (aiBeans .size ()));
81
- assertThat ("&foo" , equalTo (aiBeans .keySet ().iterator ().next ()));
80
+ assertThat (aiBeans .size (), is (1 ));
81
+ assertThat (aiBeans .keySet (), hasItem ("&foo" ));
82
+ }
83
+
84
+ /**
85
+ * SPR-10517
86
+ */
87
+ @ Test
88
+ public void findsFactoryBeanNameByTypeWithoutInstantiation () {
89
+ String [] names = bf .getBeanNamesForType (AnInterface .class , false , false );
90
+ assertThat (Arrays .asList (names ), hasItem ("&foo" ));
91
+
92
+ Map <String , AnInterface > beans = bf .getBeansOfType (AnInterface .class , false , false );
93
+ assertThat (beans .size (), is (1 ));
94
+ assertThat (beans .keySet (), hasItem ("&foo" ));
82
95
}
83
96
84
97
@@ -116,8 +129,7 @@ static class PredictingBPP extends InstantiationAwareBeanPostProcessorAdapter {
116
129
117
130
@ Override
118
131
public Class <?> predictBeanType (Class <?> beanClass , String beanName ) {
119
- return FactoryBean .class .isAssignableFrom (beanClass ) ?
120
- PredictedType .class : null ;
132
+ return FactoryBean .class .isAssignableFrom (beanClass ) ? PredictedType .class : null ;
121
133
}
122
134
}
123
135
0 commit comments