ApplicationListener beans get obtained on demand, supporting non-singletons as well; ApplicationListeners will be called in the order according to the Ordered contract; generified ApplicationListener interface
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,19 +16,16 @@
|
||||
|
||||
package org.springframework.context.event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.easymock.EasyMock;
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.Assert;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.FatalBeanException;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -37,6 +34,7 @@ import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.BeanThatBroadcasts;
|
||||
import org.springframework.context.BeanThatListens;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* Unit and integration tests for the ApplicationContext event support.
|
||||
@@ -46,70 +44,9 @@ import org.springframework.context.support.StaticApplicationContext;
|
||||
*/
|
||||
public class ApplicationContextEventTests {
|
||||
|
||||
private AbstractApplicationEventMulticaster getMulticaster() {
|
||||
return new AbstractApplicationEventMulticaster() {
|
||||
public void multicastEvent(ApplicationEvent event) {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multicasterNewCollectionClass() {
|
||||
AbstractApplicationEventMulticaster mc = getMulticaster();
|
||||
|
||||
mc.addApplicationListener(new NoOpApplicationListener());
|
||||
|
||||
mc.setCollectionClass(ArrayList.class);
|
||||
|
||||
assertEquals(1, mc.getApplicationListeners().size());
|
||||
assertEquals(ArrayList.class, mc.getApplicationListeners().getClass());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void multicasterInvalidCollectionClass_NotEvenACollectionType() {
|
||||
AbstractApplicationEventMulticaster mc = getMulticaster();
|
||||
mc.setCollectionClass(ApplicationContextEventTests.class);
|
||||
}
|
||||
|
||||
@Test(expected = FatalBeanException.class)
|
||||
public void multicasterInvalidCollectionClass_PassingAnInterfaceNotAConcreteClass() {
|
||||
AbstractApplicationEventMulticaster mc = getMulticaster();
|
||||
mc.setCollectionClass(List.class);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void multicasterNullCollectionClass() {
|
||||
AbstractApplicationEventMulticaster mc = getMulticaster();
|
||||
mc.setCollectionClass(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multicasterRemoveAll() {
|
||||
AbstractApplicationEventMulticaster mc = getMulticaster();
|
||||
mc.addApplicationListener(new NoOpApplicationListener());
|
||||
mc.removeAllListeners();
|
||||
|
||||
assertEquals(0, mc.getApplicationListeners().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multicasterRemoveOne() {
|
||||
AbstractApplicationEventMulticaster mc = getMulticaster();
|
||||
ApplicationListener one = new NoOpApplicationListener();
|
||||
ApplicationListener two = new NoOpApplicationListener();
|
||||
mc.addApplicationListener(one);
|
||||
mc.addApplicationListener(two);
|
||||
|
||||
mc.removeApplicationListener(one);
|
||||
|
||||
assertEquals(1, mc.getApplicationListeners().size());
|
||||
assertTrue("Remaining listener present", mc.getApplicationListeners().contains(two));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleApplicationEventMulticaster() {
|
||||
ApplicationListener listener = EasyMock.createMock(ApplicationListener.class);
|
||||
|
||||
ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());
|
||||
listener.onApplicationEvent(evt);
|
||||
|
||||
@@ -117,14 +54,25 @@ public class ApplicationContextEventTests {
|
||||
smc.addApplicationListener(listener);
|
||||
|
||||
replay(listener);
|
||||
|
||||
smc.multicastEvent(evt);
|
||||
|
||||
verify(listener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvenPublicationInterceptor() throws Throwable {
|
||||
public void orderedListeners() {
|
||||
MyOrderedListener1 listener1 = new MyOrderedListener1();
|
||||
MyOrderedListener2 listener2 = new MyOrderedListener2(listener1);
|
||||
|
||||
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
|
||||
smc.addApplicationListener(listener2);
|
||||
smc.addApplicationListener(listener1);
|
||||
|
||||
smc.multicastEvent(new MyEvent(this));
|
||||
smc.multicastEvent(new MyOtherEvent(this));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEventPublicationInterceptor() throws Throwable {
|
||||
MethodInvocation invocation = EasyMock.createMock(MethodInvocation.class);
|
||||
ApplicationContext ctx = EasyMock.createMock(ApplicationContext.class);
|
||||
|
||||
@@ -134,37 +82,15 @@ public class ApplicationContextEventTests {
|
||||
interceptor.afterPropertiesSet();
|
||||
|
||||
expect(invocation.proceed()).andReturn(new Object());
|
||||
|
||||
expect(invocation.getThis()).andReturn(new Object());
|
||||
|
||||
ctx.publishEvent(isA(MyEvent.class));
|
||||
|
||||
replay(invocation, ctx);
|
||||
|
||||
interceptor.invoke(invocation);
|
||||
|
||||
verify(invocation, ctx);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listenerAndBroadcasterWithUnresolvableCircularReference() {
|
||||
StaticApplicationContext context = new StaticApplicationContext();
|
||||
context.setDisplayName("listener context");
|
||||
context.registerBeanDefinition("broadcaster", new RootBeanDefinition(BeanThatBroadcasts.class));
|
||||
RootBeanDefinition listenerDef = new RootBeanDefinition(BeanThatListens.class);
|
||||
listenerDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("broadcaster"));
|
||||
context.registerBeanDefinition("listener", listenerDef);
|
||||
try {
|
||||
context.refresh();
|
||||
fail("Should have thrown BeanCreationException with nested BeanCurrentlyInCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
assertTrue(ex.contains(BeanCurrentlyInCreationException.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listenerAndBroadcasterWithResolvableCircularReference() {
|
||||
public void listenerAndBroadcasterWithCircularReference() {
|
||||
StaticApplicationContext context = new StaticApplicationContext();
|
||||
context.registerBeanDefinition("broadcaster", new RootBeanDefinition(BeanThatBroadcasts.class));
|
||||
RootBeanDefinition listenerDef = new RootBeanDefinition(BeanThatListens.class);
|
||||
@@ -177,6 +103,7 @@ public class ApplicationContextEventTests {
|
||||
Assert.assertEquals("The event was not received by the listener", 2, broadcaster.receivedCount);
|
||||
}
|
||||
|
||||
|
||||
public static class MyEvent extends ApplicationEvent {
|
||||
|
||||
public MyEvent(Object source) {
|
||||
@@ -184,11 +111,52 @@ public class ApplicationContextEventTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static final class NoOpApplicationListener implements ApplicationListener {
|
||||
|
||||
public static class MyOtherEvent extends ApplicationEvent {
|
||||
|
||||
public MyOtherEvent(Object source) {
|
||||
super(source);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyOrderedListener1 implements ApplicationListener, Ordered {
|
||||
|
||||
public final Set<ApplicationEvent> seenEvents = new HashSet<ApplicationEvent>();
|
||||
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
this.seenEvents.add(event);
|
||||
}
|
||||
|
||||
public int getOrder() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface MyOrderedListenerIfc<E extends ApplicationEvent> extends ApplicationListener<E>, Ordered {
|
||||
}
|
||||
|
||||
|
||||
public static abstract class MyOrderedListenerBase implements MyOrderedListenerIfc<MyEvent> {
|
||||
|
||||
public int getOrder() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyOrderedListener2 extends MyOrderedListenerBase {
|
||||
|
||||
private final MyOrderedListener1 otherListener;
|
||||
|
||||
public MyOrderedListener2(MyOrderedListener1 otherListener) {
|
||||
this.otherListener = otherListener;
|
||||
}
|
||||
|
||||
public void onApplicationEvent(MyEvent event) {
|
||||
assertTrue(otherListener.seenEvents.contains(event));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user