1 16 17 package org.springframework.beans.factory.xml; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.InputStreamReader ; 22 import java.io.StringWriter ; 23 import java.lang.reflect.Method ; 24 import java.net.URL ; 25 import java.util.Arrays ; 26 import java.util.Collections ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import javax.mail.Session ; 31 import javax.servlet.ServletException ; 32 33 import junit.framework.AssertionFailedError; 34 import junit.framework.TestCase; 35 36 import org.springframework.beans.DerivedTestBean; 37 import org.springframework.beans.FatalBeanException; 38 import org.springframework.beans.ITestBean; 39 import org.springframework.beans.IndexedTestBean; 40 import org.springframework.beans.MutablePropertyValues; 41 import org.springframework.beans.ResourceTestBean; 42 import org.springframework.beans.TestBean; 43 import org.springframework.beans.factory.BeanCreationException; 44 import org.springframework.beans.factory.BeanDefinitionStoreException; 45 import org.springframework.beans.factory.BeanFactory; 46 import org.springframework.beans.factory.BeanIsAbstractException; 47 import org.springframework.beans.factory.DisposableBean; 48 import org.springframework.beans.factory.DummyFactory; 49 import org.springframework.beans.factory.InitializingBean; 50 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 51 import org.springframework.beans.factory.UnsatisfiedDependencyException; 52 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 53 import org.springframework.beans.factory.support.MethodReplacer; 54 import org.springframework.beans.factory.support.RootBeanDefinition; 55 import org.springframework.core.io.ClassPathResource; 56 import org.springframework.core.io.FileSystemResource; 57 import org.springframework.core.io.UrlResource; 58 import org.springframework.util.FileCopyUtils; 59 import org.springframework.util.SerializationTestUtils; 60 import org.springframework.util.StopWatch; 61 62 68 public class XmlBeanFactoryTests extends TestCase { 69 70 public void testRefToSeparatePrototypeInstances() throws Exception { 71 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 72 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 73 reader.setValidating(false); 74 reader.loadBeanDefinitions(new ClassPathResource("reftypes.xml", getClass())); 75 assertTrue("7 beans in reftypes, not " + xbf.getBeanDefinitionCount(), xbf.getBeanDefinitionCount() == 7); 76 TestBean emma = (TestBean) xbf.getBean("emma"); 77 TestBean georgia = (TestBean) xbf.getBean("georgia"); 78 ITestBean emmasJenks = emma.getSpouse(); 79 ITestBean georgiasJenks = georgia.getSpouse(); 80 assertTrue("Emma and georgia think they have a different boyfriend", emmasJenks != georgiasJenks); 81 assertTrue("Emmas jenks has right name", emmasJenks.getName().equals("Andrew")); 82 assertTrue("Emmas doesn't equal new ref", emmasJenks != xbf.getBean("jenks")); 83 assertTrue("Georgias jenks has right name", emmasJenks.getName().equals("Andrew")); 84 assertTrue("They are object equal", emmasJenks.equals(georgiasJenks)); 85 assertTrue("They object equal direct ref", emmasJenks.equals(xbf.getBean("jenks"))); 86 } 87 88 public void testRefToSingleton() throws Exception { 89 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 90 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 91 reader.setValidating(false); 92 reader.loadBeanDefinitions(new ClassPathResource("reftypes.xml", getClass())); 93 assertTrue("7 beans in reftypes, not " + xbf.getBeanDefinitionCount(), xbf.getBeanDefinitionCount() == 7); 94 TestBean jen = (TestBean) xbf.getBean("jenny"); 95 TestBean dave = (TestBean) xbf.getBean("david"); 96 TestBean jenks = (TestBean) xbf.getBean("jenks"); 97 ITestBean davesJen = dave.getSpouse(); 98 ITestBean jenksJen = jenks.getSpouse(); 99 assertTrue("1 jen instance", davesJen == jenksJen); 100 assertTrue("1 jen instance", davesJen == jen); 101 } 102 103 public void testInnerBeans() { 104 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 105 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 106 reader.setValidating(false); 107 reader.loadBeanDefinitions(new ClassPathResource("reftypes.xml", getClass())); 108 109 TestBean hasInnerBeans = (TestBean) xbf.getBean("hasInnerBeans"); 110 assertEquals(5, hasInnerBeans.getAge()); 111 TestBean inner1 = (TestBean) hasInnerBeans.getSpouse(); 112 assertNotNull(inner1); 113 assertEquals("innerBean", inner1.getBeanName()); 114 assertEquals("inner1", inner1.getName()); 115 assertEquals(6, inner1.getAge()); 116 117 assertNotNull(hasInnerBeans.getFriends()); 118 List friends = (List ) hasInnerBeans.getFriends(); 119 assertEquals(3, friends.size()); 120 DerivedTestBean inner2 = (DerivedTestBean) friends.get(0); 121 assertEquals("inner2", inner2.getName()); 122 assertEquals(DerivedTestBean.class.getName(), inner2.getBeanName()); 123 assertFalse(xbf.containsBean("innerBean")); 124 assertNotNull(inner2); 125 assertEquals(7, inner2.getAge()); 126 TestBean innerFactory = (TestBean) friends.get(1); 127 assertEquals(DummyFactory.SINGLETON_NAME, innerFactory.getName()); 128 TestBean inner5 = (TestBean) friends.get(2); 129 assertEquals("innerBean", inner5.getBeanName()); 130 131 assertNotNull(hasInnerBeans.getSomeMap()); 132 assertEquals(2, hasInnerBeans.getSomeMap().size()); 133 TestBean inner3 = (TestBean) hasInnerBeans.getSomeMap().get("someKey"); 134 assertEquals("Jenny", inner3.getName()); 135 assertEquals(30, inner3.getAge()); 136 TestBean inner4 = (TestBean) hasInnerBeans.getSomeMap().get("someOtherKey"); 137 assertEquals("inner4", inner4.getName()); 138 assertEquals(9, inner4.getAge()); 139 140 xbf.destroySingletons(); 141 assertTrue(inner1.wasDestroyed()); 142 assertTrue(inner2.wasDestroyed()); 143 assertTrue(innerFactory.getName() == null); 144 assertTrue(inner5.wasDestroyed()); 145 } 146 147 public void testSingletonInheritanceFromParentFactorySingleton() throws Exception { 148 XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass())); 149 XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent); 150 assertEquals(TestBean.class, child.getType("inheritsFromParentFactory")); 151 TestBean inherits = (TestBean) child.getBean("inheritsFromParentFactory"); 152 assertTrue(inherits.getName().equals("override")); 154 assertTrue(inherits.getAge() == 1); 156 TestBean inherits2 = (TestBean) child.getBean("inheritsFromParentFactory"); 157 assertTrue(inherits2 == inherits); 158 } 159 160 public void testInheritanceWithDifferentClass() throws Exception { 161 XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass())); 162 XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent); 163 assertEquals(DerivedTestBean.class, child.getType("inheritsWithClass")); 164 DerivedTestBean inherits = (DerivedTestBean) child.getBean("inheritsWithDifferentClass"); 165 assertTrue(inherits.getName().equals("override")); 167 assertTrue(inherits.getAge() == 1); 169 assertTrue(inherits.wasInitialized()); 170 } 171 172 public void testInheritanceWithClass() throws Exception { 173 XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass())); 174 XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent); 175 assertEquals(DerivedTestBean.class, child.getType("inheritsWithClass")); 176 DerivedTestBean inherits = (DerivedTestBean) child.getBean("inheritsWithClass"); 177 assertTrue(inherits.getName().equals("override")); 179 assertTrue(inherits.getAge() == 1); 181 assertTrue(inherits.wasInitialized()); 182 } 183 184 public void testPrototypeInheritanceFromParentFactoryPrototype() throws Exception { 185 XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass())); 186 XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent); 187 assertEquals(TestBean.class, child.getType("prototypeInheritsFromParentFactoryPrototype")); 188 TestBean inherits = (TestBean) child.getBean("prototypeInheritsFromParentFactoryPrototype"); 189 assertTrue(inherits.getName().equals("prototype-override")); 191 assertTrue(inherits.getAge() == 2); 193 TestBean inherits2 = (TestBean) child.getBean("prototypeInheritsFromParentFactoryPrototype"); 194 assertFalse(inherits2 == inherits); 195 inherits2.setAge(13); 196 assertTrue(inherits2.getAge() == 13); 197 assertTrue(inherits.getAge() == 2); 199 } 200 201 public void testPrototypeInheritanceFromParentFactorySingleton() throws Exception { 202 XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass())); 203 XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent); 204 TestBean inherits = (TestBean) child.getBean("protoypeInheritsFromParentFactorySingleton"); 205 assertTrue(inherits.getName().equals("prototypeOverridesInheritedSingleton")); 207 assertTrue(inherits.getAge() == 1); 209 TestBean inherits2 = (TestBean) child.getBean("protoypeInheritsFromParentFactorySingleton"); 210 assertFalse(inherits2 == inherits); 211 inherits2.setAge(13); 212 assertTrue(inherits2.getAge() == 13); 213 assertTrue(inherits.getAge() == 1); 215 } 216 217 public void testAbstractParentBeans() { 218 XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass())); 219 parent.preInstantiateSingletons(); 220 assertTrue(parent.isSingleton("inheritedTestBeanWithoutClass")); 221 222 Map tbs = parent.getBeansOfType(TestBean.class); 224 assertEquals(2, tbs.size()); 225 assertTrue(tbs.containsKey("inheritedTestBeanPrototype")); 226 assertTrue(tbs.containsKey("inheritedTestBeanSingleton")); 227 228 try { 230 parent.getBean("inheritedTestBeanWithoutClass"); 231 fail("Should have thrown BeanIsAbstractException"); 232 } 233 catch (BeanIsAbstractException ex) { 234 } 236 237 assertTrue(parent.getBean("inheritedTestBeanPrototype") instanceof TestBean); 239 } 240 241 public void testDependenciesMaterializeThis() throws Exception { 242 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("dependenciesMaterializeThis.xml", getClass())); 243 DummyBoImpl bos = (DummyBoImpl) xbf.getBean("boSingleton"); 244 DummyBoImpl bop = (DummyBoImpl) xbf.getBean("boPrototype"); 245 assertNotSame(bos, bop); 246 assertTrue(bos.dao == bop.dao); 247 248 assertEquals(2, xbf.getBeansOfType(DummyBoImpl.class, true, true).size()); 249 assertEquals(1, xbf.getBeansOfType(DummyBoImpl.class, false, true).size()); 250 assertEquals(2, xbf.getBeansOfType(DummyBoImpl.class).size()); 251 assertEquals(3, xbf.getBeansOfType(DummyBo.class, true, true).size()); 252 assertEquals(2, xbf.getBeansOfType(DummyBo.class, true, false).size()); 253 assertEquals(3, xbf.getBeansOfType(DummyBo.class).size()); 254 } 255 256 public void testChildOverridesParentBean() throws Exception { 257 XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass())); 258 XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent); 259 TestBean inherits = (TestBean) child.getBean("inheritedTestBean"); 260 assertTrue(inherits.getName().equals("overrideParentBean")); 262 assertTrue(inherits.getAge() == 1); 264 TestBean inherits2 = (TestBean) child.getBean("inheritedTestBean"); 265 assertTrue(inherits2 == inherits); 266 } 267 268 272 public void testBogusParentageFromParentFactory() throws Exception { 273 XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass())); 274 XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent); 275 try { 276 TestBean inherits = (TestBean) child.getBean("bogusParent"); 277 fail(); 278 } 279 catch (NoSuchBeanDefinitionException ex) { 280 assertTrue(ex.getMessage().indexOf("bogusParent") != -1); 283 } 284 } 285 286 291 public void testSingletonInheritsFromParentFactoryPrototype() throws Exception { 292 XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass())); 293 XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent); 294 TestBean inherits = (TestBean) child.getBean("singletonInheritsFromParentFactoryPrototype"); 295 assertTrue(inherits.getName().equals("prototype-override")); 297 assertTrue(inherits.getAge() == 2); 299 TestBean inherits2 = (TestBean) child.getBean("singletonInheritsFromParentFactoryPrototype"); 300 assertTrue(inherits2 == inherits); 301 } 302 303 public void testSingletonFromParent() { 304 XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass())); 305 TestBean beanFromParent = (TestBean) parent.getBean("inheritedTestBeanSingleton"); 306 XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent); 307 TestBean beanFromChild = (TestBean) child.getBean("inheritedTestBeanSingleton"); 308 assertTrue("singleton from parent and child is the same", beanFromParent == beanFromChild); 309 } 310 311 public void testNestedPropertyValue() { 312 XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass())); 313 XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent); 314 IndexedTestBean bean = (IndexedTestBean) child.getBean("indexedTestBean"); 315 assertEquals("name applied correctly", "myname", bean.getArray()[0].getName()); 316 } 317 318 public void testCircularReferences() { 319 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 320 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 321 reader.setValidating(false); 322 reader.loadBeanDefinitions(new ClassPathResource("reftypes.xml", getClass())); 323 TestBean jenny = (TestBean) xbf.getBean("jenny"); 324 TestBean david = (TestBean) xbf.getBean("david"); 325 TestBean ego = (TestBean) xbf.getBean("ego"); 326 assertTrue("Correct circular reference", jenny.getSpouse() == david); 327 assertTrue("Correct circular reference", david.getSpouse() == jenny); 328 assertTrue("Correct circular reference", ego.getSpouse() == ego); 329 } 330 331 public void testFactoryReferenceCircle() { 332 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("factoryCircle.xml", getClass())); 333 TestBean tb = (TestBean) xbf.getBean("singletonFactory"); 334 DummyFactory db = (DummyFactory) xbf.getBean("&singletonFactory"); 335 assertTrue(tb == db.getOtherTestBean()); 336 } 337 338 public void testComplexFactoryReferenceCircle() { 339 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("complexFactoryCircle.xml", getClass())); 340 xbf.getBean("proxy1"); 341 assertEquals(5, xbf.getSingletonCount()); 343 xbf.getBean("proxy2"); 345 assertEquals(7, xbf.getSingletonCount()); 346 } 347 348 public void testInitMethodIsInvoked() throws Exception { 349 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("initializers.xml", getClass())); 350 DoubleInitializer in = (DoubleInitializer) xbf.getBean("init-method1"); 351 assertEquals(14, in.getNum()); 353 } 354 355 358 public void testInitMethodThrowsException() { 359 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("initializers.xml", getClass())); 360 try { 361 xbf.getBean("init-method2"); 362 fail(); 363 } 364 catch (BeanCreationException ex) { 365 assertTrue(ex.getResourceDescription().indexOf("initializers.xml") != -1); 366 assertEquals("init-method2", ex.getBeanName()); 367 assertTrue(ex.getCause() instanceof ServletException ); 368 } 369 } 370 371 public void testNoSuchInitMethod() throws Exception { 372 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("initializers.xml", getClass())); 373 try { 374 xbf.getBean("init-method3"); 375 fail(); 376 } 377 catch (FatalBeanException ex) { 378 assertTrue(ex.getMessage().indexOf("initializers.xml") != -1); 380 assertTrue(ex.getMessage().indexOf("init-method3") != -1); 381 assertTrue(ex.getMessage().indexOf("init") != -1); 382 } 383 } 384 385 388 public void testInitializingBeanAndInitMethod() throws Exception { 389 InitAndIB.constructed = false; 390 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("initializers.xml", getClass())); 391 assertFalse(InitAndIB.constructed); 392 xbf.preInstantiateSingletons(); 393 assertFalse(InitAndIB.constructed); 394 InitAndIB iib = (InitAndIB) xbf.getBean("init-and-ib"); 395 assertTrue(InitAndIB.constructed); 396 assertTrue(iib.afterPropertiesSetInvoked && iib.initMethodInvoked); 397 assertTrue(!iib.destroyed && !iib.customDestroyed); 398 xbf.destroySingletons(); 399 assertTrue(iib.destroyed && iib.customDestroyed); 400 xbf.destroySingletons(); 401 assertTrue(iib.destroyed && iib.customDestroyed); 402 } 403 404 407 public void testDefaultLazyInit() throws Exception { 408 InitAndIB.constructed = false; 409 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("default-lazy-init.xml", getClass())); 410 assertFalse(InitAndIB.constructed); 411 xbf.preInstantiateSingletons(); 412 assertTrue(InitAndIB.constructed); 413 try { 414 xbf.getBean("lazy-and-bad"); 415 } 416 catch (BeanCreationException ex) { 417 assertTrue(ex.getCause() instanceof ServletException ); 418 } 419 } 420 421 public void testNoSuchXmlFile() throws Exception { 422 try { 423 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("missing.xml", getClass())); 424 fail("Shouldn't create factory from missing XML"); 425 } 426 catch (BeanDefinitionStoreException ex) { 427 } 430 } 431 432 public void testInvalidXmlFile() throws Exception { 433 try { 434 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("invalid.xml", getClass())); 435 fail("Shouldn't create factory from invalid XML"); 436 } 437 catch (BeanDefinitionStoreException ex) { 438 } 441 } 442 443 public void testUnsatisfiedObjectDependencyCheck() throws Exception { 444 try { 445 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("unsatisfiedObjectDependencyCheck.xml", getClass())); 446 DependenciesBean a = (DependenciesBean) xbf.getBean("a"); 447 fail(); 448 } 449 catch (UnsatisfiedDependencyException ex) { 450 } 454 } 455 456 public void testUnsatisfiedSimpleDependencyCheck() throws Exception { 457 try { 458 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("unsatisfiedSimpleDependencyCheck.xml", getClass())); 459 DependenciesBean a = (DependenciesBean) xbf.getBean("a"); 460 fail(); 461 } 462 catch (UnsatisfiedDependencyException ex) { 463 } 467 } 468 469 public void testSatisfiedObjectDependencyCheck() throws Exception { 470 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("satisfiedObjectDependencyCheck.xml", getClass())); 471 DependenciesBean a = (DependenciesBean) xbf.getBean("a"); 472 assertNotNull(a.getSpouse()); 473 assertEquals(xbf, a.getBeanFactory()); 474 } 475 476 public void testSatisfiedSimpleDependencyCheck() throws Exception { 477 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("satisfiedSimpleDependencyCheck.xml", getClass())); 478 DependenciesBean a = (DependenciesBean) xbf.getBean("a"); 479 assertEquals(a.getAge(), 33); 480 } 481 482 public void testUnsatisfiedAllDependencyCheck() throws Exception { 483 try { 484 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("unsatisfiedAllDependencyCheckMissingObjects.xml", getClass())); 485 DependenciesBean a = (DependenciesBean) xbf.getBean("a"); 486 fail(); 487 } 488 catch (UnsatisfiedDependencyException ex) { 489 } 493 } 494 495 public void testSatisfiedAllDependencyCheck() throws Exception { 496 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("satisfiedAllDependencyCheck.xml", getClass())); 497 DependenciesBean a = (DependenciesBean) xbf.getBean("a"); 498 assertEquals(a.getAge(), 33); 499 assertNotNull(a.getName()); 500 assertNotNull(a.getSpouse()); 501 } 502 503 public void testAutowire() throws Exception { 504 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("autowire.xml", getClass())); 505 TestBean spouse = new TestBean("kerry", 0); 506 xbf.registerSingleton("spouse", spouse); 507 doTestAutowire(xbf); 508 } 509 510 public void testAutowireWithParent() throws Exception { 511 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("autowire.xml", getClass())); 512 DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); 513 MutablePropertyValues pvs = new MutablePropertyValues(); 514 pvs.addPropertyValue("name", "kerry"); 515 lbf.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class, pvs)); 516 xbf.setParentBeanFactory(lbf); 517 doTestAutowire(xbf); 518 } 519 520 private void doTestAutowire(XmlBeanFactory xbf) throws Exception { 521 DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1"); 522 TestBean kerry = (TestBean) xbf.getBean("spouse"); 523 assertEquals(kerry, rod1.getSpouse()); 525 526 DependenciesBean rod1a = (DependenciesBean) xbf.getBean("rod1a"); 527 assertEquals(kerry, rod1a.getSpouse()); 529 530 DependenciesBean rod2 = (DependenciesBean) xbf.getBean("rod2"); 531 assertEquals(kerry, rod2.getSpouse()); 533 534 ConstructorDependenciesBean rod3 = (ConstructorDependenciesBean) xbf.getBean("rod3"); 535 IndexedTestBean other = (IndexedTestBean) xbf.getBean("other"); 536 assertEquals(kerry, rod3.getSpouse1()); 538 assertEquals(kerry, rod3.getSpouse2()); 539 assertEquals(other, rod3.getOther()); 540 541 ConstructorDependenciesBean rod3a = (ConstructorDependenciesBean) xbf.getBean("rod3a"); 542 assertEquals(kerry, rod3a.getSpouse1()); 544 assertEquals(kerry, rod3a.getSpouse2()); 545 assertEquals(other, rod3a.getOther()); 546 547 try { 548 ConstructorDependenciesBean rod4 = (ConstructorDependenciesBean) xbf.getBean("rod4"); 549 fail("Should not have thrown FatalBeanException"); 550 } 551 catch (FatalBeanException ex) { 552 } 554 555 DependenciesBean rod5 = (DependenciesBean) xbf.getBean("rod5"); 556 assertNull(rod5.getSpouse()); 558 559 BeanFactory appCtx = (BeanFactory) xbf.getBean("childAppCtx"); 560 assertTrue(appCtx.containsBean("rod1")); 561 assertTrue(appCtx.containsBean("jenny")); 562 } 563 564 public void testAutowireWithDefault() throws Exception { 565 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("default-autowire.xml", getClass())); 566 567 DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1"); 568 assertNotNull(rod1.getSpouse()); 570 assertTrue(rod1.getSpouse().getName().equals("Kerry")); 571 572 DependenciesBean rod2 = (DependenciesBean) xbf.getBean("rod2"); 573 assertNotNull(rod2.getSpouse()); 575 assertTrue(rod2.getSpouse().getName().equals("Kerry")); 576 577 try { 578 DependenciesBean rod3 = (DependenciesBean) xbf.getBean("rod3"); 579 fail("Should have thrown UnsatisfiedDependencyException"); 580 } 581 catch (UnsatisfiedDependencyException ex) { 582 } 584 } 585 586 public void testAutowireByConstructor() throws Exception { 587 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass())); 588 ConstructorDependenciesBean rod1 = (ConstructorDependenciesBean) xbf.getBean("rod1"); 589 TestBean kerry = (TestBean) xbf.getBean("kerry2"); 590 assertEquals(kerry, rod1.getSpouse1()); 592 assertEquals(0, rod1.getAge()); 593 assertEquals(null, rod1.getName()); 594 595 ConstructorDependenciesBean rod2 = (ConstructorDependenciesBean) xbf.getBean("rod2"); 596 TestBean kerry1 = (TestBean) xbf.getBean("kerry1"); 597 TestBean kerry2 = (TestBean) xbf.getBean("kerry2"); 598 assertEquals(kerry2, rod2.getSpouse1()); 600 assertEquals(kerry1, rod2.getSpouse2()); 601 assertEquals(0, rod2.getAge()); 602 assertEquals(null, rod2.getName()); 603 604 ConstructorDependenciesBean rod = (ConstructorDependenciesBean) xbf.getBean("rod3"); 605 IndexedTestBean other = (IndexedTestBean) xbf.getBean("other"); 606 assertEquals(kerry, rod.getSpouse1()); 608 assertEquals(kerry, rod.getSpouse2()); 609 assertEquals(other, rod.getOther()); 610 assertEquals(0, rod.getAge()); 611 assertEquals(null, rod.getName()); 612 613 ConstructorDependenciesBean rod4 = (ConstructorDependenciesBean) xbf.getBean("rod4"); 614 assertEquals(kerry, rod.getSpouse1()); 616 assertEquals(kerry, rod.getSpouse2()); 617 assertEquals(other, rod.getOther()); 618 assertEquals(0, rod.getAge()); 619 assertEquals(null, rod.getName()); 620 } 621 622 public void testAutowireByConstructorWithSimpleValues() throws Exception { 623 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass())); 624 625 ConstructorDependenciesBean rod5 = (ConstructorDependenciesBean) xbf.getBean("rod5"); 626 TestBean kerry1 = (TestBean) xbf.getBean("kerry1"); 627 TestBean kerry2 = (TestBean) xbf.getBean("kerry2"); 628 IndexedTestBean other = (IndexedTestBean) xbf.getBean("other"); 629 assertEquals(kerry2, rod5.getSpouse1()); 631 assertEquals(kerry1, rod5.getSpouse2()); 632 assertEquals(other, rod5.getOther()); 633 assertEquals(99, rod5.getAge()); 634 assertEquals("myname", rod5.getName()); 635 636 DerivedConstructorDependenciesBean rod6 = (DerivedConstructorDependenciesBean) xbf.getBean("rod6"); 637 assertTrue(rod6.initialized); 639 assertTrue(!rod6.destroyed); 640 assertEquals(kerry2, rod6.getSpouse1()); 641 assertEquals(kerry1, rod6.getSpouse2()); 642 assertEquals(other, rod6.getOther()); 643 assertEquals(0, rod6.getAge()); 644 assertEquals(null, rod6.getName()); 645 646 xbf.destroySingletons(); 647 assertTrue(rod6.destroyed); 648 } 649 650 public void testConstructorArgResolution() { 651 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass())); 652 TestBean kerry1 = (TestBean) xbf.getBean("kerry1"); 653 TestBean kerry2 = (TestBean) xbf.getBean("kerry2"); 654 655 658 ConstructorDependenciesBean rod10 = (ConstructorDependenciesBean) xbf.getBean("rod10"); 659 assertEquals(null, rod10.getName()); 660 661 ConstructorDependenciesBean rod11 = (ConstructorDependenciesBean) xbf.getBean("rod11"); 662 assertEquals(kerry2, rod11.getSpouse1()); 663 664 ConstructorDependenciesBean rod12 = (ConstructorDependenciesBean) xbf.getBean("rod12"); 665 assertEquals(kerry1, rod12.getSpouse1()); 666 assertNull(rod12.getSpouse2()); 667 668 ConstructorDependenciesBean rod13 = (ConstructorDependenciesBean) xbf.getBean("rod13"); 669 assertEquals(kerry1, rod13.getSpouse1()); 670 assertEquals(kerry2, rod13.getSpouse2()); 671 672 ConstructorDependenciesBean rod14 = (ConstructorDependenciesBean) xbf.getBean("rod14"); 673 assertEquals(kerry1, rod14.getSpouse1()); 674 assertEquals(kerry2, rod14.getSpouse2()); 675 676 ConstructorDependenciesBean rod15 = (ConstructorDependenciesBean) xbf.getBean("rod15"); 677 assertEquals(kerry2, rod15.getSpouse1()); 678 assertEquals(kerry1, rod15.getSpouse2()); 679 680 ConstructorDependenciesBean rod16 = (ConstructorDependenciesBean) xbf.getBean("rod16"); 681 assertEquals(kerry2, rod16.getSpouse1()); 682 assertEquals(kerry1, rod16.getSpouse2()); 683 assertEquals(29, rod16.getAge()); 684 } 685 686 public void testConstructorArgWithSingleMatch() { 687 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass())); 688 File file = (File ) xbf.getBean("file"); 689 assertEquals(File.separator + "test", file.getPath()); 690 } 691 692 public void testThrowsExceptionOnTooManyArguments() throws Exception { 693 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass())); 694 try { 695 ConstructorDependenciesBean rod = (ConstructorDependenciesBean) xbf.getBean("rod7"); 696 fail("Should have thrown BeanDefinitionStoreException"); 697 } 698 catch (BeanCreationException ex) { 699 } 701 } 702 703 public void testThrowsExceptionOnAmbiguousResolution() throws Exception { 704 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass())); 705 try { 706 ConstructorDependenciesBean rod = (ConstructorDependenciesBean) xbf.getBean("rod8"); 707 fail("Should have thrown UnsatisfiedDependencyException"); 708 } 709 catch (UnsatisfiedDependencyException ex) { 710 } 712 } 713 714 public void testFactoryBeanDefinedAsPrototype() { 715 try { 716 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("invalid-factory.xml", getClass())); 717 fail("Should have thrown BeanDefinitionStoreException"); 718 } 719 catch (BeanDefinitionStoreException ex) { 720 } 722 } 723 724 public void testDependsOn() { 725 doTestDependencies("dependencies-dependsOn.xml"); 726 } 727 728 public void testDependsOnInInnerBean() { 729 doTestDependencies("dependencies-dependsOn-inner.xml"); 730 } 731 732 public void testDependenciesThroughConstructorArguments() { 733 doTestDependencies("dependencies-carg.xml"); 734 } 735 736 public void testDependenciesThroughConstructorArgumentAutowiring() { 737 doTestDependencies("dependencies-carg-autowire.xml"); 738 } 739 740 public void testDependenciesThroughConstructorArgumentsInInnerBean() { 741 doTestDependencies("dependencies-carg-inner.xml"); 742 } 743 744 public void testDependenciesThroughProperties() { 745 doTestDependencies("dependencies-prop.xml"); 746 } 747 748 public void testDependenciesThroughPropertiesWithInTheMiddle() { 749 doTestDependencies("dependencies-prop-inTheMiddle.xml"); 750 } 751 752 public void testDependenciesThroughPropertyAutowiringByName() { 753 doTestDependencies("dependencies-prop-autowireByName.xml"); 754 } 755 756 public void testDependenciesThroughPropertyAutowiringByType() { 757 doTestDependencies("dependencies-prop-autowireByType.xml"); 758 } 759 760 public void testDependenciesThroughPropertiesInInnerBean() { 761 doTestDependencies("dependencies-prop-inner.xml"); 762 } 763 764 private void doTestDependencies(String filename) { 765 PreparingBean1.prepared = false; 766 PreparingBean1.destroyed = false; 767 PreparingBean2.prepared = false; 768 PreparingBean2.destroyed = false; 769 DependingBean.destroyed = false; 770 HoldingBean.destroyed = false; 771 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource(filename, getClass())); 772 xbf.preInstantiateSingletons(); 773 xbf.destroySingletons(); 774 assertTrue(PreparingBean1.prepared); 775 assertTrue(PreparingBean1.destroyed); 776 assertTrue(PreparingBean2.prepared); 777 assertTrue(PreparingBean2.destroyed); 778 assertTrue(DependingBean.destroyed); 779 if (xbf.containsBean("holdingBean")) { 780 assertTrue(HoldingBean.destroyed); 781 } 782 } 783 784 public void testClassNotFoundWithDefault() { 785 try { 786 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("classNotFound.xml", getClass())); 787 } 789 catch (BeanDefinitionStoreException ex) { 790 assertTrue(ex.getResourceDescription().indexOf("classNotFound.xml") != -1); 791 assertEquals("classNotFound", ex.getBeanName()); 792 assertTrue(ex.getCause() instanceof ClassNotFoundException ); 793 } 795 } 796 797 public void testClassNotFoundWithNoBeanClassLoader() { 798 try { 799 DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); 800 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf); 801 reader.setBeanClassLoader(null); 802 reader.loadBeanDefinitions(new ClassPathResource("classNotFound.xml", getClass())); 803 assertTrue(bf.getBeanDefinition("classNotFound") instanceof RootBeanDefinition); 804 assertEquals(((RootBeanDefinition) bf.getBeanDefinition("classNotFound")).getBeanClassName(), 805 "org.springframework.beans.TestBeana"); 806 } 807 catch (BeanDefinitionStoreException ex) { 808 fail("Should not have thrown BeanDefinitionStoreException"); 809 } 810 } 811 812 public void testResourceAndInputStream() throws IOException { 813 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("resource.xml", getClass())); 814 ResourceTestBean resource1 = (ResourceTestBean) xbf.getBean("resource1"); 816 ResourceTestBean resource2 = (ResourceTestBean) xbf.getBean("resource2"); 818 819 assertTrue(resource1.getResource() instanceof ClassPathResource); 820 StringWriter writer = new StringWriter (); 821 FileCopyUtils.copy(new InputStreamReader (resource1.getResource().getInputStream()), writer); 822 assertEquals("test", writer.toString()); 823 writer = new StringWriter (); 824 FileCopyUtils.copy(new InputStreamReader (resource1.getInputStream()), writer); 825 assertEquals("test", writer.toString()); 826 writer = new StringWriter (); 827 FileCopyUtils.copy(new InputStreamReader (resource2.getResource().getInputStream()), writer); 828 assertEquals("test", writer.toString()); 829 writer = new StringWriter (); 830 FileCopyUtils.copy(new InputStreamReader (resource2.getInputStream()), writer); 831 assertEquals("test", writer.toString()); 832 } 833 834 public void testClassPathResourceWithImport() { 835 XmlBeanFactory xbf = new XmlBeanFactory( 836 new ClassPathResource("org/springframework/beans/factory/xml/resource.xml")); 837 ResourceTestBean resource1 = (ResourceTestBean) xbf.getBean("resource1"); 839 ResourceTestBean resource2 = (ResourceTestBean) xbf.getBean("resource2"); 841 } 842 843 public void testUrlResourceWithImport() { 844 URL url = getClass().getResource("resource.xml"); 845 XmlBeanFactory xbf = new XmlBeanFactory(new UrlResource(url)); 846 ResourceTestBean resource1 = (ResourceTestBean) xbf.getBean("resource1"); 848 ResourceTestBean resource2 = (ResourceTestBean) xbf.getBean("resource2"); 850 } 851 852 public void testFileSystemResourceWithImport() { 853 String file = getClass().getResource("resource.xml").getFile(); 854 XmlBeanFactory xbf = new XmlBeanFactory(new FileSystemResource(file)); 855 ResourceTestBean resource1 = (ResourceTestBean) xbf.getBean("resource1"); 857 ResourceTestBean resource2 = (ResourceTestBean) xbf.getBean("resource2"); 859 } 860 861 public void testLookupOverrideMethodsWithSetterInjection() { 862 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 863 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 864 reader.setValidating(true); 865 reader.loadBeanDefinitions(new ClassPathResource("overrides.xml", getClass())); 866 867 testLookupOverrideMethodsWithSetterInjection(xbf, "overrideOneMethod", true); 868 testLookupOverrideMethodsWithSetterInjection(xbf, "overrideInheritedMethod", true); 871 872 int howmany = 100; 875 StopWatch sw = new StopWatch(); 876 sw.start("Look up " + howmany + " prototype bean instances with method overrides"); 877 for (int i = 0; i < howmany; i++) { 878 testLookupOverrideMethodsWithSetterInjection(xbf, "overrideOnPrototype", false); 879 } 880 sw.stop(); 881 System.out.println(sw); 882 assertTrue(sw.getTotalTimeMillis() < 1000); 883 884 OverrideOneMethod swappedOom = (OverrideOneMethod) xbf.getBean("overrideOneMethodSwappedReturnValues"); 886 887 TestBean tb = swappedOom.getPrototypeDependency(); 888 assertEquals("David", tb.getName()); 889 tb = swappedOom.protectedOverrideSingleton(); 890 assertEquals("Jenny", tb.getName()); 891 } 892 893 private void testLookupOverrideMethodsWithSetterInjection(BeanFactory xbf, String beanName, boolean singleton) { 894 OverrideOneMethod oom = (OverrideOneMethod) xbf.getBean(beanName); 895 896 if (singleton) { 897 assertSame(oom, xbf.getBean(beanName)); 898 } 899 else { 900 assertNotSame(oom, xbf.getBean(beanName)); 901 } 902 903 TestBean jenny1 = oom.getPrototypeDependency(); 904 assertEquals("Jenny", jenny1.getName()); 905 TestBean jenny2 = oom.getPrototypeDependency(); 906 assertEquals("Jenny", jenny2.getName()); 907 assertNotSame(jenny1, jenny2); 908 909 TestBean jenny3 = oom.invokesOverridenMethodOnSelf(); 914 assertEquals("Jenny", jenny3.getName()); 915 assertNotSame(jenny1, jenny3); 916 917 TestBean dave1 = oom.protectedOverrideSingleton(); 919 assertEquals("David", dave1.getName()); 920 TestBean dave2 = oom.protectedOverrideSingleton(); 921 assertEquals("David", dave2.getName()); 922 assertSame(dave1, dave2); 923 } 924 925 public void testReplaceMethodOverrideWithSetterInjection() { 926 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 927 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 928 reader.setValidating(true); 929 reader.loadBeanDefinitions(new ClassPathResource("delegationOverrides.xml", getClass())); 930 931 OverrideOneMethod oom = (OverrideOneMethod) xbf.getBean("overrideOneMethod"); 932 933 TestBean jenny1 = oom.getPrototypeDependency(); 935 assertEquals("Jenny", jenny1.getName()); 936 TestBean jenny2 = oom.getPrototypeDependency(); 937 assertEquals("Jenny", jenny2.getName()); 938 assertNotSame(jenny1, jenny2); 939 940 TestBean dave1 = oom.protectedOverrideSingleton(); 942 assertEquals("David", dave1.getName()); 943 TestBean dave2 = oom.protectedOverrideSingleton(); 944 assertEquals("David", dave2.getName()); 945 assertSame(dave1, dave2); 946 947 String str = "woierowijeiowiej"; 949 assertEquals(str, oom.echo(str)); 950 951 String s = "this is not a palindrome"; 953 String reverse = new StringBuffer (s).reverse().toString(); 954 assertEquals("Should have overridden to reverse, not echo", reverse, oom.replaceMe(s)); 955 956 assertEquals("Should have overridden no-arg overloaded replaceMe method to return fixed value", 957 FixedMethodReplacer.VALUE, oom.replaceMe()); 958 959 OverrideOneMethodSubclass ooms = (OverrideOneMethodSubclass) xbf.getBean("replaceVoidMethod"); 960 DoSomethingReplacer dos = (DoSomethingReplacer) xbf.getBean("doSomethingReplacer"); 961 assertEquals(null, dos.lastArg); 962 String s1 = ""; 963 String s2 = "foo bar black sheep"; 964 ooms.doSomething(s1); 965 assertEquals(s1, dos.lastArg); 966 ooms.doSomething(s2); 967 assertEquals(s2, dos.lastArg); 968 } 969 970 public void testLookupOverrideOneMethodWithConstructorInjection() { 971 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 972 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 973 reader.setValidating(true); 974 reader.loadBeanDefinitions(new ClassPathResource("constructorOverrides.xml", getClass())); 975 976 ConstructorInjectedOverrides cio = (ConstructorInjectedOverrides) xbf.getBean("constructorOverrides"); 977 978 assertEquals("Setter string was set", "from property element", cio.getSetterString()); 982 983 TestBean jenny = (TestBean) xbf.getBean("jenny"); 985 assertSame(jenny, cio.getTestBean()); 986 assertSame(jenny, cio.getTestBean()); 987 FactoryMethods fm1 = cio.createFactoryMethods(); 988 FactoryMethods fm2 = cio.createFactoryMethods(); 989 assertNotSame("FactoryMethods reference is to a prototype", fm1, fm2); 990 assertSame("The two prototypes hold the same singleton reference", 991 fm1.getTestBean(), fm2.getTestBean()); 992 } 993 994 public void testRejectsOverrideOfBogusMethodName() { 995 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 996 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 997 reader.setValidating(true); 998 try { 999 reader.loadBeanDefinitions(new ClassPathResource("invalidOverridesNoSuchMethod.xml", getClass())); 1000 fail("Shouldn't allow override of bogus method"); 1001 } 1002 catch (BeanDefinitionStoreException ex) { 1003 assertTrue("Bogus method name correctly reported", ex.getMessage().indexOf("bogusMethod") != -1); 1005 } 1006 } 1007 1008 1011 public void testSerializabilityOfMethodReplacer() throws Exception { 1012 try { 1013 BUGtestSerializableMethodReplacerAndSuperclass(); 1014 fail(); 1015 } 1016 catch (AssertionFailedError ex) { 1017 System.err.println("****** SPR-356: Objects with MethodReplace overrides are not serializable"); 1018 } 1019 } 1020 1021 public void BUGtestSerializableMethodReplacerAndSuperclass() throws IOException , ClassNotFoundException { 1022 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 1023 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 1024 reader.setValidating(true); 1025 reader.loadBeanDefinitions(new ClassPathResource("delegationOverrides.xml", getClass())); 1026 SerializableMethodReplacerCandidate s = (SerializableMethodReplacerCandidate) xbf.getBean("serializableReplacer"); 1027 String forwards = "this is forwards"; 1028 String backwards = new StringBuffer (forwards).reverse().toString(); 1029 assertEquals(backwards, s.replaceMe(forwards)); 1030 assertTrue(SerializationTestUtils.isSerializable(s)); 1031 s = (SerializableMethodReplacerCandidate) SerializationTestUtils.serializeAndDeserialize(s); 1032 assertEquals("Method replace still works after serialization and deserialization", backwards, s.replaceMe(forwards)); 1033 } 1034 1035 public void testFactoryMethodsSingletonOnTargetClass() { 1036 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 1037 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 1038 reader.setValidating(true); 1039 reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); 1040 1041 TestBean tb = (TestBean) xbf.getBean("defaultTestBean"); 1042 assertEquals("defaultInstance", tb.getName()); 1043 assertEquals(1, tb.getAge()); 1044 1045 FactoryMethods fm = (FactoryMethods) xbf.getBean("default"); 1046 assertEquals(0, fm.getNum()); 1047 assertEquals("default", fm.getName()); 1048 assertEquals("defaultInstance", fm.getTestBean().getName()); 1049 assertEquals("setterString", fm.getStringValue()); 1050 1051 fm = (FactoryMethods) xbf.getBean("testBeanOnly"); 1052 assertEquals(0, fm.getNum()); 1053 assertEquals("default", fm.getName()); 1054 assertEquals("Juergen", fm.getTestBean().getName()); 1056 1057 fm = (FactoryMethods) xbf.getBean("full"); 1058 assertEquals(27, fm.getNum()); 1059 assertEquals("gotcha", fm.getName()); 1060 assertEquals("Juergen", fm.getTestBean().getName()); 1061 1062 FactoryMethods fm2 = (FactoryMethods) xbf.getBean("full"); 1063 assertSame(fm, fm2); 1064 1065 xbf.destroySingletons(); 1066 assertTrue(tb.wasDestroyed()); 1067 } 1068 1069 public void testFactoryMethodsWithNull() { 1070 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 1071 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 1072 reader.setValidating(true); 1073 reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); 1074 1075 FactoryMethods fm = (FactoryMethods) xbf.getBean("fullWithNull"); 1076 assertEquals(27, fm.getNum()); 1077 assertEquals(null, fm.getName()); 1078 assertEquals("Juergen", fm.getTestBean().getName()); 1079 1080 fm = (FactoryMethods) xbf.getBean("fullWithGenericNull"); 1081 assertEquals(27, fm.getNum()); 1082 assertEquals(null, fm.getName()); 1083 assertEquals("Juergen", fm.getTestBean().getName()); 1084 } 1085 1086 public void testFactoryMethodsWithAutowire() { 1087 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 1088 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 1089 reader.setValidating(true); 1090 reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); 1091 1092 FactoryMethods fm = (FactoryMethods) xbf.getBean("fullWithAutowire"); 1093 assertEquals(27, fm.getNum()); 1094 assertEquals("gotchaAutowired", fm.getName()); 1095 assertEquals("Juergen", fm.getTestBean().getName()); 1096 } 1097 1098 public void testFactoryMethodsPrototypeOnTargetClass() { 1099 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 1100 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 1101 reader.setValidating(true); 1102 reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); 1103 FactoryMethods fm = (FactoryMethods) xbf.getBean("defaultPrototype"); 1104 FactoryMethods fm2 = (FactoryMethods) xbf.getBean("defaultPrototype"); 1105 assertEquals(0, fm.getNum()); 1106 assertEquals("default", fm.getName()); 1107 assertEquals("defaultInstance", fm.getTestBean().getName()); 1108 assertEquals("setterString", fm.getStringValue()); 1109 assertEquals(fm.getNum(), fm2.getNum()); 1110 assertEquals(fm.getStringValue(), fm2.getStringValue()); 1111 assertNotSame(fm.getTestBean(), fm2.getTestBean()); 1113 assertNotSame(fm, fm2); 1114 1115 fm = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype"); 1116 fm2 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype"); 1117 assertEquals(0, fm.getNum()); 1118 assertEquals("default", fm.getName()); 1119 assertEquals("Juergen", fm.getTestBean().getName()); 1121 assertEquals(fm.getNum(), fm2.getNum()); 1122 assertEquals(fm.getStringValue(), fm2.getStringValue()); 1123 assertSame(fm.getTestBean(), fm2.getTestBean()); 1125 assertNotSame(fm, fm2); 1126 1127 fm = (FactoryMethods) xbf.getBean("fullPrototype"); 1128 fm2 = (FactoryMethods) xbf.getBean("fullPrototype"); 1129 assertEquals(27, fm.getNum()); 1130 assertEquals("gotcha", fm.getName()); 1131 assertEquals("Juergen", fm.getTestBean().getName()); 1132 assertEquals(fm.getNum(), fm2.getNum()); 1133 assertEquals(fm.getStringValue(), fm2.getStringValue()); 1134 assertSame(fm.getTestBean(), fm2.getTestBean()); 1136 assertNotSame(fm, fm2); 1137 } 1138 1139 1142 public void testFactoryMethodsOnExternalClass() { 1143 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 1144 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 1145 reader.setValidating(true); 1146 reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); 1147 1148 assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithoutArgs")); 1149 assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithArgs")); 1150 String [] names = xbf.getBeanNamesForType(TestBean.class); 1151 assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithoutArgs")); 1152 assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithArgs")); 1153 1154 TestBean tb = (TestBean) xbf.getBean("externalFactoryMethodWithoutArgs"); 1155 assertEquals(2, tb.getAge()); 1156 assertEquals("Tristan", tb.getName()); 1157 tb = (TestBean) xbf.getBean("externalFactoryMethodWithArgs"); 1158 assertEquals(33, tb.getAge()); 1159 assertEquals("Rod", tb.getName()); 1160 1161 assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithoutArgs")); 1162 assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithArgs")); 1163 names = xbf.getBeanNamesForType(TestBean.class); 1164 assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithoutArgs")); 1165 assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithArgs")); 1166 } 1167 1168 public void testInstanceFactoryMethodWithoutArgs() { 1169 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 1170 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 1171 reader.setValidating(true); 1172 reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); 1173 1174 InstanceFactory.count = 0; 1175 xbf.preInstantiateSingletons(); 1176 assertEquals(1, InstanceFactory.count); 1177 FactoryMethods fm = (FactoryMethods) xbf.getBean("instanceFactoryMethodWithoutArgs"); 1178 assertEquals("instanceFactory", fm.getTestBean().getName()); 1179 assertEquals(1, InstanceFactory.count); 1180 FactoryMethods fm2 = (FactoryMethods) xbf.getBean("instanceFactoryMethodWithoutArgs"); 1181 assertEquals("instanceFactory", fm2.getTestBean().getName()); 1182 assertSame(fm2, fm); 1183 assertEquals(1, InstanceFactory.count); 1184 } 1185 1186 public void testFactoryMethodNoMatchingStaticMethod() { 1187 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 1188 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 1189 reader.setValidating(true); 1190 reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); 1191 try { 1192 xbf.getBean("noMatchPrototype"); 1193 fail("No static method matched"); 1194 } 1195 catch (BeanCreationException ex) { 1196 } 1198 } 1199 1200 public void testCanSpecifyFactoryMethodArgumentsOnFactoryMethodPrototype() { 1201 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 1202 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 1203 reader.setValidating(true); 1204 reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); 1205 TestBean tbArg = new TestBean(); 1206 tbArg.setName("arg1"); 1207 TestBean tbArg2 = new TestBean(); 1208 tbArg2.setName("arg2"); 1209 FactoryMethods fm1 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", new Object []{tbArg}); 1210 FactoryMethods fm2 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", new Object []{tbArg2}); 1211 1212 assertEquals(0, fm1.getNum()); 1213 assertEquals("default", fm1.getName()); 1214 assertEquals("arg1", fm1.getTestBean().getName()); 1216 assertEquals("arg2", fm2.getTestBean().getName()); 1217 assertEquals(fm1.getNum(), fm2.getNum()); 1218 assertEquals(fm2.getStringValue(), "testBeanOnlyPrototypeDISetterString"); 1219 assertEquals(fm2.getStringValue(), fm2.getStringValue()); 1220 assertSame(fm2.getTestBean(), fm2.getTestBean()); 1222 assertNotSame(fm1, fm2); 1223 } 1224 1225 public void testCannotSpecifyFactoryMethodArgumentsOnSingleton() { 1226 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 1227 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 1228 reader.setValidating(true); 1229 reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); 1230 try { 1231 xbf.getBean("testBeanOnly", new Object []{new TestBean()}); 1232 fail("Shouldn't allow args to be passed to a singleton"); 1233 } 1234 catch (BeanDefinitionStoreException ex) { 1235 } 1237 } 1238 1239 public void testFactoryMethodWithDifferentReturnType() { 1240 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 1241 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 1242 reader.setValidating(true); 1243 reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); 1244 1245 assertTrue(List .class.isAssignableFrom(xbf.getType("listInstance"))); 1247 String [] names = xbf.getBeanNamesForType(FactoryMethods.class); 1248 assertTrue(!Arrays.asList(names).contains("listInstance")); 1249 names = xbf.getBeanNamesForType(List .class); 1250 assertTrue(Arrays.asList(names).contains("listInstance")); 1251 1252 xbf.preInstantiateSingletons(); 1253 assertTrue(List .class.isAssignableFrom(xbf.getType("listInstance"))); 1254 names = xbf.getBeanNamesForType(FactoryMethods.class); 1255 assertTrue(!Arrays.asList(names).contains("listInstance")); 1256 names = xbf.getBeanNamesForType(List .class); 1257 assertTrue(Arrays.asList(names).contains("listInstance")); 1258 List list = (List ) xbf.getBean("listInstance"); 1259 assertEquals(Collections.EMPTY_LIST, list); 1260 } 1261 1262 public void testFactoryMethodForJavaMailSession() { 1263 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 1264 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 1265 reader.setValidating(true); 1266 reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); 1267 1268 Session session = (Session ) xbf.getBean("javaMailSession"); 1269 assertEquals("someuser", session.getProperty("mail.smtp.user")); 1270 assertEquals("somepw", session.getProperty("mail.smtp.password")); 1271 } 1272 1273 public void testCannotSpecifyFactoryMethodArgumentsExceptWithFactoryMethod() { 1274 DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); 1275 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); 1276 reader.setValidating(true); 1277 reader.loadBeanDefinitions(new ClassPathResource("overrides.xml", getClass())); 1278 try { 1279 xbf.getBean("overrideOnPrototype", new Object []{new TestBean()}); 1280 fail("Shouldn't allow args to be passed to a Setter-Injected object"); 1281 } 1282 catch (BeanDefinitionStoreException ex) { 1283 } 1285 } 1286 1287 public void testConstructorArgWithSingleSimpleTypeMatch() { 1288 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass())); 1289 1290 SingleSimpleTypeConstructorBean bean = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBoolean"); 1291 assertTrue(bean.isSingleBoolean()); 1292 1293 SingleSimpleTypeConstructorBean bean2 = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBoolean2"); 1294 assertTrue(bean2.isSingleBoolean()); 1295 } 1296 1297 public void testConstructorArgWithDoubleSimpleTypeMatch() { 1298 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass())); 1299 1300 SingleSimpleTypeConstructorBean bean = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBooleanAndString"); 1301 assertTrue(bean.isSecondBoolean()); 1302 assertEquals("A String", bean.getTestString()); 1303 1304 SingleSimpleTypeConstructorBean bean2 = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBooleanAndString2"); 1305 assertTrue(bean2.isSecondBoolean()); 1306 assertEquals("A String", bean2.getTestString()); 1307 } 1308 1309 public void testDoubleBooleanAutowire() { 1310 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass())); 1311 DoubleBooleanConstructorBean bean = (DoubleBooleanConstructorBean) xbf.getBean("beanWithDoubleBoolean"); 1312 assertEquals(Boolean.TRUE, bean.boolean1); 1313 assertEquals(Boolean.FALSE, bean.boolean2); 1314 } 1315 1316 public void testDoubleBooleanAutowireWithIndex() { 1317 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass())); 1318 DoubleBooleanConstructorBean bean = (DoubleBooleanConstructorBean) xbf.getBean("beanWithDoubleBooleanAndIndex"); 1319 assertEquals(Boolean.FALSE, bean.boolean1); 1320 assertEquals(Boolean.TRUE, bean.boolean2); 1321 } 1322 1323 1324 public static class DoSomethingReplacer implements MethodReplacer { 1325 1326 public Object lastArg; 1327 1328 public Object reimplement(Object obj, Method method, Object [] args) throws Throwable { 1329 assertEquals(1, args.length); 1330 assertEquals("doSomething", method.getName()); 1331 lastArg = args[0]; 1332 return null; 1333 } 1334 } 1335 1336 1337 public static class BadInitializer { 1338 1339 1340 public void init2() throws ServletException { 1341 throw new ServletException (); 1342 } 1343 } 1344 1345 1346 public static class DoubleInitializer { 1347 1348 private int num; 1349 1350 public int getNum() { 1351 return num; 1352 } 1353 1354 public void setNum(int i) { 1355 num = i; 1356 } 1357 1358 1359 public void init() { 1360 this.num *= 2; 1361 } 1362 } 1363 1364 1365 public static class InitAndIB implements InitializingBean, DisposableBean { 1366 1367 public static boolean constructed; 1368 1369 public boolean afterPropertiesSetInvoked, initMethodInvoked, destroyed, customDestroyed; 1370 1371 public InitAndIB() { 1372 constructed = true; 1373 } 1374 1375 public void afterPropertiesSet() { 1376 if (this.initMethodInvoked) { 1377 fail(); 1378 } 1379 this.afterPropertiesSetInvoked = true; 1380 } 1381 1382 1383 public void customInit() throws ServletException { 1384 if (!this.afterPropertiesSetInvoked) { 1385 fail(); 1386 } 1387 this.initMethodInvoked = true; 1388 } 1389 1390 public void destroy() { 1391 if (this.customDestroyed) { 1392 fail(); 1393 } 1394 if (this.destroyed) { 1395 throw new IllegalStateException ("Already destroyed"); 1396 } 1397 this.destroyed = true; 1398 } 1399 1400 public void customDestroy() { 1401 if (!this.destroyed) { 1402 fail(); 1403 } 1404 if (this.customDestroyed) { 1405 throw new IllegalStateException ("Already customDestroyed"); 1406 } 1407 this.customDestroyed = true; 1408 } 1409 } 1410 1411 1412 public static class PreparingBean1 implements DisposableBean { 1413 1414 public static boolean prepared = false; 1415 1416 public static boolean destroyed = false; 1417 1418 public PreparingBean1() { 1419 prepared = true; 1420 } 1421 1422 public void destroy() { 1423 destroyed = true; 1424 } 1425 } 1426 1427 1428 public static class PreparingBean2 implements DisposableBean { 1429 1430 public static boolean prepared = false; 1431 1432 public static boolean destroyed = false; 1433 1434 public PreparingBean2() { 1435 prepared = true; 1436 } 1437 1438 public void destroy() { 1439 destroyed = true; 1440 } 1441 } 1442 1443 1444 public static class DependingBean implements InitializingBean, DisposableBean { 1445 1446 public static boolean destroyed = false; 1447 1448 public DependingBean() { 1449 } 1450 1451 public DependingBean(PreparingBean1 bean1, PreparingBean2 bean2) { 1452 } 1453 1454 public void setBean1(PreparingBean1 bean1) { 1455 } 1456 1457 public void setBean2(PreparingBean2 bean2) { 1458 } 1459 1460 public void setInTheMiddleBean(InTheMiddleBean bean) { 1461 } 1462 1463 public void afterPropertiesSet() { 1464 if (!(PreparingBean1.prepared && PreparingBean2.prepared)) { 1465 throw new IllegalStateException ("Need prepared PreparedBeans!"); 1466 } 1467 } 1468 1469 public void destroy() { 1470 if (PreparingBean1.destroyed || PreparingBean2.destroyed) { 1471 throw new IllegalStateException ("Should not be destroyed before PreparedBeans"); 1472 } 1473 destroyed = true; 1474 } 1475 } 1476 1477 1478 public static class InTheMiddleBean { 1479 1480 public void setBean1(PreparingBean1 bean1) { 1481 } 1482 1483 public void setBean2(PreparingBean2 bean2) { 1484 } 1485 } 1486 1487 1488 public static class HoldingBean implements DisposableBean { 1489 1490 public static boolean destroyed = false; 1491 1492 public void setDependingBean(DependingBean dependingBean) { 1493 } 1494 1495 public void destroy() { 1496 if (DependingBean.destroyed) { 1497 throw new IllegalStateException ("Should not be destroyed before DependingBean"); 1498 } 1499 destroyed = true; 1500 } 1501 } 1502 1503 1504 public static class DoubleBooleanConstructorBean { 1505 1506 private Boolean boolean1; 1507 private Boolean boolean2; 1508 1509 public DoubleBooleanConstructorBean(Boolean b1, Boolean b2) { 1510 this.boolean1 = b1; 1511 this.boolean2 = b2; 1512 } 1513 } 1514 1515} 1516 | Popular Tags |