@@ -50,52 +50,48 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
50
50
51
51
private final ApplicationContext context = mock (ApplicationContext .class );
52
52
53
+
53
54
@ Test
54
55
public void rawListener () {
55
- Method method = ReflectionUtils .findMethod (SampleEvents .class ,
56
- "handleRaw" , ApplicationEvent .class );
56
+ Method method = ReflectionUtils .findMethod (SampleEvents .class , "handleRaw" , ApplicationEvent .class );
57
57
supportsEventType (true , method , getGenericApplicationEventType ("applicationEvent" ));
58
58
}
59
59
60
60
@ Test
61
61
public void rawListenerWithGenericEvent () {
62
- Method method = ReflectionUtils .findMethod (SampleEvents .class ,
63
- "handleRaw" , ApplicationEvent .class );
62
+ Method method = ReflectionUtils .findMethod (SampleEvents .class , "handleRaw" , ApplicationEvent .class );
64
63
supportsEventType (true , method , getGenericApplicationEventType ("stringEvent" ));
65
64
}
66
65
67
66
@ Test
68
67
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 );
71
70
supportsEventType (true , method , getGenericApplicationEventType ("stringEvent" ));
72
71
}
73
72
74
73
@ Test
75
74
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 );
78
77
supportsEventType (false , method , getGenericApplicationEventType ("longEvent" ));
79
78
}
80
79
81
80
@ Test
82
81
public void listenerWithPayloadAndGenericInformation () {
83
- Method method = ReflectionUtils .findMethod (SampleEvents .class ,
84
- "handleString" , String .class );
82
+ Method method = ReflectionUtils .findMethod (SampleEvents .class , "handleString" , String .class );
85
83
supportsEventType (true , method , createGenericEventType (String .class ));
86
84
}
87
85
88
86
@ Test
89
87
public void listenerWithInvalidPayloadAndGenericInformation () {
90
- Method method = ReflectionUtils .findMethod (SampleEvents .class ,
91
- "handleString" , String .class );
88
+ Method method = ReflectionUtils .findMethod (SampleEvents .class , "handleString" , String .class );
92
89
supportsEventType (false , method , createGenericEventType (Integer .class ));
93
90
}
94
91
95
92
@ 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 );
99
95
supportsEventType (true , method , ResolvableType .forClass (PayloadApplicationEvent .class ));
100
96
}
101
97
@@ -108,145 +104,137 @@ public void listenerWithSubTypeSeveralGenerics() {
108
104
109
105
@ Test
110
106
public void listenerWithSubTypeSeveralGenericsResolved () {
111
- Method method = ReflectionUtils .findMethod (SampleEvents .class ,
112
- "handleString" , String .class );
107
+ Method method = ReflectionUtils .findMethod (SampleEvents .class , "handleString" , String .class );
113
108
supportsEventType (true , method , ResolvableType .forClass (PayloadStringTestEvent .class ));
114
109
}
115
110
116
111
@ Test
117
112
public void listenerWithAnnotationValue () {
118
- Method method = ReflectionUtils .findMethod (SampleEvents .class ,
119
- "handleStringAnnotationValue" );
113
+ Method method = ReflectionUtils .findMethod (SampleEvents .class , "handleStringAnnotationValue" );
120
114
supportsEventType (true , method , createGenericEventType (String .class ));
121
115
}
122
116
123
117
@ Test
124
118
public void listenerWithAnnotationClasses () {
125
- Method method = ReflectionUtils .findMethod (SampleEvents .class ,
126
- "handleStringAnnotationClasses" );
119
+ Method method = ReflectionUtils .findMethod (SampleEvents .class , "handleStringAnnotationClasses" );
127
120
supportsEventType (true , method , createGenericEventType (String .class ));
128
121
}
129
122
130
123
@ Test
131
124
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 );
134
127
supportsEventType (true , method , createGenericEventType (String .class ));
135
128
}
136
129
137
130
@ Test
138
131
public void listenerWithSeveralTypes () {
139
- Method method = ReflectionUtils .findMethod (SampleEvents .class ,
140
- "handleStringOrInteger" );
132
+ Method method = ReflectionUtils .findMethod (SampleEvents .class , "handleStringOrInteger" );
141
133
supportsEventType (true , method , createGenericEventType (String .class ));
142
134
supportsEventType (true , method , createGenericEventType (Integer .class ));
143
135
supportsEventType (false , method , createGenericEventType (Double .class ));
144
136
}
145
137
146
138
@ Test
147
139
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 );
151
142
this .thrown .expect (IllegalStateException .class );
152
143
createTestInstance (method );
153
144
}
154
145
155
146
@ Test
156
147
public void listenerWithNoParameter () {
157
- Method method = ReflectionUtils .findMethod (SampleEvents .class ,
158
- "noParameter" );
159
-
148
+ Method method = ReflectionUtils .findMethod (SampleEvents .class , "noParameter" );
160
149
this .thrown .expect (IllegalStateException .class );
161
150
createTestInstance (method );
162
151
}
163
152
164
153
@ Test
165
154
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 );
169
157
this .thrown .expect (IllegalStateException .class );
170
158
createTestInstance (method );
171
159
}
172
160
173
161
@ Test
174
162
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 );
177
165
ApplicationListenerMethodAdapter adapter = createTestInstance (method );
178
166
assertEquals (0 , adapter .getOrder ());
179
167
}
180
168
181
169
@ Test
182
170
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 );
185
173
ApplicationListenerMethodAdapter adapter = createTestInstance (method );
186
174
assertEquals (42 , adapter .getOrder ());
187
175
}
188
176
189
177
@ Test
190
178
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 );
193
181
GenericTestEvent <String > event = createGenericTestEvent ("test" );
194
182
invokeListener (method , event );
195
183
verify (this .sampleEvents , times (1 )).handleGenericString (event );
196
184
}
197
185
198
186
@ Test
199
187
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 );
202
190
GenericTestEvent <String > event = new SmartGenericTestEvent <>(this , "test" );
203
191
invokeListener (method , event );
204
192
verify (this .sampleEvents , times (1 )).handleGenericString (event );
205
193
}
206
194
207
195
@ Test
208
196
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 );
211
199
EntityWrapper <String > payload = new EntityWrapper <>("test" );
212
200
invokeListener (method , new PayloadApplicationEvent <>(this , payload ));
213
201
verify (this .sampleEvents , times (1 )).handleGenericStringPayload (payload );
214
202
}
215
203
216
204
@ Test
217
205
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 );
220
208
EntityWrapper <Integer > payload = new EntityWrapper <>(123 );
221
209
invokeListener (method , new PayloadApplicationEvent <>(this , payload ));
222
210
verify (this .sampleEvents , times (0 )).handleGenericStringPayload (any ());
223
211
}
224
212
225
213
@ Test
226
214
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 );
229
217
EntityWrapper <String > payload = new EntityWrapper <>("test" );
230
218
invokeListener (method , new PayloadApplicationEvent <>(this , payload ));
231
219
verify (this .sampleEvents , times (1 )).handleGenericAnyPayload (payload );
232
220
}
233
221
234
222
@ Test
235
223
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 );
238
226
GenericTestEvent <String > event = createGenericTestEvent ("fail" );
239
227
240
228
this .thrown .expect (IllegalStateException .class );
241
229
this .thrown .expectMessage ("Test exception" );
242
- this .thrown .expectCause (is (isNull (Throwable . class )));
230
+ this .thrown .expectCause (is ((Throwable ) isNull ( )));
243
231
invokeListener (method , event );
244
232
}
245
233
246
234
@ Test
247
235
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 );
250
238
GenericTestEvent <String > event = createGenericTestEvent ("fail" );
251
239
252
240
this .thrown .expect (UndeclaredThrowableException .class );
@@ -262,7 +250,8 @@ public void invokeListenerInvalidProxy() {
262
250
proxyFactory .addInterface (SimpleService .class );
263
251
Object bean = proxyFactory .getProxy (getClass ().getClassLoader ());
264
252
265
- Method method = ReflectionUtils .findMethod (InvalidProxyTestBean .class , "handleIt2" , ApplicationEvent .class );
253
+ Method method = ReflectionUtils .findMethod (
254
+ InvalidProxyTestBean .class , "handleIt2" , ApplicationEvent .class );
266
255
StaticApplicationListenerMethodAdapter listener =
267
256
new StaticApplicationListenerMethodAdapter (method , bean );
268
257
this .thrown .expect (IllegalStateException .class );
@@ -272,44 +261,40 @@ public void invokeListenerInvalidProxy() {
272
261
273
262
@ Test
274
263
public void invokeListenerWithPayload () {
275
- Method method = ReflectionUtils .findMethod (SampleEvents .class ,
276
- "handleString" , String .class );
264
+ Method method = ReflectionUtils .findMethod (SampleEvents .class , "handleString" , String .class );
277
265
PayloadApplicationEvent <String > event = new PayloadApplicationEvent <>(this , "test" );
278
266
invokeListener (method , event );
279
267
verify (this .sampleEvents , times (1 )).handleString ("test" );
280
268
}
281
269
282
270
@ Test
283
271
public void invokeListenerWithPayloadWrongType () {
284
- Method method = ReflectionUtils .findMethod (SampleEvents .class ,
285
- "handleString" , String .class );
272
+ Method method = ReflectionUtils .findMethod (SampleEvents .class , "handleString" , String .class );
286
273
PayloadApplicationEvent <Long > event = new PayloadApplicationEvent <>(this , 123L );
287
274
invokeListener (method , event );
288
275
verify (this .sampleEvents , never ()).handleString (anyString ());
289
276
}
290
277
291
278
@ Test
292
279
public void invokeListenerWithAnnotationValue () {
293
- Method method = ReflectionUtils .findMethod (SampleEvents .class ,
294
- "handleStringAnnotationClasses" );
280
+ Method method = ReflectionUtils .findMethod (SampleEvents .class , "handleStringAnnotationClasses" );
295
281
PayloadApplicationEvent <String > event = new PayloadApplicationEvent <>(this , "test" );
296
282
invokeListener (method , event );
297
283
verify (this .sampleEvents , times (1 )).handleStringAnnotationClasses ();
298
284
}
299
285
300
286
@ Test
301
287
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 );
304
290
PayloadApplicationEvent <String > event = new PayloadApplicationEvent <>(this , "test" );
305
291
invokeListener (method , event );
306
292
verify (this .sampleEvents , times (1 )).handleStringAnnotationValueAndParameter ("test" );
307
293
}
308
294
309
295
@ Test
310
296
public void invokeListenerWithSeveralTypes () {
311
- Method method = ReflectionUtils .findMethod (SampleEvents .class ,
312
- "handleStringOrInteger" );
297
+ Method method = ReflectionUtils .findMethod (SampleEvents .class , "handleStringOrInteger" );
313
298
PayloadApplicationEvent <String > event = new PayloadApplicationEvent <>(this , "test" );
314
299
invokeListener (method , event );
315
300
verify (this .sampleEvents , times (1 )).handleStringOrInteger ();
@@ -321,11 +306,10 @@ public void invokeListenerWithSeveralTypes() {
321
306
verify (this .sampleEvents , times (2 )).handleStringOrInteger ();
322
307
}
323
308
324
-
325
309
@ Test
326
310
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 );
329
313
when (this .context .getBean ("testBean" )).thenReturn (this .sampleEvents );
330
314
ApplicationListenerMethodAdapter listener = new ApplicationListenerMethodAdapter (
331
315
"testBean" , GenericTestEvent .class , method );
@@ -342,6 +326,7 @@ public void beanInstanceRetrievedAtEveryInvocation() {
342
326
verify (this .context , times (2 )).getBean ("testBean" );
343
327
}
344
328
329
+
345
330
private void supportsEventType (boolean match , Method method , ResolvableType eventType ) {
346
331
ApplicationListenerMethodAdapter adapter = createTestInstance (method );
347
332
assertEquals ("Wrong match for event '" + eventType + "' on " + method ,
@@ -361,8 +346,8 @@ private ResolvableType createGenericEventType(Class<?> payloadType) {
361
346
return ResolvableType .forClassWithGenerics (PayloadApplicationEvent .class , payloadType );
362
347
}
363
348
364
- private static class StaticApplicationListenerMethodAdapter
365
- extends ApplicationListenerMethodAdapter {
349
+
350
+ private static class StaticApplicationListenerMethodAdapter extends ApplicationListenerMethodAdapter {
366
351
367
352
private final Object targetBean ;
368
353
@@ -380,7 +365,6 @@ public Object getTargetBean() {
380
365
381
366
private static class SampleEvents {
382
367
383
-
384
368
@ EventListener
385
369
@ Order (42 )
386
370
public void handleRaw (ApplicationEvent event ) {
@@ -449,13 +433,15 @@ public void generateCheckedException(GenericTestEvent<String> event) throws IOEx
449
433
}
450
434
}
451
435
436
+
452
437
interface SimpleService {
453
438
454
439
void handleIt (ApplicationEvent event );
455
-
456
440
}
457
441
442
+
458
443
private static class EntityWrapper <T > implements ResolvableTypeProvider {
444
+
459
445
private final T entity ;
460
446
461
447
public EntityWrapper (T entity ) {
@@ -468,6 +454,7 @@ public ResolvableType getResolvableType() {
468
454
}
469
455
}
470
456
457
+
471
458
static class InvalidProxyTestBean implements SimpleService {
472
459
473
460
@ Override
@@ -479,6 +466,7 @@ public void handleIt2(ApplicationEvent event) {
479
466
}
480
467
}
481
468
469
+
482
470
@ SuppressWarnings ({"unused" , "serial" })
483
471
static class PayloadTestEvent <V , T > extends PayloadApplicationEvent <T > {
484
472
@@ -490,8 +478,10 @@ public PayloadTestEvent(Object source, T payload, V something) {
490
478
}
491
479
}
492
480
481
+
493
482
@ SuppressWarnings ({ "serial" })
494
483
static class PayloadStringTestEvent extends PayloadTestEvent <Long , String > {
484
+
495
485
public PayloadStringTestEvent (Object source , String payload , Long something ) {
496
486
super (source , payload , something );
497
487
}
0 commit comments