Search in sources :

Example 6 with DummyFactory

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

the class BeanFactoryUtilsTests method isSingletonAndIsPrototypeWithStaticFactory.

@Test
public void isSingletonAndIsPrototypeWithStaticFactory() {
    StaticListableBeanFactory lbf = new StaticListableBeanFactory();
    TestBean bean = new TestBean();
    DummyFactory fb1 = new DummyFactory();
    DummyFactory fb2 = new DummyFactory();
    fb2.setSingleton(false);
    TestBeanSmartFactoryBean sfb1 = new TestBeanSmartFactoryBean(true, true);
    TestBeanSmartFactoryBean sfb2 = new TestBeanSmartFactoryBean(true, false);
    TestBeanSmartFactoryBean sfb3 = new TestBeanSmartFactoryBean(false, true);
    TestBeanSmartFactoryBean sfb4 = new TestBeanSmartFactoryBean(false, false);
    lbf.addBean("bean", bean);
    lbf.addBean("fb1", fb1);
    lbf.addBean("fb2", fb2);
    lbf.addBean("sfb1", sfb1);
    lbf.addBean("sfb2", sfb2);
    lbf.addBean("sfb3", sfb3);
    lbf.addBean("sfb4", sfb4);
    Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true);
    assertThat(beans.get("bean")).isSameAs(bean);
    assertThat(beans.get("fb1")).isSameAs(fb1.getObject());
    assertThat(beans.get("fb2")).isInstanceOf(TestBean.class);
    assertThat(beans.get("sfb1")).isInstanceOf(TestBean.class);
    assertThat(beans.get("sfb2")).isInstanceOf(TestBean.class);
    assertThat(beans.get("sfb3")).isInstanceOf(TestBean.class);
    assertThat(beans.get("sfb4")).isInstanceOf(TestBean.class);
    assertThat(lbf.getBeanDefinitionCount()).isEqualTo(7);
    assertThat(lbf.getBean("bean")).isInstanceOf(TestBean.class);
    assertThat(lbf.getBean("&fb1")).isInstanceOf(FactoryBean.class);
    assertThat(lbf.getBean("&fb2")).isInstanceOf(FactoryBean.class);
    assertThat(lbf.getBean("&sfb1")).isInstanceOf(SmartFactoryBean.class);
    assertThat(lbf.getBean("&sfb2")).isInstanceOf(SmartFactoryBean.class);
    assertThat(lbf.getBean("&sfb3")).isInstanceOf(SmartFactoryBean.class);
    assertThat(lbf.getBean("&sfb4")).isInstanceOf(SmartFactoryBean.class);
    assertThat(lbf.isSingleton("bean")).isTrue();
    assertThat(lbf.isSingleton("fb1")).isTrue();
    assertThat(lbf.isSingleton("fb2")).isTrue();
    assertThat(lbf.isSingleton("sfb1")).isTrue();
    assertThat(lbf.isSingleton("sfb2")).isTrue();
    assertThat(lbf.isSingleton("sfb3")).isTrue();
    assertThat(lbf.isSingleton("sfb4")).isTrue();
    assertThat(lbf.isSingleton("&fb1")).isTrue();
    assertThat(lbf.isSingleton("&fb2")).isFalse();
    assertThat(lbf.isSingleton("&sfb1")).isTrue();
    assertThat(lbf.isSingleton("&sfb2")).isTrue();
    assertThat(lbf.isSingleton("&sfb3")).isFalse();
    assertThat(lbf.isSingleton("&sfb4")).isFalse();
    assertThat(lbf.isPrototype("bean")).isFalse();
    assertThat(lbf.isPrototype("fb1")).isFalse();
    assertThat(lbf.isPrototype("fb2")).isFalse();
    assertThat(lbf.isPrototype("sfb1")).isFalse();
    assertThat(lbf.isPrototype("sfb2")).isFalse();
    assertThat(lbf.isPrototype("sfb3")).isFalse();
    assertThat(lbf.isPrototype("sfb4")).isFalse();
    assertThat(lbf.isPrototype("&fb1")).isFalse();
    assertThat(lbf.isPrototype("&fb2")).isTrue();
    assertThat(lbf.isPrototype("&sfb1")).isTrue();
    assertThat(lbf.isPrototype("&sfb2")).isFalse();
    assertThat(lbf.isPrototype("&sfb3")).isTrue();
    assertThat(lbf.isPrototype("&sfb4")).isTrue();
}
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 7 with DummyFactory

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

the class XmlListableBeanFactoryTests method setup.

@BeforeEach
public void setup() {
    parent = new DefaultListableBeanFactory();
    Map map = new HashMap();
    map.put("name", "Albert");
    RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
    bd1.setPropertyValues(new MutablePropertyValues(map));
    parent.registerBeanDefinition("father", bd1);
    map = new HashMap();
    map.put("name", "Roderick");
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    bd2.setPropertyValues(new MutablePropertyValues(map));
    parent.registerBeanDefinition("rod", bd2);
    this.factory = new DefaultListableBeanFactory(parent);
    new XmlBeanDefinitionReader(this.factory).loadBeanDefinitions(new ClassPathResource("test.xml", getClass()));
    this.factory.addBeanPostProcessor(new BeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
            if (bean instanceof TestBean) {
                ((TestBean) bean).setPostProcessed(true);
            }
            if (bean instanceof DummyFactory) {
                ((DummyFactory) bean).setPostProcessed(true);
            }
            return bean;
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
            return bean;
        }
    });
    this.factory.addBeanPostProcessor(new LifecycleBean.PostProcessor());
    this.factory.addBeanPostProcessor(new ProtectedLifecycleBean.PostProcessor());
// this.factory.preInstantiateSingletons();
}
Also used : HashMap(java.util.HashMap) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) DummyFactory(org.springframework.beans.testfixture.beans.factory.DummyFactory) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) LifecycleBean(org.springframework.beans.testfixture.beans.LifecycleBean) HashMap(java.util.HashMap) Map(java.util.Map) BeansException(org.springframework.beans.BeansException) BeforeEach(org.junit.jupiter.api.BeforeEach)

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