1616
1717package org .springframework .beans .factory .support ;
1818
19- import static org .hamcrest .CoreMatchers .*;
20- import static org .junit .Assert .*;
21-
19+ import java .util .Arrays ;
2220import java .util .Map ;
2321
22+ import org .junit .Before ;
2423import org .junit .Test ;
2524
26- import org .springframework .beans .BeansException ;
2725import org .springframework .beans .factory .FactoryBean ;
26+ import org .springframework .beans .factory .config .InstantiationAwareBeanPostProcessor ;
2827import 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 .*;
3131
3232/**
3333 * Unit tests for SPR-8954, in which a custom {@link InstantiationAwareBeanPostProcessor}
3434 * forces the predicted type of a FactoryBean, effectively preventing retrieval of the
3535 * 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.
3939 *
4040 * @author Chris Beams
4141 * @author Oliver Gierke
4242 */
4343public class Spr8954Tests {
4444
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 ();
4850 bf .registerBeanDefinition ("foo" , new RootBeanDefinition (FooFactoryBean .class ));
4951 bf .addBeanPostProcessor (new PredictingBPP ());
52+ }
5053
54+ @ Test
55+ public void repro () {
5156 assertThat (bf .getBean ("foo" ), instanceOf (Foo .class ));
5257 assertThat (bf .getBean ("&foo" ), instanceOf (FooFactoryBean .class ));
53-
5458 assertThat (bf .isTypeMatch ("&foo" , FactoryBean .class ), is (true ));
5559
5660 @ SuppressWarnings ("rawtypes" )
5761 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" ));
6064
6165 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" ));
6468 }
6569
6670 @ Test
6771 public void findsBeansByTypeIfNotInstantiated () {
68- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
69- bf .registerBeanDefinition ("foo" , new RootBeanDefinition (FooFactoryBean .class ));
70- bf .addBeanPostProcessor (new PredictingBPP ());
71-
7272 assertThat (bf .isTypeMatch ("&foo" , FactoryBean .class ), is (true ));
7373
7474 @ SuppressWarnings ("rawtypes" )
@@ -77,8 +77,21 @@ public void findsBeansByTypeIfNotInstantiated() {
7777 assertThat ("&foo" , equalTo (fbBeans .keySet ().iterator ().next ()));
7878
7979 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" ));
8295 }
8396
8497
@@ -116,8 +129,7 @@ static class PredictingBPP extends InstantiationAwareBeanPostProcessorAdapter {
116129
117130 @ Override
118131 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 ;
121133 }
122134 }
123135
0 commit comments