Search in sources :

Example 71 with TestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testConstructorResourceInjectionWithMultipleOrderedCandidates.

@Test
public void testConstructorResourceInjectionWithMultipleOrderedCandidates() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsResourceInjectionBean.class));
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    FixedOrder2NestedTestBean ntb1 = new FixedOrder2NestedTestBean();
    bf.registerSingleton("nestedTestBean1", ntb1);
    FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean();
    bf.registerSingleton("nestedTestBean2", ntb2);
    ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean");
    assertNull(bean.getTestBean3());
    assertSame(tb, bean.getTestBean4());
    assertEquals(2, bean.getNestedTestBeans().length);
    assertSame(ntb2, bean.getNestedTestBeans()[0]);
    assertSame(ntb1, bean.getNestedTestBeans()[1]);
    bf.destroySingletons();
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) 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 72 with TestBean

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

the class PropertyResourceConfigurerTests method testPropertyPlaceholderConfigurerWithSystemPropertyFallback.

@Test
public void testPropertyPlaceholderConfigurerWithSystemPropertyFallback() {
    factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class).addPropertyValue("country", "${os.name}").getBeanDefinition());
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    ppc.postProcessBeanFactory(factory);
    TestBean tb = (TestBean) factory.getBean("tb");
    assertEquals(System.getProperty("os.name"), tb.getCountry());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 73 with TestBean

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

the class PropertyResourceConfigurerTests method testPropertyPlaceholderConfigurerWithSystemPropertyNotUsed.

@Test
public void testPropertyPlaceholderConfigurerWithSystemPropertyNotUsed() {
    factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class).addPropertyValue("country", "${os.name}").getBeanDefinition());
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    Properties props = new Properties();
    props.setProperty("os.name", "myos");
    ppc.setProperties(props);
    ppc.postProcessBeanFactory(factory);
    TestBean tb = (TestBean) factory.getBean("tb");
    assertEquals("myos", tb.getCountry());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Properties(java.util.Properties) Test(org.junit.Test)

Example 74 with TestBean

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

the class PropertyResourceConfigurerTests method testPropertyPlaceholderConfigurerWithOverridingSystemProperty.

@Test
public void testPropertyPlaceholderConfigurerWithOverridingSystemProperty() {
    factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class).addPropertyValue("country", "${os.name}").getBeanDefinition());
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    Properties props = new Properties();
    props.put("os.name", "myos");
    ppc.setProperties(props);
    ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
    ppc.postProcessBeanFactory(factory);
    TestBean tb = (TestBean) factory.getBean("tb");
    assertEquals(System.getProperty("os.name"), tb.getCountry());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Properties(java.util.Properties) Test(org.junit.Test)

Example 75 with TestBean

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

the class MailSession method testFactoryMethodsSingletonOnTargetClass.

@Test
public void testFactoryMethodsSingletonOnTargetClass() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
    TestBean tb = (TestBean) xbf.getBean("defaultTestBean");
    assertEquals("defaultInstance", tb.getName());
    assertEquals(1, tb.getAge());
    FactoryMethods fm = (FactoryMethods) xbf.getBean("default");
    assertEquals(0, fm.getNum());
    assertEquals("default", fm.getName());
    assertEquals("defaultInstance", fm.getTestBean().getName());
    assertEquals("setterString", fm.getStringValue());
    fm = (FactoryMethods) xbf.getBean("testBeanOnly");
    assertEquals(0, fm.getNum());
    assertEquals("default", fm.getName());
    // This comes from the test bean
    assertEquals("Juergen", fm.getTestBean().getName());
    fm = (FactoryMethods) xbf.getBean("full");
    assertEquals(27, fm.getNum());
    assertEquals("gotcha", fm.getName());
    assertEquals("Juergen", fm.getTestBean().getName());
    FactoryMethods fm2 = (FactoryMethods) xbf.getBean("full");
    assertSame(fm, fm2);
    xbf.destroySingletons();
    assertTrue(tb.wasDestroyed());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) 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