Skip to content

Commit db1a84e

Browse files
committed
Polishing
1 parent 3d2e4c3 commit db1a84e

File tree

1 file changed

+61
-71
lines changed

1 file changed

+61
-71
lines changed

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

Lines changed: 61 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -50,52 +50,48 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
5050

5151
private final ApplicationContext context = mock(ApplicationContext.class);
5252

53+
5354
@Test
5455
public void rawListener() {
55-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
56-
"handleRaw", ApplicationEvent.class);
56+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleRaw", ApplicationEvent.class);
5757
supportsEventType(true, method, getGenericApplicationEventType("applicationEvent"));
5858
}
5959

6060
@Test
6161
public void rawListenerWithGenericEvent() {
62-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
63-
"handleRaw", ApplicationEvent.class);
62+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleRaw", ApplicationEvent.class);
6463
supportsEventType(true, method, getGenericApplicationEventType("stringEvent"));
6564
}
6665

6766
@Test
6867
public void genericListener() {
69-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
70-
"handleGenericString", GenericTestEvent.class);
68+
Method method = ReflectionUtils.findMethod(
69+
SampleEvents.class, "handleGenericString", GenericTestEvent.class);
7170
supportsEventType(true, method, getGenericApplicationEventType("stringEvent"));
7271
}
7372

7473
@Test
7574
public void genericListenerWrongParameterizedType() {
76-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
77-
"handleGenericString", GenericTestEvent.class);
75+
Method method = ReflectionUtils.findMethod(
76+
SampleEvents.class, "handleGenericString", GenericTestEvent.class);
7877
supportsEventType(false, method, getGenericApplicationEventType("longEvent"));
7978
}
8079

8180
@Test
8281
public void listenerWithPayloadAndGenericInformation() {
83-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
84-
"handleString", String.class);
82+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
8583
supportsEventType(true, method, createGenericEventType(String.class));
8684
}
8785

8886
@Test
8987
public void listenerWithInvalidPayloadAndGenericInformation() {
90-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
91-
"handleString", String.class);
88+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
9289
supportsEventType(false, method, createGenericEventType(Integer.class));
9390
}
9491

9592
@Test
96-
public void listenerWithPayloadTypeErasure() { // Always accept such event when the type is unknown
97-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
98-
"handleString", String.class);
93+
public void listenerWithPayloadTypeErasure() { // Always accept such event when the type is unknown
94+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
9995
supportsEventType(true, method, ResolvableType.forClass(PayloadApplicationEvent.class));
10096
}
10197

@@ -108,145 +104,137 @@ public void listenerWithSubTypeSeveralGenerics() {
108104

109105
@Test
110106
public void listenerWithSubTypeSeveralGenericsResolved() {
111-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
112-
"handleString", String.class);
107+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
113108
supportsEventType(true, method, ResolvableType.forClass(PayloadStringTestEvent.class));
114109
}
115110

116111
@Test
117112
public void listenerWithAnnotationValue() {
118-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
119-
"handleStringAnnotationValue");
113+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringAnnotationValue");
120114
supportsEventType(true, method, createGenericEventType(String.class));
121115
}
122116

123117
@Test
124118
public void listenerWithAnnotationClasses() {
125-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
126-
"handleStringAnnotationClasses");
119+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringAnnotationClasses");
127120
supportsEventType(true, method, createGenericEventType(String.class));
128121
}
129122

130123
@Test
131124
public void listenerWithAnnotationValueAndParameter() {
132-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
133-
"handleStringAnnotationValueAndParameter", String.class);
125+
Method method = ReflectionUtils.findMethod(
126+
SampleEvents.class, "handleStringAnnotationValueAndParameter", String.class);
134127
supportsEventType(true, method, createGenericEventType(String.class));
135128
}
136129

137130
@Test
138131
public void listenerWithSeveralTypes() {
139-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
140-
"handleStringOrInteger");
132+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringOrInteger");
141133
supportsEventType(true, method, createGenericEventType(String.class));
142134
supportsEventType(true, method, createGenericEventType(Integer.class));
143135
supportsEventType(false, method, createGenericEventType(Double.class));
144136
}
145137

146138
@Test
147139
public void listenerWithTooManyParameters() {
148-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
149-
"tooManyParameters", String.class, String.class);
150-
140+
Method method = ReflectionUtils.findMethod(
141+
SampleEvents.class, "tooManyParameters", String.class, String.class);
151142
this.thrown.expect(IllegalStateException.class);
152143
createTestInstance(method);
153144
}
154145

155146
@Test
156147
public void listenerWithNoParameter() {
157-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
158-
"noParameter");
159-
148+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "noParameter");
160149
this.thrown.expect(IllegalStateException.class);
161150
createTestInstance(method);
162151
}
163152

164153
@Test
165154
public void listenerWithMoreThanOneParameter() {
166-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
167-
"moreThanOneParameter", String.class, Integer.class);
168-
155+
Method method = ReflectionUtils.findMethod(
156+
SampleEvents.class, "moreThanOneParameter", String.class, Integer.class);
169157
this.thrown.expect(IllegalStateException.class);
170158
createTestInstance(method);
171159
}
172160

173161
@Test
174162
public void defaultOrder() {
175-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
176-
"handleGenericString", GenericTestEvent.class);
163+
Method method = ReflectionUtils.findMethod(
164+
SampleEvents.class, "handleGenericString", GenericTestEvent.class);
177165
ApplicationListenerMethodAdapter adapter = createTestInstance(method);
178166
assertEquals(0, adapter.getOrder());
179167
}
180168

181169
@Test
182170
public void specifiedOrder() {
183-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
184-
"handleRaw", ApplicationEvent.class);
171+
Method method = ReflectionUtils.findMethod(
172+
SampleEvents.class, "handleRaw", ApplicationEvent.class);
185173
ApplicationListenerMethodAdapter adapter = createTestInstance(method);
186174
assertEquals(42, adapter.getOrder());
187175
}
188176

189177
@Test
190178
public void invokeListener() {
191-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
192-
"handleGenericString", GenericTestEvent.class);
179+
Method method = ReflectionUtils.findMethod(
180+
SampleEvents.class, "handleGenericString", GenericTestEvent.class);
193181
GenericTestEvent<String> event = createGenericTestEvent("test");
194182
invokeListener(method, event);
195183
verify(this.sampleEvents, times(1)).handleGenericString(event);
196184
}
197185

198186
@Test
199187
public void invokeListenerWithGenericEvent() {
200-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
201-
"handleGenericString", GenericTestEvent.class);
188+
Method method = ReflectionUtils.findMethod(
189+
SampleEvents.class, "handleGenericString", GenericTestEvent.class);
202190
GenericTestEvent<String> event = new SmartGenericTestEvent<>(this, "test");
203191
invokeListener(method, event);
204192
verify(this.sampleEvents, times(1)).handleGenericString(event);
205193
}
206194

207195
@Test
208196
public void invokeListenerWithGenericPayload() {
209-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
210-
"handleGenericStringPayload", EntityWrapper.class);
197+
Method method = ReflectionUtils.findMethod(
198+
SampleEvents.class, "handleGenericStringPayload", EntityWrapper.class);
211199
EntityWrapper<String> payload = new EntityWrapper<>("test");
212200
invokeListener(method, new PayloadApplicationEvent<>(this, payload));
213201
verify(this.sampleEvents, times(1)).handleGenericStringPayload(payload);
214202
}
215203

216204
@Test
217205
public void invokeListenerWithWrongGenericPayload() {
218-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
219-
"handleGenericStringPayload", EntityWrapper.class);
206+
Method method = ReflectionUtils.findMethod
207+
(SampleEvents.class, "handleGenericStringPayload", EntityWrapper.class);
220208
EntityWrapper<Integer> payload = new EntityWrapper<>(123);
221209
invokeListener(method, new PayloadApplicationEvent<>(this, payload));
222210
verify(this.sampleEvents, times(0)).handleGenericStringPayload(any());
223211
}
224212

225213
@Test
226214
public void invokeListenerWithAnyGenericPayload() {
227-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
228-
"handleGenericAnyPayload", EntityWrapper.class);
215+
Method method = ReflectionUtils.findMethod(
216+
SampleEvents.class, "handleGenericAnyPayload", EntityWrapper.class);
229217
EntityWrapper<String> payload = new EntityWrapper<>("test");
230218
invokeListener(method, new PayloadApplicationEvent<>(this, payload));
231219
verify(this.sampleEvents, times(1)).handleGenericAnyPayload(payload);
232220
}
233221

234222
@Test
235223
public void invokeListenerRuntimeException() {
236-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
237-
"generateRuntimeException", GenericTestEvent.class);
224+
Method method = ReflectionUtils.findMethod(
225+
SampleEvents.class, "generateRuntimeException", GenericTestEvent.class);
238226
GenericTestEvent<String> event = createGenericTestEvent("fail");
239227

240228
this.thrown.expect(IllegalStateException.class);
241229
this.thrown.expectMessage("Test exception");
242-
this.thrown.expectCause(is(isNull(Throwable.class)));
230+
this.thrown.expectCause(is((Throwable) isNull()));
243231
invokeListener(method, event);
244232
}
245233

246234
@Test
247235
public void invokeListenerCheckedException() {
248-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
249-
"generateCheckedException", GenericTestEvent.class);
236+
Method method = ReflectionUtils.findMethod(
237+
SampleEvents.class, "generateCheckedException", GenericTestEvent.class);
250238
GenericTestEvent<String> event = createGenericTestEvent("fail");
251239

252240
this.thrown.expect(UndeclaredThrowableException.class);
@@ -262,7 +250,8 @@ public void invokeListenerInvalidProxy() {
262250
proxyFactory.addInterface(SimpleService.class);
263251
Object bean = proxyFactory.getProxy(getClass().getClassLoader());
264252

265-
Method method = ReflectionUtils.findMethod(InvalidProxyTestBean.class, "handleIt2", ApplicationEvent.class);
253+
Method method = ReflectionUtils.findMethod(
254+
InvalidProxyTestBean.class, "handleIt2", ApplicationEvent.class);
266255
StaticApplicationListenerMethodAdapter listener =
267256
new StaticApplicationListenerMethodAdapter(method, bean);
268257
this.thrown.expect(IllegalStateException.class);
@@ -272,44 +261,40 @@ public void invokeListenerInvalidProxy() {
272261

273262
@Test
274263
public void invokeListenerWithPayload() {
275-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
276-
"handleString", String.class);
264+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
277265
PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
278266
invokeListener(method, event);
279267
verify(this.sampleEvents, times(1)).handleString("test");
280268
}
281269

282270
@Test
283271
public void invokeListenerWithPayloadWrongType() {
284-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
285-
"handleString", String.class);
272+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
286273
PayloadApplicationEvent<Long> event = new PayloadApplicationEvent<>(this, 123L);
287274
invokeListener(method, event);
288275
verify(this.sampleEvents, never()).handleString(anyString());
289276
}
290277

291278
@Test
292279
public void invokeListenerWithAnnotationValue() {
293-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
294-
"handleStringAnnotationClasses");
280+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringAnnotationClasses");
295281
PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
296282
invokeListener(method, event);
297283
verify(this.sampleEvents, times(1)).handleStringAnnotationClasses();
298284
}
299285

300286
@Test
301287
public void invokeListenerWithAnnotationValueAndParameter() {
302-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
303-
"handleStringAnnotationValueAndParameter", String.class);
288+
Method method = ReflectionUtils.findMethod(
289+
SampleEvents.class, "handleStringAnnotationValueAndParameter", String.class);
304290
PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
305291
invokeListener(method, event);
306292
verify(this.sampleEvents, times(1)).handleStringAnnotationValueAndParameter("test");
307293
}
308294

309295
@Test
310296
public void invokeListenerWithSeveralTypes() {
311-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
312-
"handleStringOrInteger");
297+
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringOrInteger");
313298
PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
314299
invokeListener(method, event);
315300
verify(this.sampleEvents, times(1)).handleStringOrInteger();
@@ -321,11 +306,10 @@ public void invokeListenerWithSeveralTypes() {
321306
verify(this.sampleEvents, times(2)).handleStringOrInteger();
322307
}
323308

324-
325309
@Test
326310
public void beanInstanceRetrievedAtEveryInvocation() {
327-
Method method = ReflectionUtils.findMethod(SampleEvents.class,
328-
"handleGenericString", GenericTestEvent.class);
311+
Method method = ReflectionUtils.findMethod(
312+
SampleEvents.class, "handleGenericString", GenericTestEvent.class);
329313
when(this.context.getBean("testBean")).thenReturn(this.sampleEvents);
330314
ApplicationListenerMethodAdapter listener = new ApplicationListenerMethodAdapter(
331315
"testBean", GenericTestEvent.class, method);
@@ -342,6 +326,7 @@ public void beanInstanceRetrievedAtEveryInvocation() {
342326
verify(this.context, times(2)).getBean("testBean");
343327
}
344328

329+
345330
private void supportsEventType(boolean match, Method method, ResolvableType eventType) {
346331
ApplicationListenerMethodAdapter adapter = createTestInstance(method);
347332
assertEquals("Wrong match for event '" + eventType + "' on " + method,
@@ -361,8 +346,8 @@ private ResolvableType createGenericEventType(Class<?> payloadType) {
361346
return ResolvableType.forClassWithGenerics(PayloadApplicationEvent.class, payloadType);
362347
}
363348

364-
private static class StaticApplicationListenerMethodAdapter
365-
extends ApplicationListenerMethodAdapter {
349+
350+
private static class StaticApplicationListenerMethodAdapter extends ApplicationListenerMethodAdapter {
366351

367352
private final Object targetBean;
368353

@@ -380,7 +365,6 @@ public Object getTargetBean() {
380365

381366
private static class SampleEvents {
382367

383-
384368
@EventListener
385369
@Order(42)
386370
public void handleRaw(ApplicationEvent event) {
@@ -449,13 +433,15 @@ public void generateCheckedException(GenericTestEvent<String> event) throws IOEx
449433
}
450434
}
451435

436+
452437
interface SimpleService {
453438

454439
void handleIt(ApplicationEvent event);
455-
456440
}
457441

442+
458443
private static class EntityWrapper<T> implements ResolvableTypeProvider {
444+
459445
private final T entity;
460446

461447
public EntityWrapper(T entity) {
@@ -468,6 +454,7 @@ public ResolvableType getResolvableType() {
468454
}
469455
}
470456

457+
471458
static class InvalidProxyTestBean implements SimpleService {
472459

473460
@Override
@@ -479,6 +466,7 @@ public void handleIt2(ApplicationEvent event) {
479466
}
480467
}
481468

469+
482470
@SuppressWarnings({"unused", "serial"})
483471
static class PayloadTestEvent<V, T> extends PayloadApplicationEvent<T> {
484472

@@ -490,8 +478,10 @@ public PayloadTestEvent(Object source, T payload, V something) {
490478
}
491479
}
492480

481+
493482
@SuppressWarnings({ "serial" })
494483
static class PayloadStringTestEvent extends PayloadTestEvent<Long, String> {
484+
495485
public PayloadStringTestEvent(Object source, String payload, Long something) {
496486
super(source, payload, something);
497487
}

0 commit comments

Comments
 (0)