Support comments in SQL scripts in JdbcTestUtils
Prior to this commit, utility methods in JdbcTestUtils interpreted SQL comments as separate statements, resulting in an exception when such a script is executed. This commit addresses this issue by introducing a readScript(lineNumberReader, String) method that accepts a comment prefix. Comment lines are therefore no longer returned in the parsed script. Furthermore, the existing readScript(lineNumberReader) method now delegates to this new readScript() method, supplying "--" as the default comment prefix. Issue: SPR-9593
This commit is contained in:
@@ -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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,18 +16,21 @@
|
||||
|
||||
package org.springframework.test.jdbc;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.LineNumberReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.support.EncodedResource;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link JdbcTestUtils}.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Sam Brannen
|
||||
* @since 2.5.4
|
||||
*/
|
||||
public class JdbcTestUtilsTests {
|
||||
@@ -45,12 +48,29 @@ public class JdbcTestUtilsTests {
|
||||
|
||||
@Test
|
||||
public void splitSqlScriptDelimitedWithSemicolon() {
|
||||
String statement1 = "insert into customer (id, name) \n"
|
||||
+ "values (1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')";
|
||||
String statement2 = "insert into orders(id, order_date, customer_id) \n" + "values (1, '2008-01-02', 2)";
|
||||
String statement3 = "insert into orders(id, order_date, customer_id) " + "values (1, '2008-01-02', 2)";
|
||||
String rawStatement1 = "insert into customer (id, name)\nvalues (1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')";
|
||||
String cleanedStatement1 = "insert into customer (id, name) values (1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')";
|
||||
String rawStatement2 = "insert into orders(id, order_date, customer_id)\nvalues (1, '2008-01-02', 2)";
|
||||
String cleanedStatement2 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
|
||||
String rawStatement3 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
|
||||
String cleanedStatement3 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
|
||||
char delim = ';';
|
||||
String script = statement1 + delim + statement2 + delim + statement3;
|
||||
String script = rawStatement1 + delim + rawStatement2 + delim + rawStatement3 + delim;
|
||||
List<String> statements = new ArrayList<String>();
|
||||
JdbcTestUtils.splitSqlScript(script, delim, statements);
|
||||
assertEquals("wrong number of statements", 3, statements.size());
|
||||
assertEquals("statement 1 not split correctly", cleanedStatement1, statements.get(0));
|
||||
assertEquals("statement 2 not split correctly", cleanedStatement2, statements.get(1));
|
||||
assertEquals("statement 3 not split correctly", cleanedStatement3, statements.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void splitSqlScriptDelimitedWithNewLine() {
|
||||
String statement1 = "insert into customer (id, name) values (1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')";
|
||||
String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
|
||||
String statement3 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
|
||||
char delim = '\n';
|
||||
String script = statement1 + delim + statement2 + delim + statement3 + delim;
|
||||
List<String> statements = new ArrayList<String>();
|
||||
JdbcTestUtils.splitSqlScript(script, delim, statements);
|
||||
assertEquals("wrong number of statements", 3, statements.size());
|
||||
@@ -60,14 +80,22 @@ public class JdbcTestUtilsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void splitSqlScriptDelimitedWithNewLine() {
|
||||
String statement1 = "insert into customer (id, name) " + "values (1, 'Rod ; Johnson'), (2, 'Adrian ; Collier')";
|
||||
String statement2 = "insert into orders(id, order_date, customer_id) " + "values (1, '2008-01-02', 2)";
|
||||
String statement3 = "insert into orders(id, order_date, customer_id) " + "values (1, '2008-01-02', 2)";
|
||||
char delim = '\n';
|
||||
String script = statement1 + delim + statement2 + delim + statement3;
|
||||
public void readAndSplitScriptContainingComments() throws Exception {
|
||||
|
||||
EncodedResource resource = new EncodedResource(new ClassPathResource("test-data-with-comments.sql", getClass()));
|
||||
LineNumberReader lineNumberReader = new LineNumberReader(resource.getReader());
|
||||
|
||||
String script = JdbcTestUtils.readScript(lineNumberReader);
|
||||
assertFalse("script should not contain --", script.contains("--"));
|
||||
|
||||
char delim = ';';
|
||||
List<String> statements = new ArrayList<String>();
|
||||
JdbcTestUtils.splitSqlScript(script, delim, statements);
|
||||
|
||||
String statement1 = "insert into customer (id, name) values (1, 'Rod; Johnson'), (2, 'Adrian Collier')";
|
||||
String statement2 = " insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
|
||||
String statement3 = " insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
|
||||
|
||||
assertEquals("wrong number of statements", 3, statements.size());
|
||||
assertEquals("statement 1 not split correctly", statement1, statements.get(0));
|
||||
assertEquals("statement 2 not split correctly", statement2, statements.get(1));
|
||||
|
||||
Reference in New Issue
Block a user