Delete legacy JdbcTestUtils in spring-orm
This commit is contained in:
parent
dba48a1ff5
commit
e581244370
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2015 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.
|
||||||
|
|
@ -16,18 +16,12 @@
|
||||||
|
|
||||||
package org.springframework.test;
|
package org.springframework.test;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.LineNumberReader;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.springframework.core.io.support.EncodedResource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
import org.springframework.dao.DataAccessResourceFailureException;
|
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.test.jdbc.JdbcTestUtils;
|
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is only used within tests in the spring-orm module.
|
* This class is only used within tests in the spring-orm module.
|
||||||
|
|
@ -139,61 +133,20 @@ public abstract class AbstractTransactionalDataSourceSpringContextTests extends
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the given SQL script. Will be rolled back by default,
|
* Execute the given SQL script.
|
||||||
* according to the fate of the current transaction.
|
* <p>Use with caution outside of a transaction!
|
||||||
* @param sqlResourcePath Spring resource path for the SQL script.
|
* <p>The script will normally be loaded by classpath.
|
||||||
* Should 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>
|
* <p><b>Do not use this method to execute DDL if you expect rollback.</b>
|
||||||
* @param continueOnError whether or not to continue without throwing
|
* @param sqlResourcePath the Spring resource path for the SQL script
|
||||||
* an exception in the event of an error
|
* @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
|
* @throws DataAccessException if there is an error executing a statement
|
||||||
* and continueOnError was false
|
* @see ResourceDatabasePopulator
|
||||||
|
* @see #setSqlScriptEncoding
|
||||||
*/
|
*/
|
||||||
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
|
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
|
||||||
if (logger.isInfoEnabled()) {
|
Resource resource = this.applicationContext.getResource(sqlResourcePath);
|
||||||
logger.info("Executing SQL script '" + sqlResourcePath + "'");
|
new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(jdbcTemplate.getDataSource());
|
||||||
}
|
|
||||||
|
|
||||||
EncodedResource resource =
|
|
||||||
new EncodedResource(getApplicationContext().getResource(sqlResourcePath), this.sqlScriptEncoding);
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
List statements = new LinkedList();
|
|
||||||
try {
|
|
||||||
LineNumberReader lnr = new LineNumberReader(resource.getReader());
|
|
||||||
String script = JdbcTestUtils.readScript(lnr);
|
|
||||||
char delimiter = ';';
|
|
||||||
if (!JdbcTestUtils.containsSqlScriptDelimiters(script, delimiter)) {
|
|
||||||
delimiter = '\n';
|
|
||||||
}
|
|
||||||
JdbcTestUtils.splitSqlScript(script, delimiter, statements);
|
|
||||||
for (Iterator itr = statements.iterator(); itr.hasNext(); ) {
|
|
||||||
String statement = (String) itr.next();
|
|
||||||
try {
|
|
||||||
int rowsAffected = this.jdbcTemplate.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;
|
|
||||||
logger.info("Done executing SQL scriptBuilder '" + sqlResourcePath + "' in " + elapsedTime + " ms");
|
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
throw new DataAccessResourceFailureException("Failed to open SQL script '" + sqlResourcePath + "'", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,104 +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.List;
|
|
||||||
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* JdbcTestUtils is a collection of JDBC related utility methods for
|
|
||||||
* use in unit and integration testing scenarios.
|
|
||||||
*
|
|
||||||
* @author Thomas Risberg
|
|
||||||
* @since 2.5.4
|
|
||||||
*/
|
|
||||||
public class JdbcTestUtils {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read a script from the LineNumberReaded and build a String containing the lines.
|
|
||||||
* @param lineNumberReader the {@code LineNumberReader} containing the script to be processed
|
|
||||||
* @return {@code String} containing the script lines
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public static String readScript(LineNumberReader lineNumberReader) throws IOException {
|
|
||||||
String currentStatement = lineNumberReader.readLine();
|
|
||||||
StringBuilder scriptBuilder = new StringBuilder();
|
|
||||||
while (currentStatement != null) {
|
|
||||||
if (StringUtils.hasText(currentStatement)) {
|
|
||||||
if (scriptBuilder.length() > 0) {
|
|
||||||
scriptBuilder.append('\n');
|
|
||||||
}
|
|
||||||
scriptBuilder.append(currentStatement);
|
|
||||||
}
|
|
||||||
currentStatement = lineNumberReader.readLine();
|
|
||||||
}
|
|
||||||
return scriptBuilder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Does the provided SQL script contain the specified delimiter?
|
|
||||||
* @param script the SQL script
|
|
||||||
* @param delim charecter delimiting each statement - typically a ';' character
|
|
||||||
*/
|
|
||||||
public static boolean containsSqlScriptDelimiters(String script, char delim) {
|
|
||||||
boolean inLiteral = false;
|
|
||||||
char[] content = script.toCharArray();
|
|
||||||
for (int i = 0; i < script.length(); i++) {
|
|
||||||
if (content[i] == '\'') {
|
|
||||||
inLiteral = !inLiteral;
|
|
||||||
}
|
|
||||||
if (content[i] == delim && !inLiteral) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Split an SQL script into separate statements delimited with the provided delimiter character. Each
|
|
||||||
* individual statement will be added to the provided {@code List}.
|
|
||||||
* @param script the SQL script
|
|
||||||
* @param delim charecter delimiting each statement - typically a ';' character
|
|
||||||
* @param statements the List that will contain the individual statements
|
|
||||||
*/
|
|
||||||
public static void splitSqlScript(String script, char delim, List<String> statements) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
boolean inLiteral = false;
|
|
||||||
char[] content = script.toCharArray();
|
|
||||||
for (int i = 0; i < script.length(); i++) {
|
|
||||||
if (content[i] == '\'') {
|
|
||||||
inLiteral = !inLiteral;
|
|
||||||
}
|
|
||||||
if (content[i] == delim && !inLiteral) {
|
|
||||||
if (sb.length() > 0) {
|
|
||||||
statements.add(sb.toString());
|
|
||||||
sb = new StringBuilder();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
sb.append(content[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (sb.length() > 0) {
|
|
||||||
statements.add(sb.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue