Browse Source
This is in preparation for a migration from JUnit 3 to JUnit 4. Issue: SPR-13514, SPR-13515pull/875/merge
22 changed files with 270 additions and 900 deletions
@ -1,133 +0,0 @@
@@ -1,133 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test; |
||||
|
||||
import java.lang.reflect.Method; |
||||
import java.lang.reflect.Modifier; |
||||
|
||||
import org.springframework.transaction.TransactionDefinition; |
||||
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource; |
||||
import org.springframework.transaction.interceptor.TransactionAttributeSource; |
||||
|
||||
/** |
||||
* This class is only used within tests in the spring-orm module. |
||||
* |
||||
* <p>Java 5 specific subclass of |
||||
* {@link AbstractTransactionalDataSourceSpringContextTests}, obeying annotations |
||||
* for transaction control. |
||||
* |
||||
* <p>For example, test methods can be annotated with the regular Spring |
||||
* {@link org.springframework.transaction.annotation.Transactional @Transactional} |
||||
* annotation — for example, to force execution in a read-only transaction |
||||
* or to prevent any transaction from being created at all by setting the propagation |
||||
* level to {@code NOT_SUPPORTED}. |
||||
* |
||||
* @author Rod Johnson |
||||
* @author Sam Brannen |
||||
* @author Juergen Hoeller |
||||
* @since 2.0 |
||||
* @deprecated as of Spring 3.0, in favor of using the listener-based TestContext framework |
||||
*/ |
||||
@Deprecated |
||||
public abstract class AbstractAnnotationAwareTransactionalTests extends |
||||
AbstractTransactionalDataSourceSpringContextTests { |
||||
|
||||
private final TransactionAttributeSource transactionAttributeSource = new AnnotationTransactionAttributeSource(); |
||||
|
||||
|
||||
/** |
||||
* Default constructor for AbstractAnnotationAwareTransactionalTests, which |
||||
* delegates to {@link #AbstractAnnotationAwareTransactionalTests(String)}. |
||||
*/ |
||||
public AbstractAnnotationAwareTransactionalTests() { |
||||
this(null); |
||||
} |
||||
|
||||
/** |
||||
* Constructs a new AbstractAnnotationAwareTransactionalTests instance with |
||||
* the specified JUnit {@code name}. |
||||
* @param name the name of the current test |
||||
*/ |
||||
public AbstractAnnotationAwareTransactionalTests(String name) { |
||||
super(name); |
||||
} |
||||
|
||||
/** |
||||
* Overridden to populate transaction definition from annotations. |
||||
*/ |
||||
@Override |
||||
public void runBare() throws Throwable { |
||||
// getName will return the name of the method being run.
|
||||
if (isDisabledInThisEnvironment(getName())) { |
||||
// Let superclass log that we didn't run the test.
|
||||
super.runBare(); |
||||
return; |
||||
} |
||||
|
||||
final Method testMethod = getTestMethod(); |
||||
|
||||
TransactionDefinition explicitTransactionDefinition = this.transactionAttributeSource.getTransactionAttribute( |
||||
testMethod, getClass()); |
||||
if (explicitTransactionDefinition != null) { |
||||
this.logger.info("Custom transaction definition [" + explicitTransactionDefinition + "] for test method [" |
||||
+ getName() + "]."); |
||||
setTransactionDefinition(explicitTransactionDefinition); |
||||
} |
||||
|
||||
if (this.transactionDefinition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) { |
||||
preventTransaction(); |
||||
} |
||||
|
||||
super.runBare(); |
||||
} |
||||
|
||||
/** |
||||
* Get the current test method. |
||||
*/ |
||||
protected Method getTestMethod() { |
||||
assertNotNull("TestCase.getName() cannot be null", getName()); |
||||
Method testMethod = null; |
||||
try { |
||||
// Use same algorithm as JUnit itself to retrieve the test method
|
||||
// about to be executed (the method name is returned by getName). It
|
||||
// has to be public so we can retrieve it.
|
||||
testMethod = getClass().getMethod(getName(), (Class[]) null); |
||||
} |
||||
catch (NoSuchMethodException ex) { |
||||
fail("Method '" + getName() + "' not found"); |
||||
} |
||||
if (!Modifier.isPublic(testMethod.getModifiers())) { |
||||
fail("Method '" + getName() + "' should be public"); |
||||
} |
||||
return testMethod; |
||||
} |
||||
|
||||
/** |
||||
* Determine whether or not to roll back transactions for the current test. |
||||
* <p>The default implementation simply delegates to {@link #isDefaultRollback()}. |
||||
* @return the <em>rollback</em> flag for the current test |
||||
*/ |
||||
@Override |
||||
protected boolean isRollback() { |
||||
boolean rollback = isDefaultRollback(); |
||||
if (this.logger.isDebugEnabled()) { |
||||
this.logger.debug("Using default rollback [" + rollback + "] for test [" + getName() + "]."); |
||||
} |
||||
return rollback; |
||||
} |
||||
|
||||
} |
||||
@ -1,152 +0,0 @@
@@ -1,152 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2015 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test; |
||||
|
||||
import javax.sql.DataSource; |
||||
|
||||
import org.springframework.core.io.Resource; |
||||
import org.springframework.dao.DataAccessException; |
||||
import org.springframework.jdbc.core.JdbcTemplate; |
||||
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; |
||||
|
||||
/** |
||||
* This class is only used within tests in the spring-orm module. |
||||
* |
||||
* <p>Subclass of AbstractTransactionalSpringContextTests that adds some convenience |
||||
* functionality for JDBC access. Expects a {@link javax.sql.DataSource} bean |
||||
* to be defined in the Spring application context. |
||||
* |
||||
* <p>This class exposes a {@link org.springframework.jdbc.core.JdbcTemplate} |
||||
* and provides an easy way to delete from the database in a new transaction. |
||||
* |
||||
* @author Rod Johnson |
||||
* @author Juergen Hoeller |
||||
* @author Thomas Risberg |
||||
* @since 1.1.1 |
||||
* @see #setDataSource(javax.sql.DataSource) |
||||
* @see #getJdbcTemplate() |
||||
* @deprecated as of Spring 3.0, in favor of using the listener-based test context framework |
||||
* ({@link org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests}) |
||||
*/ |
||||
@Deprecated |
||||
abstract class AbstractTransactionalDataSourceSpringContextTests extends AbstractTransactionalSpringContextTests { |
||||
|
||||
protected JdbcTemplate jdbcTemplate; |
||||
|
||||
private String sqlScriptEncoding; |
||||
|
||||
/** |
||||
* Did this test delete any tables? If so, we forbid transaction completion, |
||||
* and only allow rollback. |
||||
*/ |
||||
private boolean zappedTables; |
||||
|
||||
|
||||
/** |
||||
* Default constructor for AbstractTransactionalDataSourceSpringContextTests. |
||||
*/ |
||||
public AbstractTransactionalDataSourceSpringContextTests() { |
||||
} |
||||
|
||||
/** |
||||
* Constructor for AbstractTransactionalDataSourceSpringContextTests with a JUnit name. |
||||
*/ |
||||
public AbstractTransactionalDataSourceSpringContextTests(String name) { |
||||
super(name); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Setter: DataSource is provided by Dependency Injection. |
||||
*/ |
||||
public void setDataSource(DataSource dataSource) { |
||||
this.jdbcTemplate = new JdbcTemplate(dataSource); |
||||
} |
||||
|
||||
/** |
||||
* Return the JdbcTemplate that this base class manages. |
||||
*/ |
||||
public final JdbcTemplate getJdbcTemplate() { |
||||
return this.jdbcTemplate; |
||||
} |
||||
|
||||
/** |
||||
* Specify the encoding for SQL scripts, if different from the platform encoding. |
||||
* @see #executeSqlScript |
||||
*/ |
||||
public void setSqlScriptEncoding(String sqlScriptEncoding) { |
||||
this.sqlScriptEncoding = sqlScriptEncoding; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Convenient method to delete all rows from these tables. |
||||
* Calling this method will make avoidance of rollback by calling |
||||
* {@code setComplete()} impossible. |
||||
* @see #setComplete |
||||
*/ |
||||
protected void deleteFromTables(String... names) { |
||||
for (String name : names) { |
||||
int rowCount = this.jdbcTemplate.update("DELETE FROM " + name); |
||||
if (logger.isInfoEnabled()) { |
||||
logger.info("Deleted " + rowCount + " rows from table " + name); |
||||
} |
||||
} |
||||
this.zappedTables = true; |
||||
} |
||||
|
||||
/** |
||||
* Overridden to prevent the transaction committing if a number of tables have been |
||||
* cleared, as a defensive measure against accidental <i>permanent</i> wiping of a database. |
||||
* @see org.springframework.test.AbstractTransactionalSpringContextTests#setComplete() |
||||
*/ |
||||
@Override |
||||
protected final void setComplete() { |
||||
if (this.zappedTables) { |
||||
throw new IllegalStateException("Cannot set complete after deleting tables"); |
||||
} |
||||
super.setComplete(); |
||||
} |
||||
|
||||
/** |
||||
* Count the rows in the given table |
||||
* @param tableName table name to count rows in |
||||
* @return the number of rows in the table |
||||
*/ |
||||
protected int countRowsInTable(String tableName) { |
||||
return this.jdbcTemplate.queryForObject("SELECT COUNT(0) FROM " + tableName, Integer.class); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Execute the given SQL script. |
||||
* <p>Use with caution outside of a transaction! |
||||
* <p>The script will normally be loaded by classpath. |
||||
* <p><b>Do not use this method to execute DDL if you expect rollback.</b> |
||||
* @param sqlResourcePath the Spring resource path for the SQL script |
||||
* @param continueOnError whether or not to continue without throwing an |
||||
* exception in the event of an error |
||||
* @throws DataAccessException if there is an error executing a statement |
||||
* @see ResourceDatabasePopulator |
||||
* @see #setSqlScriptEncoding |
||||
*/ |
||||
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException { |
||||
Resource resource = this.applicationContext.getResource(sqlResourcePath); |
||||
new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(jdbcTemplate.getDataSource()); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue