Detect invalid transaction configuration for transactional event listeners

Closes gh-30679
This commit is contained in:
Juergen Hoeller
2023-06-17 16:13:59 +02:00
parent 3568f6c61a
commit 842569c9e5
2 changed files with 35 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,12 +24,15 @@ import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.event.ApplicationListenerMethodAdapter;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
@@ -123,6 +126,19 @@ public class TransactionalApplicationListenerMethodAdapterTests {
assertThat(adapter.getListenerId()).endsWith("identifier");
}
@Test
public void withTransactionalAnnotation() {
Method m = ReflectionUtils.findMethod(SampleEvents.class, "withTransactionalAnnotation", String.class);
assertThatIllegalStateException().isThrownBy(() -> createTestInstance(m));
}
@Test
public void withAsyncTransactionalAnnotation() {
Method m = ReflectionUtils.findMethod(SampleEvents.class, "withAsyncTransactionalAnnotation", String.class);
supportsEventType(true, m, createGenericEventType(String.class));
supportsEventType(false, m, createGenericEventType(Double.class));
}
private static void assertPhase(Method method, TransactionPhase expected) {
assertThat(method).as("Method must not be null").isNotNull();
@@ -194,6 +210,16 @@ public class TransactionalApplicationListenerMethodAdapterTests {
@TransactionalEventListener(id = "identifier")
public void identified(String data) {
}
@TransactionalEventListener
@Transactional
public void withTransactionalAnnotation(String data) {
}
@TransactionalEventListener
@Async @Transactional
public void withAsyncTransactionalAnnotation(String data) {
}
}
}