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 335497aa51..c1494050a2 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 @@ -86,8 +86,7 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst /** * Set the {@code DataSource}, typically provided via Dependency Injection. - *

This method also instantiates the {@link #jdbcTemplate} instance - * variable. + *

This method also instantiates the {@link #jdbcTemplate} instance variable. */ @Autowired public void setDataSource(DataSource dataSource) { @@ -103,49 +102,75 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst } /** - * Count the rows in the given table. + * Convenience method for counting the rows in the given table. * @param tableName table name to count rows in * @return the number of rows in the table + * @see JdbcTestUtils#countRowsInTable */ protected int countRowsInTable(String tableName) { return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName); } /** - * Count the rows in the given table, using the provided {@code WHERE} clause. + * Convenience method for counting 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 + * @see JdbcTestUtils#countRowsInTableWhere */ 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! + * 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 + * @see JdbcTestUtils#deleteFromTables */ protected int deleteFromTables(String... names) { return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names); } /** - * Convenience method for dropping all of the specified tables. Use - * with caution outside of a transaction! + * Convenience method for deleting all rows from the given table, using the + * provided {@code WHERE} clause. + *

Use with caution outside of a transaction! + *

See the Javadoc for {@link JdbcTestUtils#deleteFromTableWhere} for details. + * @param tableName the name of the table to delete rows from + * @param whereClause the {@code WHERE} clause to append to the query + * @param args arguments to bind to the query (leaving it to the {@code + * PreparedStatement} to guess the corresponding SQL type); may also contain + * {@link org.springframework.jdbc.core.SqlParameterValue SqlParameterValue} + * objects which indicate not only the argument value but also the SQL type + * and optionally the scale. + * @return the number of rows deleted from the table + * @since 4.0 + * @see JdbcTestUtils#deleteFromTableWhere + */ + protected int deleteFromTableWhere(String tableName, String whereClause, Object... args) { + return JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, tableName, whereClause, args); + } + + /** + * 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 + * @see JdbcTestUtils#dropTables */ 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! *

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. @@ -154,11 +179,13 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst * exception in the event of an error * @throws DataAccessException if there is an error executing a statement * and continueOnError was {@code false} + * @see JdbcTestUtils#executeSqlScript(JdbcTemplate, EncodedResource, boolean) + * @see #setSqlScriptEncoding */ protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException { Resource resource = this.applicationContext.getResource(sqlResourcePath); - JdbcTestUtils.executeSqlScript(this.jdbcTemplate, new EncodedResource(resource, - this.sqlScriptEncoding), continueOnError); + 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 c5d7a6770b..e71b82d529 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 @@ -77,8 +77,7 @@ public abstract class AbstractTransactionalTestNGSpringContextTests extends Abst /** * Set the {@code DataSource}, typically provided via Dependency Injection. - *

This method also instantiates the {@link #jdbcTemplate} instance - * variable. + *

This method also instantiates the {@link #jdbcTemplate} instance variable. */ @Autowired public void setDataSource(DataSource dataSource) { @@ -94,49 +93,75 @@ public abstract class AbstractTransactionalTestNGSpringContextTests extends Abst } /** - * Count the rows in the given table. + * Convenience method for counting the rows in the given table. * @param tableName table name to count rows in * @return the number of rows in the table + * @see JdbcTestUtils#countRowsInTable */ protected int countRowsInTable(String tableName) { return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName); } /** - * Count the rows in the given table, using the provided {@code WHERE} clause. + * Convenience method for counting 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 + * @see JdbcTestUtils#countRowsInTableWhere */ 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! + * 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 + * @see JdbcTestUtils#deleteFromTables */ protected int deleteFromTables(String... names) { return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names); } /** - * Convenience method for dropping all of the specified tables. Use - * with caution outside of a transaction! + * Convenience method for deleting all rows from the given table, using the + * provided {@code WHERE} clause. + *

Use with caution outside of a transaction! + *

See the Javadoc for {@link JdbcTestUtils#deleteFromTableWhere} for details. + * @param tableName the name of the table to delete rows from + * @param whereClause the {@code WHERE} clause to append to the query + * @param args arguments to bind to the query (leaving it to the {@code + * PreparedStatement} to guess the corresponding SQL type); may also contain + * {@link org.springframework.jdbc.core.SqlParameterValue SqlParameterValue} + * objects which indicate not only the argument value but also the SQL type + * and optionally the scale. + * @return the number of rows deleted from the table + * @since 4.0 + * @see JdbcTestUtils#deleteFromTableWhere + */ + protected int deleteFromTableWhere(String tableName, String whereClause, Object... args) { + return JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, tableName, whereClause, args); + } + + /** + * 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 + * @see JdbcTestUtils#dropTables */ 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! *

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. @@ -145,11 +170,13 @@ public abstract class AbstractTransactionalTestNGSpringContextTests extends Abst * exception in the event of an error * @throws DataAccessException if there is an error executing a statement * and continueOnError was {@code false} + * @see JdbcTestUtils#executeSqlScript(JdbcTemplate, EncodedResource, boolean) + * @see #setSqlScriptEncoding */ protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException { Resource resource = this.applicationContext.getResource(sqlResourcePath); - JdbcTestUtils.executeSqlScript(this.jdbcTemplate, new EncodedResource(resource, - this.sqlScriptEncoding), continueOnError); + JdbcTestUtils.executeSqlScript(this.jdbcTemplate, new EncodedResource(resource, this.sqlScriptEncoding), + continueOnError); } }