Search in sources :

Example 16 with TestBean

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

the class DefaultListableBeanFactoryTests method testPropertiesWithDotsInKey.

@Test
public void testPropertiesWithDotsInKey() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("tb.(class)", TestBean.class.getName());
    p.setProperty("tb.someMap[my.key]", "my.value");
    int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    assertTrue("1 beans registered, not " + count, count == 1);
    assertEquals(1, lbf.getBeanDefinitionCount());
    TestBean tb = lbf.getBean("tb", TestBean.class);
    assertEquals("my.value", tb.getSomeMap().get("my.key"));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) PropertiesBeanDefinitionReader(org.springframework.beans.factory.support.PropertiesBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Properties(java.util.Properties) Test(org.junit.Test)

Example 17 with TestBean

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

the class DefaultListableBeanFactoryTests method testSingleTestBean.

private void testSingleTestBean(ListableBeanFactory lbf) {
    assertTrue("1 beans defined", lbf.getBeanDefinitionCount() == 1);
    String[] names = lbf.getBeanDefinitionNames();
    assertTrue(names != lbf.getBeanDefinitionNames());
    assertTrue("Array length == 1", names.length == 1);
    assertTrue("0th element == test", names[0].equals("test"));
    TestBean tb = (TestBean) lbf.getBean("test");
    assertTrue("Test is non null", tb != null);
    assertTrue("Test bean name is Tony", "Tony".equals(tb.getName()));
    assertTrue("Test bean age is 48", tb.getAge() == 48);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean)

Example 18 with TestBean

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

the class DefaultListableBeanFactoryTests method testAutowireBeanByName.

@Test
public void testAutowireBeanByName() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spouse", bd);
    DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
    TestBean spouse = (TestBean) lbf.getBean("spouse");
    assertEquals(spouse, bean.getSpouse());
    assertTrue(BeanFactoryUtils.beanOfType(lbf, TestBean.class) == spouse);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.tests.sample.beans.DependenciesBean) Test(org.junit.Test)

Example 19 with TestBean

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

the class DefaultListableBeanFactoryTests method testGetBeanByTypeFiltersOutNonAutowireCandidates.

@Test
public void testGetBeanByTypeFiltersOutNonAutowireCandidates() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    RootBeanDefinition na1 = new RootBeanDefinition(TestBean.class);
    na1.setAutowireCandidate(false);
    lbf.registerBeanDefinition("bd1", bd1);
    lbf.registerBeanDefinition("na1", na1);
    // na1 was filtered
    TestBean actual = lbf.getBean(TestBean.class);
    assertSame(lbf.getBean("bd1", TestBean.class), actual);
    lbf.registerBeanDefinition("bd2", bd2);
    try {
        lbf.getBean(TestBean.class);
        fail("Should have thrown NoSuchBeanDefinitionException");
    } catch (NoSuchBeanDefinitionException ex) {
    // expected
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 20 with TestBean

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

the class DefaultListableBeanFactoryTests method testConfigureBeanWithAutowiring.

@Test
public void testConfigureBeanWithAutowiring() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spouse", bd);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("age", "99");
    RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
    tbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_NAME);
    lbf.registerBeanDefinition("test", tbd);
    TestBean tb = new TestBean();
    lbf.configureBean(tb, "test");
    assertSame(lbf, tb.getBeanFactory());
    TestBean spouse = (TestBean) lbf.getBean("spouse");
    assertEquals(spouse, tb.getSpouse());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Aggregations

TestBean (org.springframework.tests.sample.beans.TestBean)788 Test (org.junit.Test)740 ITestBean (org.springframework.tests.sample.beans.ITestBean)456 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)248 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)225 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)164 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)160 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)144 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)86 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)44 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)40 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)40 Properties (java.util.Properties)35 Errors (org.springframework.validation.Errors)34 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)33 PropertyEditorSupport (java.beans.PropertyEditorSupport)31 HashMap (java.util.HashMap)30 List (java.util.List)27 Map (java.util.Map)27 PageContext (javax.servlet.jsp.PageContext)27