Search in sources :

Example 1 with DummyFactory

use of cn.taketoday.beans.testfixture.beans.factory.DummyFactory in project today-infrastructure by TAKETODAY.

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(cn.taketoday.beans.testfixture.beans.factory.DummyFactory) Test(org.junit.jupiter.api.Test)

Example 2 with DummyFactory

use of cn.taketoday.beans.testfixture.beans.factory.DummyFactory in project today-infrastructure by TAKETODAY.

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(cn.taketoday.beans.testfixture.beans.factory.DummyFactory) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Example 3 with DummyFactory

use of cn.taketoday.beans.testfixture.beans.factory.DummyFactory in project today-infrastructure by TAKETODAY.

the class BeanFactoryUtilsTests method testFindsBeansOfTypeWithStaticFactory.

@Test
public void testFindsBeansOfTypeWithStaticFactory() {
    StandardBeanFactory lbf = new StandardBeanFactory();
    TestBean t1 = new TestBean();
    TestBean t2 = new TestBean();
    DummyFactory t3 = new DummyFactory();
    DummyFactory t4 = new DummyFactory();
    t4.setSingleton(false);
    lbf.registerSingleton("t1", t1);
    lbf.registerSingleton("t2", t2);
    lbf.registerSingleton("t3", t3);
    lbf.registerSingleton("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(cn.taketoday.beans.testfixture.beans.factory.DummyFactory) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 4 with DummyFactory

use of cn.taketoday.beans.testfixture.beans.factory.DummyFactory in project today-infrastructure by TAKETODAY.

the class BeanFactoryUtilsTests method isSingletonAndIsPrototypeWithStaticFactory.

@Test
public void isSingletonAndIsPrototypeWithStaticFactory() {
    StandardBeanFactory lbf = new StandardBeanFactory();
    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.registerBeanDefinition("bean", new RootBeanDefinition(TestBean.class, () -> bean));
    lbf.registerBeanDefinition("fb1", new RootBeanDefinition(DummyFactory.class, () -> fb1));
    lbf.registerBeanDefinition("fb2", new RootBeanDefinition(DummyFactory.class, () -> fb2));
    lbf.registerBeanDefinition("sfb1", new RootBeanDefinition(TestBeanSmartFactoryBean.class, () -> sfb1));
    lbf.registerBeanDefinition("sfb2", new RootBeanDefinition(TestBeanSmartFactoryBean.class, () -> sfb2));
    lbf.registerBeanDefinition("sfb3", new RootBeanDefinition(TestBeanSmartFactoryBean.class, () -> sfb3));
    lbf.registerBeanDefinition("sfb4", new RootBeanDefinition(TestBeanSmartFactoryBean.class, () -> 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")).isFalse();
    assertThat(lbf.isSingleton("sfb1")).isTrue();
    assertThat(lbf.isSingleton("sfb2")).isTrue();
    assertThat(lbf.isSingleton("sfb3")).isFalse();
    assertThat(lbf.isSingleton("sfb4")).isFalse();
    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.isPrototype("bean")).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();
    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();
}
Also used : DummyFactory(cn.taketoday.beans.testfixture.beans.factory.DummyFactory) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 5 with DummyFactory

use of cn.taketoday.beans.testfixture.beans.factory.DummyFactory in project today-infrastructure by TAKETODAY.

the class XmlBeanFactoryTests method factoryReferenceCircle.

@Test
void factoryReferenceCircle() {
    StandardBeanFactory xbf = new StandardBeanFactory();
    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(cn.taketoday.beans.testfixture.beans.factory.DummyFactory) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ResourceTestBean(cn.taketoday.tests.sample.beans.ResourceTestBean) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Aggregations

DummyFactory (cn.taketoday.beans.testfixture.beans.factory.DummyFactory)14 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)12 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)12 Test (org.junit.jupiter.api.Test)12 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)8 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)8 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)4 BeansException (cn.taketoday.beans.BeansException)2 PropertyValues (cn.taketoday.beans.PropertyValues)2 InitializationBeanPostProcessor (cn.taketoday.beans.factory.InitializationBeanPostProcessor)2 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)2 LifecycleBean (cn.taketoday.beans.testfixture.beans.LifecycleBean)2 ClassPathResource (cn.taketoday.core.io.ClassPathResource)2 ResourceTestBean (cn.taketoday.tests.sample.beans.ResourceTestBean)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2