Search in sources :

Example 1 with DummyFactory

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

the class XmlListableBeanFactoryTests method beanPostProcessor.

@Test
public void beanPostProcessor() {
    TestBean kerry = (TestBean) getBeanFactory().getBean("kerry");
    TestBean kathy = (TestBean) getBeanFactory().getBean("kathy");
    DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
    TestBean factoryCreated = (TestBean) getBeanFactory().getBean("singletonFactory");
    assertThat(kerry.isPostProcessed()).isTrue();
    assertThat(kathy.isPostProcessed()).isTrue();
    assertThat(factory.isPostProcessed()).isTrue();
    assertThat(factoryCreated.isPostProcessed()).isTrue();
}
Also used : DummyFactory(org.springframework.beans.testfixture.beans.factory.DummyFactory) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 2 with DummyFactory

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

the class XmlListableBeanFactoryTests method factoryReferences.

@Test
public void factoryReferences() {
    DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
    DummyReferencer ref = (DummyReferencer) getBeanFactory().getBean("factoryReferencer");
    assertThat(ref.getTestBean1() == ref.getTestBean2()).isTrue();
    assertThat(ref.getDummyFactory() == factory).isTrue();
    DummyReferencer ref2 = (DummyReferencer) getBeanFactory().getBean("factoryReferencerWithConstructor");
    assertThat(ref2.getTestBean1() == ref2.getTestBean2()).isTrue();
    assertThat(ref2.getDummyFactory() == factory).isTrue();
}
Also used : DummyFactory(org.springframework.beans.testfixture.beans.factory.DummyFactory) Test(org.junit.jupiter.api.Test)

Example 3 with DummyFactory

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

the class BeanFactoryUtilsTests method testFindsBeansOfTypeWithStaticFactory.

@Test
public void testFindsBeansOfTypeWithStaticFactory() {
    StaticListableBeanFactory lbf = new StaticListableBeanFactory();
    TestBean t1 = new TestBean();
    TestBean t2 = new TestBean();
    DummyFactory t3 = new DummyFactory();
    DummyFactory t4 = new DummyFactory();
    t4.setSingleton(false);
    lbf.addBean("t1", t1);
    lbf.addBean("t2", t2);
    lbf.addBean("t3", t3);
    lbf.addBean("t4", t4);
    Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true);
    assertThat(beans.size()).isEqualTo(4);
    assertThat(beans.get("t1")).isEqualTo(t1);
    assertThat(beans.get("t2")).isEqualTo(t2);
    assertThat(beans.get("t3")).isEqualTo(t3.getObject());
    boolean condition = beans.get("t4") instanceof TestBean;
    assertThat(condition).isTrue();
    beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, DummyFactory.class, true, true);
    assertThat(beans.size()).isEqualTo(2);
    assertThat(beans.get("&t3")).isEqualTo(t3);
    assertThat(beans.get("&t4")).isEqualTo(t4);
    beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, FactoryBean.class, true, true);
    assertThat(beans.size()).isEqualTo(2);
    assertThat(beans.get("&t3")).isEqualTo(t3);
    assertThat(beans.get("&t4")).isEqualTo(t4);
}
Also used : DummyFactory(org.springframework.beans.testfixture.beans.factory.DummyFactory) StaticListableBeanFactory(org.springframework.beans.factory.support.StaticListableBeanFactory) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 4 with DummyFactory

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

the class BeanFactoryUtilsTests method testFindsBeansOfTypeWithDefaultFactory.

@Test
public void testFindsBeansOfTypeWithDefaultFactory() {
    Object test3 = this.listableBeanFactory.getBean("test3");
    Object test = this.listableBeanFactory.getBean("test");
    TestBean t1 = new TestBean();
    TestBean t2 = new TestBean();
    DummyFactory t3 = new DummyFactory();
    DummyFactory t4 = new DummyFactory();
    t4.setSingleton(false);
    this.listableBeanFactory.registerSingleton("t1", t1);
    this.listableBeanFactory.registerSingleton("t2", t2);
    this.listableBeanFactory.registerSingleton("t3", t3);
    this.listableBeanFactory.registerSingleton("t4", t4);
    Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
    assertThat(beans.size()).isEqualTo(6);
    assertThat(beans.get("test3")).isEqualTo(test3);
    assertThat(beans.get("test")).isEqualTo(test);
    assertThat(beans.get("t1")).isEqualTo(t1);
    assertThat(beans.get("t2")).isEqualTo(t2);
    assertThat(beans.get("t3")).isEqualTo(t3.getObject());
    boolean condition2 = beans.get("t4") instanceof TestBean;
    assertThat(condition2).isTrue();
    // t3 and t4 are found here as of Spring 2.0, since they are pre-registered
    // singleton instances, while testFactory1 and testFactory are *not* found
    // because they are FactoryBean definitions that haven't been initialized yet.
    beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true);
    Object testFactory1 = this.listableBeanFactory.getBean("testFactory1");
    assertThat(beans.size()).isEqualTo(5);
    assertThat(beans.get("test")).isEqualTo(test);
    assertThat(beans.get("testFactory1")).isEqualTo(testFactory1);
    assertThat(beans.get("t1")).isEqualTo(t1);
    assertThat(beans.get("t2")).isEqualTo(t2);
    assertThat(beans.get("t3")).isEqualTo(t3.getObject());
    beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, true);
    assertThat(beans.size()).isEqualTo(8);
    assertThat(beans.get("test3")).isEqualTo(test3);
    assertThat(beans.get("test")).isEqualTo(test);
    assertThat(beans.get("testFactory1")).isEqualTo(testFactory1);
    boolean condition1 = beans.get("testFactory2") instanceof TestBean;
    assertThat(condition1).isTrue();
    assertThat(beans.get("t1")).isEqualTo(t1);
    assertThat(beans.get("t2")).isEqualTo(t2);
    assertThat(beans.get("t3")).isEqualTo(t3.getObject());
    boolean condition = beans.get("t4") instanceof TestBean;
    assertThat(condition).isTrue();
    beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, DummyFactory.class, true, true);
    assertThat(beans.size()).isEqualTo(4);
    assertThat(beans.get("&testFactory1")).isEqualTo(this.listableBeanFactory.getBean("&testFactory1"));
    assertThat(beans.get("&testFactory2")).isEqualTo(this.listableBeanFactory.getBean("&testFactory2"));
    assertThat(beans.get("&t3")).isEqualTo(t3);
    assertThat(beans.get("&t4")).isEqualTo(t4);
    beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, FactoryBean.class, true, true);
    assertThat(beans.size()).isEqualTo(4);
    assertThat(beans.get("&testFactory1")).isEqualTo(this.listableBeanFactory.getBean("&testFactory1"));
    assertThat(beans.get("&testFactory2")).isEqualTo(this.listableBeanFactory.getBean("&testFactory2"));
    assertThat(beans.get("&t3")).isEqualTo(t3);
    assertThat(beans.get("&t4")).isEqualTo(t4);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DummyFactory(org.springframework.beans.testfixture.beans.factory.DummyFactory) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 5 with DummyFactory

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

the class XmlBeanFactoryTests method factoryReferenceCircle.

@Test
void factoryReferenceCircle() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(FACTORY_CIRCLE_CONTEXT);
    TestBean tb = (TestBean) xbf.getBean("singletonFactory");
    DummyFactory db = (DummyFactory) xbf.getBean("&singletonFactory");
    assertThat(tb == db.getOtherTestBean()).isTrue();
}
Also used : DummyFactory(org.springframework.beans.testfixture.beans.factory.DummyFactory) 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) Test(org.junit.jupiter.api.Test)

Aggregations

DummyFactory (org.springframework.beans.testfixture.beans.factory.DummyFactory)7 Test (org.junit.jupiter.api.Test)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)6 TestBean (org.springframework.beans.testfixture.beans.TestBean)6 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 StaticListableBeanFactory (org.springframework.beans.factory.support.StaticListableBeanFactory)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 BeansException (org.springframework.beans.BeansException)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 BeanPostProcessor (org.springframework.beans.factory.config.BeanPostProcessor)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)1 LifecycleBean (org.springframework.beans.testfixture.beans.LifecycleBean)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)1