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