Search in sources :

Example 76 with TestBean

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

the class MailSession method testFactoryMethodArgumentsForNonExistingMethod.

@Test
public void testFactoryMethodArgumentsForNonExistingMethod() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
    try {
        xbf.getBean("invalidPrototype", new TestBean());
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        assertTrue(ex.getMessage().contains("nonExisting(TestBean)"));
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 77 with TestBean

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

the class MailSession method testCannotSpecifyFactoryMethodArgumentsOnSingletonAfterCreation.

@Test
public void testCannotSpecifyFactoryMethodArgumentsOnSingletonAfterCreation() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
    // First getBean call triggers actual creation of the singleton bean
    FactoryMethods fm1 = (FactoryMethods) xbf.getBean("testBeanOnly");
    TestBean tb = fm1.getTestBean();
    FactoryMethods fm2 = (FactoryMethods) xbf.getBean("testBeanOnly", new TestBean());
    assertSame(fm1, fm2);
    assertSame(tb, fm2.getTestBean());
}
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)

Example 78 with TestBean

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

the class MailSession method testCanSpecifyFactoryMethodArgumentsOnSingleton.

@Test
public void testCanSpecifyFactoryMethodArgumentsOnSingleton() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
    // First getBean call triggers actual creation of the singleton bean
    TestBean tb = new TestBean();
    FactoryMethods fm1 = (FactoryMethods) xbf.getBean("testBeanOnly", tb);
    assertSame(tb, fm1.getTestBean());
    FactoryMethods fm2 = (FactoryMethods) xbf.getBean("testBeanOnly", new TestBean());
    assertSame(fm1, fm2);
    assertSame(tb, fm2.getTestBean());
}
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)

Example 79 with TestBean

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

the class MailSession method testCanSpecifyFactoryMethodArgumentsOnFactoryMethodPrototype.

@Test
public void testCanSpecifyFactoryMethodArgumentsOnFactoryMethodPrototype() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
    TestBean tbArg = new TestBean();
    tbArg.setName("arg1");
    TestBean tbArg2 = new TestBean();
    tbArg2.setName("arg2");
    FactoryMethods fm1 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", tbArg);
    assertEquals(0, fm1.getNum());
    assertEquals("default", fm1.getName());
    // This comes from the test bean
    assertEquals("arg1", fm1.getTestBean().getName());
    FactoryMethods fm2 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", tbArg2);
    assertEquals("arg2", fm2.getTestBean().getName());
    assertEquals(fm1.getNum(), fm2.getNum());
    assertEquals(fm2.getStringValue(), "testBeanOnlyPrototypeDISetterString");
    assertEquals(fm2.getStringValue(), fm2.getStringValue());
    // The TestBean reference is resolved to a prototype in the factory
    assertSame(fm2.getTestBean(), fm2.getTestBean());
    assertNotSame(fm1, fm2);
    FactoryMethods fm3 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", tbArg2, new Integer(1), "myName");
    assertEquals(1, fm3.getNum());
    assertEquals("myName", fm3.getName());
    assertEquals("arg2", fm3.getTestBean().getName());
    FactoryMethods fm4 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", tbArg);
    assertEquals(0, fm4.getNum());
    assertEquals("default", fm4.getName());
    assertEquals("arg1", fm4.getTestBean().getName());
}
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)

Example 80 with TestBean

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

the class FactoryMethods method defaultInstance.

public static FactoryMethods defaultInstance() {
    TestBean tb = new TestBean();
    tb.setName("defaultInstance");
    return new FactoryMethods(tb, "default", 0);
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean)

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