Browse Source

Rename SQL script annotations in the TCF

Prior to this commit, SQL script annotations and related classes in the
TestContext framework (TCF) were named DatabaseInitializer*. However,
these annotations are not used only for initialization and are
therefore misleading when used for cleaning up the database.

This commit refines the names of annotations and related classes for
configuring SQL scripts to be executed for integration tests in the TCF
as follows:

- @DatabaseInitializer -> @Sql
- @DatabaseInitializers -> @SqlGroup
- DatabaseInitializerTestExecutionListener -> SqlScriptsTestExecutionListener

A special thanks goes out to the following attendees of the Zurich
Hackergarten meeting last night for their collective brainstorming:
@aalmiray, @atsticks, @ollin, @simkuenzi, @tangresh, @vyazelenko.

Issue: SPR-7655
pull/1385/head
Sam Brannen 11 years ago
parent
commit
abdb010fc8
  1. 29
      spring-test/src/main/java/org/springframework/test/context/jdbc/Sql.java
  2. 18
      spring-test/src/main/java/org/springframework/test/context/jdbc/SqlGroup.java
  3. 120
      spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java
  4. 6
      spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java
  5. 6
      spring-test/src/main/java/org/springframework/test/context/support/DefaultTestContextBootstrapper.java
  6. 6
      spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java
  7. 10
      spring-test/src/test/java/org/springframework/test/context/jdbc/CustomScriptSyntaxSqlScriptsTests.java
  8. 6
      spring-test/src/test/java/org/springframework/test/context/jdbc/DefaultScriptDetectionSqlScriptsTests.java
  9. 0
      spring-test/src/test/java/org/springframework/test/context/jdbc/DefaultScriptDetectionSqlScriptsTests.methodLevel.sql
  10. 0
      spring-test/src/test/java/org/springframework/test/context/jdbc/DefaultScriptDetectionSqlScriptsTests.sql
  11. 27
      spring-test/src/test/java/org/springframework/test/context/jdbc/MetaAnnotationSqlScriptsTests.java
  12. 12
      spring-test/src/test/java/org/springframework/test/context/jdbc/MultipleDataSourcesAndTransactionManagersSqlScriptsTests.java
  13. 8
      spring-test/src/test/java/org/springframework/test/context/jdbc/MultipleDataSourcesAndTransactionManagersTransactionalSqlScriptsTests.java
  14. 11
      spring-test/src/test/java/org/springframework/test/context/jdbc/NonTransactionalSqlScriptsTests.java
  15. 6
      spring-test/src/test/java/org/springframework/test/context/jdbc/PopulatedSchemaTransactionalSqlScriptsTests.java
  16. 6
      spring-test/src/test/java/org/springframework/test/context/jdbc/RequiresNewTransactionSqlScriptsTests.java
  17. 12
      spring-test/src/test/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListenerTests.java
  18. 17
      spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalAfterTestMethodSqlScriptsTests.java
  19. 9
      spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java

29
spring-test/src/main/java/org/springframework/test/context/jdbc/DatabaseInitializer.java → spring-test/src/main/java/org/springframework/test/context/jdbc/Sql.java

@ -29,23 +29,24 @@ import static java.lang.annotation.ElementType.*; @@ -29,23 +29,24 @@ import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
/**
* {@code @DatabaseInitializer} is used to annotate a test class or test method
* to configure SQL scripts to be executed against a given database during
* integration tests.
* {@code @Sql} is used to annotate a test class or test method to configure SQL
* scripts to be executed against a given database during integration tests.
*
* <p>Method-level declarations override class-level declarations.
*
* <p>Script execution is performed by the {@link DatabaseInitializerTestExecutionListener},
* <p>Script execution is performed by the {@link SqlScriptsTestExecutionListener},
* which is enabled by default.
*
* <p>The configuration options provided by this annotation are a superset of
* those provided by the {@code <jdbc:initialize-database />} XML namespace
* element. Consult the Javadoc of individual attributes for details.
* <p>The configuration options provided by this annotation are equivalent to
* those supported by {@link ScriptUtils} and {@link org.springframework.jdbc.datasource.init.ResourceDatabasePopulator
* ResourceDatabasePopulator} but are a superset of those provided by the
* {@code <jdbc:initialize-database />} XML namespace element. Consult the
* Javadoc of individual attributes in this annotation for details.
*
* <p>Beginning with Java 8, {@code @DatabaseInitializer} can be used as a
* <p>Beginning with Java 8, {@code @Sql} can be used as a
* <em>{@linkplain Repeatable repeatable}</em> annotation. Otherwise,
* {@link DatabaseInitializers @DatabaseInitializers} can be used as an explicit
* container for declaring multiple instances of {@code @DatabaseInitializer}.
* {@link SqlGroup @SqlGroup} can be used as an explicit container for declaring
* multiple instances of {@code @Sql}.
*
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom
* <em>composed annotations</em>; however, attribute overrides are not currently
@ -55,8 +56,8 @@ import static java.lang.annotation.RetentionPolicy.*; @@ -55,8 +56,8 @@ import static java.lang.annotation.RetentionPolicy.*;
* @author Sam Brannen
* @author Tadaya Tsuyukubo
* @since 4.1
* @see DatabaseInitializers
* @see DatabaseInitializerTestExecutionListener
* @see SqlGroup
* @see SqlScriptsTestExecutionListener
* @see org.springframework.transaction.annotation.Transactional
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
* @see org.springframework.jdbc.datasource.init.ResourceDatabasePopulator
@ -66,8 +67,8 @@ import static java.lang.annotation.RetentionPolicy.*; @@ -66,8 +67,8 @@ import static java.lang.annotation.RetentionPolicy.*;
@Inherited
@Retention(RUNTIME)
@Target({ TYPE, METHOD })
@Repeatable(DatabaseInitializers.class)
public @interface DatabaseInitializer {
@Repeatable(SqlGroup.class)
public @interface Sql {
/**
* Enumeration of <em>phases</em> that dictate when SQL scripts are executed.

18
spring-test/src/main/java/org/springframework/test/context/jdbc/DatabaseInitializers.java → spring-test/src/main/java/org/springframework/test/context/jdbc/SqlGroup.java

@ -25,28 +25,26 @@ import static java.lang.annotation.ElementType.*; @@ -25,28 +25,26 @@ import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
/**
* Container annotation that aggregates several {@link DatabaseInitializer}
* annotations.
* Container annotation that aggregates several {@link Sql @Sql} annotations.
*
* <p>Can be used natively, declaring several nested {@link DatabaseInitializer}
* annotations. Can also be used in conjunction with Java 8's support for
* repeatable annotations, where {@code @DatabaseInitializer} can simply be
* declared several times on the same class or method, implicitly generating
* this container annotation.
* <p>Can be used natively, declaring several nested {@code @Sql} annotations.
* Can also be used in conjunction with Java 8's support for repeatable
* annotations, where {@code @Sql} can simply be declared several times on the
* same class or method, implicitly generating this container annotation.
*
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom
* <em>composed annotations</em>.
*
* @author Sam Brannen
* @since 4.1
* @see DatabaseInitializer
* @see Sql
*/
@Documented
@Inherited
@Retention(RUNTIME)
@Target({ TYPE, METHOD })
public @interface DatabaseInitializers {
public @interface SqlGroup {
DatabaseInitializer[] value();
Sql[] value();
}

120
spring-test/src/main/java/org/springframework/test/context/jdbc/DatabaseInitializerTestExecutionListener.java → spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java

@ -28,7 +28,7 @@ import org.springframework.core.annotation.AnnotationUtils; @@ -28,7 +28,7 @@ import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.jdbc.DatabaseInitializer.ExecutionPhase;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
import org.springframework.test.context.support.AbstractTestExecutionListener;
import org.springframework.test.context.transaction.TestContextTransactionUtils;
import org.springframework.test.context.util.TestContextResourceUtils;
@ -45,17 +45,20 @@ import org.springframework.util.ResourceUtils; @@ -45,17 +45,20 @@ import org.springframework.util.ResourceUtils;
/**
* {@code TestExecutionListener} that provides support for executing SQL scripts
* configured via the {@link DatabaseInitializer @DatabaseInitializer} annotation.
* configured via the {@link Sql @Sql} annotation.
*
* <p>SQL scripts will be executed {@linkplain #beforeTestMethod(TestContext) before}
* <p>Scripts will be executed {@linkplain #beforeTestMethod(TestContext) before}
* or {@linkplain #afterTestMethod(TestContext) after} execution of the corresponding
* {@linkplain java.lang.reflect.Method test method}, depending on the configured
* value of the {@link DatabaseInitializer#requireNewTransaction requireNewTransaction}
* flag.
* value of the {@link Sql#executionPhase executionPhase} flag.
*
* <p>Scripts will be executed either within an existing Spring-managed transaction
* or within a new, isolated transaction, depending on the configured value of the
* {@link Sql#requireNewTransaction requireNewTransaction} flag.
*
* <h3>Script Resources</h3>
* <p>For details on default script detection and how explicit script locations
* are interpreted, see {@link DatabaseInitializer#scripts}.
* are interpreted, see {@link Sql#scripts}.
*
* <h3>Required Spring Beans</h3>
* <p>A {@link DataSource} and {@link PlatformTransactionManager} must be defined
@ -66,106 +69,105 @@ import org.springframework.util.ResourceUtils; @@ -66,106 +69,105 @@ import org.springframework.util.ResourceUtils;
*
* @author Sam Brannen
* @since 4.1
* @see DatabaseInitializer
* @see DatabaseInitializers
* @see Sql
* @see SqlGroup
* @see org.springframework.test.context.transaction.TestContextTransactionUtils
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
* @see org.springframework.jdbc.datasource.init.ResourceDatabasePopulator
* @see org.springframework.jdbc.datasource.init.ScriptUtils
*/
public class DatabaseInitializerTestExecutionListener extends AbstractTestExecutionListener {
public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListener {
private static final Log logger = LogFactory.getLog(DatabaseInitializerTestExecutionListener.class);
private static final Log logger = LogFactory.getLog(SqlScriptsTestExecutionListener.class);
/**
* Execute SQL scripts configured via {@link DatabaseInitializer @DatabaseInitializer}
* for the supplied {@link TestContext} <em>before</em> the current test method.
* Execute SQL scripts configured via {@link Sql @Sql} for the supplied
* {@link TestContext} <em>before</em> the current test method.
*/
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
executeDatabaseInitializers(testContext, ExecutionPhase.BEFORE_TEST_METHOD);
executeSqlScripts(testContext, ExecutionPhase.BEFORE_TEST_METHOD);
}
/**
* Execute SQL scripts configured via {@link DatabaseInitializer @DatabaseInitializer}
* for the supplied {@link TestContext} <em>after</em> the current test method.
* Execute SQL scripts configured via {@link Sql @Sql} for the supplied
* {@link TestContext} <em>after</em> the current test method.
*/
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
executeDatabaseInitializers(testContext, ExecutionPhase.AFTER_TEST_METHOD);
executeSqlScripts(testContext, ExecutionPhase.AFTER_TEST_METHOD);
}
/**
* Execute SQL scripts configured via {@link DatabaseInitializer @DatabaseInitializer}
* for the supplied {@link TestContext} and {@link ExecutionPhase}.
* Execute SQL scripts configured via {@link Sql @Sql} for the supplied
* {@link TestContext} and {@link ExecutionPhase}.
*/
private void executeDatabaseInitializers(TestContext testContext, ExecutionPhase executionPhase) throws Exception {
private void executeSqlScripts(TestContext testContext, ExecutionPhase executionPhase) throws Exception {
boolean classLevel = false;
Set<DatabaseInitializer> databaseInitializers = AnnotationUtils.getRepeatableAnnotation(
testContext.getTestMethod(), DatabaseInitializers.class, DatabaseInitializer.class);
if (databaseInitializers.isEmpty()) {
databaseInitializers = AnnotationUtils.getRepeatableAnnotation(testContext.getTestClass(),
DatabaseInitializers.class, DatabaseInitializer.class);
if (!databaseInitializers.isEmpty()) {
Set<Sql> sqlAnnotations = AnnotationUtils.getRepeatableAnnotation(testContext.getTestMethod(), SqlGroup.class,
Sql.class);
if (sqlAnnotations.isEmpty()) {
sqlAnnotations = AnnotationUtils.getRepeatableAnnotation(testContext.getTestClass(), SqlGroup.class,
Sql.class);
if (!sqlAnnotations.isEmpty()) {
classLevel = true;
}
}
for (DatabaseInitializer databaseInitializer : databaseInitializers) {
executeDatabaseInitializer(databaseInitializer, executionPhase, testContext, classLevel);
for (Sql sql : sqlAnnotations) {
executeSqlScripts(sql, executionPhase, testContext, classLevel);
}
}
/**
* Execute the SQL scripts configured via the supplied
* {@link DatabaseInitializer @DatabaseInitializer} for the given
* {@link ExecutionPhase} and {@link TestContext}.
* Execute the SQL scripts configured via the supplied {@link Sql @Sql}
* annotation for the given {@link ExecutionPhase} and {@link TestContext}.
*
* <p>Special care must be taken in order to properly support the
* {@link DatabaseInitializer#requireNewTransaction requireNewTransaction}
* {@link Sql#requireNewTransaction requireNewTransaction}
* flag.
*
* @param databaseInitializer the {@code @DatabaseInitializer} to parse
* @param sql the {@code @Sql} annotation to parse
* @param executionPhase the current execution phase
* @param testContext the current {@code TestContext}
* @param classLevel {@code true} if {@link DatabaseInitializer @DatabaseInitializer}
* was declared at the class level
* @param classLevel {@code true} if {@link Sql @Sql} was declared at the
* class level
*/
@SuppressWarnings("serial")
private void executeDatabaseInitializer(DatabaseInitializer databaseInitializer, ExecutionPhase executionPhase,
TestContext testContext, boolean classLevel) throws Exception {
private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestContext testContext, boolean classLevel)
throws Exception {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Processing %s for execution phase [%s] and test context %s.",
databaseInitializer, executionPhase, testContext));
logger.debug(String.format("Processing %s for execution phase [%s] and test context %s.", sql,
executionPhase, testContext));
}
if (executionPhase != databaseInitializer.executionPhase()) {
if (executionPhase != sql.executionPhase()) {
return;
}
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.setSqlScriptEncoding(databaseInitializer.encoding());
populator.setSeparator(databaseInitializer.separator());
populator.setCommentPrefix(databaseInitializer.commentPrefix());
populator.setBlockCommentStartDelimiter(databaseInitializer.blockCommentStartDelimiter());
populator.setBlockCommentEndDelimiter(databaseInitializer.blockCommentEndDelimiter());
populator.setContinueOnError(databaseInitializer.continueOnError());
populator.setIgnoreFailedDrops(databaseInitializer.ignoreFailedDrops());
String[] scripts = getScripts(databaseInitializer, testContext, classLevel);
populator.setSqlScriptEncoding(sql.encoding());
populator.setSeparator(sql.separator());
populator.setCommentPrefix(sql.commentPrefix());
populator.setBlockCommentStartDelimiter(sql.blockCommentStartDelimiter());
populator.setBlockCommentEndDelimiter(sql.blockCommentEndDelimiter());
populator.setContinueOnError(sql.continueOnError());
populator.setIgnoreFailedDrops(sql.ignoreFailedDrops());
String[] scripts = getScripts(sql, testContext, classLevel);
scripts = TestContextResourceUtils.convertToClasspathResourcePaths(testContext.getTestClass(), scripts);
populator.setScripts(TestContextResourceUtils.convertToResources(testContext.getApplicationContext(), scripts));
if (logger.isDebugEnabled()) {
logger.debug("Executing SQL scripts: " + ObjectUtils.nullSafeToString(scripts));
}
final DataSource dataSource = TestContextTransactionUtils.retrieveDataSource(testContext,
databaseInitializer.dataSource());
final DataSource dataSource = TestContextTransactionUtils.retrieveDataSource(testContext, sql.dataSource());
final PlatformTransactionManager transactionManager = TestContextTransactionUtils.retrieveTransactionManager(
testContext, databaseInitializer.transactionManager());
testContext, sql.transactionManager());
int propagation = databaseInitializer.requireNewTransaction() ? TransactionDefinition.PROPAGATION_REQUIRES_NEW
int propagation = sql.requireNewTransaction() ? TransactionDefinition.PROPAGATION_REQUIRES_NEW
: TransactionDefinition.PROPAGATION_REQUIRED;
TransactionAttribute transactionAttribute = TestContextTransactionUtils.createDelegatingTransactionAttribute(
@ -180,9 +182,9 @@ public class DatabaseInitializerTestExecutionListener extends AbstractTestExecut @@ -180,9 +182,9 @@ public class DatabaseInitializerTestExecutionListener extends AbstractTestExecut
});
}
private String[] getScripts(DatabaseInitializer databaseInitializer, TestContext testContext, boolean classLevel) {
String[] scripts = databaseInitializer.scripts();
String[] value = databaseInitializer.value();
private String[] getScripts(Sql sql, TestContext testContext, boolean classLevel) {
String[] scripts = sql.scripts();
String[] value = sql.value();
boolean scriptsDeclared = !ObjectUtils.isEmpty(scripts);
boolean valueDeclared = !ObjectUtils.isEmpty(value);
@ -190,9 +192,9 @@ public class DatabaseInitializerTestExecutionListener extends AbstractTestExecut @@ -190,9 +192,9 @@ public class DatabaseInitializerTestExecutionListener extends AbstractTestExecut
String elementType = (classLevel ? "class" : "method");
String elementName = (classLevel ? testContext.getTestClass().getName()
: testContext.getTestMethod().toString());
String msg = String.format("Test %s [%s] has been configured with @DatabaseInitializer's 'value' [%s] "
String msg = String.format("Test %s [%s] has been configured with @Sql's 'value' [%s] "
+ "and 'scripts' [%s] attributes. Only one declaration of SQL script "
+ "paths is permitted per @DatabaseInitializer annotation.", elementType, elementName,
+ "paths is permitted per @Sql annotation.", elementType, elementName,
ObjectUtils.nullSafeToString(value), ObjectUtils.nullSafeToString(scripts));
logger.error(msg);
throw new IllegalStateException(msg);
@ -208,7 +210,7 @@ public class DatabaseInitializerTestExecutionListener extends AbstractTestExecut @@ -208,7 +210,7 @@ public class DatabaseInitializerTestExecutionListener extends AbstractTestExecut
/**
* Detect a default SQL script by implementing the algorithm defined in
* {@link DatabaseInitializer#scripts}.
* {@link Sql#scripts}.
*/
private String detectDefaultScript(TestContext testContext, boolean classLevel) {
Class<?> clazz = testContext.getTestClass();
@ -234,7 +236,7 @@ public class DatabaseInitializerTestExecutionListener extends AbstractTestExecut @@ -234,7 +236,7 @@ public class DatabaseInitializerTestExecutionListener extends AbstractTestExecut
}
else {
String msg = String.format("Could not detect default SQL script for test %s [%s]: "
+ "%s does not exist. Either declare scripts via @DatabaseInitializer or make the "
+ "%s does not exist. Either declare scripts via @Sql or make the "
+ "default SQL script available.", elementType, elementName, classPathResource);
logger.error(msg);
throw new IllegalStateException(msg);

6
spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java

@ -26,7 +26,7 @@ import org.springframework.jdbc.core.JdbcTemplate; @@ -26,7 +26,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.jdbc.DatabaseInitializerTestExecutionListener;
import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
@ -64,7 +64,7 @@ import org.springframework.transaction.annotation.Transactional; @@ -64,7 +64,7 @@ import org.springframework.transaction.annotation.Transactional;
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.TestExecutionListeners
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
* @see org.springframework.test.context.jdbc.DatabaseInitializerTestExecutionListener
* @see org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener
* @see org.springframework.test.context.transaction.TransactionConfiguration
* @see org.springframework.transaction.annotation.Transactional
* @see org.springframework.test.annotation.Rollback
@ -73,7 +73,7 @@ import org.springframework.transaction.annotation.Transactional; @@ -73,7 +73,7 @@ import org.springframework.transaction.annotation.Transactional;
* @see org.springframework.test.jdbc.JdbcTestUtils
* @see org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests
*/
@TestExecutionListeners({ TransactionalTestExecutionListener.class, DatabaseInitializerTestExecutionListener.class })
@TestExecutionListeners({ TransactionalTestExecutionListener.class, SqlScriptsTestExecutionListener.class })
@Transactional
public abstract class AbstractTransactionalJUnit4SpringContextTests extends AbstractJUnit4SpringContextTests {

6
spring-test/src/main/java/org/springframework/test/context/support/DefaultTestContextBootstrapper.java

@ -38,7 +38,7 @@ import org.springframework.test.context.TestExecutionListener; @@ -38,7 +38,7 @@ import org.springframework.test.context.TestExecutionListener;
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
* <li>{@link org.springframework.test.context.transaction.TransactionalTestExecutionListener}
* <li>{@link org.springframework.test.context.jdbc.DatabaseInitializerTestExecutionListener}
* <li>{@link org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener}
* </ol>
* <li>Uses {@link DelegatingSmartContextLoader} as the default {@link ContextLoader}.
* <li>Builds a standard {@link MergedContextConfiguration}.
@ -53,7 +53,7 @@ public class DefaultTestContextBootstrapper extends AbstractTestContextBootstrap @@ -53,7 +53,7 @@ public class DefaultTestContextBootstrapper extends AbstractTestContextBootstrap
"org.springframework.test.context.support.DependencyInjectionTestExecutionListener",
"org.springframework.test.context.support.DirtiesContextTestExecutionListener",
"org.springframework.test.context.transaction.TransactionalTestExecutionListener",
"org.springframework.test.context.jdbc.DatabaseInitializerTestExecutionListener"));
"org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener"));
/**
@ -63,7 +63,7 @@ public class DefaultTestContextBootstrapper extends AbstractTestContextBootstrap @@ -63,7 +63,7 @@ public class DefaultTestContextBootstrapper extends AbstractTestContextBootstrap
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
* <li>{@link org.springframework.test.context.transaction.TransactionalTestExecutionListener}
* <li>{@link org.springframework.test.context.jdbc.DatabaseInitializerTestExecutionListener}
* <li>{@link org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener}
* </ol>
*/
protected List<String> getDefaultTestExecutionListenerClassNames() {

6
spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java

@ -25,7 +25,7 @@ import org.springframework.dao.DataAccessException; @@ -25,7 +25,7 @@ import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.jdbc.DatabaseInitializerTestExecutionListener;
import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
@ -55,7 +55,7 @@ import org.springframework.transaction.annotation.Transactional; @@ -55,7 +55,7 @@ import org.springframework.transaction.annotation.Transactional;
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.TestExecutionListeners
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
* @see org.springframework.test.context.jdbc.DatabaseInitializerTestExecutionListener
* @see org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener
* @see org.springframework.test.context.transaction.TransactionConfiguration
* @see org.springframework.transaction.annotation.Transactional
* @see org.springframework.test.annotation.Rollback
@ -64,7 +64,7 @@ import org.springframework.transaction.annotation.Transactional; @@ -64,7 +64,7 @@ import org.springframework.transaction.annotation.Transactional;
* @see org.springframework.test.jdbc.JdbcTestUtils
* @see org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests
*/
@TestExecutionListeners({ TransactionalTestExecutionListener.class, DatabaseInitializerTestExecutionListener.class })
@TestExecutionListeners({ TransactionalTestExecutionListener.class, SqlScriptsTestExecutionListener.class })
@Transactional
public abstract class AbstractTransactionalTestNGSpringContextTests extends AbstractTestNGSpringContextTests {

10
spring-test/src/test/java/org/springframework/test/context/jdbc/CustomScriptSyntaxDatabaseInitializerTests.java → spring-test/src/test/java/org/springframework/test/context/jdbc/CustomScriptSyntaxSqlScriptsTests.java

@ -25,19 +25,19 @@ import static org.junit.Assert.*; @@ -25,19 +25,19 @@ import static org.junit.Assert.*;
/**
* Integration tests that verify support for custom SQL script syntax
* configured via {@link DatabaseInitializer @DatabaseInitializer}.
* configured via {@link Sql @Sql}.
*
* @author Sam Brannen
* @since 4.1
*/
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
@DirtiesContext
public class CustomScriptSyntaxDatabaseInitializerTests extends AbstractTransactionalJUnit4SpringContextTests {
public class CustomScriptSyntaxSqlScriptsTests extends AbstractTransactionalJUnit4SpringContextTests {
@Test
@DatabaseInitializers({//
@DatabaseInitializer("schema.sql"),//
@DatabaseInitializer(//
@SqlGroup({//
@Sql("schema.sql"),//
@Sql(//
scripts = "data-add-users-with-custom-script-syntax.sql",//
commentPrefix = "`",//
blockCommentStartDelimiter = "#$",//

6
spring-test/src/test/java/org/springframework/test/context/jdbc/DefaultScriptDetectionDatabaseInitializerTests.java → spring-test/src/test/java/org/springframework/test/context/jdbc/DefaultScriptDetectionSqlScriptsTests.java

@ -30,9 +30,9 @@ import static org.junit.Assert.*; @@ -30,9 +30,9 @@ import static org.junit.Assert.*;
* @since 4.1
*/
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
@DatabaseInitializer
@Sql
@DirtiesContext
public class DefaultScriptDetectionDatabaseInitializerTests extends AbstractTransactionalJUnit4SpringContextTests {
public class DefaultScriptDetectionSqlScriptsTests extends AbstractTransactionalJUnit4SpringContextTests {
@Test
public void classLevel() {
@ -40,7 +40,7 @@ public class DefaultScriptDetectionDatabaseInitializerTests extends AbstractTran @@ -40,7 +40,7 @@ public class DefaultScriptDetectionDatabaseInitializerTests extends AbstractTran
}
@Test
@DatabaseInitializer
@Sql
public void methodLevel() {
assertNumUsers(3);
}

0
spring-test/src/test/java/org/springframework/test/context/jdbc/DefaultScriptDetectionDatabaseInitializerTests.methodLevel.sql → spring-test/src/test/java/org/springframework/test/context/jdbc/DefaultScriptDetectionSqlScriptsTests.methodLevel.sql

0
spring-test/src/test/java/org/springframework/test/context/jdbc/DefaultScriptDetectionDatabaseInitializerTests.sql → spring-test/src/test/java/org/springframework/test/context/jdbc/DefaultScriptDetectionSqlScriptsTests.sql

27
spring-test/src/test/java/org/springframework/test/context/jdbc/MetaAnnotationDatabaseInitializerTests.java → spring-test/src/test/java/org/springframework/test/context/jdbc/MetaAnnotationSqlScriptsTests.java

@ -29,26 +29,25 @@ import static java.lang.annotation.RetentionPolicy.*; @@ -29,26 +29,25 @@ import static java.lang.annotation.RetentionPolicy.*;
import static org.junit.Assert.*;
/**
* Integration tests that verify support for using
* {@link DatabaseInitializer @DatabaseInitializer} and
* {@link DatabaseInitializers @DatabaseInitializers} as a meta-annotations.
* Integration tests that verify support for using {@link Sql @Sql} and
* {@link SqlGroup @SqlGroup} as a meta-annotations.
*
* @author Sam Brannen
* @since 4.1
*/
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
@DirtiesContext
public class MetaAnnotationDatabaseInitializerTests extends AbstractTransactionalJUnit4SpringContextTests {
public class MetaAnnotationSqlScriptsTests extends AbstractTransactionalJUnit4SpringContextTests {
@Test
@MetaDbInitializer
public void metaDatabaseInitializer() {
@MetaSql
public void metaSqlAnnotation() {
assertNumUsers(1);
}
@Test
@MetaDbInitializers
public void metaDatabaseInitializers() {
@MetaSqlGroup
public void metaSqlGroupAnnotation() {
assertNumUsers(1);
}
@ -57,20 +56,16 @@ public class MetaAnnotationDatabaseInitializerTests extends AbstractTransactiona @@ -57,20 +56,16 @@ public class MetaAnnotationDatabaseInitializerTests extends AbstractTransactiona
}
@DatabaseInitializer({ "drop-schema.sql", "schema.sql", "data.sql" })
@Sql({ "drop-schema.sql", "schema.sql", "data.sql" })
@Retention(RUNTIME)
@Target(METHOD)
static @interface MetaDbInitializer {
static @interface MetaSql {
}
@DatabaseInitializers({//
@DatabaseInitializer("drop-schema.sql"),//
@DatabaseInitializer("schema.sql"),//
@DatabaseInitializer("data.sql") //
})
@SqlGroup({ @Sql("drop-schema.sql"), @Sql("schema.sql"), @Sql("data.sql") })
@Retention(RUNTIME)
@Target(METHOD)
static @interface MetaDbInitializers {
static @interface MetaSqlGroup {
}
}

12
spring-test/src/test/java/org/springframework/test/context/jdbc/MultipleDataSourcesAndTransactionManagersDatabaseInitializerTests.java → spring-test/src/test/java/org/springframework/test/context/jdbc/MultipleDataSourcesAndTransactionManagersSqlScriptsTests.java

@ -34,17 +34,17 @@ import org.springframework.transaction.PlatformTransactionManager; @@ -34,17 +34,17 @@ import org.springframework.transaction.PlatformTransactionManager;
import static org.junit.Assert.*;
/**
* Integration tests for {@link DatabaseInitializer @DatabaseInitializer} that
* verify support for multiple {@link DataSource}s and {@link PlatformTransactionManager}s.
* Integration tests for {@link Sql @Sql} that verify support for multiple
* {@link DataSource}s and {@link PlatformTransactionManager}s.
*
* @author Sam Brannen
* @since 4.1
* @see MultipleDataSourcesAndTransactionManagersTransactionalDatabaseInitializerTests
* @see MultipleDataSourcesAndTransactionManagersTransactionalSqlScriptsTests
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@DirtiesContext
public class MultipleDataSourcesAndTransactionManagersDatabaseInitializerTests {
public class MultipleDataSourcesAndTransactionManagersSqlScriptsTests {
@Autowired
private DataSource dataSource1;
@ -54,13 +54,13 @@ public class MultipleDataSourcesAndTransactionManagersDatabaseInitializerTests { @@ -54,13 +54,13 @@ public class MultipleDataSourcesAndTransactionManagersDatabaseInitializerTests {
@Test
@DatabaseInitializer(scripts = "data-add-dogbert.sql", dataSource = "dataSource1", transactionManager = "txMgr1")
@Sql(scripts = "data-add-dogbert.sql", dataSource = "dataSource1", transactionManager = "txMgr1")
public void database1() {
assertUsersExist(new JdbcTemplate(dataSource1), "Dilbert", "Dogbert");
}
@Test
@DatabaseInitializer(scripts = "data-add-catbert.sql", dataSource = "dataSource2", transactionManager = "txMgr2")
@Sql(scripts = "data-add-catbert.sql", dataSource = "dataSource2", transactionManager = "txMgr2")
public void database2() {
assertUsersExist(new JdbcTemplate(dataSource2), "Dilbert", "Catbert");
}

8
spring-test/src/test/java/org/springframework/test/context/jdbc/MultipleDataSourcesAndTransactionManagersTransactionalDatabaseInitializerTests.java → spring-test/src/test/java/org/springframework/test/context/jdbc/MultipleDataSourcesAndTransactionManagersTransactionalSqlScriptsTests.java

@ -35,7 +35,7 @@ import org.springframework.transaction.annotation.Transactional; @@ -35,7 +35,7 @@ import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*;
/**
* Exact copy of {@link MultipleDataSourcesAndTransactionManagersDatabaseInitializerTests},
* Exact copy of {@link MultipleDataSourcesAndTransactionManagersSqlScriptsTests},
* except that the test methods are transactional.
*
* @author Sam Brannen
@ -44,7 +44,7 @@ import static org.junit.Assert.*; @@ -44,7 +44,7 @@ import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@DirtiesContext
public class MultipleDataSourcesAndTransactionManagersTransactionalDatabaseInitializerTests {
public class MultipleDataSourcesAndTransactionManagersTransactionalSqlScriptsTests {
@Autowired
private DataSource dataSource1;
@ -55,14 +55,14 @@ public class MultipleDataSourcesAndTransactionManagersTransactionalDatabaseIniti @@ -55,14 +55,14 @@ public class MultipleDataSourcesAndTransactionManagersTransactionalDatabaseIniti
@Test
@Transactional("txMgr1")
@DatabaseInitializer(scripts = "data-add-dogbert.sql", dataSource = "dataSource1", transactionManager = "txMgr1")
@Sql(scripts = "data-add-dogbert.sql", dataSource = "dataSource1", transactionManager = "txMgr1")
public void database1() {
assertUsersExist(new JdbcTemplate(dataSource1), "Dilbert", "Dogbert");
}
@Test
@Transactional("txMgr2")
@DatabaseInitializer(scripts = "data-add-catbert.sql", dataSource = "dataSource2", transactionManager = "txMgr2")
@Sql(scripts = "data-add-catbert.sql", dataSource = "dataSource2", transactionManager = "txMgr2")
public void database2() {
assertUsersExist(new JdbcTemplate(dataSource2), "Dilbert", "Catbert");
}

11
spring-test/src/test/java/org/springframework/test/context/jdbc/NonTransactionalDatabaseInitializerTests.java → spring-test/src/test/java/org/springframework/test/context/jdbc/NonTransactionalSqlScriptsTests.java

@ -32,9 +32,8 @@ import org.springframework.test.jdbc.JdbcTestUtils; @@ -32,9 +32,8 @@ import org.springframework.test.jdbc.JdbcTestUtils;
import static org.junit.Assert.*;
/**
* Integration tests which verify that scripts executed via
* {@link DatabaseInitializer @DatabaseInitializer} will persist between
* non-transactional test methods.
* Integration tests which verify that scripts executed via {@link Sql @Sql}
* will persist between non-transactional test methods.
*
* @author Sam Brannen
* @since 4.1
@ -42,9 +41,9 @@ import static org.junit.Assert.*; @@ -42,9 +41,9 @@ import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
@DatabaseInitializer({ "schema.sql", "data.sql" })
@Sql({ "schema.sql", "data.sql" })
@DirtiesContext
public class NonTransactionalDatabaseInitializerTests {
public class NonTransactionalSqlScriptsTests {
protected JdbcTemplate jdbcTemplate;
@ -61,7 +60,7 @@ public class NonTransactionalDatabaseInitializerTests { @@ -61,7 +60,7 @@ public class NonTransactionalDatabaseInitializerTests {
}
@Test
@DatabaseInitializers(@DatabaseInitializer("data-add-dogbert.sql"))
@Sql("data-add-dogbert.sql")
// test##_ prefix is required for @FixMethodOrder.
public void test02_methodLevelScripts() {
assertNumUsers(2);

6
spring-test/src/test/java/org/springframework/test/context/jdbc/PopulatedSchemaTransactionalDatabaseInitializerTests.java → spring-test/src/test/java/org/springframework/test/context/jdbc/PopulatedSchemaTransactionalSqlScriptsTests.java

@ -27,14 +27,14 @@ import static org.junit.Assert.*; @@ -27,14 +27,14 @@ import static org.junit.Assert.*;
/**
* Transactional integration tests that verify rollback semantics for
* {@link DatabaseInitializer @DatabaseInitializer} support.
* {@link Sql @Sql} support.
*
* @author Sam Brannen
* @since 4.1
*/
@ContextConfiguration(classes = PopulatedSchemaDatabaseConfig.class)
@DirtiesContext
public class PopulatedSchemaTransactionalDatabaseInitializerTests extends AbstractTransactionalJUnit4SpringContextTests {
public class PopulatedSchemaTransactionalSqlScriptsTests extends AbstractTransactionalJUnit4SpringContextTests {
@BeforeTransaction
@AfterTransaction
@ -43,7 +43,7 @@ public class PopulatedSchemaTransactionalDatabaseInitializerTests extends Abstra @@ -43,7 +43,7 @@ public class PopulatedSchemaTransactionalDatabaseInitializerTests extends Abstra
}
@Test
@DatabaseInitializers(@DatabaseInitializer("data-add-dogbert.sql"))
@SqlGroup(@Sql("data-add-dogbert.sql"))
public void methodLevelScripts() {
assertNumUsers(1);
}

6
spring-test/src/test/java/org/springframework/test/context/jdbc/RequiresNewTransactionDatabaseInitializerTests.java → spring-test/src/test/java/org/springframework/test/context/jdbc/RequiresNewTransactionSqlScriptsTests.java

@ -27,14 +27,14 @@ import static org.junit.Assert.*; @@ -27,14 +27,14 @@ import static org.junit.Assert.*;
/**
* Transactional integration tests that verify commit semantics for
* {@link DatabaseInitializer#requireNewTransaction}.
* {@link Sql#requireNewTransaction}.
*
* @author Sam Brannen
* @since 4.1
*/
@ContextConfiguration(classes = PopulatedSchemaDatabaseConfig.class)
@DirtiesContext
public class RequiresNewTransactionDatabaseInitializerTests extends AbstractTransactionalJUnit4SpringContextTests {
public class RequiresNewTransactionSqlScriptsTests extends AbstractTransactionalJUnit4SpringContextTests {
@BeforeTransaction
public void beforeTransaction() {
@ -42,7 +42,7 @@ public class RequiresNewTransactionDatabaseInitializerTests extends AbstractTran @@ -42,7 +42,7 @@ public class RequiresNewTransactionDatabaseInitializerTests extends AbstractTran
}
@Test
@DatabaseInitializers(@DatabaseInitializer(scripts = "data-add-dogbert.sql", requireNewTransaction = true))
@SqlGroup(@Sql(scripts = "data-add-dogbert.sql", requireNewTransaction = true))
public void methodLevelScripts() {
assertNumUsers(1);
}

12
spring-test/src/test/java/org/springframework/test/context/jdbc/DatabaseInitializerTestExecutionListenerTests.java → spring-test/src/test/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListenerTests.java

@ -24,14 +24,14 @@ import static org.junit.Assert.*; @@ -24,14 +24,14 @@ import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* Unit tests for {@link DatabaseInitializerTestExecutionListener}.
* Unit tests for {@link SqlScriptsTestExecutionListener}.
*
* @author Sam Brannen
* @since 4.1
*/
public class DatabaseInitializerTestExecutionListenerTests {
public class SqlScriptsTestExecutionListenerTests {
private final DatabaseInitializerTestExecutionListener listener = new DatabaseInitializerTestExecutionListener();
private final SqlScriptsTestExecutionListener listener = new SqlScriptsTestExecutionListener();
private final TestContext testContext = mock(TestContext.class);
@ -76,7 +76,7 @@ public class DatabaseInitializerTestExecutionListenerTests { @@ -76,7 +76,7 @@ public class DatabaseInitializerTestExecutionListenerTests {
// -------------------------------------------------------------------------
@DatabaseInitializer
@Sql
static class MissingValueAndScriptsAtClassLevel {
public void foo() {
@ -85,14 +85,14 @@ public class DatabaseInitializerTestExecutionListenerTests { @@ -85,14 +85,14 @@ public class DatabaseInitializerTestExecutionListenerTests {
static class MissingValueAndScriptsAtMethodLevel {
@DatabaseInitializer
@Sql
public void foo() {
}
}
static class ValueAndScriptsDeclared {
@DatabaseInitializer(value = "foo", scripts = "bar")
@Sql(value = "foo", scripts = "bar")
public void valueAndScriptsDeclared() {
}
}

17
spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalAfterTestMethodDatabaseInitializerTests.java → spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalAfterTestMethodSqlScriptsTests.java

@ -24,15 +24,16 @@ import org.junit.runners.MethodSorters; @@ -24,15 +24,16 @@ import org.junit.runners.MethodSorters;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.jdbc.DatabaseInitializer.ExecutionPhase;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.AfterTransaction;
import static org.junit.Assert.*;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.*;
/**
* Transactional integration tests for {@link DatabaseInitializer @DatabaseInitializer}
* that verify proper support for {@link ExecutionPhase#AFTER_TEST_METHOD}.
* Transactional integration tests for {@link Sql @Sql} that verify proper
* support for {@link ExecutionPhase#AFTER_TEST_METHOD}.
*
* @author Sam Brannen
* @since 4.1
@ -40,7 +41,7 @@ import static org.junit.Assert.*; @@ -40,7 +41,7 @@ import static org.junit.Assert.*;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
@DirtiesContext
public class TransactionalAfterTestMethodDatabaseInitializerTests extends AbstractTransactionalJUnit4SpringContextTests {
public class TransactionalAfterTestMethodSqlScriptsTests extends AbstractTransactionalJUnit4SpringContextTests {
@Rule
public TestName testName = new TestName();
@ -60,9 +61,9 @@ public class TransactionalAfterTestMethodDatabaseInitializerTests extends Abstra @@ -60,9 +61,9 @@ public class TransactionalAfterTestMethodDatabaseInitializerTests extends Abstra
}
@Test
@DatabaseInitializers({//
@DatabaseInitializer({ "schema.sql", "data.sql" }),//
@DatabaseInitializer(scripts = "drop-schema.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD) //
@SqlGroup({//
@Sql({ "schema.sql", "data.sql" }),//
@Sql(scripts = "drop-schema.sql", executionPhase = AFTER_TEST_METHOD) //
})
// test## is required for @FixMethodOrder.
public void test01() {
@ -70,7 +71,7 @@ public class TransactionalAfterTestMethodDatabaseInitializerTests extends Abstra @@ -70,7 +71,7 @@ public class TransactionalAfterTestMethodDatabaseInitializerTests extends Abstra
}
@Test
@DatabaseInitializers(@DatabaseInitializer({ "schema.sql", "data.sql", "data-add-dogbert.sql" }))
@Sql({ "schema.sql", "data.sql", "data-add-dogbert.sql" })
// test## is required for @FixMethodOrder.
public void test02() {
assertNumUsers(2);

9
spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalDatabaseInitializerTests.java → spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java

@ -26,17 +26,16 @@ import org.springframework.test.context.junit4.AbstractTransactionalJUnit4Spring @@ -26,17 +26,16 @@ import org.springframework.test.context.junit4.AbstractTransactionalJUnit4Spring
import static org.junit.Assert.*;
/**
* Transactional integration tests for {@link DatabaseInitializer @DatabaseInitializer}
* support.
* Transactional integration tests for {@link Sql @Sql} support.
*
* @author Sam Brannen
* @since 4.1
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
@DatabaseInitializers(@DatabaseInitializer({ "schema.sql", "data.sql" }))
@Sql({ "schema.sql", "data.sql" })
@DirtiesContext
public class TransactionalDatabaseInitializerTests extends AbstractTransactionalJUnit4SpringContextTests {
public class TransactionalSqlScriptsTests extends AbstractTransactionalJUnit4SpringContextTests {
@Test
// test##_ prefix is required for @FixMethodOrder.
@ -45,7 +44,7 @@ public class TransactionalDatabaseInitializerTests extends AbstractTransactional @@ -45,7 +44,7 @@ public class TransactionalDatabaseInitializerTests extends AbstractTransactional
}
@Test
@DatabaseInitializers(@DatabaseInitializer({ "drop-schema.sql", "schema.sql", "data.sql", "data-add-dogbert.sql" }))
@Sql({ "drop-schema.sql", "schema.sql", "data.sql", "data-add-dogbert.sql" })
// test##_ prefix is required for @FixMethodOrder.
public void test02_methodLevelScripts() {
assertNumUsers(2);
Loading…
Cancel
Save