Skip to content

Commit 34e8ee9

Browse files
committed
Add deleteFromTableWhere() to base classes in TCF
This commit introduces a deleteFromTableWhere() convenience method in AbstractTransactionalJUnit4SpringContextTests and AbstractTransactionalTestNGSpringContextTests that delegates to the recently introduced method of the same name in JdbcTestUtils. Issue: SPR-10639
1 parent 96da406 commit 34e8ee9

File tree

2 files changed

+76
-22
lines changed

2 files changed

+76
-22
lines changed

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

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst
8686

8787
/**
8888
* Set the {@code DataSource}, typically provided via Dependency Injection.
89-
* <p>This method also instantiates the {@link #jdbcTemplate} instance
90-
* variable.
89+
* <p>This method also instantiates the {@link #jdbcTemplate} instance variable.
9190
*/
9291
@Autowired
9392
public void setDataSource(DataSource dataSource) {
@@ -103,49 +102,75 @@ public void setSqlScriptEncoding(String sqlScriptEncoding) {
103102
}
104103

105104
/**
106-
* Count the rows in the given table.
105+
* Convenience method for counting the rows in the given table.
107106
* @param tableName table name to count rows in
108107
* @return the number of rows in the table
108+
* @see JdbcTestUtils#countRowsInTable
109109
*/
110110
protected int countRowsInTable(String tableName) {
111111
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
112112
}
113113

114114
/**
115-
* Count the rows in the given table, using the provided {@code WHERE} clause.
115+
* Convenience method for counting the rows in the given table, using the
116+
* provided {@code WHERE} clause.
116117
* <p>See the Javadoc for {@link JdbcTestUtils#countRowsInTableWhere} for details.
117118
* @param tableName the name of the table to count rows in
118119
* @param whereClause the {@code WHERE} clause to append to the query
119120
* @return the number of rows in the table that match the provided
120121
* {@code WHERE} clause
121122
* @since 3.2
123+
* @see JdbcTestUtils#countRowsInTableWhere
122124
*/
123125
protected int countRowsInTableWhere(String tableName, String whereClause) {
124126
return JdbcTestUtils.countRowsInTableWhere(this.jdbcTemplate, tableName, whereClause);
125127
}
126128

127129
/**
128-
* Convenience method for deleting all rows from the specified tables. Use
129-
* with caution outside of a transaction!
130+
* Convenience method for deleting all rows from the specified tables.
131+
* <p>Use with caution outside of a transaction!
130132
* @param names the names of the tables from which to delete
131133
* @return the total number of rows deleted from all specified tables
134+
* @see JdbcTestUtils#deleteFromTables
132135
*/
133136
protected int deleteFromTables(String... names) {
134137
return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names);
135138
}
136139

137140
/**
138-
* Convenience method for dropping all of the specified tables. Use
139-
* with caution outside of a transaction!
141+
* Convenience method for deleting all rows from the given table, using the
142+
* provided {@code WHERE} clause.
143+
* <p>Use with caution outside of a transaction!
144+
* <p>See the Javadoc for {@link JdbcTestUtils#deleteFromTableWhere} for details.
145+
* @param tableName the name of the table to delete rows from
146+
* @param whereClause the {@code WHERE} clause to append to the query
147+
* @param args arguments to bind to the query (leaving it to the {@code
148+
* PreparedStatement} to guess the corresponding SQL type); may also contain
149+
* {@link org.springframework.jdbc.core.SqlParameterValue SqlParameterValue}
150+
* objects which indicate not only the argument value but also the SQL type
151+
* and optionally the scale.
152+
* @return the number of rows deleted from the table
153+
* @since 4.0
154+
* @see JdbcTestUtils#deleteFromTableWhere
155+
*/
156+
protected int deleteFromTableWhere(String tableName, String whereClause, Object... args) {
157+
return JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, tableName, whereClause, args);
158+
}
159+
160+
/**
161+
* Convenience method for dropping all of the specified tables.
162+
* <p>Use with caution outside of a transaction!
140163
* @param names the names of the tables to drop
141164
* @since 3.2
165+
* @see JdbcTestUtils#dropTables
142166
*/
143167
protected void dropTables(String... names) {
144168
JdbcTestUtils.dropTables(this.jdbcTemplate, names);
145169
}
146170

147171
/**
148-
* Execute the given SQL script. Use with caution outside of a transaction!
172+
* Execute the given SQL script.
173+
* <p>Use with caution outside of a transaction!
149174
* <p>The script will normally be loaded by classpath. There should be one
150175
* statement per line. Any semicolons will be removed. <b>Do not use this
151176
* method to execute DDL if you expect rollback.</b>
@@ -154,11 +179,13 @@ protected void dropTables(String... names) {
154179
* exception in the event of an error
155180
* @throws DataAccessException if there is an error executing a statement
156181
* and continueOnError was {@code false}
182+
* @see JdbcTestUtils#executeSqlScript(JdbcTemplate, EncodedResource, boolean)
183+
* @see #setSqlScriptEncoding
157184
*/
158185
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
159186
Resource resource = this.applicationContext.getResource(sqlResourcePath);
160-
JdbcTestUtils.executeSqlScript(this.jdbcTemplate, new EncodedResource(resource,
161-
this.sqlScriptEncoding), continueOnError);
187+
JdbcTestUtils.executeSqlScript(this.jdbcTemplate, new EncodedResource(resource, this.sqlScriptEncoding),
188+
continueOnError);
162189
}
163190

164191
}

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

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ public abstract class AbstractTransactionalTestNGSpringContextTests extends Abst
7777

7878
/**
7979
* Set the {@code DataSource}, typically provided via Dependency Injection.
80-
* <p>This method also instantiates the {@link #jdbcTemplate} instance
81-
* variable.
80+
* <p>This method also instantiates the {@link #jdbcTemplate} instance variable.
8281
*/
8382
@Autowired
8483
public void setDataSource(DataSource dataSource) {
@@ -94,49 +93,75 @@ public void setSqlScriptEncoding(String sqlScriptEncoding) {
9493
}
9594

9695
/**
97-
* Count the rows in the given table.
96+
* Convenience method for counting the rows in the given table.
9897
* @param tableName table name to count rows in
9998
* @return the number of rows in the table
99+
* @see JdbcTestUtils#countRowsInTable
100100
*/
101101
protected int countRowsInTable(String tableName) {
102102
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
103103
}
104104

105105
/**
106-
* Count the rows in the given table, using the provided {@code WHERE} clause.
106+
* Convenience method for counting the rows in the given table, using the
107+
* provided {@code WHERE} clause.
107108
* <p>See the Javadoc for {@link JdbcTestUtils#countRowsInTableWhere} for details.
108109
* @param tableName the name of the table to count rows in
109110
* @param whereClause the {@code WHERE} clause to append to the query
110111
* @return the number of rows in the table that match the provided
111112
* {@code WHERE} clause
112113
* @since 3.2
114+
* @see JdbcTestUtils#countRowsInTableWhere
113115
*/
114116
protected int countRowsInTableWhere(String tableName, String whereClause) {
115117
return JdbcTestUtils.countRowsInTableWhere(this.jdbcTemplate, tableName, whereClause);
116118
}
117119

118120
/**
119-
* Convenience method for deleting all rows from the specified tables. Use
120-
* with caution outside of a transaction!
121+
* Convenience method for deleting all rows from the specified tables.
122+
* <p>Use with caution outside of a transaction!
121123
* @param names the names of the tables from which to delete
122124
* @return the total number of rows deleted from all specified tables
125+
* @see JdbcTestUtils#deleteFromTables
123126
*/
124127
protected int deleteFromTables(String... names) {
125128
return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names);
126129
}
127130

128131
/**
129-
* Convenience method for dropping all of the specified tables. Use
130-
* with caution outside of a transaction!
132+
* Convenience method for deleting all rows from the given table, using the
133+
* provided {@code WHERE} clause.
134+
* <p>Use with caution outside of a transaction!
135+
* <p>See the Javadoc for {@link JdbcTestUtils#deleteFromTableWhere} for details.
136+
* @param tableName the name of the table to delete rows from
137+
* @param whereClause the {@code WHERE} clause to append to the query
138+
* @param args arguments to bind to the query (leaving it to the {@code
139+
* PreparedStatement} to guess the corresponding SQL type); may also contain
140+
* {@link org.springframework.jdbc.core.SqlParameterValue SqlParameterValue}
141+
* objects which indicate not only the argument value but also the SQL type
142+
* and optionally the scale.
143+
* @return the number of rows deleted from the table
144+
* @since 4.0
145+
* @see JdbcTestUtils#deleteFromTableWhere
146+
*/
147+
protected int deleteFromTableWhere(String tableName, String whereClause, Object... args) {
148+
return JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, tableName, whereClause, args);
149+
}
150+
151+
/**
152+
* Convenience method for dropping all of the specified tables.
153+
* <p>Use with caution outside of a transaction!
131154
* @param names the names of the tables to drop
132155
* @since 3.2
156+
* @see JdbcTestUtils#dropTables
133157
*/
134158
protected void dropTables(String... names) {
135159
JdbcTestUtils.dropTables(this.jdbcTemplate, names);
136160
}
137161

138162
/**
139-
* Execute the given SQL script. Use with caution outside of a transaction!
163+
* Execute the given SQL script.
164+
* <p>Use with caution outside of a transaction!
140165
* <p>The script will normally be loaded by classpath. There should be one
141166
* statement per line. Any semicolons will be removed. <b>Do not use this
142167
* method to execute DDL if you expect rollback.</b>
@@ -145,11 +170,13 @@ protected void dropTables(String... names) {
145170
* exception in the event of an error
146171
* @throws DataAccessException if there is an error executing a statement
147172
* and continueOnError was {@code false}
173+
* @see JdbcTestUtils#executeSqlScript(JdbcTemplate, EncodedResource, boolean)
174+
* @see #setSqlScriptEncoding
148175
*/
149176
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
150177
Resource resource = this.applicationContext.getResource(sqlResourcePath);
151-
JdbcTestUtils.executeSqlScript(this.jdbcTemplate, new EncodedResource(resource,
152-
this.sqlScriptEncoding), continueOnError);
178+
JdbcTestUtils.executeSqlScript(this.jdbcTemplate, new EncodedResource(resource, this.sqlScriptEncoding),
179+
continueOnError);
153180
}
154181

155182
}

0 commit comments

Comments
 (0)