Provide JdbcTemplate in tx base classes in the TCF

Since Spring 2.5, the abstract transactional base classes in the
TestContext framework have defined and delegated to a protected
SimpleJdbcTemplate instance variable; however, SimpleJdbcTemplate has
deprecated since Spring 3.1. Consequently, subclasses of
AbstractTransactionalJUnit4SpringContextTests and
AbstractTransactionalTestNGSpringContextTests that use this instance
variable suffer from seemingly unnecessary deprecation warnings.

This commit addresses this issue by introducing a protected JdbcTemplate
instance variable in abstract transactional base classes to replace the
use of the existing SimpleJdbcTemplate. Furthermore, the existing
simpleJdbcTemplate instance variable has been deprecated, and utility
methods in the affected base classes now delegate to JdbcTestUtils
instead of the now deprecated SimpleJdbcTestUtils.

Issue: SPR-8990
This commit is contained in:
Sam Brannen 2012-08-03 21:55:06 +02:00
parent a7d43773e8
commit 8d9637ada6
4 changed files with 52 additions and 28 deletions

View File

@ -23,11 +23,12 @@ import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
@ -38,7 +39,7 @@ import org.springframework.transaction.annotation.Transactional;
* {@link PlatformTransactionManager} bean to be defined in the Spring
* {@link ApplicationContext application context}.
*
* <p>This class exposes a {@link SimpleJdbcTemplate} and provides an easy way
* <p>This class exposes a {@link JdbcTemplate} and provides an easy way
* to {@link #countRowsInTable(String) count the number of rows in a table},
* {@link #deleteFromTables(String...) delete from tables}, and
* {@link #executeSqlScript(String, boolean) execute SQL scripts} within a
@ -68,7 +69,7 @@ import org.springframework.transaction.annotation.Transactional;
* @see org.springframework.test.annotation.Rollback
* @see org.springframework.test.context.transaction.BeforeTransaction
* @see org.springframework.test.context.transaction.AfterTransaction
* @see org.springframework.test.jdbc.SimpleJdbcTestUtils
* @see org.springframework.test.jdbc.JdbcTestUtils
* @see org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests
*/
@TestExecutionListeners(TransactionalTestExecutionListener.class)
@ -77,19 +78,29 @@ import org.springframework.transaction.annotation.Transactional;
public abstract class AbstractTransactionalJUnit4SpringContextTests extends AbstractJUnit4SpringContextTests {
/**
* The SimpleJdbcTemplate that this base class manages, available to subclasses.
* The {@code SimpleJdbcTemplate} that this base class manages, available to subclasses.
* @deprecated As of Spring 3.2, use {@link #jdbcTemplate} instead.
*/
@Deprecated
protected SimpleJdbcTemplate simpleJdbcTemplate;
/**
* The {@code JdbcTemplate} that this base class manages, available to subclasses.
*/
protected JdbcTemplate jdbcTemplate;
private String sqlScriptEncoding;
/**
* Set the DataSource, typically provided via Dependency Injection.
* Set the {@code DataSource}, typically provided via Dependency Injection.
* <p>This method also instantiates the {@link #simpleJdbcTemplate} and
* {@link #jdbcTemplate} instance variables.
*/
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
/**
@ -106,7 +117,7 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst
* @return the number of rows in the table
*/
protected int countRowsInTable(String tableName) {
return SimpleJdbcTestUtils.countRowsInTable(this.simpleJdbcTemplate, tableName);
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
}
/**
@ -116,7 +127,7 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst
* @return the total number of rows deleted from all specified tables
*/
protected int deleteFromTables(String... names) {
return SimpleJdbcTestUtils.deleteFromTables(this.simpleJdbcTemplate, names);
return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names);
}
/**
@ -132,7 +143,7 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst
*/
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
Resource resource = this.applicationContext.getResource(sqlResourcePath);
SimpleJdbcTestUtils.executeSqlScript(this.simpleJdbcTemplate, new EncodedResource(resource,
JdbcTestUtils.executeSqlScript(this.jdbcTemplate, new EncodedResource(resource,
this.sqlScriptEncoding), continueOnError);
}

View File

@ -23,10 +23,11 @@ import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
@ -37,7 +38,7 @@ import org.springframework.transaction.annotation.Transactional;
* {@link PlatformTransactionManager} bean to be defined in the Spring
* {@link ApplicationContext application context}.
*
* <p>This class exposes a {@link SimpleJdbcTemplate} and provides an easy way
* <p>This class exposes a {@link JdbcTemplate} and provides an easy way
* to {@link #countRowsInTable(String) count the number of rows in a table},
* {@link #deleteFromTables(String...) delete from tables}, and
* {@link #executeSqlScript(String, boolean) execute SQL scripts} within a
@ -57,7 +58,9 @@ import org.springframework.transaction.annotation.Transactional;
* @see org.springframework.transaction.annotation.Transactional
* @see org.springframework.test.annotation.NotTransactional
* @see org.springframework.test.annotation.Rollback
* @see org.springframework.test.jdbc.SimpleJdbcTestUtils
* @see org.springframework.test.context.transaction.BeforeTransaction
* @see org.springframework.test.context.transaction.AfterTransaction
* @see org.springframework.test.jdbc.JdbcTestUtils
* @see org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests
*/
@TestExecutionListeners(TransactionalTestExecutionListener.class)
@ -66,20 +69,29 @@ import org.springframework.transaction.annotation.Transactional;
public abstract class AbstractTransactionalTestNGSpringContextTests extends AbstractTestNGSpringContextTests {
/**
* The SimpleJdbcTemplate that this base class manages, available to subclasses.
* The {@code SimpleJdbcTemplate} that this base class manages, available to subclasses.
* @deprecated As of Spring 3.2, use {@link #jdbcTemplate} instead.
*/
@Deprecated
protected SimpleJdbcTemplate simpleJdbcTemplate;
/**
* The {@code JdbcTemplate} that this base class manages, available to subclasses.
*/
protected JdbcTemplate jdbcTemplate;
private String sqlScriptEncoding;
/**
* Set the DataSource, typically provided via Dependency Injection.
* @param dataSource the DataSource to inject
* Set the {@code DataSource}, typically provided via Dependency Injection.
* <p>This method also instantiates the {@link #simpleJdbcTemplate} and
* {@link #jdbcTemplate} instance variables.
*/
@Autowired
public void setDataSource(final DataSource dataSource) {
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
/**
@ -96,24 +108,24 @@ public abstract class AbstractTransactionalTestNGSpringContextTests extends Abst
* @return the number of rows in the table
*/
protected int countRowsInTable(String tableName) {
return SimpleJdbcTestUtils.countRowsInTable(this.simpleJdbcTemplate, tableName);
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
}
/**
* Convenience method for deleting all rows from the specified tables.
* Use with caution outside of a transaction!
* Convenience method for deleting all rows from the specified tables. Use
* with caution outside of a transaction!
* @param names the names of the tables from which to delete
* @return the total number of rows deleted from all specified tables
*/
protected int deleteFromTables(String... names) {
return SimpleJdbcTestUtils.deleteFromTables(this.simpleJdbcTemplate, names);
return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names);
}
/**
* Execute the given SQL script. Use with caution outside of a transaction!
* <p>The script will normally be loaded by classpath. There should be one statement
* per line. Any semicolons will be removed. <b>Do not use this method to execute
* DDL if you expect rollback.</b>
* <p>The script will normally be loaded by classpath. There should be one
* statement per line. Any semicolons will be removed. <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
@ -122,7 +134,7 @@ public abstract class AbstractTransactionalTestNGSpringContextTests extends Abst
*/
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
Resource resource = this.applicationContext.getResource(sqlResourcePath);
SimpleJdbcTestUtils.executeSqlScript(this.simpleJdbcTemplate, new EncodedResource(resource,
JdbcTestUtils.executeSqlScript(this.jdbcTemplate, new EncodedResource(resource,
this.sqlScriptEncoding), continueOnError);
}

View File

@ -32,6 +32,7 @@ Changes in version 3.2 M2 (2012-08-xx)
* introduced MockEnvironment in the spring-test module (SPR-9492)
* deprecated SimpleJdbcTestUtils in favor of JdbcTestUtils (SPR-9235)
* introduced countRowsInTableWhere() and dropTables() in JdbcTestUtils (SPR-9235)
* introduced JdbcTemplate in tx base classes in the TestContext framework (SPR-8990)
Changes in version 3.2 M1 (2012-05-28)

View File

@ -396,7 +396,7 @@
</listitem>
<listitem>
<para>A <classname>SimpleJdbcTemplate</classname>, for executing
<para>A <classname>JdbcTemplate</classname>, for executing
SQL statements to query the database. Such queries can be used to
confirm database state both <emphasis>prior to</emphasis> and
<emphasis>after</emphasis> execution of database-related
@ -422,7 +422,7 @@
<title>JDBC Testing Support</title>
<para>The <literal>org.springframework.test.jdbc</literal> package
contains <classname>SimpleJdbcTestUtils</classname>, which is a
contains <classname>JdbcTestUtils</classname>, which is a
collection of JDBC related utility functions intended to simplify
standard database testing scenarios. <emphasis>Note that <link
linkend="testcontext-support-classes-junit4">
@ -430,7 +430,7 @@
</link> and <link linkend="testcontext-support-classes-testng">
<classname>AbstractTransactionalTestNGSpringContextTests</classname>
</link> provide convenience methods which delegate to
<classname>SimpleJdbcTestUtils</classname> internally.</emphasis></para>
<classname>JdbcTestUtils</classname> internally.</emphasis></para>
<para>The <literal>spring-jdbc</literal> module provides support for
configuring and launching an embedded database which can be used in
@ -2157,7 +2157,7 @@ public void updateWithSessionFlush() {
</listitem>
<listitem>
<para><literal>simpleJdbcTemplate</literal>: Use this
<para><literal>jdbcTemplate</literal>: Use this
variable to execute SQL statements to query the database.
Such queries can be used to confirm database state both
<emphasis>prior to</emphasis> and <emphasis>after</emphasis>
@ -2266,7 +2266,7 @@ public class SimpleTest {
</listitem>
<listitem>
<para><literal>simpleJdbcTemplate</literal>: Use this
<para><literal>jdbcTemplate</literal>: Use this
variable to execute SQL statements to query the database.
Such queries can be used to confirm database state both
<emphasis>prior to</emphasis> and <emphasis>after</emphasis>