1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2015 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.
16
16
17
17
package org .springframework .beans .factory ;
18
18
19
- import static org .junit .Assert .assertEquals ;
20
- import static org .junit .Assert .assertTrue ;
21
- import static org .springframework .tests .TestResourceUtils .qualifiedResource ;
22
-
23
19
import java .util .Arrays ;
24
20
import java .util .List ;
25
21
import java .util .Map ;
26
22
27
23
import org .junit .Before ;
28
24
import org .junit .Test ;
29
- import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
30
- import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
25
+
31
26
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
32
27
import org .springframework .beans .factory .support .StaticListableBeanFactory ;
33
28
import org .springframework .beans .factory .xml .XmlBeanDefinitionReader ;
39
34
import org .springframework .tests .sample .beans .factory .DummyFactory ;
40
35
import org .springframework .util .ObjectUtils ;
41
36
37
+ import static org .junit .Assert .*;
38
+ import static org .springframework .tests .TestResourceUtils .*;
42
39
43
40
/**
44
41
* @author Rod Johnson
@@ -54,29 +51,30 @@ public final class BeanFactoryUtilsTests {
54
51
private static final Resource LEAF_CONTEXT = qualifiedResource (CLASS , "leaf.xml" );
55
52
private static final Resource DEPENDENT_BEANS_CONTEXT = qualifiedResource (CLASS , "dependentBeans.xml" );
56
53
57
- private ConfigurableListableBeanFactory listableBeanFactory ;
54
+ private DefaultListableBeanFactory listableBeanFactory ;
55
+
56
+ private DefaultListableBeanFactory dependentBeansFactory ;
58
57
59
- private ConfigurableListableBeanFactory dependentBeansBF ;
60
58
61
59
@ Before
62
60
public void setUp () {
63
61
// Interesting hierarchical factory to test counts.
64
62
// Slow to read so we cache it.
65
63
66
-
67
64
DefaultListableBeanFactory grandParent = new DefaultListableBeanFactory ();
68
65
new XmlBeanDefinitionReader (grandParent ).loadBeanDefinitions (ROOT_CONTEXT );
69
66
DefaultListableBeanFactory parent = new DefaultListableBeanFactory (grandParent );
70
67
new XmlBeanDefinitionReader (parent ).loadBeanDefinitions (MIDDLE_CONTEXT );
71
68
DefaultListableBeanFactory child = new DefaultListableBeanFactory (parent );
72
69
new XmlBeanDefinitionReader (child ).loadBeanDefinitions (LEAF_CONTEXT );
73
70
74
- this .dependentBeansBF = new DefaultListableBeanFactory ();
75
- new XmlBeanDefinitionReader (( BeanDefinitionRegistry ) this .dependentBeansBF ).loadBeanDefinitions (DEPENDENT_BEANS_CONTEXT );
76
- dependentBeansBF .preInstantiateSingletons ();
71
+ this .dependentBeansFactory = new DefaultListableBeanFactory ();
72
+ new XmlBeanDefinitionReader (this .dependentBeansFactory ).loadBeanDefinitions (DEPENDENT_BEANS_CONTEXT );
73
+ dependentBeansFactory .preInstantiateSingletons ();
77
74
this .listableBeanFactory = child ;
78
75
}
79
76
77
+
80
78
@ Test
81
79
public void testHierarchicalCountBeansWithNonHierarchicalFactory () {
82
80
StaticListableBeanFactory lbf = new StaticListableBeanFactory ();
@@ -93,22 +91,21 @@ public void testHierarchicalCountBeansWithOverride() throws Exception {
93
91
// Leaf count
94
92
assertTrue (this .listableBeanFactory .getBeanDefinitionCount () == 1 );
95
93
// Count minus duplicate
96
- assertTrue ("Should count 7 beans, not "
97
- + BeanFactoryUtils .countBeansIncludingAncestors (this .listableBeanFactory ),
98
- BeanFactoryUtils .countBeansIncludingAncestors (this .listableBeanFactory ) == 7 );
94
+ assertTrue ("Should count 7 beans, not " + BeanFactoryUtils .countBeansIncludingAncestors (this .listableBeanFactory ),
95
+ BeanFactoryUtils .countBeansIncludingAncestors (this .listableBeanFactory ) == 7 );
99
96
}
100
97
101
98
@ Test
102
99
public void testHierarchicalNamesWithNoMatch () throws Exception {
103
- List <String > names = Arrays .asList (BeanFactoryUtils . beanNamesForTypeIncludingAncestors ( this . listableBeanFactory ,
104
- NoOp .class ));
100
+ List <String > names = Arrays .asList (
101
+ BeanFactoryUtils . beanNamesForTypeIncludingAncestors ( this . listableBeanFactory , NoOp .class ));
105
102
assertEquals (0 , names .size ());
106
103
}
107
104
108
105
@ Test
109
106
public void testHierarchicalNamesWithMatchOnlyInRoot () throws Exception {
110
- List <String > names = Arrays .asList (BeanFactoryUtils . beanNamesForTypeIncludingAncestors ( this . listableBeanFactory ,
111
- IndexedTestBean .class ));
107
+ List <String > names = Arrays .asList (
108
+ BeanFactoryUtils . beanNamesForTypeIncludingAncestors ( this . listableBeanFactory , IndexedTestBean .class ));
112
109
assertEquals (1 , names .size ());
113
110
assertTrue (names .contains ("indexedBean" ));
114
111
// Distinguish from default ListableBeanFactory behavior
@@ -117,8 +114,8 @@ public void testHierarchicalNamesWithMatchOnlyInRoot() throws Exception {
117
114
118
115
@ Test
119
116
public void testGetBeanNamesForTypeWithOverride () throws Exception {
120
- List <String > names = Arrays .asList (BeanFactoryUtils . beanNamesForTypeIncludingAncestors ( this . listableBeanFactory ,
121
- ITestBean .class ));
117
+ List <String > names = Arrays .asList (
118
+ BeanFactoryUtils . beanNamesForTypeIncludingAncestors ( this . listableBeanFactory , ITestBean .class ));
122
119
// includes 2 TestBeans from FactoryBeans (DummyFactory definitions)
123
120
assertEquals (4 , names .size ());
124
121
assertTrue (names .contains ("test" ));
@@ -131,7 +128,7 @@ public void testGetBeanNamesForTypeWithOverride() throws Exception {
131
128
public void testNoBeansOfType () {
132
129
StaticListableBeanFactory lbf = new StaticListableBeanFactory ();
133
130
lbf .addBean ("foo" , new Object ());
134
- Map <? , ?> beans = BeanFactoryUtils .beansOfTypeIncludingAncestors (lbf , ITestBean .class , true , false );
131
+ Map <String , ?> beans = BeanFactoryUtils .beansOfTypeIncludingAncestors (lbf , ITestBean .class , true , false );
135
132
assertTrue (beans .isEmpty ());
136
133
}
137
134
@@ -148,7 +145,7 @@ public void testFindsBeansOfTypeWithStaticFactory() {
148
145
lbf .addBean ("t3" , t3 );
149
146
lbf .addBean ("t4" , t4 );
150
147
151
- Map <? , ?> beans = BeanFactoryUtils .beansOfTypeIncludingAncestors (lbf , ITestBean .class , true , false );
148
+ Map <String , ?> beans = BeanFactoryUtils .beansOfTypeIncludingAncestors (lbf , ITestBean .class , true , false );
152
149
assertEquals (2 , beans .size ());
153
150
assertEquals (t1 , beans .get ("t1" ));
154
151
assertEquals (t2 , beans .get ("t2" ));
@@ -192,21 +189,18 @@ public void testFindsBeansOfTypeWithDefaultFactory() {
192
189
this .listableBeanFactory .registerSingleton ("t3" , t3 );
193
190
this .listableBeanFactory .registerSingleton ("t4" , t4 );
194
191
195
- Map <? , ?> beans = BeanFactoryUtils . beansOfTypeIncludingAncestors ( this . listableBeanFactory , ITestBean . class , true ,
196
- false );
192
+ Map <String , ?> beans =
193
+ BeanFactoryUtils . beansOfTypeIncludingAncestors ( this . listableBeanFactory , ITestBean . class , true , false );
197
194
assertEquals (6 , beans .size ());
198
195
assertEquals (test3 , beans .get ("test3" ));
199
196
assertEquals (test , beans .get ("test" ));
200
197
assertEquals (t1 , beans .get ("t1" ));
201
198
assertEquals (t2 , beans .get ("t2" ));
202
199
assertEquals (t3 .getObject (), beans .get ("t3" ));
203
200
assertTrue (beans .get ("t4" ) instanceof TestBean );
204
- // t3 and t4 are found here as of Spring 2.0, since they are
205
- // pre-registered
206
- // singleton instances, while testFactory1 and testFactory are *not*
207
- // found
208
- // because they are FactoryBean definitions that haven't been
209
- // initialized yet.
201
+ // t3 and t4 are found here as of Spring 2.0, since they are pre-registered
202
+ // singleton instances, while testFactory1 and testFactory are *not* found
203
+ // because they are FactoryBean definitions that haven't been initialized yet.
210
204
211
205
beans = BeanFactoryUtils .beansOfTypeIncludingAncestors (this .listableBeanFactory , ITestBean .class , false , true );
212
206
Object testFactory1 = this .listableBeanFactory .getBean ("testFactory1" );
@@ -248,8 +242,8 @@ public void testHierarchicalResolutionWithOverride() throws Exception {
248
242
Object test3 = this .listableBeanFactory .getBean ("test3" );
249
243
Object test = this .listableBeanFactory .getBean ("test" );
250
244
251
- Map <? , ?> beans = BeanFactoryUtils . beansOfTypeIncludingAncestors ( this . listableBeanFactory , ITestBean . class , true ,
252
- false );
245
+ Map <String , ?> beans =
246
+ BeanFactoryUtils . beansOfTypeIncludingAncestors ( this . listableBeanFactory , ITestBean . class , true , false );
253
247
assertEquals (2 , beans .size ());
254
248
assertEquals (test3 , beans .get ("test3" ));
255
249
assertEquals (test , beans .get ("test" ));
@@ -284,25 +278,25 @@ public void testHierarchicalResolutionWithOverride() throws Exception {
284
278
285
279
@ Test
286
280
public void testADependencies () {
287
- String [] deps = this .dependentBeansBF .getDependentBeans ("a" );
281
+ String [] deps = this .dependentBeansFactory .getDependentBeans ("a" );
288
282
assertTrue (ObjectUtils .isEmpty (deps ));
289
283
}
290
284
291
285
@ Test
292
286
public void testBDependencies () {
293
- String [] deps = this .dependentBeansBF .getDependentBeans ("b" );
287
+ String [] deps = this .dependentBeansFactory .getDependentBeans ("b" );
294
288
assertTrue (Arrays .equals (new String [] { "c" }, deps ));
295
289
}
296
290
297
291
@ Test
298
292
public void testCDependencies () {
299
- String [] deps = this .dependentBeansBF .getDependentBeans ("c" );
293
+ String [] deps = this .dependentBeansFactory .getDependentBeans ("c" );
300
294
assertTrue (Arrays .equals (new String [] { "int" , "long" }, deps ));
301
295
}
302
296
303
297
@ Test
304
298
public void testIntDependencies () {
305
- String [] deps = this .dependentBeansBF .getDependentBeans ("int" );
299
+ String [] deps = this .dependentBeansFactory .getDependentBeans ("int" );
306
300
assertTrue (Arrays .equals (new String [] { "buffer" }, deps ));
307
301
}
308
302
0 commit comments