adapted to deprecation backport in 3.1.3

Issue: SPR-9664
This commit is contained in:
Juergen Hoeller 2012-08-31 16:52:28 +02:00
parent 1bd775f828
commit 50df4d08c2
2 changed files with 20 additions and 44 deletions

View File

@ -36,7 +36,7 @@ import org.springframework.util.StringUtils;
* {@code JdbcTestUtils} is a collection of JDBC related utility functions * {@code JdbcTestUtils} is a collection of JDBC related utility functions
* intended to simplify standard database testing scenarios. * intended to simplify standard database testing scenarios.
* *
* <p>As of Spring 3.2, {@code JdbcTestUtils} supersedes {@link SimpleJdbcTestUtils}. * <p>As of Spring 3.1.3, {@code JdbcTestUtils} supersedes {@link SimpleJdbcTestUtils}.
* *
* @author Thomas Risberg * @author Thomas Risberg
* @author Sam Brannen * @author Sam Brannen
@ -50,11 +50,9 @@ public class JdbcTestUtils {
/** /**
* Count the rows in the given table. * Count the rows in the given table.
*
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
* @param tableName name of the table to count rows in * @param tableName name of the table to count rows in
* @return the number of rows in the table * @return the number of rows in the table
* @since 3.2
*/ */
public static int countRowsInTable(JdbcTemplate jdbcTemplate, String tableName) { public static int countRowsInTable(JdbcTemplate jdbcTemplate, String tableName) {
return jdbcTemplate.queryForInt("SELECT COUNT(0) FROM " + tableName); return jdbcTemplate.queryForInt("SELECT COUNT(0) FROM " + tableName);
@ -62,38 +60,31 @@ public class JdbcTestUtils {
/** /**
* Count the rows in the given table, using the provided {@code WHERE} clause. * Count the rows in the given table, using the provided {@code WHERE} clause.
*
* <p>If the provided {@code WHERE} clause contains text, it will be prefixed * <p>If the provided {@code WHERE} clause contains text, it will be prefixed
* with {@code " WHERE "} and then appended to the generated {@code SELECT} * with {@code " WHERE "} and then appended to the generated {@code SELECT}
* statement. For example, if the provided table name is {@code "person"} and * statement. For example, if the provided table name is {@code "person"} and
* the provided where clause is {@code "name = 'Bob' and age > 25"}, the * the provided where clause is {@code "name = 'Bob' and age > 25"}, the
* resulting SQL statement to execute will be * resulting SQL statement to execute will be
* {@code "SELECT COUNT(0) FROM person WHERE name = 'Bob' and age > 25"}. * {@code "SELECT COUNT(0) FROM person WHERE name = 'Bob' and age > 25"}.
*
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
* @param tableName the name of the table to count rows in * @param tableName the name of the table to count rows in
* @param whereClause the {@code WHERE} clause to append to the query * @param whereClause the {@code WHERE} clause to append to the query
* @return the number of rows in the table that match the provided * @return the number of rows in the table that match the provided
* {@code WHERE} clause * {@code WHERE} clause
* @since 3.2
*/ */
public static int countRowsInTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause) { public static int countRowsInTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause) {
String sql = "SELECT COUNT(0) FROM " + tableName; String sql = "SELECT COUNT(0) FROM " + tableName;
if (StringUtils.hasText(whereClause)) { if (StringUtils.hasText(whereClause)) {
sql += " WHERE " + whereClause; sql += " WHERE " + whereClause;
} }
return jdbcTemplate.queryForInt(sql); return jdbcTemplate.queryForInt(sql);
} }
/** /**
* Delete all rows from the specified tables. * Delete all rows from the specified tables.
*
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
* @param tableNames the names of the tables to delete from * @param tableNames the names of the tables to delete from
* @return the total number of rows deleted from all specified tables * @return the total number of rows deleted from all specified tables
* @since 3.2
*/ */
public static int deleteFromTables(JdbcTemplate jdbcTemplate, String... tableNames) { public static int deleteFromTables(JdbcTemplate jdbcTemplate, String... tableNames) {
int totalRowCount = 0; int totalRowCount = 0;
@ -109,10 +100,8 @@ public class JdbcTestUtils {
/** /**
* Drop the specified tables. * Drop the specified tables.
*
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
* @param tableNames the names of the tables to drop * @param tableNames the names of the tables to drop
* @since 3.2
*/ */
public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames) { public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames) {
for (String tableName : tableNames) { for (String tableName : tableNames) {
@ -125,12 +114,9 @@ public class JdbcTestUtils {
/** /**
* Execute the given SQL script. * Execute the given SQL script.
*
* <p>The script will typically be loaded from the classpath. There should * <p>The script will typically be loaded from the classpath. There should
* be one statement per line. Any semicolons will be removed. * be one statement per line. Any semicolons will be removed.
*
* <p><b>Do not use this method to execute DDL if you expect rollback.</b> * <p><b>Do not use this method to execute DDL if you expect rollback.</b>
*
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
* @param resourceLoader the resource loader with which to load the SQL script * @param resourceLoader the resource loader with which to load the SQL script
* @param sqlResourcePath the Spring resource path for the SQL script * @param sqlResourcePath the Spring resource path for the SQL script
@ -138,45 +124,39 @@ public class JdbcTestUtils {
* exception in the event of an error * exception in the event of an error
* @throws DataAccessException if there is an error executing a statement * @throws DataAccessException if there is an error executing a statement
* and {@code continueOnError} is {@code false} * and {@code continueOnError} is {@code false}
* @since 3.2
*/ */
public static void executeSqlScript(JdbcTemplate jdbcTemplate, ResourceLoader resourceLoader, public static void executeSqlScript(JdbcTemplate jdbcTemplate, ResourceLoader resourceLoader,
String sqlResourcePath, boolean continueOnError) throws DataAccessException { String sqlResourcePath, boolean continueOnError) throws DataAccessException {
Resource resource = resourceLoader.getResource(sqlResourcePath); Resource resource = resourceLoader.getResource(sqlResourcePath);
executeSqlScript(jdbcTemplate, resource, continueOnError); executeSqlScript(jdbcTemplate, resource, continueOnError);
} }
/** /**
* Execute the given SQL script. * Execute the given SQL script.
*
* <p>The script will typically be loaded from the classpath. Statements * <p>The script will typically be loaded from the classpath. Statements
* should be delimited with a semicolon. If statements are not delimited with * should be delimited with a semicolon. If statements are not delimited with
* a semicolon then there should be one statement per line. Statements are * a semicolon then there should be one statement per line. Statements are
* allowed to span lines only if they are delimited with a semicolon. * 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> * <p><b>Do not use this method to execute DDL if you expect rollback.</b>
*
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
* @param resource the resource to load the SQL script from * @param resource the resource to load the SQL script from
* @param continueOnError whether or not to continue without throwing an * @param continueOnError whether or not to continue without throwing an
* exception in the event of an error * exception in the event of an error
* @throws DataAccessException if there is an error executing a statement * @throws DataAccessException if there is an error executing a statement
* and {@code continueOnError} is {@code false} * and {@code continueOnError} is {@code false}
* @since 3.2
*/ */
public static void executeSqlScript(JdbcTemplate jdbcTemplate, Resource resource, boolean continueOnError) public static void executeSqlScript(JdbcTemplate jdbcTemplate, Resource resource, boolean continueOnError)
throws DataAccessException { throws DataAccessException {
executeSqlScript(jdbcTemplate, new EncodedResource(resource), continueOnError); executeSqlScript(jdbcTemplate, new EncodedResource(resource), continueOnError);
} }
/** /**
* Execute the given SQL script. * Execute the given SQL script.
*
* <p>The script will typically be loaded from the classpath. There should * <p>The script will typically be loaded from the classpath. There should
* be one statement per line. Any semicolons will be removed. * be one statement per line. Any semicolons will be removed.
*
* <p><b>Do not use this method to execute DDL if you expect rollback.</b> * <p><b>Do not use this method to execute DDL if you expect rollback.</b>
*
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
* @param resource the resource (potentially associated with a specific encoding) * @param resource the resource (potentially associated with a specific encoding)
* to load the SQL script from * to load the SQL script from
@ -184,7 +164,6 @@ public class JdbcTestUtils {
* exception in the event of an error * exception in the event of an error
* @throws DataAccessException if there is an error executing a statement * @throws DataAccessException if there is an error executing a statement
* and {@code continueOnError} is {@code false} * and {@code continueOnError} is {@code false}
* @since 3.2
*/ */
public static void executeSqlScript(JdbcTemplate jdbcTemplate, EncodedResource resource, boolean continueOnError) public static void executeSqlScript(JdbcTemplate jdbcTemplate, EncodedResource resource, boolean continueOnError)
throws DataAccessException { throws DataAccessException {
@ -192,7 +171,6 @@ public class JdbcTestUtils {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info("Executing SQL script from " + resource); logger.info("Executing SQL script from " + resource);
} }
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
List<String> statements = new LinkedList<String>(); List<String> statements = new LinkedList<String>();
LineNumberReader reader = null; LineNumberReader reader = null;
@ -210,12 +188,14 @@ public class JdbcTestUtils {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(rowsAffected + " rows affected by SQL: " + statement); logger.debug(rowsAffected + " rows affected by SQL: " + statement);
} }
} catch (DataAccessException ex) { }
catch (DataAccessException ex) {
if (continueOnError) { if (continueOnError) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn("SQL statement [" + statement + "] failed", ex); logger.warn("SQL statement [" + statement + "] failed", ex);
} }
} else { }
else {
throw ex; throw ex;
} }
} }
@ -224,9 +204,11 @@ public class JdbcTestUtils {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info(String.format("Executed SQL script from %s in %s ms.", resource, elapsedTime)); logger.info(String.format("Executed SQL script from %s in %s ms.", resource, elapsedTime));
} }
} catch (IOException ex) { }
catch (IOException ex) {
throw new DataAccessResourceFailureException("Failed to open SQL script from " + resource, ex); throw new DataAccessResourceFailureException("Failed to open SQL script from " + resource, ex);
} finally { }
finally {
try { try {
if (reader != null) { if (reader != null) {
reader.close(); reader.close();
@ -240,7 +222,6 @@ public class JdbcTestUtils {
/** /**
* Read a script from the provided {@code LineNumberReader} and build a * Read a script from the provided {@code LineNumberReader} and build a
* {@code String} containing the lines. * {@code String} containing the lines.
*
* @param lineNumberReader the {@code LineNumberReader} containing the script * @param lineNumberReader the {@code LineNumberReader} containing the script
* to be processed * to be processed
* @return a {@code String} containing the script lines * @return a {@code String} containing the script lines
@ -262,12 +243,9 @@ public class JdbcTestUtils {
/** /**
* Determine if the provided SQL script contains the specified delimiter. * Determine if the provided SQL script contains the specified delimiter.
*
* @param script the SQL script * @param script the SQL script
* @param delim character delimiting each statement &mdash; typically a ';' * @param delim character delimiting each statement &mdash; typically a ';' character
* character * @return {@code true} if the script contains the delimiter; {@code false} otherwise
* @return {@code true} if the script contains the delimiter; {@code false}
* otherwise
*/ */
public static boolean containsSqlScriptDelimiters(String script, char delim) { public static boolean containsSqlScriptDelimiters(String script, char delim) {
boolean inLiteral = false; boolean inLiteral = false;
@ -287,10 +265,8 @@ public class JdbcTestUtils {
* Split an SQL script into separate statements delimited with the provided * Split an SQL script into separate statements delimited with the provided
* delimiter character. Each individual statement will be added to the * delimiter character. Each individual statement will be added to the
* provided <code>List</code>. * provided <code>List</code>.
*
* @param script the SQL script * @param script the SQL script
* @param delim character delimiting each statement &mdash; typically a ';' * @param delim character delimiting each statement &mdash; typically a ';' character
* character
* @param statements the list that will contain the individual statements * @param statements the list that will contain the individual statements
*/ */
public static void splitSqlScript(String script, char delim, List<String> statements) { public static void splitSqlScript(String script, char delim, List<String> statements) {
@ -306,7 +282,8 @@ public class JdbcTestUtils {
statements.add(sb.toString()); statements.add(sb.toString());
sb = new StringBuilder(); sb = new StringBuilder();
} }
} else { }
else {
sb.append(content[i]); sb.append(content[i]);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +18,6 @@ package org.springframework.test.jdbc;
import java.io.IOException; import java.io.IOException;
import java.io.LineNumberReader; import java.io.LineNumberReader;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -40,7 +39,7 @@ import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Thomas Risberg * @author Thomas Risberg
* @since 2.5 * @since 2.5
* @deprecated As of Spring 3.2, use {@link JdbcTestUtils} instead. * @deprecated as of Spring 3.1.3; use {@link JdbcTestUtils} instead.
*/ */
@Deprecated @Deprecated
public abstract class SimpleJdbcTestUtils { public abstract class SimpleJdbcTestUtils {