Skip to content

Commit 8d9637a

Browse files
committed
Provide JdbcTemplate in tx base classes in the TCF
Since Spring 2.5, the abstract transactional base classes in the TestContext framework have defined and delegated to a protected SimpleJdbcTemplate instance variable; however, SimpleJdbcTemplate has deprecated since Spring 3.1. Consequently, subclasses of AbstractTransactionalJUnit4SpringContextTests and AbstractTransactionalTestNGSpringContextTests that use this instance variable suffer from seemingly unnecessary deprecation warnings. This commit addresses this issue by introducing a protected JdbcTemplate instance variable in abstract transactional base classes to replace the use of the existing SimpleJdbcTemplate. Furthermore, the existing simpleJdbcTemplate instance variable has been deprecated, and utility methods in the affected base classes now delegate to JdbcTestUtils instead of the now deprecated SimpleJdbcTestUtils. Issue: SPR-8990
1 parent a7d4377 commit 8d9637a

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
import org.springframework.core.io.Resource;
2424
import org.springframework.core.io.support.EncodedResource;
2525
import org.springframework.dao.DataAccessException;
26+
import org.springframework.jdbc.core.JdbcTemplate;
2627
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
2728
import org.springframework.test.context.ContextConfiguration;
2829
import org.springframework.test.context.TestExecutionListeners;
2930
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
30-
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
31+
import org.springframework.test.jdbc.JdbcTestUtils;
3132
import org.springframework.transaction.PlatformTransactionManager;
3233
import org.springframework.transaction.annotation.Transactional;
3334

@@ -38,7 +39,7 @@
3839
* {@link PlatformTransactionManager} bean to be defined in the Spring
3940
* {@link ApplicationContext application context}.
4041
*
41-
* <p>This class exposes a {@link SimpleJdbcTemplate} and provides an easy way
42+
* <p>This class exposes a {@link JdbcTemplate} and provides an easy way
4243
* to {@link #countRowsInTable(String) count the number of rows in a table},
4344
* {@link #deleteFromTables(String...) delete from tables}, and
4445
* {@link #executeSqlScript(String, boolean) execute SQL scripts} within a
@@ -68,7 +69,7 @@
6869
* @see org.springframework.test.annotation.Rollback
6970
* @see org.springframework.test.context.transaction.BeforeTransaction
7071
* @see org.springframework.test.context.transaction.AfterTransaction
71-
* @see org.springframework.test.jdbc.SimpleJdbcTestUtils
72+
* @see org.springframework.test.jdbc.JdbcTestUtils
7273
* @see org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests
7374
*/
7475
@TestExecutionListeners(TransactionalTestExecutionListener.class)
@@ -77,19 +78,29 @@
7778
public abstract class AbstractTransactionalJUnit4SpringContextTests extends AbstractJUnit4SpringContextTests {
7879

7980
/**
80-
* The SimpleJdbcTemplate that this base class manages, available to subclasses.
81+
* The {@code SimpleJdbcTemplate} that this base class manages, available to subclasses.
82+
* @deprecated As of Spring 3.2, use {@link #jdbcTemplate} instead.
8183
*/
84+
@Deprecated
8285
protected SimpleJdbcTemplate simpleJdbcTemplate;
8386

87+
/**
88+
* The {@code JdbcTemplate} that this base class manages, available to subclasses.
89+
*/
90+
protected JdbcTemplate jdbcTemplate;
91+
8492
private String sqlScriptEncoding;
8593

8694

8795
/**
88-
* Set the DataSource, typically provided via Dependency Injection.
96+
* Set the {@code DataSource}, typically provided via Dependency Injection.
97+
* <p>This method also instantiates the {@link #simpleJdbcTemplate} and
98+
* {@link #jdbcTemplate} instance variables.
8999
*/
90100
@Autowired
91101
public void setDataSource(DataSource dataSource) {
92102
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
103+
this.jdbcTemplate = new JdbcTemplate(dataSource);
93104
}
94105

95106
/**
@@ -106,7 +117,7 @@ public void setSqlScriptEncoding(String sqlScriptEncoding) {
106117
* @return the number of rows in the table
107118
*/
108119
protected int countRowsInTable(String tableName) {
109-
return SimpleJdbcTestUtils.countRowsInTable(this.simpleJdbcTemplate, tableName);
120+
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
110121
}
111122

112123
/**
@@ -116,7 +127,7 @@ protected int countRowsInTable(String tableName) {
116127
* @return the total number of rows deleted from all specified tables
117128
*/
118129
protected int deleteFromTables(String... names) {
119-
return SimpleJdbcTestUtils.deleteFromTables(this.simpleJdbcTemplate, names);
130+
return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names);
120131
}
121132

122133
/**
@@ -132,7 +143,7 @@ protected int deleteFromTables(String... names) {
132143
*/
133144
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
134145
Resource resource = this.applicationContext.getResource(sqlResourcePath);
135-
SimpleJdbcTestUtils.executeSqlScript(this.simpleJdbcTemplate, new EncodedResource(resource,
146+
JdbcTestUtils.executeSqlScript(this.jdbcTemplate, new EncodedResource(resource,
136147
this.sqlScriptEncoding), continueOnError);
137148
}
138149

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
import org.springframework.core.io.Resource;
2424
import org.springframework.core.io.support.EncodedResource;
2525
import org.springframework.dao.DataAccessException;
26+
import org.springframework.jdbc.core.JdbcTemplate;
2627
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
2728
import org.springframework.test.context.TestExecutionListeners;
2829
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
29-
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
30+
import org.springframework.test.jdbc.JdbcTestUtils;
3031
import org.springframework.transaction.PlatformTransactionManager;
3132
import org.springframework.transaction.annotation.Transactional;
3233

@@ -37,7 +38,7 @@
3738
* {@link PlatformTransactionManager} bean to be defined in the Spring
3839
* {@link ApplicationContext application context}.
3940
*
40-
* <p>This class exposes a {@link SimpleJdbcTemplate} and provides an easy way
41+
* <p>This class exposes a {@link JdbcTemplate} and provides an easy way
4142
* to {@link #countRowsInTable(String) count the number of rows in a table},
4243
* {@link #deleteFromTables(String...) delete from tables}, and
4344
* {@link #executeSqlScript(String, boolean) execute SQL scripts} within a
@@ -57,7 +58,9 @@
5758
* @see org.springframework.transaction.annotation.Transactional
5859
* @see org.springframework.test.annotation.NotTransactional
5960
* @see org.springframework.test.annotation.Rollback
60-
* @see org.springframework.test.jdbc.SimpleJdbcTestUtils
61+
* @see org.springframework.test.context.transaction.BeforeTransaction
62+
* @see org.springframework.test.context.transaction.AfterTransaction
63+
* @see org.springframework.test.jdbc.JdbcTestUtils
6164
* @see org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests
6265
*/
6366
@TestExecutionListeners(TransactionalTestExecutionListener.class)
@@ -66,20 +69,29 @@
6669
public abstract class AbstractTransactionalTestNGSpringContextTests extends AbstractTestNGSpringContextTests {
6770

6871
/**
69-
* The SimpleJdbcTemplate that this base class manages, available to subclasses.
72+
* The {@code SimpleJdbcTemplate} that this base class manages, available to subclasses.
73+
* @deprecated As of Spring 3.2, use {@link #jdbcTemplate} instead.
7074
*/
75+
@Deprecated
7176
protected SimpleJdbcTemplate simpleJdbcTemplate;
7277

78+
/**
79+
* The {@code JdbcTemplate} that this base class manages, available to subclasses.
80+
*/
81+
protected JdbcTemplate jdbcTemplate;
82+
7383
private String sqlScriptEncoding;
7484

7585

7686
/**
77-
* Set the DataSource, typically provided via Dependency Injection.
78-
* @param dataSource the DataSource to inject
87+
* Set the {@code DataSource}, typically provided via Dependency Injection.
88+
* <p>This method also instantiates the {@link #simpleJdbcTemplate} and
89+
* {@link #jdbcTemplate} instance variables.
7990
*/
8091
@Autowired
81-
public void setDataSource(final DataSource dataSource) {
92+
public void setDataSource(DataSource dataSource) {
8293
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
94+
this.jdbcTemplate = new JdbcTemplate(dataSource);
8395
}
8496

8597
/**
@@ -96,24 +108,24 @@ public void setSqlScriptEncoding(String sqlScriptEncoding) {
96108
* @return the number of rows in the table
97109
*/
98110
protected int countRowsInTable(String tableName) {
99-
return SimpleJdbcTestUtils.countRowsInTable(this.simpleJdbcTemplate, tableName);
111+
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
100112
}
101113

102114
/**
103-
* Convenience method for deleting all rows from the specified tables.
104-
* Use with caution outside of a transaction!
115+
* Convenience method for deleting all rows from the specified tables. Use
116+
* with caution outside of a transaction!
105117
* @param names the names of the tables from which to delete
106118
* @return the total number of rows deleted from all specified tables
107119
*/
108120
protected int deleteFromTables(String... names) {
109-
return SimpleJdbcTestUtils.deleteFromTables(this.simpleJdbcTemplate, names);
121+
return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names);
110122
}
111123

112124
/**
113125
* Execute the given SQL script. Use with caution outside of a transaction!
114-
* <p>The script will normally be loaded by classpath. There should be one statement
115-
* per line. Any semicolons will be removed. <b>Do not use this method to execute
116-
* DDL if you expect rollback.</b>
126+
* <p>The script will normally be loaded by classpath. There should be one
127+
* statement per line. Any semicolons will be removed. <b>Do not use this
128+
* method to execute DDL if you expect rollback.</b>
117129
* @param sqlResourcePath the Spring resource path for the SQL script
118130
* @param continueOnError whether or not to continue without throwing an
119131
* exception in the event of an error
@@ -122,7 +134,7 @@ protected int deleteFromTables(String... names) {
122134
*/
123135
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
124136
Resource resource = this.applicationContext.getResource(sqlResourcePath);
125-
SimpleJdbcTestUtils.executeSqlScript(this.simpleJdbcTemplate, new EncodedResource(resource,
137+
JdbcTestUtils.executeSqlScript(this.jdbcTemplate, new EncodedResource(resource,
126138
this.sqlScriptEncoding), continueOnError);
127139
}
128140

src/dist/changelog.txt

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

3637

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

src/reference/docbook/testing.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@
396396
</listitem>
397397

398398
<listitem>
399-
<para>A <classname>SimpleJdbcTemplate</classname>, for executing
399+
<para>A <classname>JdbcTemplate</classname>, for executing
400400
SQL statements to query the database. Such queries can be used to
401401
confirm database state both <emphasis>prior to</emphasis> and
402402
<emphasis>after</emphasis> execution of database-related
@@ -422,15 +422,15 @@
422422
<title>JDBC Testing Support</title>
423423

424424
<para>The <literal>org.springframework.test.jdbc</literal> package
425-
contains <classname>SimpleJdbcTestUtils</classname>, which is a
425+
contains <classname>JdbcTestUtils</classname>, which is a
426426
collection of JDBC related utility functions intended to simplify
427427
standard database testing scenarios. <emphasis>Note that <link
428428
linkend="testcontext-support-classes-junit4">
429429
<classname>AbstractTransactionalJUnit4SpringContextTests</classname>
430430
</link> and <link linkend="testcontext-support-classes-testng">
431431
<classname>AbstractTransactionalTestNGSpringContextTests</classname>
432432
</link> provide convenience methods which delegate to
433-
<classname>SimpleJdbcTestUtils</classname> internally.</emphasis></para>
433+
<classname>JdbcTestUtils</classname> internally.</emphasis></para>
434434

435435
<para>The <literal>spring-jdbc</literal> module provides support for
436436
configuring and launching an embedded database which can be used in
@@ -2157,7 +2157,7 @@ public void updateWithSessionFlush() {
21572157
</listitem>
21582158

21592159
<listitem>
2160-
<para><literal>simpleJdbcTemplate</literal>: Use this
2160+
<para><literal>jdbcTemplate</literal>: Use this
21612161
variable to execute SQL statements to query the database.
21622162
Such queries can be used to confirm database state both
21632163
<emphasis>prior to</emphasis> and <emphasis>after</emphasis>
@@ -2266,7 +2266,7 @@ public class SimpleTest {
22662266
</listitem>
22672267

22682268
<listitem>
2269-
<para><literal>simpleJdbcTemplate</literal>: Use this
2269+
<para><literal>jdbcTemplate</literal>: Use this
22702270
variable to execute SQL statements to query the database.
22712271
Such queries can be used to confirm database state both
22722272
<emphasis>prior to</emphasis> and <emphasis>after</emphasis>

0 commit comments

Comments
 (0)