|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.lang.reflect.Method;
|
21 | 21 | import java.lang.reflect.UndeclaredThrowableException;
|
| 22 | +import java.util.List; |
22 | 23 |
|
23 | 24 | import org.junit.jupiter.api.Test;
|
24 | 25 |
|
@@ -325,6 +326,77 @@ public void beanInstanceRetrievedAtEveryInvocation() {
|
325 | 326 | verify(this.context, times(2)).getBean("testBean");
|
326 | 327 | }
|
327 | 328 |
|
| 329 | + // see https://github.com/spring-projects/spring-framework/issues/30399 |
| 330 | + @Test |
| 331 | + void simplePayloadDoesNotSupportArbitraryGenericEventType() throws Exception { |
| 332 | + var method = SampleEvents.class.getDeclaredMethod("handleString", String.class); |
| 333 | + var adapter = new ApplicationListenerMethodAdapter(null, ApplicationListenerMethodAdapterTests.class, method); |
| 334 | + |
| 335 | + assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(EntityWrapper.class, Integer.class))) |
| 336 | + .as("handleString(String) with EntityWrapper<Integer>").isFalse(); |
| 337 | + assertThat(adapter.supportsEventType(ResolvableType.forClass(EntityWrapper.class))) |
| 338 | + .as("handleString(String) with EntityWrapper<?>").isFalse(); |
| 339 | + assertThat(adapter.supportsEventType(ResolvableType.forClass(String.class))) |
| 340 | + .as("handleString(String) with String").isTrue(); |
| 341 | + } |
| 342 | + |
| 343 | + // see https://github.com/spring-projects/spring-framework/issues/30399 |
| 344 | + @Test |
| 345 | + void genericPayloadDoesNotSupportArbitraryGenericEventType() throws Exception { |
| 346 | + var method = SampleEvents.class.getDeclaredMethod("handleGenericStringPayload", EntityWrapper.class); |
| 347 | + var adapter = new ApplicationListenerMethodAdapter(null, ApplicationListenerMethodAdapterTests.class, method); |
| 348 | + |
| 349 | + assertThat(adapter.supportsEventType(ResolvableType.forClass(EntityWrapper.class))) |
| 350 | + .as("handleGenericStringPayload(EntityWrapper<String>) with EntityWrapper<?>").isFalse(); |
| 351 | + assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(EntityWrapper.class, Integer.class))) |
| 352 | + .as("handleGenericStringPayload(EntityWrapper<String>) with EntityWrapper<Integer>").isFalse(); |
| 353 | + assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(EntityWrapper.class, String.class))) |
| 354 | + .as("handleGenericStringPayload(EntityWrapper<String>) with EntityWrapper<String>").isTrue(); |
| 355 | + } |
| 356 | + |
| 357 | + // see https://github.com/spring-projects/spring-framework/issues/30399 |
| 358 | + @Test |
| 359 | + void rawGenericPayloadDoesNotSupportArbitraryGenericEventType() throws Exception { |
| 360 | + var method = SampleEvents.class.getDeclaredMethod("handleGenericAnyPayload", EntityWrapper.class); |
| 361 | + var adapter = new ApplicationListenerMethodAdapter(null, ApplicationListenerMethodAdapterTests.class, method); |
| 362 | + |
| 363 | + assertThat(adapter.supportsEventType(ResolvableType.forClass(EntityWrapper.class))) |
| 364 | + .as("handleGenericAnyPayload(EntityWrapper<?>) with EntityWrapper<?>").isTrue(); |
| 365 | + assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(EntityWrapper.class, Integer.class))) |
| 366 | + .as("handleGenericAnyPayload(EntityWrapper<?>) with EntityWrapper<Integer>").isTrue(); |
| 367 | + assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(EntityWrapper.class, String.class))) |
| 368 | + .as("handleGenericAnyPayload(EntityWrapper<?>) with EntityWrapper<String>").isTrue(); |
| 369 | + assertThat(adapter.supportsEventType(ResolvableType.forClass(List.class))) |
| 370 | + .as("handleGenericAnyPayload(EntityWrapper<?>) with List<?>").isFalse(); |
| 371 | + assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(List.class, String.class))) |
| 372 | + .as("handleGenericAnyPayload(EntityWrapper<?>) with List<String>").isFalse(); |
| 373 | + } |
| 374 | + |
| 375 | + @Test |
| 376 | + void genericApplicationEventSupportsSpecificType() throws Exception { |
| 377 | + var method = SampleEvents.class.getDeclaredMethod("handleGenericString", GenericTestEvent.class); |
| 378 | + var adapter = new ApplicationListenerMethodAdapter(null, ApplicationListenerMethodAdapterTests.class, method); |
| 379 | + |
| 380 | + assertThat(adapter.supportsEventType(ResolvableType.forClass(GenericTestEvent.class))) |
| 381 | + .as("handleGenericString(GenericTestEvent<String>) with GenericTestEvent<?>").isFalse(); |
| 382 | + assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(GenericTestEvent.class, Integer.class))) |
| 383 | + .as("handleGenericString(GenericTestEvent<String>) with GenericTestEvent<Integer>").isFalse(); |
| 384 | + assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(GenericTestEvent.class, String.class))) |
| 385 | + .as("handleGenericString(GenericTestEvent<String>) with GenericTestEvent<String>").isTrue(); |
| 386 | + } |
| 387 | + |
| 388 | + @Test |
| 389 | + void genericRawApplicationEventSupportsRawTypeAndAnySpecificType() throws Exception { |
| 390 | + var method = SampleEvents.class.getDeclaredMethod("handleGenericRaw", GenericTestEvent.class); |
| 391 | + var adapter = new ApplicationListenerMethodAdapter(null, ApplicationListenerMethodAdapterTests.class, method); |
| 392 | + |
| 393 | + assertThat(adapter.supportsEventType(ResolvableType.forClass(GenericTestEvent.class))) |
| 394 | + .as("handleGenericRaw(GenericTestEvent<?>) with GenericTestEvent<?>").isTrue(); |
| 395 | + assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(GenericTestEvent.class, String.class))) |
| 396 | + .as("handleGenericRaw(GenericTestEvent<?>) with GenericTestEvent<String>").isTrue(); |
| 397 | + assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(GenericTestEvent.class, Integer.class))) |
| 398 | + .as("handleGenericRaw(GenericTestEvent<?>) with GenericTestEvent<Integer>").isTrue(); |
| 399 | + } |
328 | 400 |
|
329 | 401 | private void supportsEventType(boolean match, Method method, ResolvableType eventType) {
|
330 | 402 | ApplicationListenerMethodAdapter adapter = createTestInstance(method);
|
@@ -373,6 +445,10 @@ public void handleRaw(ApplicationEvent event) {
|
373 | 445 | public void handleGenericString(GenericTestEvent<String> event) {
|
374 | 446 | }
|
375 | 447 |
|
| 448 | + @EventListener |
| 449 | + public void handleGenericRaw(GenericTestEvent<?> event) { |
| 450 | + } |
| 451 | + |
376 | 452 | @EventListener
|
377 | 453 | public void handleString(String payload) {
|
378 | 454 | }
|
|
0 commit comments