Browse Source

Polishing

pull/1257/head
Juergen Hoeller 10 years ago
parent
commit
db1a84ede1
  1. 132
      spring-context/src/test/java/org/springframework/context/event/ApplicationListenerMethodAdapterTests.java

132
spring-context/src/test/java/org/springframework/context/event/ApplicationListenerMethodAdapterTests.java

@ -50,52 +50,48 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
private final ApplicationContext context = mock(ApplicationContext.class); private final ApplicationContext context = mock(ApplicationContext.class);
@Test @Test
public void rawListener() { public void rawListener() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleRaw", ApplicationEvent.class);
"handleRaw", ApplicationEvent.class);
supportsEventType(true, method, getGenericApplicationEventType("applicationEvent")); supportsEventType(true, method, getGenericApplicationEventType("applicationEvent"));
} }
@Test @Test
public void rawListenerWithGenericEvent() { public void rawListenerWithGenericEvent() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleRaw", ApplicationEvent.class);
"handleRaw", ApplicationEvent.class);
supportsEventType(true, method, getGenericApplicationEventType("stringEvent")); supportsEventType(true, method, getGenericApplicationEventType("stringEvent"));
} }
@Test @Test
public void genericListener() { public void genericListener() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"handleGenericString", GenericTestEvent.class); SampleEvents.class, "handleGenericString", GenericTestEvent.class);
supportsEventType(true, method, getGenericApplicationEventType("stringEvent")); supportsEventType(true, method, getGenericApplicationEventType("stringEvent"));
} }
@Test @Test
public void genericListenerWrongParameterizedType() { public void genericListenerWrongParameterizedType() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"handleGenericString", GenericTestEvent.class); SampleEvents.class, "handleGenericString", GenericTestEvent.class);
supportsEventType(false, method, getGenericApplicationEventType("longEvent")); supportsEventType(false, method, getGenericApplicationEventType("longEvent"));
} }
@Test @Test
public void listenerWithPayloadAndGenericInformation() { public void listenerWithPayloadAndGenericInformation() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
"handleString", String.class);
supportsEventType(true, method, createGenericEventType(String.class)); supportsEventType(true, method, createGenericEventType(String.class));
} }
@Test @Test
public void listenerWithInvalidPayloadAndGenericInformation() { public void listenerWithInvalidPayloadAndGenericInformation() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
"handleString", String.class);
supportsEventType(false, method, createGenericEventType(Integer.class)); supportsEventType(false, method, createGenericEventType(Integer.class));
} }
@Test @Test
public void listenerWithPayloadTypeErasure() { // Always accept such event when the type is unknown public void listenerWithPayloadTypeErasure() { // Always accept such event when the type is unknown
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
"handleString", String.class);
supportsEventType(true, method, ResolvableType.forClass(PayloadApplicationEvent.class)); supportsEventType(true, method, ResolvableType.forClass(PayloadApplicationEvent.class));
} }
@ -108,36 +104,32 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
@Test @Test
public void listenerWithSubTypeSeveralGenericsResolved() { public void listenerWithSubTypeSeveralGenericsResolved() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
"handleString", String.class);
supportsEventType(true, method, ResolvableType.forClass(PayloadStringTestEvent.class)); supportsEventType(true, method, ResolvableType.forClass(PayloadStringTestEvent.class));
} }
@Test @Test
public void listenerWithAnnotationValue() { public void listenerWithAnnotationValue() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringAnnotationValue");
"handleStringAnnotationValue");
supportsEventType(true, method, createGenericEventType(String.class)); supportsEventType(true, method, createGenericEventType(String.class));
} }
@Test @Test
public void listenerWithAnnotationClasses() { public void listenerWithAnnotationClasses() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringAnnotationClasses");
"handleStringAnnotationClasses");
supportsEventType(true, method, createGenericEventType(String.class)); supportsEventType(true, method, createGenericEventType(String.class));
} }
@Test @Test
public void listenerWithAnnotationValueAndParameter() { public void listenerWithAnnotationValueAndParameter() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"handleStringAnnotationValueAndParameter", String.class); SampleEvents.class, "handleStringAnnotationValueAndParameter", String.class);
supportsEventType(true, method, createGenericEventType(String.class)); supportsEventType(true, method, createGenericEventType(String.class));
} }
@Test @Test
public void listenerWithSeveralTypes() { public void listenerWithSeveralTypes() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringOrInteger");
"handleStringOrInteger");
supportsEventType(true, method, createGenericEventType(String.class)); supportsEventType(true, method, createGenericEventType(String.class));
supportsEventType(true, method, createGenericEventType(Integer.class)); supportsEventType(true, method, createGenericEventType(Integer.class));
supportsEventType(false, method, createGenericEventType(Double.class)); supportsEventType(false, method, createGenericEventType(Double.class));
@ -145,51 +137,47 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
@Test @Test
public void listenerWithTooManyParameters() { public void listenerWithTooManyParameters() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"tooManyParameters", String.class, String.class); SampleEvents.class, "tooManyParameters", String.class, String.class);
this.thrown.expect(IllegalStateException.class); this.thrown.expect(IllegalStateException.class);
createTestInstance(method); createTestInstance(method);
} }
@Test @Test
public void listenerWithNoParameter() { public void listenerWithNoParameter() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "noParameter");
"noParameter");
this.thrown.expect(IllegalStateException.class); this.thrown.expect(IllegalStateException.class);
createTestInstance(method); createTestInstance(method);
} }
@Test @Test
public void listenerWithMoreThanOneParameter() { public void listenerWithMoreThanOneParameter() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"moreThanOneParameter", String.class, Integer.class); SampleEvents.class, "moreThanOneParameter", String.class, Integer.class);
this.thrown.expect(IllegalStateException.class); this.thrown.expect(IllegalStateException.class);
createTestInstance(method); createTestInstance(method);
} }
@Test @Test
public void defaultOrder() { public void defaultOrder() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"handleGenericString", GenericTestEvent.class); SampleEvents.class, "handleGenericString", GenericTestEvent.class);
ApplicationListenerMethodAdapter adapter = createTestInstance(method); ApplicationListenerMethodAdapter adapter = createTestInstance(method);
assertEquals(0, adapter.getOrder()); assertEquals(0, adapter.getOrder());
} }
@Test @Test
public void specifiedOrder() { public void specifiedOrder() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"handleRaw", ApplicationEvent.class); SampleEvents.class, "handleRaw", ApplicationEvent.class);
ApplicationListenerMethodAdapter adapter = createTestInstance(method); ApplicationListenerMethodAdapter adapter = createTestInstance(method);
assertEquals(42, adapter.getOrder()); assertEquals(42, adapter.getOrder());
} }
@Test @Test
public void invokeListener() { public void invokeListener() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"handleGenericString", GenericTestEvent.class); SampleEvents.class, "handleGenericString", GenericTestEvent.class);
GenericTestEvent<String> event = createGenericTestEvent("test"); GenericTestEvent<String> event = createGenericTestEvent("test");
invokeListener(method, event); invokeListener(method, event);
verify(this.sampleEvents, times(1)).handleGenericString(event); verify(this.sampleEvents, times(1)).handleGenericString(event);
@ -197,8 +185,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
@Test @Test
public void invokeListenerWithGenericEvent() { public void invokeListenerWithGenericEvent() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"handleGenericString", GenericTestEvent.class); SampleEvents.class, "handleGenericString", GenericTestEvent.class);
GenericTestEvent<String> event = new SmartGenericTestEvent<>(this, "test"); GenericTestEvent<String> event = new SmartGenericTestEvent<>(this, "test");
invokeListener(method, event); invokeListener(method, event);
verify(this.sampleEvents, times(1)).handleGenericString(event); verify(this.sampleEvents, times(1)).handleGenericString(event);
@ -206,8 +194,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
@Test @Test
public void invokeListenerWithGenericPayload() { public void invokeListenerWithGenericPayload() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"handleGenericStringPayload", EntityWrapper.class); SampleEvents.class, "handleGenericStringPayload", EntityWrapper.class);
EntityWrapper<String> payload = new EntityWrapper<>("test"); EntityWrapper<String> payload = new EntityWrapper<>("test");
invokeListener(method, new PayloadApplicationEvent<>(this, payload)); invokeListener(method, new PayloadApplicationEvent<>(this, payload));
verify(this.sampleEvents, times(1)).handleGenericStringPayload(payload); verify(this.sampleEvents, times(1)).handleGenericStringPayload(payload);
@ -215,8 +203,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
@Test @Test
public void invokeListenerWithWrongGenericPayload() { public void invokeListenerWithWrongGenericPayload() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod
"handleGenericStringPayload", EntityWrapper.class); (SampleEvents.class, "handleGenericStringPayload", EntityWrapper.class);
EntityWrapper<Integer> payload = new EntityWrapper<>(123); EntityWrapper<Integer> payload = new EntityWrapper<>(123);
invokeListener(method, new PayloadApplicationEvent<>(this, payload)); invokeListener(method, new PayloadApplicationEvent<>(this, payload));
verify(this.sampleEvents, times(0)).handleGenericStringPayload(any()); verify(this.sampleEvents, times(0)).handleGenericStringPayload(any());
@ -224,8 +212,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
@Test @Test
public void invokeListenerWithAnyGenericPayload() { public void invokeListenerWithAnyGenericPayload() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"handleGenericAnyPayload", EntityWrapper.class); SampleEvents.class, "handleGenericAnyPayload", EntityWrapper.class);
EntityWrapper<String> payload = new EntityWrapper<>("test"); EntityWrapper<String> payload = new EntityWrapper<>("test");
invokeListener(method, new PayloadApplicationEvent<>(this, payload)); invokeListener(method, new PayloadApplicationEvent<>(this, payload));
verify(this.sampleEvents, times(1)).handleGenericAnyPayload(payload); verify(this.sampleEvents, times(1)).handleGenericAnyPayload(payload);
@ -233,20 +221,20 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
@Test @Test
public void invokeListenerRuntimeException() { public void invokeListenerRuntimeException() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"generateRuntimeException", GenericTestEvent.class); SampleEvents.class, "generateRuntimeException", GenericTestEvent.class);
GenericTestEvent<String> event = createGenericTestEvent("fail"); GenericTestEvent<String> event = createGenericTestEvent("fail");
this.thrown.expect(IllegalStateException.class); this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Test exception"); this.thrown.expectMessage("Test exception");
this.thrown.expectCause(is(isNull(Throwable.class))); this.thrown.expectCause(is((Throwable) isNull()));
invokeListener(method, event); invokeListener(method, event);
} }
@Test @Test
public void invokeListenerCheckedException() { public void invokeListenerCheckedException() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"generateCheckedException", GenericTestEvent.class); SampleEvents.class, "generateCheckedException", GenericTestEvent.class);
GenericTestEvent<String> event = createGenericTestEvent("fail"); GenericTestEvent<String> event = createGenericTestEvent("fail");
this.thrown.expect(UndeclaredThrowableException.class); this.thrown.expect(UndeclaredThrowableException.class);
@ -262,7 +250,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
proxyFactory.addInterface(SimpleService.class); proxyFactory.addInterface(SimpleService.class);
Object bean = proxyFactory.getProxy(getClass().getClassLoader()); Object bean = proxyFactory.getProxy(getClass().getClassLoader());
Method method = ReflectionUtils.findMethod(InvalidProxyTestBean.class, "handleIt2", ApplicationEvent.class); Method method = ReflectionUtils.findMethod(
InvalidProxyTestBean.class, "handleIt2", ApplicationEvent.class);
StaticApplicationListenerMethodAdapter listener = StaticApplicationListenerMethodAdapter listener =
new StaticApplicationListenerMethodAdapter(method, bean); new StaticApplicationListenerMethodAdapter(method, bean);
this.thrown.expect(IllegalStateException.class); this.thrown.expect(IllegalStateException.class);
@ -272,8 +261,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
@Test @Test
public void invokeListenerWithPayload() { public void invokeListenerWithPayload() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
"handleString", String.class);
PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test"); PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
invokeListener(method, event); invokeListener(method, event);
verify(this.sampleEvents, times(1)).handleString("test"); verify(this.sampleEvents, times(1)).handleString("test");
@ -281,8 +269,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
@Test @Test
public void invokeListenerWithPayloadWrongType() { public void invokeListenerWithPayloadWrongType() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
"handleString", String.class);
PayloadApplicationEvent<Long> event = new PayloadApplicationEvent<>(this, 123L); PayloadApplicationEvent<Long> event = new PayloadApplicationEvent<>(this, 123L);
invokeListener(method, event); invokeListener(method, event);
verify(this.sampleEvents, never()).handleString(anyString()); verify(this.sampleEvents, never()).handleString(anyString());
@ -290,8 +277,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
@Test @Test
public void invokeListenerWithAnnotationValue() { public void invokeListenerWithAnnotationValue() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringAnnotationClasses");
"handleStringAnnotationClasses");
PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test"); PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
invokeListener(method, event); invokeListener(method, event);
verify(this.sampleEvents, times(1)).handleStringAnnotationClasses(); verify(this.sampleEvents, times(1)).handleStringAnnotationClasses();
@ -299,8 +285,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
@Test @Test
public void invokeListenerWithAnnotationValueAndParameter() { public void invokeListenerWithAnnotationValueAndParameter() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"handleStringAnnotationValueAndParameter", String.class); SampleEvents.class, "handleStringAnnotationValueAndParameter", String.class);
PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test"); PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
invokeListener(method, event); invokeListener(method, event);
verify(this.sampleEvents, times(1)).handleStringAnnotationValueAndParameter("test"); verify(this.sampleEvents, times(1)).handleStringAnnotationValueAndParameter("test");
@ -308,8 +294,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
@Test @Test
public void invokeListenerWithSeveralTypes() { public void invokeListenerWithSeveralTypes() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringOrInteger");
"handleStringOrInteger");
PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test"); PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
invokeListener(method, event); invokeListener(method, event);
verify(this.sampleEvents, times(1)).handleStringOrInteger(); verify(this.sampleEvents, times(1)).handleStringOrInteger();
@ -321,11 +306,10 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
verify(this.sampleEvents, times(2)).handleStringOrInteger(); verify(this.sampleEvents, times(2)).handleStringOrInteger();
} }
@Test @Test
public void beanInstanceRetrievedAtEveryInvocation() { public void beanInstanceRetrievedAtEveryInvocation() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, Method method = ReflectionUtils.findMethod(
"handleGenericString", GenericTestEvent.class); SampleEvents.class, "handleGenericString", GenericTestEvent.class);
when(this.context.getBean("testBean")).thenReturn(this.sampleEvents); when(this.context.getBean("testBean")).thenReturn(this.sampleEvents);
ApplicationListenerMethodAdapter listener = new ApplicationListenerMethodAdapter( ApplicationListenerMethodAdapter listener = new ApplicationListenerMethodAdapter(
"testBean", GenericTestEvent.class, method); "testBean", GenericTestEvent.class, method);
@ -342,6 +326,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
verify(this.context, times(2)).getBean("testBean"); verify(this.context, times(2)).getBean("testBean");
} }
private void supportsEventType(boolean match, Method method, ResolvableType eventType) { private void supportsEventType(boolean match, Method method, ResolvableType eventType) {
ApplicationListenerMethodAdapter adapter = createTestInstance(method); ApplicationListenerMethodAdapter adapter = createTestInstance(method);
assertEquals("Wrong match for event '" + eventType + "' on " + method, assertEquals("Wrong match for event '" + eventType + "' on " + method,
@ -361,8 +346,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
return ResolvableType.forClassWithGenerics(PayloadApplicationEvent.class, payloadType); return ResolvableType.forClassWithGenerics(PayloadApplicationEvent.class, payloadType);
} }
private static class StaticApplicationListenerMethodAdapter
extends ApplicationListenerMethodAdapter { private static class StaticApplicationListenerMethodAdapter extends ApplicationListenerMethodAdapter {
private final Object targetBean; private final Object targetBean;
@ -380,7 +365,6 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
private static class SampleEvents { private static class SampleEvents {
@EventListener @EventListener
@Order(42) @Order(42)
public void handleRaw(ApplicationEvent event) { public void handleRaw(ApplicationEvent event) {
@ -449,13 +433,15 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
} }
} }
interface SimpleService { interface SimpleService {
void handleIt(ApplicationEvent event); void handleIt(ApplicationEvent event);
} }
private static class EntityWrapper<T> implements ResolvableTypeProvider { private static class EntityWrapper<T> implements ResolvableTypeProvider {
private final T entity; private final T entity;
public EntityWrapper(T entity) { public EntityWrapper(T entity) {
@ -468,6 +454,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
} }
} }
static class InvalidProxyTestBean implements SimpleService { static class InvalidProxyTestBean implements SimpleService {
@Override @Override
@ -479,6 +466,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
} }
} }
@SuppressWarnings({"unused", "serial"}) @SuppressWarnings({"unused", "serial"})
static class PayloadTestEvent<V, T> extends PayloadApplicationEvent<T> { static class PayloadTestEvent<V, T> extends PayloadApplicationEvent<T> {
@ -490,8 +478,10 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
} }
} }
@SuppressWarnings({ "serial" }) @SuppressWarnings({ "serial" })
static class PayloadStringTestEvent extends PayloadTestEvent<Long, String> { static class PayloadStringTestEvent extends PayloadTestEvent<Long, String> {
public PayloadStringTestEvent(Object source, String payload, Long something) { public PayloadStringTestEvent(Object source, String payload, Long something) {
super(source, payload, something); super(source, payload, something);
} }

Loading…
Cancel
Save