Skip to content

Commit

Permalink
Test for supportsEventType mismatch with unrelated event type
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Jun 21, 2023
1 parent 9a5290e commit 09cf489
Showing 1 changed file with 33 additions and 18 deletions.
Expand Up @@ -335,11 +335,10 @@ public void beanInstanceRetrievedAtEveryInvocation() {
verify(this.context, times(2)).getBean("testBean");
}

// see https://github.com/spring-projects/spring-framework/issues/30399
@Test
@Test // gh-30399
void simplePayloadDoesNotSupportArbitraryGenericEventType() throws Exception {
var method = SampleEvents.class.getDeclaredMethod("handleString", String.class);
var adapter = new ApplicationListenerMethodAdapter(null, ApplicationListenerMethodAdapterTests.class, method);
Method method = SampleEvents.class.getDeclaredMethod("handleString", String.class);
ApplicationListenerMethodAdapter adapter = createTestInstance(method);

assertThat(adapter.supportsEventType(createPayloadEventType(ResolvableType.forClassWithGenerics(EntityWrapper.class, Integer.class))))
.as("handleString(String) with EntityWrapper<Integer>").isFalse();
Expand All @@ -349,11 +348,10 @@ void simplePayloadDoesNotSupportArbitraryGenericEventType() throws Exception {
.as("handleString(String) with String").isTrue();
}

// see https://github.com/spring-projects/spring-framework/issues/30399
@Test
@Test // gh-30399
void genericPayloadDoesNotSupportArbitraryGenericEventType() throws Exception {
var method = SampleEvents.class.getDeclaredMethod("handleGenericStringPayload", EntityWrapper.class);
var adapter = new ApplicationListenerMethodAdapter(null, ApplicationListenerMethodAdapterTests.class, method);
Method method = SampleEvents.class.getDeclaredMethod("handleGenericStringPayload", EntityWrapper.class);
ApplicationListenerMethodAdapter adapter = createTestInstance(method);

assertThat(adapter.supportsEventType(createPayloadEventType(ResolvableType.forClass(EntityWrapper.class))))
.as("handleGenericStringPayload(EntityWrapper<String>) with EntityWrapper<?>").isFalse();
Expand All @@ -363,11 +361,10 @@ void genericPayloadDoesNotSupportArbitraryGenericEventType() throws Exception {
.as("handleGenericStringPayload(EntityWrapper<String>) with EntityWrapper<String>").isTrue();
}

// see https://github.com/spring-projects/spring-framework/issues/30399
@Test
@Test // gh-30399
void rawGenericPayloadDoesNotSupportArbitraryGenericEventType() throws Exception {
var method = SampleEvents.class.getDeclaredMethod("handleGenericAnyPayload", EntityWrapper.class);
var adapter = new ApplicationListenerMethodAdapter(null, ApplicationListenerMethodAdapterTests.class, method);
Method method = SampleEvents.class.getDeclaredMethod("handleGenericAnyPayload", EntityWrapper.class);
ApplicationListenerMethodAdapter adapter = createTestInstance(method);

assertThat(adapter.supportsEventType(createPayloadEventType(ResolvableType.forClass(EntityWrapper.class))))
.as("handleGenericAnyPayload(EntityWrapper<?>) with EntityWrapper<?>").isTrue();
Expand All @@ -381,10 +378,10 @@ void rawGenericPayloadDoesNotSupportArbitraryGenericEventType() throws Exception
.as("handleGenericAnyPayload(EntityWrapper<?>) with List<String>").isFalse();
}

@Test
@Test // gh-30399
void genericApplicationEventSupportsSpecificType() throws Exception {
var method = SampleEvents.class.getDeclaredMethod("handleGenericString", GenericTestEvent.class);
var adapter = new ApplicationListenerMethodAdapter(null, ApplicationListenerMethodAdapterTests.class, method);
Method method = SampleEvents.class.getDeclaredMethod("handleGenericString", GenericTestEvent.class);
ApplicationListenerMethodAdapter adapter = createTestInstance(method);

assertThat(adapter.supportsEventType(ResolvableType.forClass(GenericTestEvent.class)))
.as("handleGenericString(GenericTestEvent<String>) with GenericTestEvent<?>").isTrue();
Expand All @@ -394,10 +391,10 @@ void genericApplicationEventSupportsSpecificType() throws Exception {
.as("handleGenericString(GenericTestEvent<String>) with GenericTestEvent<String>").isTrue();
}

@Test
@Test // gh-30399
void genericRawApplicationEventSupportsRawTypeAndAnySpecificType() throws Exception {
var method = SampleEvents.class.getDeclaredMethod("handleGenericRaw", GenericTestEvent.class);
var adapter = new ApplicationListenerMethodAdapter(null, ApplicationListenerMethodAdapterTests.class, method);
Method method = SampleEvents.class.getDeclaredMethod("handleGenericRaw", GenericTestEvent.class);
ApplicationListenerMethodAdapter adapter = createTestInstance(method);

assertThat(adapter.supportsEventType(ResolvableType.forClass(GenericTestEvent.class)))
.as("handleGenericRaw(GenericTestEvent<?>) with GenericTestEvent<?>").isTrue();
Expand All @@ -407,6 +404,20 @@ void genericRawApplicationEventSupportsRawTypeAndAnySpecificType() throws Except
.as("handleGenericRaw(GenericTestEvent<?>) with GenericTestEvent<Integer>").isTrue();
}

@Test // gh-30399
void unrelatedApplicationEventDoesNotSupportRawTypeOrAnySpecificType() throws Exception {
Method method = SampleEvents.class.getDeclaredMethod("handleUnrelated", ContextRefreshedEvent.class);
ApplicationListenerMethodAdapter adapter = createTestInstance(method);

assertThat(adapter.supportsEventType(ResolvableType.forClass(GenericTestEvent.class)))
.as("handleUnrelated(ContextRefreshedEvent) with GenericTestEvent<?>").isFalse();
assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(GenericTestEvent.class, String.class)))
.as("handleUnrelated(ContextRefreshedEvent) with GenericTestEvent<String>").isFalse();
assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(GenericTestEvent.class, Integer.class)))
.as("handleUnrelated(ContextRefreshedEvent) with GenericTestEvent<Integer>").isFalse();
}


private void supportsEventType(boolean match, Method method, ResolvableType eventType) {
ApplicationListenerMethodAdapter adapter = createTestInstance(method);
assertThat(adapter.supportsEventType(eventType))
Expand Down Expand Up @@ -462,6 +473,10 @@ public void handleGenericString(GenericTestEvent<String> event) {
public void handleGenericRaw(GenericTestEvent<?> event) {
}

@EventListener
public void handleUnrelated(ContextRefreshedEvent event) {
}

@EventListener
public void handleString(String payload) {
}
Expand Down

0 comments on commit 09cf489

Please sign in to comment.