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();
}
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();
}
Aggregations