Search in sources :

Example 1 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class XmlBeanFactoryTests method testLenientDependencyMatchingFactoryMethod.

@Test
public void testLenientDependencyMatchingFactoryMethod() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    LenientDependencyTestBean bean = (LenientDependencyTestBean) xbf.getBean("lenientDependencyTestBeanFactoryMethod");
    assertTrue(bean.tb instanceof DerivedTestBean);
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 2 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class XmlBeanFactoryTests method testInnerBeans.

@Test
public void testInnerBeans() throws IOException {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
    InputStream inputStream = getClass().getResourceAsStream(REFTYPES_CONTEXT.getPath());
    try {
        reader.loadBeanDefinitions(new InputSource(inputStream));
    } finally {
        inputStream.close();
    }
    // Let's create the outer bean named "innerBean",
    // to check whether it doesn't create any conflicts
    // with the actual inner beans named "innerBean".
    xbf.getBean("innerBean");
    TestBean hasInnerBeans = (TestBean) xbf.getBean("hasInnerBeans");
    assertEquals(5, hasInnerBeans.getAge());
    TestBean inner1 = (TestBean) hasInnerBeans.getSpouse();
    assertNotNull(inner1);
    assertEquals("innerBean#1", inner1.getBeanName());
    assertEquals("inner1", inner1.getName());
    assertEquals(6, inner1.getAge());
    assertNotNull(hasInnerBeans.getFriends());
    Object[] friends = hasInnerBeans.getFriends().toArray();
    assertEquals(3, friends.length);
    DerivedTestBean inner2 = (DerivedTestBean) friends[0];
    assertEquals("inner2", inner2.getName());
    assertTrue(inner2.getBeanName().startsWith(DerivedTestBean.class.getName()));
    assertFalse(xbf.containsBean("innerBean#1"));
    assertNotNull(inner2);
    assertEquals(7, inner2.getAge());
    TestBean innerFactory = (TestBean) friends[1];
    assertEquals(DummyFactory.SINGLETON_NAME, innerFactory.getName());
    TestBean inner5 = (TestBean) friends[2];
    assertEquals("innerBean#2", inner5.getBeanName());
    assertNotNull(hasInnerBeans.getSomeMap());
    assertEquals(2, hasInnerBeans.getSomeMap().size());
    TestBean inner3 = (TestBean) hasInnerBeans.getSomeMap().get("someKey");
    assertEquals("Jenny", inner3.getName());
    assertEquals(30, inner3.getAge());
    TestBean inner4 = (TestBean) hasInnerBeans.getSomeMap().get("someOtherKey");
    assertEquals("inner4", inner4.getName());
    assertEquals(9, inner4.getAge());
    TestBean hasInnerBeansForConstructor = (TestBean) xbf.getBean("hasInnerBeansForConstructor");
    TestBean innerForConstructor = (TestBean) hasInnerBeansForConstructor.getSpouse();
    assertNotNull(innerForConstructor);
    assertEquals("innerBean#3", innerForConstructor.getBeanName());
    assertEquals("inner1", innerForConstructor.getName());
    assertEquals(6, innerForConstructor.getAge());
    hasInnerBeansForConstructor = (TestBean) xbf.getBean("hasInnerBeansAsPrototype");
    innerForConstructor = (TestBean) hasInnerBeansForConstructor.getSpouse();
    assertNotNull(innerForConstructor);
    assertEquals("innerBean", innerForConstructor.getBeanName());
    assertEquals("inner1", innerForConstructor.getName());
    assertEquals(6, innerForConstructor.getAge());
    hasInnerBeansForConstructor = (TestBean) xbf.getBean("hasInnerBeansAsPrototype");
    innerForConstructor = (TestBean) hasInnerBeansForConstructor.getSpouse();
    assertNotNull(innerForConstructor);
    assertEquals("innerBean", innerForConstructor.getBeanName());
    assertEquals("inner1", innerForConstructor.getName());
    assertEquals(6, innerForConstructor.getAge());
    xbf.destroySingletons();
    assertTrue(inner1.wasDestroyed());
    assertTrue(inner2.wasDestroyed());
    assertTrue(innerFactory.getName() == null);
    assertTrue(inner5.wasDestroyed());
}
Also used : InputSource(org.xml.sax.InputSource) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) InputStream(java.io.InputStream) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 3 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class XmlBeanFactoryTests method testInheritanceWithClass.

@Test
public void testInheritanceWithClass() throws Exception {
    DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
    DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
    new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
    assertEquals(DerivedTestBean.class, child.getType("inheritsWithClass"));
    DerivedTestBean inherits = (DerivedTestBean) child.getBean("inheritsWithClass");
    // Name property value is overridden
    assertTrue(inherits.getName().equals("override"));
    // Age property is inherited from bean in parent factory
    assertTrue(inherits.getAge() == 1);
    assertTrue(inherits.wasInitialized());
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 4 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class XmlBeanFactoryTests method testInnerBeansWithoutDestroy.

@Test
public void testInnerBeansWithoutDestroy() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
    reader.loadBeanDefinitions(REFTYPES_CONTEXT);
    // Let's create the outer bean named "innerBean",
    // to check whether it doesn't create any conflicts
    // with the actual inner beans named "innerBean".
    xbf.getBean("innerBean");
    TestBean hasInnerBeans = (TestBean) xbf.getBean("hasInnerBeansWithoutDestroy");
    assertEquals(5, hasInnerBeans.getAge());
    TestBean inner1 = (TestBean) hasInnerBeans.getSpouse();
    assertNotNull(inner1);
    assertTrue(inner1.getBeanName().startsWith("innerBean"));
    assertEquals("inner1", inner1.getName());
    assertEquals(6, inner1.getAge());
    assertNotNull(hasInnerBeans.getFriends());
    Object[] friends = hasInnerBeans.getFriends().toArray();
    assertEquals(3, friends.length);
    DerivedTestBean inner2 = (DerivedTestBean) friends[0];
    assertEquals("inner2", inner2.getName());
    assertTrue(inner2.getBeanName().startsWith(DerivedTestBean.class.getName()));
    assertNotNull(inner2);
    assertEquals(7, inner2.getAge());
    TestBean innerFactory = (TestBean) friends[1];
    assertEquals(DummyFactory.SINGLETON_NAME, innerFactory.getName());
    TestBean inner5 = (TestBean) friends[2];
    assertTrue(inner5.getBeanName().startsWith("innerBean"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 5 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testPrototypeWithArrayConversionForConstructor.

@Test
public void testPrototypeWithArrayConversionForConstructor() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    List<String> list = new ManagedList<>();
    list.add("myName");
    list.add("myBeanName");
    RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
    bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    bd.getConstructorArgumentValues().addGenericArgumentValue(list);
    lbf.registerBeanDefinition("test", bd);
    DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test");
    assertEquals("myName", tb.getName());
    assertEquals("myBeanName", tb.getBeanName());
    DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test");
    assertTrue(tb != tb2);
    assertEquals("myName", tb2.getName());
    assertEquals("myBeanName", tb2.getBeanName());
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Aggregations

DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)27 Test (org.junit.Test)26 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)6 ITestBean (org.springframework.tests.sample.beans.ITestBean)6 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)6 TestBean (org.springframework.tests.sample.beans.TestBean)6 PropertyEditorSupport (java.beans.PropertyEditorSupport)4 WebApplicationContext (org.springframework.web.context.WebApplicationContext)3 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)3 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)2 ManagedList (org.springframework.beans.factory.support.ManagedList)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 MockHttpSession (org.springframework.mock.web.test.MockHttpSession)2 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)2 InputStream (java.io.InputStream)1 Serializable (java.io.Serializable)1 HashSet (java.util.HashSet)1