提交 015086cb 编写于 作者: S Sam Brannen

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
上级 8d9637ad
...@@ -33,17 +33,18 @@ import org.springframework.transaction.PlatformTransactionManager; ...@@ -33,17 +33,18 @@ import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
/** /**
* Abstract {@link Transactional transactional} extension of * Abstract {@linkplain Transactional transactional} extension of
* {@link AbstractJUnit4SpringContextTests} which adds convenience functionality * {@link AbstractJUnit4SpringContextTests} which adds convenience functionality
* for JDBC access. Expects a {@link DataSource} bean and a * for JDBC access. Expects a {@link DataSource} bean and a
* {@link PlatformTransactionManager} bean to be defined in the Spring * {@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 * <p>This class exposes a {@link JdbcTemplate} and provides an easy way to
* to {@link #countRowsInTable(String) count the number of rows in a table}, * {@linkplain #countRowsInTable count the number of rows in a table}
* {@link #deleteFromTables(String...) delete from tables}, and * (potentially {@linkplain #countRowsInTableWhere with a WHERE clause}),
* {@link #executeSqlScript(String, boolean) execute SQL scripts} within a * {@linkplain #deleteFromTables delete from tables},
* transaction. * {@linkplain #dropTables drop tables}, and
* {@linkplain #executeSqlScript execute SQL scripts} within a transaction.
* *
* <p>Concrete subclasses must fulfill the same requirements outlined in * <p>Concrete subclasses must fulfill the same requirements outlined in
* {@link AbstractJUnit4SpringContextTests}. * {@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. * The {@code JdbcTemplate} that this base class manages, available to subclasses.
* @since 3.2
*/ */
protected JdbcTemplate jdbcTemplate; 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); 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 * Convenience method for deleting all rows from the specified tables. Use
* with caution outside of a transaction! * 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); 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! * 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 * <p>The script will normally be loaded by classpath. There should be one
......
...@@ -32,17 +32,18 @@ import org.springframework.transaction.PlatformTransactionManager; ...@@ -32,17 +32,18 @@ import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
/** /**
* Abstract {@link Transactional transactional} extension of * Abstract {@linkplain Transactional transactional} extension of
* {@link AbstractTestNGSpringContextTests} which adds convenience functionality * {@link AbstractTestNGSpringContextTests} which adds convenience functionality
* for JDBC access. Expects a {@link DataSource} bean and a * for JDBC access. Expects a {@link DataSource} bean and a
* {@link PlatformTransactionManager} bean to be defined in the Spring * {@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 * <p>This class exposes a {@link JdbcTemplate} and provides an easy way to
* to {@link #countRowsInTable(String) count the number of rows in a table}, * {@linkplain #countRowsInTable count the number of rows in a table}
* {@link #deleteFromTables(String...) delete from tables}, and * (potentially {@linkplain #countRowsInTableWhere with a WHERE clause}),
* {@link #executeSqlScript(String, boolean) execute SQL scripts} within a * {@linkplain #deleteFromTables delete from tables},
* transaction. * {@linkplain #dropTables drop tables}, and
* {@linkplain #executeSqlScript execute SQL scripts} within a transaction.
* *
* <p>Concrete subclasses must fulfill the same requirements outlined in * <p>Concrete subclasses must fulfill the same requirements outlined in
* {@link AbstractTestNGSpringContextTests}. * {@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. * The {@code JdbcTemplate} that this base class manages, available to subclasses.
* @since 3.2
*/ */
protected JdbcTemplate jdbcTemplate; 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); 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 * Convenience method for deleting all rows from the specified tables. Use
* with caution outside of a transaction! * 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); 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! * 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 * <p>The script will normally be loaded by classpath. There should be one
......
...@@ -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 tableName the name of the table to count rows in
* @param whereClause the {@code WHERE} clause to append to the query * @param whereClause the {@code WHERE} clause to append to the query
* @return the number of rows in the table that match the provided * @return the number of rows in the table that match the provided
* {@code WHERE} clause * {@code WHERE} clause
* @since 3.2 * @since 3.2
*/ */
public static int countRowsInTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause) { 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. * Drop the specified tables.
* *
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations * @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 * @since 3.2
*/ */
public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames) { public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames) {
......
...@@ -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) * deprecated SimpleJdbcTestUtils in favor of JdbcTestUtils (SPR-9235)
* introduced countRowsInTableWhere() and dropTables() in JdbcTestUtils (SPR-9235) * introduced countRowsInTableWhere() and dropTables() in JdbcTestUtils (SPR-9235)
* introduced JdbcTemplate in tx base classes in the TestContext framework (SPR-8990) * 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) Changes in version 3.2 M1 (2012-05-28)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册