Search in sources :

Example 1 with DefaultSingletonBeanRegistry

use of cn.taketoday.beans.factory.support.DefaultSingletonBeanRegistry in project today-infrastructure by TAKETODAY.

the class DefaultSingletonBeanRegistryTests method testSingletons.

@Test
public void testSingletons() {
    DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry();
    BeanMappingTestBean tb = new BeanMappingTestBean();
    beanRegistry.registerSingleton("tb", tb);
    assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb);
    BeanMappingTestBean tb2 = (BeanMappingTestBean) beanRegistry.getSingleton("tb2", BeanMappingTestBean::new);
    assertThat(beanRegistry.getSingleton("tb2")).isSameAs(tb2);
    assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb);
    assertThat(beanRegistry.getSingleton("tb2")).isSameAs(tb2);
    assertThat(beanRegistry.getSingletonCount()).isEqualTo(2);
    String[] names = beanRegistry.getSingletonNames();
    assertThat(names).hasSize(2).contains("tb", "tb2");
    assertThat(beanRegistry.getSingletonCount()).isEqualTo(2);
    assertThat(beanRegistry.getSingletonNames()).hasSize(2);
}
Also used : DefaultSingletonBeanRegistry(cn.taketoday.beans.factory.support.DefaultSingletonBeanRegistry) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultSingletonBeanRegistry

use of cn.taketoday.beans.factory.support.DefaultSingletonBeanRegistry in project today-framework by TAKETODAY.

the class DefaultSingletonBeanRegistryTests method testDisposableBean.

@Test
public void testDisposableBean() {
    DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry();
    DerivedTestBean tb = new DerivedTestBean();
    beanRegistry.registerSingleton("tb", tb);
    beanRegistry.registerDisposableBean("tb", tb);
    assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb);
    assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb);
    assertThat(beanRegistry.getSingletonCount()).isEqualTo(1);
    String[] names = beanRegistry.getSingletonNames();
    assertThat(names.length).isEqualTo(1);
    assertThat(names[0]).isEqualTo("tb");
    assertThat(tb.wasDestroyed()).isFalse();
    beanRegistry.destroySingletons();
    assertThat(beanRegistry.getSingletonCount()).isEqualTo(0);
    assertThat(beanRegistry.getSingletonNames().length).isEqualTo(0);
    assertThat(tb.wasDestroyed()).isTrue();
}
Also used : DefaultSingletonBeanRegistry(cn.taketoday.beans.factory.support.DefaultSingletonBeanRegistry) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultSingletonBeanRegistry

use of cn.taketoday.beans.factory.support.DefaultSingletonBeanRegistry in project today-framework by TAKETODAY.

the class DefaultSingletonBeanRegistryTests method testSingletons.

@Test
public void testSingletons() {
    DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry();
    BeanMappingTestBean tb = new BeanMappingTestBean();
    beanRegistry.registerSingleton("tb", tb);
    assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb);
    BeanMappingTestBean tb2 = (BeanMappingTestBean) beanRegistry.getSingleton("tb2", BeanMappingTestBean::new);
    assertThat(beanRegistry.getSingleton("tb2")).isSameAs(tb2);
    assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb);
    assertThat(beanRegistry.getSingleton("tb2")).isSameAs(tb2);
    assertThat(beanRegistry.getSingletonCount()).isEqualTo(2);
    String[] names = beanRegistry.getSingletonNames();
    assertThat(names).hasSize(2).contains("tb", "tb2");
    assertThat(beanRegistry.getSingletonCount()).isEqualTo(2);
    assertThat(beanRegistry.getSingletonNames()).hasSize(2);
}
Also used : DefaultSingletonBeanRegistry(cn.taketoday.beans.factory.support.DefaultSingletonBeanRegistry) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultSingletonBeanRegistry

use of cn.taketoday.beans.factory.support.DefaultSingletonBeanRegistry in project today-infrastructure by TAKETODAY.

the class DefaultSingletonBeanRegistryTests method testDisposableBean.

@Test
public void testDisposableBean() {
    DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry();
    DerivedTestBean tb = new DerivedTestBean();
    beanRegistry.registerSingleton("tb", tb);
    beanRegistry.registerDisposableBean("tb", tb);
    assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb);
    assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb);
    assertThat(beanRegistry.getSingletonCount()).isEqualTo(1);
    String[] names = beanRegistry.getSingletonNames();
    assertThat(names.length).isEqualTo(1);
    assertThat(names[0]).isEqualTo("tb");
    assertThat(tb.wasDestroyed()).isFalse();
    beanRegistry.destroySingletons();
    assertThat(beanRegistry.getSingletonCount()).isEqualTo(0);
    assertThat(beanRegistry.getSingletonNames().length).isEqualTo(0);
    assertThat(tb.wasDestroyed()).isTrue();
}
Also used : DefaultSingletonBeanRegistry(cn.taketoday.beans.factory.support.DefaultSingletonBeanRegistry) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultSingletonBeanRegistry (cn.taketoday.beans.factory.support.DefaultSingletonBeanRegistry)4 Test (org.junit.jupiter.api.Test)4 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)2