コード例 #1
0
 public void testRefSubelement() throws Exception {
   InputStream is = getClass().getResourceAsStream("collections.xml");
   XmlBeanFactory xbf = new XmlBeanFactory(is);
   // assertTrue("5 beans in reftypes, not " + xbf.getBeanDefinitionCount(),
   // xbf.getBeanDefinitionCount() == 5);
   TestBean jen = (TestBean) xbf.getBean("jenny");
   TestBean dave = (TestBean) xbf.getBean("david");
   assertTrue(jen.getSpouse() == dave);
 }
コード例 #2
0
 public void testRefSubelementsBuildCollectionFromSingleElement() throws Exception {
   InputStream is = getClass().getResourceAsStream("collections.xml");
   XmlBeanFactory xbf = new XmlBeanFactory(is);
   // assertTrue("5 beans in reftypes, not " + xbf.getBeanDefinitionCount(),
   // xbf.getBeanDefinitionCount() == 5);
   TestBean loner = (TestBean) xbf.getBean("loner");
   TestBean dave = (TestBean) xbf.getBean("david");
   assertTrue(loner.getFriends().size() == 1);
   assertTrue(loner.getFriends().contains(dave));
 }
コード例 #3
0
 public void testRefToSingleton() throws Exception {
   InputStream is = getClass().getResourceAsStream("reftypes.xml");
   XmlBeanFactory xbf = new XmlBeanFactory(is);
   assertTrue(
       "6 beans in reftypes, not " + xbf.getBeanDefinitionCount(),
       xbf.getBeanDefinitionCount() == 6);
   TestBean jen = (TestBean) xbf.getBean("jenny");
   TestBean dave = (TestBean) xbf.getBean("david");
   TestBean jenks = (TestBean) xbf.getBean("jenks");
   ITestBean davesJen = dave.getSpouse();
   ITestBean jenksJen = jenks.getSpouse();
   assertTrue("1 jen instances", davesJen == jenksJen);
 }
コード例 #4
0
 /**
  * Note that prototype/singleton distinction is <b>not</b> inherited. It's possible for a subclass
  * singleton not to return independent instances even if derived from a prototype
  *
  * @throws Exception
  */
 public void testSingletonInheritsFromParentFactoryPrototype() throws Exception {
   InputStream pis = getClass().getResourceAsStream("parent.xml");
   XmlBeanFactory parent = new XmlBeanFactory(pis);
   InputStream is = getClass().getResourceAsStream("child.xml");
   XmlBeanFactory child = new XmlBeanFactory(is, parent);
   TestBean inherits = (TestBean) child.getBean("singletonInheritsFromParentFactoryPrototype");
   // Name property value is overriden
   assertTrue(inherits.getName().equals("prototype-override"));
   // Age property is inherited from bean in parent factory
   assertTrue(inherits.getAge() == 2);
   TestBean inherits2 = (TestBean) child.getBean("singletonInheritsFromParentFactoryPrototype");
   assertTrue(inherits2 == inherits);
 }
コード例 #5
0
 /** Uses a separate factory */
 public void testRefToSeparatePrototypeInstances() throws Exception {
   InputStream is = getClass().getResourceAsStream("reftypes.xml");
   XmlBeanFactory xbf = new XmlBeanFactory(is);
   assertTrue(
       "6 beans in reftypes, not " + xbf.getBeanDefinitionCount(),
       xbf.getBeanDefinitionCount() == 6);
   TestBean emma = (TestBean) xbf.getBean("emma");
   TestBean georgia = (TestBean) xbf.getBean("georgia");
   ITestBean emmasJenks = emma.getSpouse();
   ITestBean georgiasJenks = georgia.getSpouse();
   assertTrue(
       "Emma and georgia think they have a different boyfriend", emmasJenks != georgiasJenks);
   assertTrue("Emmas jenks has right name", emmasJenks.getName().equals("Andrew"));
   assertTrue("Emmas doesn't equal new ref", emmasJenks != xbf.getBean("jenks"));
   assertTrue("Georgias jenks has right name", emmasJenks.getName().equals("Andrew"));
   assertTrue("They are object equal", emmasJenks.equals(georgiasJenks));
   assertTrue("They object equal direct ref", emmasJenks.equals(xbf.getBean("jenks")));
 }
コード例 #6
0
  public void testRefSubelementsBuildCollection() throws Exception {
    InputStream is = getClass().getResourceAsStream("collections.xml");
    XmlBeanFactory xbf = new XmlBeanFactory(is);
    // assertTrue("5 beans in reftypes, not " + xbf.getBeanDefinitionCount(),
    // xbf.getBeanDefinitionCount() == 5);
    TestBean jen = (TestBean) xbf.getBean("jenny");
    TestBean dave = (TestBean) xbf.getBean("david");
    TestBean rod = (TestBean) xbf.getBean("rod");

    // Must be a list to support ordering
    // Our bean doesn't modify the collection:
    // of course it could be a different copy in a real object
    List friends = (List) rod.getFriends();
    assertTrue(friends.size() == 2);

    assertTrue("First friend must be jen, not " + friends.get(0), friends.get(0).equals(jen));
    assertTrue(friends.get(1).equals(dave));
    // Should be ordered
  }
コード例 #7
0
 public void testCircularReferences() {
   InputStream is = getClass().getResourceAsStream("reftypes.xml");
   XmlBeanFactory xbf = new XmlBeanFactory(is);
   TestBean jenny = (TestBean) xbf.getBean("jenny");
   TestBean david = (TestBean) xbf.getBean("david");
   TestBean ego = (TestBean) xbf.getBean("ego");
   assertTrue("Correct circular reference", jenny.getSpouse() == david);
   assertTrue("Correct circular reference", david.getSpouse() == jenny);
   assertTrue("Correct circular reference", ego.getSpouse() == ego);
 }
コード例 #8
0
 public void testPrototypeInheritanceFromParentFactorySingleton() throws Exception {
   InputStream pis = getClass().getResourceAsStream("parent.xml");
   XmlBeanFactory parent = new XmlBeanFactory(pis);
   InputStream is = getClass().getResourceAsStream("child.xml");
   XmlBeanFactory child = new XmlBeanFactory(is, parent);
   TestBean inherits = (TestBean) child.getBean("protoypeInheritsFromParentFactorySingleton");
   //		Name property value is overriden
   assertTrue(inherits.getName().equals("prototypeOverridesInheritedSingleton"));
   // Age property is inherited from bean in parent factory
   assertTrue(inherits.getAge() == 1);
   TestBean inherits2 = (TestBean) child.getBean("protoypeInheritsFromParentFactorySingleton");
   assertFalse(inherits2 == inherits);
   inherits2.setAge(13);
   assertTrue(inherits2.getAge() == 13);
   // Shouldn't have changed first instance
   assertTrue(inherits.getAge() == 1);
 }
コード例 #9
0
 public void testPropertyWithLiteralValueSubelement() throws Exception {
   InputStream is = getClass().getResourceAsStream("collections.xml");
   XmlBeanFactory xbf = new XmlBeanFactory(is);
   TestBean verbose = (TestBean) xbf.getBean("verbose");
   assertTrue(verbose.getName().equals("verbose"));
 }