Search in sources :

Example 21 with DerivedTestBean

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

the class DefaultSingletonBeanRegistryTests method testDisposableBean.

@Test
public void testDisposableBean() {
    DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry();
    DerivedTestBean tb = new DerivedTestBean();
    beanRegistry.registerSingleton("tb", tb);
    beanRegistry.registerDisposableBean("tb", tb);
    assertSame(tb, beanRegistry.getSingleton("tb"));
    assertSame(tb, beanRegistry.getSingleton("tb"));
    assertEquals(1, beanRegistry.getSingletonCount());
    String[] names = beanRegistry.getSingletonNames();
    assertEquals(1, names.length);
    assertEquals("tb", names[0]);
    assertFalse(tb.wasDestroyed());
    beanRegistry.destroySingletons();
    assertEquals(0, beanRegistry.getSingletonCount());
    assertEquals(0, beanRegistry.getSingletonNames().length);
    assertTrue(tb.wasDestroyed());
}
Also used : DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 22 with DerivedTestBean

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

the class DefaultListableBeanFactoryTests method testCreateBeanWithDisposableBean.

@Test
public void testCreateBeanWithDisposableBean() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    DerivedTestBean tb = lbf.createBean(DerivedTestBean.class);
    assertSame(lbf, tb.getBeanFactory());
    lbf.destroyBean(tb);
    assertTrue(tb.wasDestroyed());
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 23 with DerivedTestBean

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

the class DefaultListableBeanFactoryTests method testPrototypeWithArrayConversionForFactoryMethod.

@Test
public void testPrototypeWithArrayConversionForFactoryMethod() {
    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.setFactoryMethodName("create");
    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)

Example 24 with DerivedTestBean

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

the class XmlBeanFactoryTests method testInheritanceWithDifferentClass.

@Test
public void testInheritanceWithDifferentClass() 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("inheritsWithDifferentClass");
    // 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 25 with DerivedTestBean

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

the class XmlBeanFactoryTests method testLenientDependencyMatching.

@Test
public void testLenientDependencyMatching() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    LenientDependencyTestBean bean = (LenientDependencyTestBean) xbf.getBean("lenientDependencyTestBean");
    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)

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