KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > BeanFactoryUtilsTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.factory;
18
19 import java.util.Arrays JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.springframework.beans.ITestBean;
26 import org.springframework.beans.IndexedTestBean;
27 import org.springframework.beans.TestBean;
28 import org.springframework.beans.factory.support.StaticListableBeanFactory;
29 import org.springframework.beans.factory.xml.XmlBeanFactory;
30 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
31 import org.springframework.core.io.ClassPathResource;
32 import org.springframework.web.servlet.HandlerAdapter;
33
34 /**
35  * @author Rod Johnson
36  * @since 04.07.2003
37  */

38 public class BeanFactoryUtilsTests extends TestCase {
39
40     private ConfigurableListableBeanFactory listableBeanFactory;
41
42     protected void setUp() {
43         // Interesting hierarchical factory to test counts.
44
// Slow to read so we cache it.
45
XmlBeanFactory grandParent = new XmlBeanFactory(new ClassPathResource("root.xml", getClass()));
46         XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("middle.xml", getClass()), grandParent);
47         XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("leaf.xml", getClass()), parent);
48         this.listableBeanFactory = child;
49     }
50
51     public void testHierarchicalCountBeansWithNonHierarchicalFactory() {
52         StaticListableBeanFactory lbf = new StaticListableBeanFactory();
53         lbf.addBean("t1", new TestBean());
54         lbf.addBean("t2", new TestBean());
55         assertTrue(BeanFactoryUtils.countBeansIncludingAncestors(lbf) == 2);
56     }
57
58     /**
59      * Check that override doesn't count as too separate beans.
60      */

61     public void testHierarchicalCountBeansWithOverride() throws Exception JavaDoc {
62         // Leaf count
63
assertTrue(this.listableBeanFactory.getBeanDefinitionCount() == 1);
64         // Count minus duplicate
65
assertTrue("Should count 7 beans, not " + BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory),
66                 BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory) == 7);
67     }
68
69     public void testHierarchicalNamesWithOverride() throws Exception JavaDoc {
70         List JavaDoc names = Arrays.asList(
71                 BeanFactoryUtils.beanNamesIncludingAncestors(this.listableBeanFactory, ITestBean.class));
72         // includes 2 TestBeans from FactoryBeans (DummyFactory definitions)
73
assertEquals(2, names.size());
74         assertTrue(names.contains("test"));
75         assertTrue(names.contains("test3"));
76     }
77
78     public void testHierarchicalNamesWithNoMatch() throws Exception JavaDoc {
79         List JavaDoc names = Arrays.asList(
80                 BeanFactoryUtils.beanNamesIncludingAncestors(this.listableBeanFactory, HandlerAdapter.class));
81         assertEquals(0, names.size());
82     }
83
84     public void testHierarchicalNamesWithMatchOnlyInRoot() throws Exception JavaDoc {
85         List JavaDoc names = Arrays.asList(
86                 BeanFactoryUtils.beanNamesIncludingAncestors(this.listableBeanFactory, IndexedTestBean.class));
87         assertEquals(1, names.size());
88         assertTrue(names.contains("indexedBean"));
89         // Distinguish from default ListableBeanFactory behavior
90
assertTrue(listableBeanFactory.getBeanDefinitionNames(IndexedTestBean.class).length == 0);
91     }
92
93     public void testGetBeanNamesForTypeWithOverride() throws Exception JavaDoc {
94         List JavaDoc names = Arrays.asList(
95                 BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class));
96         // includes 2 TestBeans from FactoryBeans (DummyFactory definitions)
97
assertEquals(4, names.size());
98         assertTrue(names.contains("test"));
99         assertTrue(names.contains("test3"));
100         assertTrue(names.contains("testFactory1"));
101         assertTrue(names.contains("testFactory2"));
102     }
103
104     public void testNoBeansOfType() {
105         StaticListableBeanFactory lbf = new StaticListableBeanFactory();
106         lbf.addBean("foo", new Object JavaDoc());
107         Map JavaDoc beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
108         assertTrue(beans.isEmpty());
109     }
110
111     public void testFindsBeansOfTypeWithStaticFactory() {
112         StaticListableBeanFactory lbf = new StaticListableBeanFactory();
113         TestBean t1 = new TestBean();
114         TestBean t2 = new TestBean();
115         DummyFactory t3 = new DummyFactory();
116         DummyFactory t4 = new DummyFactory();
117         t4.setSingleton(false);
118         lbf.addBean("t1", t1);
119         lbf.addBean("t2", t2);
120         lbf.addBean("t3", t3);
121         lbf.addBean("t4", t4);
122
123         Map JavaDoc beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
124         assertEquals(2, beans.size());
125         assertEquals(t1, beans.get("t1"));
126         assertEquals(t2, beans.get("t2"));
127         beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, false, true);
128         assertEquals(3, beans.size());
129         assertEquals(t1, beans.get("t1"));
130         assertEquals(t2, beans.get("t2"));
131         assertEquals(t3.getObject(), beans.get("t3"));
132         beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true);
133         assertEquals(4, beans.size());
134         assertEquals(t1, beans.get("t1"));
135         assertEquals(t2, beans.get("t2"));
136         assertEquals(t3.getObject(), beans.get("t3"));
137         assertTrue(beans.get("t4") instanceof TestBean);
138         beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, DummyFactory.class, true, true);
139         assertEquals(2, beans.size());
140         assertEquals(t3, beans.get("&t3"));
141         assertEquals(t4, beans.get("&t4"));
142         beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, FactoryBean.class, true, true);
143         assertEquals(2, beans.size());
144         assertEquals(t3, beans.get("&t3"));
145         assertEquals(t4, beans.get("&t4"));
146     }
147
148     public void testFindsBeansOfTypeWithDefaultFactory() {
149         Object JavaDoc test3 = this.listableBeanFactory.getBean("test3");
150         Object JavaDoc test = this.listableBeanFactory.getBean("test");
151         Object JavaDoc testFactory1 = this.listableBeanFactory.getBean("testFactory1");
152
153         TestBean t1 = new TestBean();
154         TestBean t2 = new TestBean();
155         DummyFactory t3 = new DummyFactory();
156         DummyFactory t4 = new DummyFactory();
157         t4.setSingleton(false);
158         this.listableBeanFactory.registerSingleton("t1", t1);
159         this.listableBeanFactory.registerSingleton("t2", t2);
160         this.listableBeanFactory.registerSingleton("t3", t3);
161         this.listableBeanFactory.registerSingleton("t4", t4);
162
163         Map JavaDoc beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
164         assertEquals(4, beans.size());
165         assertEquals(test3, beans.get("test3"));
166         assertEquals(test, beans.get("test"));
167         assertEquals(t1, beans.get("t1"));
168         assertEquals(t2, beans.get("t2"));
169         beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true);
170         assertEquals(5, beans.size());
171         assertEquals(test, beans.get("test"));
172         assertEquals(testFactory1, beans.get("testFactory1"));
173         assertEquals(t1, beans.get("t1"));
174         assertEquals(t2, beans.get("t2"));
175         assertEquals(t3.getObject(), beans.get("t3"));
176         beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, true);
177         assertEquals(8, beans.size());
178         assertEquals(test3, beans.get("test3"));
179         assertEquals(test, beans.get("test"));
180         assertEquals(testFactory1, beans.get("testFactory1"));
181         assertTrue(beans.get("testFactory2") instanceof TestBean);
182         assertEquals(t1, beans.get("t1"));
183         assertEquals(t2, beans.get("t2"));
184         assertEquals(t3.getObject(), beans.get("t3"));
185         assertTrue(beans.get("t4") instanceof TestBean);
186         beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, DummyFactory.class, true, true);
187         assertEquals(4, beans.size());
188         assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1"));
189         assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2"));
190         assertEquals(t3, beans.get("&t3"));
191         assertEquals(t4, beans.get("&t4"));
192         beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, FactoryBean.class, true, true);
193         assertEquals(4, beans.size());
194         assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1"));
195         assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2"));
196         assertEquals(t3, beans.get("&t3"));
197         assertEquals(t4, beans.get("&t4"));
198     }
199
200     public void testHierarchicalResolutionWithOverride() throws Exception JavaDoc {
201         Object JavaDoc test3 = this.listableBeanFactory.getBean("test3");
202         Object JavaDoc test = this.listableBeanFactory.getBean("test");
203         Object JavaDoc testFactory1 = this.listableBeanFactory.getBean("testFactory1");
204
205         Map JavaDoc beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
206         assertEquals(2, beans.size());
207         assertEquals(test3, beans.get("test3"));
208         assertEquals(test, beans.get("test"));
209         beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, false);
210         assertEquals(1, beans.size());
211         assertEquals(test, beans.get("test"));
212         beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true);
213         assertEquals(2, beans.size());
214         assertEquals(test, beans.get("test"));
215         assertEquals(testFactory1, beans.get("testFactory1"));
216         beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, true);
217         assertEquals(4, beans.size());
218         assertEquals(test3, beans.get("test3"));
219         assertEquals(test, beans.get("test"));
220         assertEquals(testFactory1, beans.get("testFactory1"));
221         assertTrue(beans.get("testFactory2") instanceof TestBean);
222         beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, DummyFactory.class, true, true);
223         assertEquals(2, beans.size());
224         assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1"));
225         assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2"));
226         beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, FactoryBean.class, true, true);
227         assertEquals(2, beans.size());
228         assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1"));
229         assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2"));
230     }
231
232 }
233
Popular Tags