Search in sources :

Example 1 with DerivedTestBean

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

the class XmlBeanFactoryTests method inheritanceWithClass.

@Test
void inheritanceWithClass() {
    DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
    DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
    new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
    assertThat(child.getType("inheritsWithClass")).isEqualTo(DerivedTestBean.class);
    DerivedTestBean inherits = (DerivedTestBean) child.getBean("inheritsWithClass");
    // Name property value is overridden
    assertThat(inherits.getName().equals("override")).isTrue();
    // Age property is inherited from bean in parent factory
    assertThat(inherits.getAge() == 1).isTrue();
    assertThat(inherits.wasInitialized()).isTrue();
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 2 with DerivedTestBean

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

the class XmlBeanFactoryTests method innerBeansWithoutDestroy.

@Test
void innerBeansWithoutDestroy() {
    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");
    assertThat(hasInnerBeans.getAge()).isEqualTo(5);
    TestBean inner1 = (TestBean) hasInnerBeans.getSpouse();
    assertThat(inner1).isNotNull();
    assertThat(inner1.getBeanName().startsWith("innerBean")).isTrue();
    assertThat(inner1.getName()).isEqualTo("inner1");
    assertThat(inner1.getAge()).isEqualTo(6);
    assertThat(hasInnerBeans.getFriends()).isNotNull();
    Object[] friends = hasInnerBeans.getFriends().toArray();
    assertThat(friends.length).isEqualTo(3);
    DerivedTestBean inner2 = (DerivedTestBean) friends[0];
    assertThat(inner2.getName()).isEqualTo("inner2");
    assertThat(inner2.getBeanName().startsWith(DerivedTestBean.class.getName())).isTrue();
    assertThat(inner2).isNotNull();
    assertThat(inner2.getAge()).isEqualTo(7);
    TestBean innerFactory = (TestBean) friends[1];
    assertThat(innerFactory.getName()).isEqualTo(DummyFactory.SINGLETON_NAME);
    TestBean inner5 = (TestBean) friends[2];
    assertThat(inner5.getBeanName().startsWith("innerBean")).isTrue();
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 3 with DerivedTestBean

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

the class XmlBeanFactoryTests method innerBeans.

@Test
void innerBeans() throws IOException {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
    try (InputStream inputStream = getClass().getResourceAsStream(REFTYPES_CONTEXT.getPath())) {
        reader.loadBeanDefinitions(new InputSource(inputStream));
    }
    // 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");
    assertThat(hasInnerBeans.getAge()).isEqualTo(5);
    TestBean inner1 = (TestBean) hasInnerBeans.getSpouse();
    assertThat(inner1).isNotNull();
    assertThat(inner1.getBeanName()).isEqualTo("innerBean#1");
    assertThat(inner1.getName()).isEqualTo("inner1");
    assertThat(inner1.getAge()).isEqualTo(6);
    assertThat(hasInnerBeans.getFriends()).isNotNull();
    Object[] friends = hasInnerBeans.getFriends().toArray();
    assertThat(friends.length).isEqualTo(3);
    DerivedTestBean inner2 = (DerivedTestBean) friends[0];
    assertThat(inner2.getName()).isEqualTo("inner2");
    assertThat(inner2.getBeanName().startsWith(DerivedTestBean.class.getName())).isTrue();
    assertThat(xbf.containsBean("innerBean#1")).isFalse();
    assertThat(inner2).isNotNull();
    assertThat(inner2.getAge()).isEqualTo(7);
    TestBean innerFactory = (TestBean) friends[1];
    assertThat(innerFactory.getName()).isEqualTo(DummyFactory.SINGLETON_NAME);
    TestBean inner5 = (TestBean) friends[2];
    assertThat(inner5.getBeanName()).isEqualTo("innerBean#2");
    assertThat(hasInnerBeans.getSomeMap()).isNotNull();
    assertThat(hasInnerBeans.getSomeMap().size()).isEqualTo(2);
    TestBean inner3 = (TestBean) hasInnerBeans.getSomeMap().get("someKey");
    assertThat(inner3.getName()).isEqualTo("Jenny");
    assertThat(inner3.getAge()).isEqualTo(30);
    TestBean inner4 = (TestBean) hasInnerBeans.getSomeMap().get("someOtherKey");
    assertThat(inner4.getName()).isEqualTo("inner4");
    assertThat(inner4.getAge()).isEqualTo(9);
    TestBean hasInnerBeansForConstructor = (TestBean) xbf.getBean("hasInnerBeansForConstructor");
    TestBean innerForConstructor = (TestBean) hasInnerBeansForConstructor.getSpouse();
    assertThat(innerForConstructor).isNotNull();
    assertThat(innerForConstructor.getBeanName()).isEqualTo("innerBean#3");
    assertThat(innerForConstructor.getName()).isEqualTo("inner1");
    assertThat(innerForConstructor.getAge()).isEqualTo(6);
    hasInnerBeansForConstructor = (TestBean) xbf.getBean("hasInnerBeansAsPrototype");
    innerForConstructor = (TestBean) hasInnerBeansForConstructor.getSpouse();
    assertThat(innerForConstructor).isNotNull();
    assertThat(innerForConstructor.getBeanName()).isEqualTo("innerBean");
    assertThat(innerForConstructor.getName()).isEqualTo("inner1");
    assertThat(innerForConstructor.getAge()).isEqualTo(6);
    hasInnerBeansForConstructor = (TestBean) xbf.getBean("hasInnerBeansAsPrototype");
    innerForConstructor = (TestBean) hasInnerBeansForConstructor.getSpouse();
    assertThat(innerForConstructor).isNotNull();
    assertThat(innerForConstructor.getBeanName()).isEqualTo("innerBean");
    assertThat(innerForConstructor.getName()).isEqualTo("inner1");
    assertThat(innerForConstructor.getAge()).isEqualTo(6);
    xbf.destroySingletons();
    assertThat(inner1.wasDestroyed()).isTrue();
    assertThat(inner2.wasDestroyed()).isTrue();
    assertThat(innerFactory.getName() == null).isTrue();
    assertThat(inner5.wasDestroyed()).isTrue();
}
Also used : InputSource(org.xml.sax.InputSource) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) InputStream(java.io.InputStream) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 4 with DerivedTestBean

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

the class DefaultListableBeanFactoryTests method prototypeWithArrayConversionForConstructor.

@Test
void prototypeWithArrayConversionForConstructor() {
    List<String> list = ManagedList.of("myName", "myBeanName");
    RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
    bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bd.getConstructorArgumentValues().addGenericArgumentValue(list);
    lbf.registerBeanDefinition("test", bd);
    DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test");
    assertThat(tb.getName()).isEqualTo("myName");
    assertThat(tb.getBeanName()).isEqualTo("myBeanName");
    DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test");
    assertThat(tb != tb2).isTrue();
    assertThat(tb2.getName()).isEqualTo("myName");
    assertThat(tb2.getBeanName()).isEqualTo("myBeanName");
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 5 with DerivedTestBean

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

the class SimpleTransactionScopeTests method getFromScope.

@Test
@SuppressWarnings("resource")
public void getFromScope() throws Exception {
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
    GenericBeanDefinition bd1 = new GenericBeanDefinition();
    bd1.setBeanClass(TestBean.class);
    bd1.setScope("tx");
    bd1.setPrimary(true);
    context.registerBeanDefinition("txScopedObject1", bd1);
    GenericBeanDefinition bd2 = new GenericBeanDefinition();
    bd2.setBeanClass(DerivedTestBean.class);
    bd2.setScope("tx");
    context.registerBeanDefinition("txScopedObject2", bd2);
    context.refresh();
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(TestBean.class)).withCauseInstanceOf(IllegalStateException.class);
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(DerivedTestBean.class)).withCauseInstanceOf(IllegalStateException.class);
    TestBean bean1 = null;
    DerivedTestBean bean2 = null;
    DerivedTestBean bean2a = null;
    DerivedTestBean bean2b = null;
    TransactionSynchronizationManager.initSynchronization();
    try {
        bean1 = context.getBean(TestBean.class);
        assertThat(context.getBean(TestBean.class)).isSameAs(bean1);
        bean2 = context.getBean(DerivedTestBean.class);
        assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2);
        context.getBeanFactory().destroyScopedBean("txScopedObject2");
        assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
        assertThat(bean2.wasDestroyed()).isTrue();
        bean2a = context.getBean(DerivedTestBean.class);
        assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2a);
        assertThat(bean2a).isNotSameAs(bean2);
        context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
        assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
        assertThat(bean2a.wasDestroyed()).isFalse();
        bean2b = context.getBean(DerivedTestBean.class);
        assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2b);
        assertThat(bean2b).isNotSameAs(bean2);
        assertThat(bean2b).isNotSameAs(bean2a);
    } finally {
        TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
        TransactionSynchronizationManager.clearSynchronization();
    }
    assertThat(bean2a.wasDestroyed()).isFalse();
    assertThat(bean2b.wasDestroyed()).isTrue();
    assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(TestBean.class)).withCauseInstanceOf(IllegalStateException.class);
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(DerivedTestBean.class)).withCauseInstanceOf(IllegalStateException.class);
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Aggregations

DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)27 Test (org.junit.jupiter.api.Test)26 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)7 TestBean (org.springframework.beans.testfixture.beans.TestBean)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)6 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)6 PropertyEditorSupport (java.beans.PropertyEditorSupport)4 WebApplicationContext (org.springframework.web.context.WebApplicationContext)3 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)2 MockHttpSession (org.springframework.web.testfixture.servlet.MockHttpSession)2 ServletContextEvent (jakarta.servlet.ServletContextEvent)1 InputStream (java.io.InputStream)1 Serializable (java.io.Serializable)1