Revise "Introduce class-level execution phases for @Sql"
This commit revises the previous commit as follows. - Remove hasTestMethod() from TestContext and instead introduce a new variant of createDelegatingTransactionAttribute() in TestContextTransactionUtils which accepts a boolean `includeMethodName` flag. - Add missing @TestMethodOrder declaration to AfterTestClassSqlScriptsTests to ensure that the test methods are always executed in the required order. - Polish Javadoc and add missing @since tags. Closes gh-27285
This commit is contained in:
@@ -42,7 +42,6 @@ import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
|
||||
* override {@link #setMethodInvoker(MethodInvoker)} and {@link #getMethodInvoker()}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Andreas Ahlenstorf
|
||||
* @since 2.5
|
||||
* @see TestContextManager
|
||||
* @see TestExecutionListener
|
||||
@@ -111,25 +110,6 @@ public interface TestContext extends AttributeAccessor, Serializable {
|
||||
*/
|
||||
Object getTestInstance();
|
||||
|
||||
/**
|
||||
* Tests whether a test method is part of this test context. Returns
|
||||
* {@code true} if this context has a current test method, {@code false}
|
||||
* otherwise.
|
||||
*
|
||||
* <p>The default implementation of this method always returns {@code false}.
|
||||
* Custom {@code TestContext} implementations are therefore highly encouraged
|
||||
* to override this method with a more meaningful implementation. Note that
|
||||
* the standard {@code TestContext} implementation in Spring overrides this
|
||||
* method appropriately.
|
||||
* @return {@code true} if the test execution has already entered a test
|
||||
* method
|
||||
* @since 6.1
|
||||
* @see #getTestMethod()
|
||||
*/
|
||||
default boolean hasTestMethod() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current {@linkplain Method test method} for this test context.
|
||||
* <p>Note: this is a mutable property.
|
||||
|
||||
@@ -33,11 +33,12 @@ import org.springframework.core.annotation.AliasFor;
|
||||
*
|
||||
* <p>Method-level declarations override class-level declarations by default,
|
||||
* but this behavior can be configured via {@link SqlMergeMode @SqlMergeMode}.
|
||||
* However, this does not apply to class-level declarations that use
|
||||
* {@link ExecutionPhase#BEFORE_TEST_CLASS} or
|
||||
* {@link ExecutionPhase#AFTER_TEST_CLASS}. Such declarations are retained and
|
||||
* scripts and statements are executed once per class in addition to any
|
||||
* method-level annotations.
|
||||
* However, this does not apply to class-level declarations configured for the
|
||||
* {@link ExecutionPhase#BEFORE_TEST_CLASS BEFORE_TEST_CLASS} or
|
||||
* {@link ExecutionPhase#AFTER_TEST_CLASS AFTER_TEST_CLASS} execution phase. Such
|
||||
* declarations cannot be overridden, and the corresponding scripts and statements
|
||||
* will be executed once per class in addition to any method-level scripts and
|
||||
* statements.
|
||||
*
|
||||
* <p>Script execution is performed by the {@link SqlScriptsTestExecutionListener},
|
||||
* which is enabled by default.
|
||||
@@ -169,13 +170,15 @@ public @interface Sql {
|
||||
|
||||
/**
|
||||
* The configured SQL scripts and statements will be executed
|
||||
* once <em>before</em> any test method is run.
|
||||
* once per test class <em>before</em> any test method is run.
|
||||
* @since 6.1
|
||||
*/
|
||||
BEFORE_TEST_CLASS,
|
||||
|
||||
/**
|
||||
* The configured SQL scripts and statements will be executed
|
||||
* once <em>after</em> any test method is run.
|
||||
* once per test class <em>after</em> all test methods have run.
|
||||
* @since 6.1
|
||||
*/
|
||||
AFTER_TEST_CLASS,
|
||||
|
||||
|
||||
@@ -68,8 +68,8 @@ import static org.springframework.util.ResourceUtils.CLASSPATH_URL_PREFIX;
|
||||
* configured via the {@link Sql @Sql} annotation.
|
||||
*
|
||||
* <p>Class-level annotations that are constrained to a class-level execution
|
||||
* phase ({@link ExecutionPhase#BEFORE_TEST_CLASS} or
|
||||
* {@link ExecutionPhase#AFTER_TEST_CLASS}) will be run
|
||||
* phase ({@link ExecutionPhase#BEFORE_TEST_CLASS BEFORE_TEST_CLASS} or
|
||||
* {@link ExecutionPhase#AFTER_TEST_CLASS AFTER_TEST_CLASS}) will be run
|
||||
* {@linkplain #beforeTestClass(TestContext) once before all test methods} or
|
||||
* {@linkplain #afterTestMethod(TestContext) once after all test methods},
|
||||
* respectively. All other scripts and inlined statements will be executed
|
||||
@@ -138,20 +138,22 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
|
||||
* Execute SQL scripts configured via {@link Sql @Sql} for the supplied
|
||||
* {@link TestContext} once per test class <em>before</em> any test method
|
||||
* is run.
|
||||
* @since 6.1
|
||||
*/
|
||||
@Override
|
||||
public void beforeTestClass(TestContext testContext) throws Exception {
|
||||
executeBeforeOrAfterClassSqlScripts(testContext, ExecutionPhase.BEFORE_TEST_CLASS);
|
||||
executeClassLevelSqlScripts(testContext, ExecutionPhase.BEFORE_TEST_CLASS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute SQL scripts configured via {@link Sql @Sql} for the supplied
|
||||
* {@link TestContext} once per test class <em>after</em> all test methods
|
||||
* have been run.
|
||||
* @since 6.1
|
||||
*/
|
||||
@Override
|
||||
public void afterTestClass(TestContext testContext) throws Exception {
|
||||
executeBeforeOrAfterClassSqlScripts(testContext, ExecutionPhase.AFTER_TEST_CLASS);
|
||||
executeClassLevelSqlScripts(testContext, ExecutionPhase.AFTER_TEST_CLASS);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -189,11 +191,12 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
|
||||
|
||||
/**
|
||||
* Execute class-level SQL scripts configured via {@link Sql @Sql} for the
|
||||
* supplied {@link TestContext} and the execution phases
|
||||
* {@link ExecutionPhase#BEFORE_TEST_CLASS} and
|
||||
* {@link ExecutionPhase#AFTER_TEST_CLASS}.
|
||||
* supplied {@link TestContext} and the supplied
|
||||
* {@link ExecutionPhase#BEFORE_TEST_CLASS BEFORE_TEST_CLASS} or
|
||||
* {@link ExecutionPhase#AFTER_TEST_CLASS AFTER_TEST_CLASS} execution phase.
|
||||
* @since 6.1
|
||||
*/
|
||||
private void executeBeforeOrAfterClassSqlScripts(TestContext testContext, ExecutionPhase executionPhase) {
|
||||
private void executeClassLevelSqlScripts(TestContext testContext, ExecutionPhase executionPhase) {
|
||||
Class<?> testClass = testContext.getTestClass();
|
||||
executeSqlScripts(getSqlAnnotationsFor(testClass), testContext, executionPhase, true);
|
||||
}
|
||||
@@ -286,7 +289,7 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
|
||||
Sql sql, ExecutionPhase executionPhase, TestContext testContext, boolean classLevel) {
|
||||
|
||||
Assert.isTrue(classLevel || isValidMethodLevelPhase(sql.executionPhase()),
|
||||
() -> "%s cannot be used on methods".formatted(sql.executionPhase()));
|
||||
() -> "@SQL execution phase %s cannot be used on methods".formatted(sql.executionPhase()));
|
||||
|
||||
if (executionPhase != sql.executionPhase()) {
|
||||
return;
|
||||
@@ -302,10 +305,8 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
|
||||
.formatted(executionPhase, testContext.getTestClass().getName()));
|
||||
}
|
||||
|
||||
Method testMethod = null;
|
||||
if (testContext.hasTestMethod()) {
|
||||
testMethod = testContext.getTestMethod();
|
||||
}
|
||||
boolean methodLevel = !classLevel;
|
||||
Method testMethod = (methodLevel ? testContext.getTestMethod() : null);
|
||||
|
||||
String[] scripts = getScripts(sql, testContext.getTestClass(), testMethod, classLevel);
|
||||
List<Resource> scriptResources = TestContextResourceUtils.convertToResourceList(
|
||||
@@ -357,7 +358,7 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
|
||||
int propagation = (newTxRequired ? TransactionDefinition.PROPAGATION_REQUIRES_NEW :
|
||||
TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
TransactionAttribute txAttr = TestContextTransactionUtils.createDelegatingTransactionAttribute(
|
||||
testContext, new DefaultTransactionAttribute(propagation));
|
||||
testContext, new DefaultTransactionAttribute(propagation), methodLevel);
|
||||
new TransactionTemplate(txMgr, txAttr).executeWithoutResult(s -> populator.execute(finalDataSource));
|
||||
}
|
||||
}
|
||||
@@ -458,7 +459,8 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
|
||||
|
||||
private static boolean isValidMethodLevelPhase(ExecutionPhase executionPhase) {
|
||||
// Class-level phases cannot be used on methods.
|
||||
return executionPhase == ExecutionPhase.BEFORE_TEST_METHOD ||
|
||||
executionPhase == ExecutionPhase.AFTER_TEST_METHOD;
|
||||
return (executionPhase == ExecutionPhase.BEFORE_TEST_METHOD ||
|
||||
executionPhase == ExecutionPhase.AFTER_TEST_METHOD);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -41,7 +41,6 @@ import org.springframework.util.StringUtils;
|
||||
* @author Sam Brannen
|
||||
* @author Juergen Hoeller
|
||||
* @author Rob Harrop
|
||||
* @author Andreas Ahlenstorf
|
||||
* @since 4.0
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
@@ -167,11 +166,6 @@ public class DefaultTestContext implements TestContext {
|
||||
return testInstance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTestMethod() {
|
||||
return this.testMethod != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Method getTestMethod() {
|
||||
Method testMethod = this.testMethod;
|
||||
|
||||
@@ -228,8 +228,7 @@ public abstract class TestContextTransactionUtils {
|
||||
/**
|
||||
* Create a delegating {@link TransactionAttribute} for the supplied target
|
||||
* {@link TransactionAttribute} and {@link TestContext}, using the names of
|
||||
* the test class and test method (if available) to build the name of the
|
||||
* transaction.
|
||||
* the test class and test method to build the name of the transaction.
|
||||
* @param testContext the {@code TestContext} upon which to base the name
|
||||
* @param targetAttribute the {@code TransactionAttribute} to delegate to
|
||||
* @return the delegating {@code TransactionAttribute}
|
||||
@@ -237,9 +236,27 @@ public abstract class TestContextTransactionUtils {
|
||||
public static TransactionAttribute createDelegatingTransactionAttribute(
|
||||
TestContext testContext, TransactionAttribute targetAttribute) {
|
||||
|
||||
return createDelegatingTransactionAttribute(testContext, targetAttribute, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a delegating {@link TransactionAttribute} for the supplied target
|
||||
* {@link TransactionAttribute} and {@link TestContext}, using the names of
|
||||
* the test class and test method (if requested) to build the name of the
|
||||
* transaction.
|
||||
* @param testContext the {@code TestContext} upon which to base the name
|
||||
* @param targetAttribute the {@code TransactionAttribute} to delegate to
|
||||
* @param includeMethodName {@code true} if the test method's name should be
|
||||
* included in the name of the transaction
|
||||
* @return the delegating {@code TransactionAttribute}
|
||||
* @since 6.1
|
||||
*/
|
||||
public static TransactionAttribute createDelegatingTransactionAttribute(
|
||||
TestContext testContext, TransactionAttribute targetAttribute, boolean includeMethodName) {
|
||||
|
||||
Assert.notNull(testContext, "TestContext must not be null");
|
||||
Assert.notNull(targetAttribute, "Target TransactionAttribute must not be null");
|
||||
return new TestContextTransactionAttribute(targetAttribute, testContext);
|
||||
return new TestContextTransactionAttribute(targetAttribute, testContext, includeMethodName);
|
||||
}
|
||||
|
||||
|
||||
@@ -248,10 +265,12 @@ public abstract class TestContextTransactionUtils {
|
||||
|
||||
private final String name;
|
||||
|
||||
public TestContextTransactionAttribute(TransactionAttribute targetAttribute, TestContext testContext) {
|
||||
public TestContextTransactionAttribute(
|
||||
TransactionAttribute targetAttribute, TestContext testContext, boolean includeMethodName) {
|
||||
|
||||
super(targetAttribute);
|
||||
|
||||
if (testContext.hasTestMethod()) {
|
||||
if (includeMethodName) {
|
||||
this.name = ClassUtils.getQualifiedMethodName(testContext.getTestMethod(), testContext.getTestClass());
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user