Search in sources :

Example 1 with ApplicationContextEvent

use of org.springframework.context.event.ApplicationContextEvent in project tephra by heisedebaise.

the class ContainerTest method contextClosed.

@Test
public void contextClosed() throws Exception {
    for (int i = 0; i < 2; i++) {
        ((ContainerImpl) container).onApplicationEvent(new ContextClosedEvent(getApplicationContext()));
        Assert.assertEquals(2 + i * 2, contextClosedList.size());
        for (int j = 0; j < contextRefreshedList.size(); j++) Assert.assertEquals(j % 2 + 1, numeric.toInt(contextClosedList.get(j)));
    }
    Field field = ContainerImpl.class.getDeclaredField("closedListeners");
    field.setAccessible(true);
    Object object = field.get(container);
    field.set(container, Optional.empty());
    for (int i = 0; i < 2; i++) {
        ((ContainerImpl) container).onApplicationEvent(new ContextClosedEvent(getApplicationContext()));
        Assert.assertEquals(4, contextClosedList.size());
        for (int j = 0; j < contextClosedList.size(); j++) Assert.assertEquals(j % 2 + 1, numeric.toInt(contextClosedList.get(j)));
    }
    ((ContainerImpl) container).onApplicationEvent(new ApplicationContextEvent(getApplicationContext()) {
    });
    Assert.assertEquals(4, contextClosedList.size());
    for (int i = 0; i < contextClosedList.size(); i++) Assert.assertEquals(i % 2 + 1, numeric.toInt(contextClosedList.get(i)));
    field.set(container, object);
}
Also used : Field(java.lang.reflect.Field) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) ApplicationContextEvent(org.springframework.context.event.ApplicationContextEvent) Test(org.junit.Test)

Example 2 with ApplicationContextEvent

use of org.springframework.context.event.ApplicationContextEvent in project tephra by heisedebaise.

the class ContainerTest method contextRefreshed.

@Test
public void contextRefreshed() throws Exception {
    Assert.assertEquals(2, contextRefreshedList.size());
    for (int i = 0; i < contextRefreshedList.size(); i++) Assert.assertEquals(i + 1, numeric.toInt(contextRefreshedList.get(i)));
    for (int i = 0; i < 2; i++) {
        ((ContainerImpl) container).onApplicationEvent(new ContextRefreshedEvent(getApplicationContext()));
        Assert.assertEquals(4 + i * 2, contextRefreshedList.size());
        for (int j = 0; j < contextRefreshedList.size(); j++) Assert.assertEquals(j % 2 + 1, numeric.toInt(contextRefreshedList.get(j)));
    }
    Field field = ContainerImpl.class.getDeclaredField("refreshedListeners");
    field.setAccessible(true);
    Object object = field.get(container);
    field.set(container, Optional.empty());
    for (int i = 0; i < 2; i++) {
        ((ContainerImpl) container).onApplicationEvent(new ContextRefreshedEvent(getApplicationContext()));
        Assert.assertEquals(6, contextRefreshedList.size());
        for (int j = 0; j < contextRefreshedList.size(); j++) Assert.assertEquals(j % 2 + 1, numeric.toInt(contextRefreshedList.get(j)));
    }
    ((ContainerImpl) container).onApplicationEvent(new ApplicationContextEvent(getApplicationContext()) {
    });
    Assert.assertEquals(6, contextRefreshedList.size());
    for (int i = 0; i < contextRefreshedList.size(); i++) Assert.assertEquals(i % 2 + 1, numeric.toInt(contextRefreshedList.get(i)));
    field.set(container, object);
}
Also used : Field(java.lang.reflect.Field) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) ApplicationContextEvent(org.springframework.context.event.ApplicationContextEvent) Test(org.junit.Test)

Aggregations

Field (java.lang.reflect.Field)2 Test (org.junit.Test)2 ApplicationContextEvent (org.springframework.context.event.ApplicationContextEvent)2 ContextClosedEvent (org.springframework.context.event.ContextClosedEvent)1 ContextRefreshedEvent (org.springframework.context.event.ContextRefreshedEvent)1