Skip to content

Commit 015086c

Browse files
committed
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
1 parent 8d9637a commit 015086c

File tree

4 files changed

+67
-16
lines changed

4 files changed

+67
-16
lines changed

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,18 @@
3333
import org.springframework.transaction.annotation.Transactional;
3434

3535
/**
36-
* Abstract {@link Transactional transactional} extension of
36+
* Abstract {@linkplain Transactional transactional} extension of
3737
* {@link AbstractJUnit4SpringContextTests} which adds convenience functionality
3838
* for JDBC access. Expects a {@link DataSource} bean and a
3939
* {@link PlatformTransactionManager} bean to be defined in the Spring
40-
* {@link ApplicationContext application context}.
40+
* {@linkplain ApplicationContext application context}.
4141
*
42-
* <p>This class exposes a {@link JdbcTemplate} and provides an easy way
43-
* to {@link #countRowsInTable(String) count the number of rows in a table},
44-
* {@link #deleteFromTables(String...) delete from tables}, and
45-
* {@link #executeSqlScript(String, boolean) execute SQL scripts} within a
46-
* transaction.
42+
* <p>This class exposes a {@link JdbcTemplate} and provides an easy way to
43+
* {@linkplain #countRowsInTable count the number of rows in a table}
44+
* (potentially {@linkplain #countRowsInTableWhere with a WHERE clause}),
45+
* {@linkplain #deleteFromTables delete from tables},
46+
* {@linkplain #dropTables drop tables}, and
47+
* {@linkplain #executeSqlScript execute SQL scripts} within a transaction.
4748
*
4849
* <p>Concrete subclasses must fulfill the same requirements outlined in
4950
* {@link AbstractJUnit4SpringContextTests}.
@@ -86,6 +87,7 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst
8687

8788
/**
8889
* The {@code JdbcTemplate} that this base class manages, available to subclasses.
90+
* @since 3.2
8991
*/
9092
protected JdbcTemplate jdbcTemplate;
9193

@@ -120,6 +122,19 @@ protected int countRowsInTable(String tableName) {
120122
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
121123
}
122124

125+
/**
126+
* Count the rows in the given table, using the provided {@code WHERE} clause.
127+
* <p>See the Javadoc for {@link JdbcTestUtils#countRowsInTableWhere()} for details.
128+
* @param tableName the name of the table to count rows in
129+
* @param whereClause the {@code WHERE} clause to append to the query
130+
* @return the number of rows in the table that match the provided
131+
* {@code WHERE} clause
132+
* @since 3.2
133+
*/
134+
protected int countRowsInTableWhere(String tableName, String whereClause) {
135+
return JdbcTestUtils.countRowsInTableWhere(this.jdbcTemplate, tableName, whereClause);
136+
}
137+
123138
/**
124139
* Convenience method for deleting all rows from the specified tables. Use
125140
* with caution outside of a transaction!
@@ -130,6 +145,16 @@ protected int deleteFromTables(String... names) {
130145
return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names);
131146
}
132147

148+
/**
149+
* Convenience method for dropping all of the specified tables. Use
150+
* with caution outside of a transaction!
151+
* @param names the names of the tables to drop
152+
* @since 3.2
153+
*/
154+
protected void dropTables(String... names) {
155+
JdbcTestUtils.dropTables(this.jdbcTemplate, names);
156+
}
157+
133158
/**
134159
* Execute the given SQL script. Use with caution outside of a transaction!
135160
* <p>The script will normally be loaded by classpath. There should be one

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@
3232
import org.springframework.transaction.annotation.Transactional;
3333

3434
/**
35-
* Abstract {@link Transactional transactional} extension of
35+
* Abstract {@linkplain Transactional transactional} extension of
3636
* {@link AbstractTestNGSpringContextTests} which adds convenience functionality
3737
* for JDBC access. Expects a {@link DataSource} bean and a
3838
* {@link PlatformTransactionManager} bean to be defined in the Spring
39-
* {@link ApplicationContext application context}.
39+
* {@linkplain ApplicationContext application context}.
4040
*
41-
* <p>This class exposes a {@link JdbcTemplate} and provides an easy way
42-
* to {@link #countRowsInTable(String) count the number of rows in a table},
43-
* {@link #deleteFromTables(String...) delete from tables}, and
44-
* {@link #executeSqlScript(String, boolean) execute SQL scripts} within a
45-
* transaction.
41+
* <p>This class exposes a {@link JdbcTemplate} and provides an easy way to
42+
* {@linkplain #countRowsInTable count the number of rows in a table}
43+
* (potentially {@linkplain #countRowsInTableWhere with a WHERE clause}),
44+
* {@linkplain #deleteFromTables delete from tables},
45+
* {@linkplain #dropTables drop tables}, and
46+
* {@linkplain #executeSqlScript execute SQL scripts} within a transaction.
4647
*
4748
* <p>Concrete subclasses must fulfill the same requirements outlined in
4849
* {@link AbstractTestNGSpringContextTests}.
@@ -77,6 +78,7 @@ public abstract class AbstractTransactionalTestNGSpringContextTests extends Abst
7778

7879
/**
7980
* The {@code JdbcTemplate} that this base class manages, available to subclasses.
81+
* @since 3.2
8082
*/
8183
protected JdbcTemplate jdbcTemplate;
8284

@@ -111,6 +113,19 @@ protected int countRowsInTable(String tableName) {
111113
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
112114
}
113115

116+
/**
117+
* Count the rows in the given table, using the provided {@code WHERE} clause.
118+
* <p>See the Javadoc for {@link JdbcTestUtils#countRowsInTableWhere()} for details.
119+
* @param tableName the name of the table to count rows in
120+
* @param whereClause the {@code WHERE} clause to append to the query
121+
* @return the number of rows in the table that match the provided
122+
* {@code WHERE} clause
123+
* @since 3.2
124+
*/
125+
protected int countRowsInTableWhere(String tableName, String whereClause) {
126+
return JdbcTestUtils.countRowsInTableWhere(this.jdbcTemplate, tableName, whereClause);
127+
}
128+
114129
/**
115130
* Convenience method for deleting all rows from the specified tables. Use
116131
* with caution outside of a transaction!
@@ -121,6 +136,16 @@ protected int deleteFromTables(String... names) {
121136
return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names);
122137
}
123138

139+
/**
140+
* Convenience method for dropping all of the specified tables. Use
141+
* with caution outside of a transaction!
142+
* @param names the names of the tables to drop
143+
* @since 3.2
144+
*/
145+
protected void dropTables(String... names) {
146+
JdbcTestUtils.dropTables(this.jdbcTemplate, names);
147+
}
148+
124149
/**
125150
* Execute the given SQL script. Use with caution outside of a transaction!
126151
* <p>The script will normally be loaded by classpath. There should be one

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static int countRowsInTable(JdbcTemplate jdbcTemplate, String tableName)
7474
* @param tableName the name of the table to count rows in
7575
* @param whereClause the {@code WHERE} clause to append to the query
7676
* @return the number of rows in the table that match the provided
77-
* {@code WHERE} clause
77+
* {@code WHERE} clause
7878
* @since 3.2
7979
*/
8080
public static int countRowsInTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause) {
@@ -111,7 +111,7 @@ public static int deleteFromTables(JdbcTemplate jdbcTemplate, String... tableNam
111111
* Drop the specified tables.
112112
*
113113
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
114-
* @param tableNames the names of the tables to drop
114+
* @param tableNames the names of the tables to drop
115115
* @since 3.2
116116
*/
117117
public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames) {

src/dist/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Changes in version 3.2 M2 (2012-08-xx)
3333
* deprecated SimpleJdbcTestUtils in favor of JdbcTestUtils (SPR-9235)
3434
* introduced countRowsInTableWhere() and dropTables() in JdbcTestUtils (SPR-9235)
3535
* introduced JdbcTemplate in tx base classes in the TestContext framework (SPR-8990)
36+
* introduced countRowsInTableWhere() and dropTables() in tx base test classes (SPR-9665)
3637

3738

3839
Changes in version 3.2 M1 (2012-05-28)

0 commit comments

Comments
 (0)