1 16 17 package org.springframework.beans.factory; 18 19 import java.util.Arrays ; 20 import java.util.List ; 21 import java.util.Map ; 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 38 public class BeanFactoryUtilsTests extends TestCase { 39 40 private ConfigurableListableBeanFactory listableBeanFactory; 41 42 protected void setUp() { 43 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 61 public void testHierarchicalCountBeansWithOverride() throws Exception { 62 assertTrue(this.listableBeanFactory.getBeanDefinitionCount() == 1); 64 assertTrue("Should count 7 beans, not " + BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory), 66 BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory) == 7); 67 } 68 69 public void testHierarchicalNamesWithOverride() throws Exception { 70 List names = Arrays.asList( 71 BeanFactoryUtils.beanNamesIncludingAncestors(this.listableBeanFactory, ITestBean.class)); 72 assertEquals(2, names.size()); 74 assertTrue(names.contains("test")); 75 assertTrue(names.contains("test3")); 76 } 77 78 public void testHierarchicalNamesWithNoMatch() throws Exception { 79 List names = Arrays.asList( 80 BeanFactoryUtils.beanNamesIncludingAncestors(this.listableBeanFactory, HandlerAdapter.class)); 81 assertEquals(0, names.size()); 82 } 83 84 public void testHierarchicalNamesWithMatchOnlyInRoot() throws Exception { 85 List names = Arrays.asList( 86 BeanFactoryUtils.beanNamesIncludingAncestors(this.listableBeanFactory, IndexedTestBean.class)); 87 assertEquals(1, names.size()); 88 assertTrue(names.contains("indexedBean")); 89 assertTrue(listableBeanFactory.getBeanDefinitionNames(IndexedTestBean.class).length == 0); 91 } 92 93 public void testGetBeanNamesForTypeWithOverride() throws Exception { 94 List names = Arrays.asList( 95 BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class)); 96 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 ()); 107 Map 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 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 test3 = this.listableBeanFactory.getBean("test3"); 150 Object test = this.listableBeanFactory.getBean("test"); 151 Object 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 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 { 201 Object test3 = this.listableBeanFactory.getBean("test3"); 202 Object test = this.listableBeanFactory.getBean("test"); 203 Object testFactory1 = this.listableBeanFactory.getBean("testFactory1"); 204 205 Map 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 |