From 015086cb9c50fa4eabd47fab064bc1bbdde8caa6 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 3 Aug 2012 22:50:39 +0200 Subject: [PATCH] Introduce new methods in tx base test classes Recently new utility methods were added to JdbcTestUtils, and a JdbcTemplate was introduced in abstract transactional base classes in the TestContext framework. This presents an easy opportunity to make these new utility methods available as convenience methods in the base test classes. This commit introduces new countRowsInTableWhere() and dropTables() convenience methods in the abstract transactional base classes in the TestContext framework. These new methods internally delegate to methods of the same names in JdbcTestUtils. Issue: SPR-9665 --- ...TransactionalJUnit4SpringContextTests.java | 39 +++++++++++++++---- ...TransactionalTestNGSpringContextTests.java | 39 +++++++++++++++---- .../test/jdbc/JdbcTestUtils.java | 4 +- src/dist/changelog.txt | 1 + 4 files changed, 67 insertions(+), 16 deletions(-) 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 cf601cabb4..88a067fa7a 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 @@ -33,17 +33,18 @@ import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.Transactional; /** - * Abstract {@link Transactional transactional} extension of + * Abstract {@linkplain Transactional transactional} extension of * {@link AbstractJUnit4SpringContextTests} which adds convenience functionality * for JDBC access. Expects a {@link DataSource} bean and a * {@link PlatformTransactionManager} bean to be defined in the Spring - * {@link ApplicationContext application context}. + * {@linkplain ApplicationContext application context}. * - *

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

This class exposes a {@link JdbcTemplate} and provides an easy way to + * {@linkplain #countRowsInTable count the number of rows in a table} + * (potentially {@linkplain #countRowsInTableWhere with a WHERE clause}), + * {@linkplain #deleteFromTables delete from tables}, + * {@linkplain #dropTables drop tables}, and + * {@linkplain #executeSqlScript execute SQL scripts} within a transaction. * *

Concrete subclasses must fulfill the same requirements outlined in * {@link AbstractJUnit4SpringContextTests}. @@ -86,6 +87,7 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst /** * The {@code JdbcTemplate} that this base class manages, available to subclasses. + * @since 3.2 */ protected JdbcTemplate jdbcTemplate; @@ -120,6 +122,19 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName); } + /** + * Count the rows in the given table, using the provided {@code WHERE} clause. + *

See the Javadoc for {@link JdbcTestUtils#countRowsInTableWhere()} for details. + * @param tableName the name of the table to count rows in + * @param whereClause the {@code WHERE} clause to append to the query + * @return the number of rows in the table that match the provided + * {@code WHERE} clause + * @since 3.2 + */ + protected int countRowsInTableWhere(String tableName, String whereClause) { + return JdbcTestUtils.countRowsInTableWhere(this.jdbcTemplate, tableName, whereClause); + } + /** * Convenience method for deleting all rows from the specified tables. Use * with caution outside of a transaction! @@ -130,6 +145,16 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names); } + /** + * Convenience method for dropping all of the specified tables. Use + * with caution outside of a transaction! + * @param names the names of the tables to drop + * @since 3.2 + */ + protected void dropTables(String... names) { + JdbcTestUtils.dropTables(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 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 5020994022..6ded8dcaf5 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 @@ -32,17 +32,18 @@ import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.Transactional; /** - * Abstract {@link Transactional transactional} extension of + * Abstract {@linkplain Transactional transactional} extension of * {@link AbstractTestNGSpringContextTests} which adds convenience functionality * for JDBC access. Expects a {@link DataSource} bean and a * {@link PlatformTransactionManager} bean to be defined in the Spring - * {@link ApplicationContext application context}. + * {@linkplain ApplicationContext application context}. * - *

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

This class exposes a {@link JdbcTemplate} and provides an easy way to + * {@linkplain #countRowsInTable count the number of rows in a table} + * (potentially {@linkplain #countRowsInTableWhere with a WHERE clause}), + * {@linkplain #deleteFromTables delete from tables}, + * {@linkplain #dropTables drop tables}, and + * {@linkplain #executeSqlScript execute SQL scripts} within a transaction. * *

Concrete subclasses must fulfill the same requirements outlined in * {@link AbstractTestNGSpringContextTests}. @@ -77,6 +78,7 @@ public abstract class AbstractTransactionalTestNGSpringContextTests extends Abst /** * The {@code JdbcTemplate} that this base class manages, available to subclasses. + * @since 3.2 */ protected JdbcTemplate jdbcTemplate; @@ -111,6 +113,19 @@ public abstract class AbstractTransactionalTestNGSpringContextTests extends Abst return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName); } + /** + * Count the rows in the given table, using the provided {@code WHERE} clause. + *

See the Javadoc for {@link JdbcTestUtils#countRowsInTableWhere()} for details. + * @param tableName the name of the table to count rows in + * @param whereClause the {@code WHERE} clause to append to the query + * @return the number of rows in the table that match the provided + * {@code WHERE} clause + * @since 3.2 + */ + protected int countRowsInTableWhere(String tableName, String whereClause) { + return JdbcTestUtils.countRowsInTableWhere(this.jdbcTemplate, tableName, whereClause); + } + /** * Convenience method for deleting all rows from the specified tables. Use * with caution outside of a transaction! @@ -121,6 +136,16 @@ public abstract class AbstractTransactionalTestNGSpringContextTests extends Abst return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names); } + /** + * Convenience method for dropping all of the specified tables. Use + * with caution outside of a transaction! + * @param names the names of the tables to drop + * @since 3.2 + */ + protected void dropTables(String... names) { + JdbcTestUtils.dropTables(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 diff --git a/spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java b/spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java index 4dc8a1c975..6006d2e3d0 100644 --- a/spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java +++ b/spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java @@ -74,7 +74,7 @@ public class JdbcTestUtils { * @param tableName the name of the table to count rows in * @param whereClause the {@code WHERE} clause to append to the query * @return the number of rows in the table that match the provided - * {@code WHERE} clause + * {@code WHERE} clause * @since 3.2 */ public static int countRowsInTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause) { @@ -111,7 +111,7 @@ public class JdbcTestUtils { * Drop the specified tables. * * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations - * @param tableNames the names of the tables to drop + * @param tableNames the names of the tables to drop * @since 3.2 */ public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames) { diff --git a/src/dist/changelog.txt b/src/dist/changelog.txt index 7ceedb8d1d..d42c161f37 100644 --- a/src/dist/changelog.txt +++ b/src/dist/changelog.txt @@ -33,6 +33,7 @@ Changes in version 3.2 M2 (2012-08-xx) * 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) +* introduced countRowsInTableWhere() and dropTables() in tx base test classes (SPR-9665) Changes in version 3.2 M1 (2012-05-28) -- GitLab