diff --git a/spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java b/spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java index f0d2782790..cf601cabb4 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java @@ -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}. * - *

This class exposes a {@link SimpleJdbcTemplate} and provides an easy way + *

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. + *

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); } diff --git a/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java b/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java index b560a6fdf0..5020994022 100644 --- a/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java +++ b/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java @@ -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}. * - *

This class exposes a {@link SimpleJdbcTemplate} and provides an easy way + *

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. + *

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! - *

The script will normally be loaded by classpath. There should be one statement - * per line. Any semicolons will be removed. Do not use this method to execute - * DDL if you expect rollback. + *

The script will normally be loaded by classpath. There should be one + * statement per line. Any semicolons will be removed. Do not use this + * method to execute DDL if you expect rollback. * @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); } diff --git a/src/dist/changelog.txt b/src/dist/changelog.txt index 47d41e52be..7ceedb8d1d 100644 --- a/src/dist/changelog.txt +++ b/src/dist/changelog.txt @@ -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) diff --git a/src/reference/docbook/testing.xml b/src/reference/docbook/testing.xml index 8b81ad19c3..fde0fb88d8 100644 --- a/src/reference/docbook/testing.xml +++ b/src/reference/docbook/testing.xml @@ -396,7 +396,7 @@ - A SimpleJdbcTemplate, for executing + A JdbcTemplate, for executing SQL statements to query the database. Such queries can be used to confirm database state both prior to and after execution of database-related @@ -422,7 +422,7 @@ JDBC Testing Support The org.springframework.test.jdbc package - contains SimpleJdbcTestUtils, which is a + contains JdbcTestUtils, which is a collection of JDBC related utility functions intended to simplify standard database testing scenarios. Note that @@ -430,7 +430,7 @@ and AbstractTransactionalTestNGSpringContextTests provide convenience methods which delegate to - SimpleJdbcTestUtils internally. + JdbcTestUtils internally. The spring-jdbc module provides support for configuring and launching an embedded database which can be used in @@ -2157,7 +2157,7 @@ public void updateWithSessionFlush() { - simpleJdbcTemplate: Use this + jdbcTemplate: Use this variable to execute SQL statements to query the database. Such queries can be used to confirm database state both prior to and after @@ -2266,7 +2266,7 @@ public class SimpleTest { - simpleJdbcTemplate: Use this + jdbcTemplate: Use this variable to execute SQL statements to query the database. Such queries can be used to confirm database state both prior to and after