Browse Source

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
pull/113/merge
Sam Brannen 12 years ago
parent
commit
015086cb9c
  1. 39
      spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java
  2. 39
      spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java
  3. 4
      spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java
  4. 1
      src/dist/changelog.txt

39
spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java

@ -33,17 +33,18 @@ import org.springframework.transaction.PlatformTransactionManager; @@ -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}.
*
* <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
* transaction.
* <p>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.
*
* <p>Concrete subclasses must fulfill the same requirements outlined in
* {@link AbstractJUnit4SpringContextTests}.
@ -86,6 +87,7 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst @@ -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 @@ -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.
* <p>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 @@ -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!
* <p>The script will normally be loaded by classpath. There should be one

39
spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java

@ -32,17 +32,18 @@ import org.springframework.transaction.PlatformTransactionManager; @@ -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}.
*
* <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
* transaction.
* <p>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.
*
* <p>Concrete subclasses must fulfill the same requirements outlined in
* {@link AbstractTestNGSpringContextTests}.
@ -77,6 +78,7 @@ public abstract class AbstractTransactionalTestNGSpringContextTests extends Abst @@ -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 @@ -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.
* <p>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 @@ -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!
* <p>The script will normally be loaded by classpath. There should be one

4
spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java

@ -74,7 +74,7 @@ public class JdbcTestUtils { @@ -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 { @@ -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) {

1
src/dist/changelog.txt vendored

@ -33,6 +33,7 @@ Changes in version 3.2 M2 (2012-08-xx) @@ -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)

Loading…
Cancel
Save