Browse Source

Polishing

pull/1155/head
Juergen Hoeller 10 years ago
parent
commit
41d2d2d270
  1. 72
      spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java

72
spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java

@ -128,11 +128,11 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
private void executeSqlScripts(TestContext testContext, ExecutionPhase executionPhase) throws Exception { private void executeSqlScripts(TestContext testContext, ExecutionPhase executionPhase) throws Exception {
boolean classLevel = false; boolean classLevel = false;
Set<Sql> sqlAnnotations = AnnotationUtils.getRepeatableAnnotations(testContext.getTestMethod(), Sql.class, Set<Sql> sqlAnnotations = AnnotationUtils.getRepeatableAnnotations(
SqlGroup.class); testContext.getTestMethod(), Sql.class, SqlGroup.class);
if (sqlAnnotations.isEmpty()) { if (sqlAnnotations.isEmpty()) {
sqlAnnotations = AnnotationUtils.getRepeatableAnnotations(testContext.getTestClass(), Sql.class, sqlAnnotations = AnnotationUtils.getRepeatableAnnotations(
SqlGroup.class); testContext.getTestClass(), Sql.class, SqlGroup.class);
if (!sqlAnnotations.isEmpty()) { if (!sqlAnnotations.isEmpty()) {
classLevel = true; classLevel = true;
} }
@ -155,14 +155,15 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
*/ */
private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestContext testContext, boolean classLevel) private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestContext testContext, boolean classLevel)
throws Exception { throws Exception {
if (executionPhase != sql.executionPhase()) { if (executionPhase != sql.executionPhase()) {
return; return;
} }
MergedSqlConfig mergedSqlConfig = new MergedSqlConfig(sql.config(), testContext.getTestClass()); MergedSqlConfig mergedSqlConfig = new MergedSqlConfig(sql.config(), testContext.getTestClass());
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Processing %s for execution phase [%s] and test context %s.", mergedSqlConfig, logger.debug(String.format("Processing %s for execution phase [%s] and test context %s.",
executionPhase, testContext)); mergedSqlConfig, executionPhase, testContext));
} }
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
@ -177,15 +178,13 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
String[] scripts = getScripts(sql, testContext, classLevel); String[] scripts = getScripts(sql, testContext, classLevel);
scripts = TestContextResourceUtils.convertToClasspathResourcePaths(testContext.getTestClass(), scripts); scripts = TestContextResourceUtils.convertToClasspathResourcePaths(testContext.getTestClass(), scripts);
List<Resource> scriptResources = TestContextResourceUtils.convertToResourceList( List<Resource> scriptResources = TestContextResourceUtils.convertToResourceList(
testContext.getApplicationContext(), scripts); testContext.getApplicationContext(), scripts);
for (String stmt : sql.statements()) {
for (String statement : sql.statements()) { if (StringUtils.hasText(stmt)) {
if (StringUtils.hasText(statement)) { stmt = stmt.trim();
statement = statement.trim(); scriptResources.add(new ByteArrayResource(stmt.getBytes(), "from inlined SQL statement: " + stmt));
scriptResources.add(new ByteArrayResource(statement.getBytes(), "from inlined SQL statement: " + statement));
} }
} }
populator.setScripts(scriptResources.toArray(new Resource[scriptResources.size()])); populator.setScripts(scriptResources.toArray(new Resource[scriptResources.size()]));
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Executing SQL scripts: " + ObjectUtils.nullSafeToString(scriptResources)); logger.debug("Executing SQL scripts: " + ObjectUtils.nullSafeToString(scriptResources));
@ -194,54 +193,45 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
String dsName = mergedSqlConfig.getDataSource(); String dsName = mergedSqlConfig.getDataSource();
String tmName = mergedSqlConfig.getTransactionManager(); String tmName = mergedSqlConfig.getTransactionManager();
DataSource dataSource = TestContextTransactionUtils.retrieveDataSource(testContext, dsName); DataSource dataSource = TestContextTransactionUtils.retrieveDataSource(testContext, dsName);
final PlatformTransactionManager transactionManager = TestContextTransactionUtils.retrieveTransactionManager( PlatformTransactionManager txMgr = TestContextTransactionUtils.retrieveTransactionManager(testContext, tmName);
testContext, tmName); boolean newTxRequired = (mergedSqlConfig.getTransactionMode() == TransactionMode.ISOLATED);
final boolean newTxRequired = mergedSqlConfig.getTransactionMode() == TransactionMode.ISOLATED;
if (transactionManager == null) { if (txMgr == null) {
if (newTxRequired) { if (newTxRequired) {
throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: " throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: " +
+ "cannot execute SQL scripts using Transaction Mode " "cannot execute SQL scripts using Transaction Mode [%s] without a PlatformTransactionManager.",
+ "[%s] without a PlatformTransactionManager.", testContext, TransactionMode.ISOLATED)); testContext, TransactionMode.ISOLATED));
} }
if (dataSource == null) { if (dataSource == null) {
throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: " throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: " +
+ "supply at least a DataSource or PlatformTransactionManager.", testContext)); "supply at least a DataSource or PlatformTransactionManager.", testContext));
} }
// Execute scripts directly against the DataSource // Execute scripts directly against the DataSource
populator.execute(dataSource); populator.execute(dataSource);
} }
else { else {
DataSource dataSourceFromTxMgr = getDataSourceFromTransactionManager(transactionManager); DataSource dataSourceFromTxMgr = getDataSourceFromTransactionManager(txMgr);
// Ensure user configured an appropriate DataSource/TransactionManager pair. // Ensure user configured an appropriate DataSource/TransactionManager pair.
if (dataSource != null && dataSourceFromTxMgr != null && !dataSource.equals(dataSourceFromTxMgr)) { if (dataSource != null && dataSourceFromTxMgr != null && !dataSource.equals(dataSourceFromTxMgr)) {
throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: " + throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: " +
"the configured DataSource [%s] (named '%s') is not the one associated with " + "the configured DataSource [%s] (named '%s') is not the one associated with " +
"transaction manager [%s] (named '%s').", testContext, dataSource.getClass().getName(), "transaction manager [%s] (named '%s').", testContext, dataSource.getClass().getName(),
dsName, transactionManager.getClass().getName(), tmName)); dsName, txMgr.getClass().getName(), tmName));
} }
if (dataSource == null) { if (dataSource == null) {
dataSource = dataSourceFromTxMgr; dataSource = dataSourceFromTxMgr;
if (dataSource == null) { if (dataSource == null) {
throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: " throw new IllegalStateException(String.format("Failed to execute SQL scripts for " +
+ "could not obtain DataSource from transaction manager [%s] (named '%s').", testContext, "test context %s: could not obtain DataSource from transaction manager [%s] (named '%s').",
transactionManager.getClass().getName(), tmName)); testContext, txMgr.getClass().getName(), tmName));
} }
} }
final DataSource finalDataSource = dataSource; final DataSource finalDataSource = dataSource;
int propagation = (newTxRequired ? TransactionDefinition.PROPAGATION_REQUIRES_NEW : int propagation = (newTxRequired ? TransactionDefinition.PROPAGATION_REQUIRES_NEW :
TransactionDefinition.PROPAGATION_REQUIRED); TransactionDefinition.PROPAGATION_REQUIRED);
TransactionAttribute txAttr = TestContextTransactionUtils.createDelegatingTransactionAttribute(
TransactionAttribute transactionAttribute = TestContextTransactionUtils.createDelegatingTransactionAttribute( testContext, new DefaultTransactionAttribute(propagation));
testContext, new DefaultTransactionAttribute(propagation)); new TransactionTemplate(txMgr, txAttr).execute(new TransactionCallbackWithoutResult() {
new TransactionTemplate(transactionManager, transactionAttribute).execute(new TransactionCallbackWithoutResult() {
@Override @Override
public void doInTransactionWithoutResult(TransactionStatus status) { public void doInTransactionWithoutResult(TransactionStatus status) {
populator.execute(finalDataSource); populator.execute(finalDataSource);
@ -267,7 +257,7 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
private String[] getScripts(Sql sql, TestContext testContext, boolean classLevel) { private String[] getScripts(Sql sql, TestContext testContext, boolean classLevel) {
String[] scripts = sql.scripts(); String[] scripts = sql.scripts();
if (ObjectUtils.isEmpty(scripts) && ObjectUtils.isEmpty(sql.statements())) { if (ObjectUtils.isEmpty(scripts) && ObjectUtils.isEmpty(sql.statements())) {
scripts = new String[] { detectDefaultScript(testContext, classLevel) }; scripts = new String[] {detectDefaultScript(testContext, classLevel)};
} }
return scripts; return scripts;
} }
@ -293,8 +283,8 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
if (classPathResource.exists()) { if (classPathResource.exists()) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info(String.format("Detected default SQL script \"%s\" for test %s [%s]", prefixedResourcePath, logger.info(String.format("Detected default SQL script \"%s\" for test %s [%s]",
elementType, elementName)); prefixedResourcePath, elementType, elementName));
} }
return prefixedResourcePath; return prefixedResourcePath;
} }

Loading…
Cancel
Save