Delete deprecated code in the TestContext framework

This commit deletes the deprecated SimpleJdbcTestUtils class as well as
remaining usage of SimpleJdbcTemplate within the TestContext framework
and its test suite.

Issue: SPR-10499
This commit is contained in:
Sam Brannen 2013-04-28 19:10:53 +02:00
parent 2685818e91
commit 88e514bd56
8 changed files with 38 additions and 247 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,7 +24,6 @@ import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
@ -66,7 +65,6 @@ import org.springframework.transaction.annotation.Transactional;
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
* @see org.springframework.test.context.transaction.TransactionConfiguration
* @see org.springframework.transaction.annotation.Transactional
* @see org.springframework.test.annotation.NotTransactional
* @see org.springframework.test.annotation.Rollback
* @see org.springframework.test.context.transaction.BeforeTransaction
* @see org.springframework.test.context.transaction.AfterTransaction
@ -78,13 +76,6 @@ import org.springframework.transaction.annotation.Transactional;
@SuppressWarnings("deprecation")
public abstract class AbstractTransactionalJUnit4SpringContextTests extends AbstractJUnit4SpringContextTests {
/**
* The {@code SimpleJdbcTemplate} that this base class manages, available to subclasses.
* @deprecated As of Spring 3.2, use {@link #jdbcTemplate} instead.
*/
@Deprecated
protected SimpleJdbcTemplate simpleJdbcTemplate;
/**
* The {@code JdbcTemplate} that this base class manages, available to subclasses.
* @since 3.2
@ -96,12 +87,11 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst
/**
* Set the {@code DataSource}, typically provided via Dependency Injection.
* <p>This method also instantiates the {@link #simpleJdbcTemplate} and
* {@link #jdbcTemplate} instance variables.
* <p>This method also instantiates the {@link #jdbcTemplate} instance
* variable.
*/
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,7 +24,6 @@ import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.jdbc.JdbcTestUtils;
@ -57,7 +56,6 @@ import org.springframework.transaction.annotation.Transactional;
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
* @see org.springframework.test.context.transaction.TransactionConfiguration
* @see org.springframework.transaction.annotation.Transactional
* @see org.springframework.test.annotation.NotTransactional
* @see org.springframework.test.annotation.Rollback
* @see org.springframework.test.context.transaction.BeforeTransaction
* @see org.springframework.test.context.transaction.AfterTransaction
@ -69,13 +67,6 @@ import org.springframework.transaction.annotation.Transactional;
@SuppressWarnings("deprecation")
public abstract class AbstractTransactionalTestNGSpringContextTests extends AbstractTestNGSpringContextTests {
/**
* The {@code SimpleJdbcTemplate} that this base class manages, available to subclasses.
* @deprecated As of Spring 3.2, use {@link #jdbcTemplate} instead.
*/
@Deprecated
protected SimpleJdbcTemplate simpleJdbcTemplate;
/**
* The {@code JdbcTemplate} that this base class manages, available to subclasses.
* @since 3.2
@ -87,12 +78,11 @@ public abstract class AbstractTransactionalTestNGSpringContextTests extends Abst
/**
* Set the {@code DataSource}, typically provided via Dependency Injection.
* <p>This method also instantiates the {@link #simpleJdbcTemplate} and
* {@link #jdbcTemplate} instance variables.
* <p>This method also instantiates the {@link #jdbcTemplate} instance
* variable.
*/
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

View File

@ -37,8 +37,6 @@ import org.springframework.util.StringUtils;
* {@code JdbcTestUtils} is a collection of JDBC related utility functions
* intended to simplify standard database testing scenarios.
*
* <p>As of Spring 3.1.3, {@code JdbcTestUtils} supersedes {@link SimpleJdbcTestUtils}.
*
* @author Thomas Risberg
* @author Sam Brannen
* @author Juergen Hoeller

View File

@ -1,187 +0,0 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.jdbc;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
/**
* A Java-5-based collection of JDBC related utility functions intended to
* simplify standard database testing scenarios.
*
* @author Sam Brannen
* @author Juergen Hoeller
* @author Thomas Risberg
* @since 2.5
* @deprecated as of Spring 3.1.3; use {@link JdbcTestUtils} instead.
*/
@Deprecated
public abstract class SimpleJdbcTestUtils {
private static final Log logger = LogFactory.getLog(SimpleJdbcTestUtils.class);
/**
* Count the rows in the given table.
* @param simpleJdbcTemplate the SimpleJdbcTemplate with which to perform JDBC operations
* @param tableName table name to count rows in
* @return the number of rows in the table
*/
public static int countRowsInTable(SimpleJdbcTemplate simpleJdbcTemplate, String tableName) {
return simpleJdbcTemplate.queryForInt("SELECT COUNT(0) FROM " + tableName);
}
/**
* Delete all rows from the specified tables.
* @param simpleJdbcTemplate the SimpleJdbcTemplate with which to perform JDBC operations
* @param tableNames the names of the tables from which to delete
* @return the total number of rows deleted from all specified tables
*/
public static int deleteFromTables(SimpleJdbcTemplate simpleJdbcTemplate, String... tableNames) {
int totalRowCount = 0;
for (String tableName : tableNames) {
int rowCount = simpleJdbcTemplate.update("DELETE FROM " + tableName);
totalRowCount += rowCount;
if (logger.isInfoEnabled()) {
logger.info("Deleted " + rowCount + " rows from table " + tableName);
}
}
return totalRowCount;
}
/**
* Execute the given SQL script.
* <p>The script will normally be loaded by classpath. There should be one statement
* per line. Any semicolons will be removed. <b>Do not use this method to execute
* DDL if you expect rollback.</b>
* @param simpleJdbcTemplate the SimpleJdbcTemplate with which to perform JDBC operations
* @param resourceLoader the resource loader (with which to load the SQL script
* @param sqlResourcePath the Spring resource path for the SQL script
* @param continueOnError whether or not to continue without throwing an
* exception in the event of an error
* @throws DataAccessException if there is an error executing a statement
* and continueOnError was {@code false}
*/
public static void executeSqlScript(SimpleJdbcTemplate simpleJdbcTemplate, ResourceLoader resourceLoader,
String sqlResourcePath, boolean continueOnError) throws DataAccessException {
Resource resource = resourceLoader.getResource(sqlResourcePath);
executeSqlScript(simpleJdbcTemplate, resource, continueOnError);
}
/**
* Execute the given SQL script. The script will normally be loaded by classpath.
* <p>Statements should be delimited with a semicolon. If statements are not delimited with
* a semicolon then there should be one statement per line. Statements are allowed to span
* lines only if they are delimited with a semicolon.
* <p><b>Do not use this method to execute DDL if you expect rollback.</b>
* @param simpleJdbcTemplate the SimpleJdbcTemplate with which to perform JDBC operations
* @param resource the resource to load the SQL script from.
* @param continueOnError whether or not to continue without throwing an
* exception in the event of an error.
* @throws DataAccessException if there is an error executing a statement
* and continueOnError was {@code false}
*/
public static void executeSqlScript(SimpleJdbcTemplate simpleJdbcTemplate, Resource resource,
boolean continueOnError) throws DataAccessException {
executeSqlScript(simpleJdbcTemplate, new EncodedResource(resource), continueOnError);
}
/**
* Execute the given SQL script.
* <p>The script will normally be loaded by classpath. There should be one statement
* per line. Any semicolons will be removed. <b>Do not use this method to execute
* DDL if you expect rollback.</b>
* @param simpleJdbcTemplate the SimpleJdbcTemplate with which to perform JDBC operations
* @param resource the resource (potentially associated with a specific encoding)
* to load the SQL script from.
* @param continueOnError whether or not to continue without throwing an
* exception in the event of an error.
* @throws DataAccessException if there is an error executing a statement
* and continueOnError was {@code false}
*/
public static void executeSqlScript(SimpleJdbcTemplate simpleJdbcTemplate, EncodedResource resource,
boolean continueOnError) throws DataAccessException {
if (logger.isInfoEnabled()) {
logger.info("Executing SQL script from " + resource);
}
long startTime = System.currentTimeMillis();
List<String> statements = new LinkedList<String>();
LineNumberReader reader = null;
try {
reader = new LineNumberReader(resource.getReader());
String script = JdbcTestUtils.readScript(reader);
char delimiter = ';';
if (!JdbcTestUtils.containsSqlScriptDelimiters(script, delimiter)) {
delimiter = '\n';
}
JdbcTestUtils.splitSqlScript(script, delimiter, statements);
for (String statement : statements) {
try {
int rowsAffected = simpleJdbcTemplate.update(statement);
if (logger.isDebugEnabled()) {
logger.debug(rowsAffected + " rows affected by SQL: " + statement);
}
}
catch (DataAccessException ex) {
if (continueOnError) {
if (logger.isWarnEnabled()) {
logger.warn("SQL: " + statement + " failed", ex);
}
}
else {
throw ex;
}
}
}
long elapsedTime = System.currentTimeMillis() - startTime;
if (logger.isInfoEnabled()) {
logger.info("Done executing SQL scriptBuilder from " + resource + " in " + elapsedTime + " ms.");
}
}
catch (IOException ex) {
throw new DataAccessResourceFailureException("Failed to open SQL script from " + resource, ex);
}
finally {
try {
if (reader != null) {
reader.close();
}
}
catch (IOException ex) {
// ignore
}
}
}
}

View File

@ -35,11 +35,11 @@ import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@ -80,29 +80,29 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans
protected String bar;
protected static int clearPersonTable(final SimpleJdbcTemplate simpleJdbcTemplate) {
return SimpleJdbcTestUtils.deleteFromTables(simpleJdbcTemplate, "person");
protected static int clearPersonTable(final JdbcTemplate jdbcTemplate) {
return JdbcTestUtils.deleteFromTables(jdbcTemplate, "person");
}
protected static void createPersonTable(final SimpleJdbcTemplate simpleJdbcTemplate) {
protected static void createPersonTable(final JdbcTemplate jdbcTemplate) {
try {
simpleJdbcTemplate.update("CREATE TABLE person (name VARCHAR(20) NOT NULL, PRIMARY KEY(name))");
jdbcTemplate.update("CREATE TABLE person (name VARCHAR(20) NOT NULL, PRIMARY KEY(name))");
}
catch (final BadSqlGrammarException bsge) {
/* ignore */
}
}
protected static int countRowsInPersonTable(final SimpleJdbcTemplate simpleJdbcTemplate) {
return SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "person");
protected static int countRowsInPersonTable(final JdbcTemplate jdbcTemplate) {
return JdbcTestUtils.countRowsInTable(jdbcTemplate, "person");
}
protected static int addPerson(final SimpleJdbcTemplate simpleJdbcTemplate, final String name) {
return simpleJdbcTemplate.update("INSERT INTO person VALUES(?)", name);
protected static int addPerson(final JdbcTemplate jdbcTemplate, final String name) {
return jdbcTemplate.update("INSERT INTO person VALUES(?)", name);
}
protected static int deletePerson(final SimpleJdbcTemplate simpleJdbcTemplate, final String name) {
return simpleJdbcTemplate.update("DELETE FROM person WHERE name=?", name);
protected static int deletePerson(final JdbcTemplate jdbcTemplate, final String name) {
return jdbcTemplate.update("DELETE FROM person WHERE name=?", name);
}
@Override
@ -189,36 +189,36 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans
@BeforeTransaction
public void beforeTransaction() {
assertEquals("Verifying the number of rows in the person table before a transactional test method.", 1,
countRowsInPersonTable(super.simpleJdbcTemplate));
assertEquals("Adding yoda", 1, addPerson(super.simpleJdbcTemplate, YODA));
countRowsInPersonTable(super.jdbcTemplate));
assertEquals("Adding yoda", 1, addPerson(super.jdbcTemplate, YODA));
}
@Before
public void setUp() throws Exception {
assertEquals("Verifying the number of rows in the person table before a test method.",
(inTransaction() ? 2 : 1), countRowsInPersonTable(super.simpleJdbcTemplate));
(inTransaction() ? 2 : 1), countRowsInPersonTable(super.jdbcTemplate));
}
@Test
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Adding jane", 1, addPerson(super.simpleJdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(super.simpleJdbcTemplate, SUE));
assertEquals("Adding jane", 1, addPerson(super.jdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(super.jdbcTemplate, SUE));
assertEquals("Verifying the number of rows in the person table in modifyTestDataWithinTransaction().", 4,
countRowsInPersonTable(super.simpleJdbcTemplate));
countRowsInPersonTable(super.jdbcTemplate));
}
@After
public void tearDown() throws Exception {
assertEquals("Verifying the number of rows in the person table after a test method.",
(inTransaction() ? 4 : 1), countRowsInPersonTable(super.simpleJdbcTemplate));
(inTransaction() ? 4 : 1), countRowsInPersonTable(super.jdbcTemplate));
}
@AfterTransaction
public void afterTransaction() {
assertEquals("Deleting yoda", 1, deletePerson(super.simpleJdbcTemplate, YODA));
assertEquals("Deleting yoda", 1, deletePerson(super.jdbcTemplate, YODA));
assertEquals("Verifying the number of rows in the person table after a transactional test method.", 1,
countRowsInPersonTable(super.simpleJdbcTemplate));
countRowsInPersonTable(super.jdbcTemplate));
}
@ -226,10 +226,10 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans
@Resource
public void setDataSource(DataSource dataSource) {
SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
createPersonTable(simpleJdbcTemplate);
clearPersonTable(simpleJdbcTemplate);
addPerson(simpleJdbcTemplate, BOB);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
createPersonTable(jdbcTemplate);
clearPersonTable(jdbcTemplate);
addPerson(jdbcTemplate, BOB);
}
}

View File

@ -79,11 +79,11 @@ public class AnnotationConfigTransactionalTestNGSpringContextTests extends
private int createPerson(String name) {
return simpleJdbcTemplate.update("INSERT INTO person VALUES(?)", name);
return jdbcTemplate.update("INSERT INTO person VALUES(?)", name);
}
private int deletePerson(String name) {
return simpleJdbcTemplate.update("DELETE FROM person WHERE name=?", name);
return jdbcTemplate.update("DELETE FROM person WHERE name=?", name);
}
private void assertNumRowsInPersonTable(int expectedNumRows, String testState) {

View File

@ -84,11 +84,11 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
private int createPerson(String name) {
return simpleJdbcTemplate.update("INSERT INTO person VALUES(?)", name);
return jdbcTemplate.update("INSERT INTO person VALUES(?)", name);
}
private int deletePerson(String name) {
return simpleJdbcTemplate.update("DELETE FROM person WHERE name=?", name);
return jdbcTemplate.update("DELETE FROM person WHERE name=?", name);
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -58,8 +58,8 @@ public class DirtiesContextTransactionalTestNGSpringContextTests extends Abstrac
assertInTransaction(true);
assertNotNull(super.applicationContext,
"The application context should have been set due to ApplicationContextAware semantics.");
assertNotNull(super.simpleJdbcTemplate,
"The SimpleJdbcTemplate should have been created in setDataSource() via DI for the DataSource.");
assertNotNull(super.jdbcTemplate,
"The JdbcTemplate should have been created in setDataSource() via DI for the DataSource.");
}
@Test