Search in sources :

Example 1 with BeanThatListens

use of org.springframework.context.testfixture.beans.BeanThatListens in project spring-framework by spring-projects.

the class ApplicationContextEventTests method initMethodPublishesEvent.

@Test
public void initMethodPublishesEvent() {
    GenericApplicationContext context = new GenericApplicationContext();
    context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
    context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
    context.registerBeanDefinition("initMethod", new RootBeanDefinition(EventPublishingInitMethod.class));
    context.refresh();
    context.publishEvent(new MyEvent(this));
    BeanThatListens listener = context.getBean(BeanThatListens.class);
    assertThat(listener.getEventCount()).isEqualTo(3);
    context.close();
}
Also used : BeanThatListens(org.springframework.context.testfixture.beans.BeanThatListens) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) StaticMessageSource(org.springframework.context.support.StaticMessageSource) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 2 with BeanThatListens

use of org.springframework.context.testfixture.beans.BeanThatListens in project spring-framework by spring-projects.

the class ApplicationContextEventTests method innerBeanAsListener.

@Test
public void innerBeanAsListener() {
    StaticApplicationContext context = new StaticApplicationContext();
    RootBeanDefinition listenerDef = new RootBeanDefinition(TestBean.class);
    listenerDef.getPropertyValues().add("friends", new RootBeanDefinition(BeanThatListens.class));
    context.registerBeanDefinition("listener", listenerDef);
    context.refresh();
    context.publishEvent(new MyEvent(this));
    context.publishEvent(new MyEvent(this));
    TestBean listener = context.getBean(TestBean.class);
    assertThat(((BeanThatListens) listener.getFriends().iterator().next()).getEventCount()).isEqualTo(3);
    context.close();
}
Also used : BeanThatListens(org.springframework.context.testfixture.beans.BeanThatListens) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) TestBean(org.springframework.beans.testfixture.beans.TestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with BeanThatListens

use of org.springframework.context.testfixture.beans.BeanThatListens in project spring-framework by spring-projects.

the class ApplicationContextEventTests method initMethodPublishesAsyncEvent.

@Test
public void initMethodPublishesAsyncEvent() {
    GenericApplicationContext context = new GenericApplicationContext();
    context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
    context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
    context.registerBeanDefinition("initMethod", new RootBeanDefinition(AsyncEventPublishingInitMethod.class));
    context.refresh();
    context.publishEvent(new MyEvent(this));
    BeanThatListens listener = context.getBean(BeanThatListens.class);
    assertThat(listener.getEventCount()).isEqualTo(3);
    context.close();
}
Also used : BeanThatListens(org.springframework.context.testfixture.beans.BeanThatListens) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) StaticMessageSource(org.springframework.context.support.StaticMessageSource) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 4 with BeanThatListens

use of org.springframework.context.testfixture.beans.BeanThatListens in project spring-framework by spring-projects.

the class ApplicationContextEventTests method beanPostProcessorPublishesEvents.

@Test
public void beanPostProcessorPublishesEvents() {
    GenericApplicationContext context = new GenericApplicationContext();
    context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
    context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
    context.registerBeanDefinition("postProcessor", new RootBeanDefinition(EventPublishingBeanPostProcessor.class));
    context.refresh();
    context.publishEvent(new MyEvent(this));
    BeanThatListens listener = context.getBean(BeanThatListens.class);
    assertThat(listener.getEventCount()).isEqualTo(4);
    context.close();
}
Also used : BeanThatListens(org.springframework.context.testfixture.beans.BeanThatListens) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) StaticMessageSource(org.springframework.context.support.StaticMessageSource) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 5 with BeanThatListens

use of org.springframework.context.testfixture.beans.BeanThatListens in project spring-framework by spring-projects.

the class AbstractApplicationContextTests method beanAutomaticallyHearsEvents.

@Test
public void beanAutomaticallyHearsEvents() throws Exception {
    // String[] listenerNames = ((ListableBeanFactory) applicationContext).getBeanDefinitionNames(ApplicationListener.class);
    // assertTrue("listeners include beanThatListens", Arrays.asList(listenerNames).contains("beanThatListens"));
    BeanThatListens b = (BeanThatListens) applicationContext.getBean("beanThatListens");
    b.zero();
    assertThat(b.getEventCount() == 0).as("0 events before publication").isTrue();
    this.applicationContext.publishEvent(new MyEvent(this));
    assertThat(b.getEventCount() == 1).as("1 events after publication, not " + b.getEventCount()).isTrue();
}
Also used : BeanThatListens(org.springframework.context.testfixture.beans.BeanThatListens) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)5 BeanThatListens (org.springframework.context.testfixture.beans.BeanThatListens)5 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)4 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)3 StaticMessageSource (org.springframework.context.support.StaticMessageSource)3 TestBean (org.springframework.beans.testfixture.beans.TestBean)1 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)1