Sync ScriptUtilsUnitTests implementations to simplify maintenance
This commit is contained in:
parent
6f5e66f00c
commit
84e56bb5d9
|
@ -19,6 +19,7 @@ package org.springframework.jdbc.datasource.init;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.assertj.core.util.Strings;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
@ -55,10 +56,13 @@ public class ScriptUtilsUnitTests {
|
|||
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 = rawStatement1 + delim + rawStatement2 + delim + rawStatement3 + delim;
|
||||
|
||||
String delimiter = ";";
|
||||
String script = Strings.join(rawStatement1, rawStatement2, rawStatement3).with(delimiter);
|
||||
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, delim, statements);
|
||||
splitSqlScript(script, delimiter, statements);
|
||||
|
||||
assertThat(statements).containsExactly(cleanedStatement1, cleanedStatement2, cleanedStatement3);
|
||||
}
|
||||
|
||||
|
@ -67,10 +71,13 @@ public class ScriptUtilsUnitTests {
|
|||
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;
|
||||
|
||||
String delimiter = "\n";
|
||||
String script = Strings.join(statement1, statement2, statement3).with(delimiter);
|
||||
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, delim, statements);
|
||||
splitSqlScript(script, delimiter, statements);
|
||||
|
||||
assertThat(statements).containsExactly(statement1, statement2, statement3);
|
||||
}
|
||||
|
||||
|
@ -78,21 +85,27 @@ public class ScriptUtilsUnitTests {
|
|||
public void splitSqlScriptDelimitedWithNewLineButDefaultDelimiterSpecified() {
|
||||
String statement1 = "do something";
|
||||
String statement2 = "do something else";
|
||||
char delim = '\n';
|
||||
String script = statement1 + delim + statement2 + delim;
|
||||
|
||||
String script = Strings.join(statement1, statement2).with("\n");
|
||||
|
||||
List<String> statements = new ArrayList<>();
|
||||
|
||||
splitSqlScript(script, DEFAULT_STATEMENT_SEPARATOR, statements);
|
||||
|
||||
assertThat(statements).as("stripped but not split statements").containsExactly(script.replace('\n', ' '));
|
||||
}
|
||||
|
||||
@Test // SPR-13218
|
||||
public void splitScriptWithSingleQuotesNestedInsideDoubleQuotes() throws Exception {
|
||||
public void splitScriptWithSingleQuotesNestedInsideDoubleQuotes() {
|
||||
String statement1 = "select '1' as \"Dogbert's owner's\" from dual";
|
||||
String statement2 = "select '2' as \"Dilbert's\" from dual";
|
||||
char delim = ';';
|
||||
String script = statement1 + delim + statement2 + delim;
|
||||
|
||||
String delimiter = ";";
|
||||
String script = Strings.join(statement1, statement2).with(delimiter);
|
||||
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, ';', statements);
|
||||
splitSqlScript(script, delimiter, statements);
|
||||
|
||||
assertThat(statements).containsExactly(statement1, statement2);
|
||||
}
|
||||
|
||||
|
@ -101,8 +114,10 @@ public class ScriptUtilsUnitTests {
|
|||
String script = readScript("db-test-data-multi-newline.sql");
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, "\n\n", statements);
|
||||
|
||||
String statement1 = "insert into T_TEST (NAME) values ('Keith')";
|
||||
String statement2 = "insert into T_TEST (NAME) values ('Dave')";
|
||||
|
||||
assertThat(statements).containsExactly(statement1, statement2);
|
||||
}
|
||||
|
||||
|
@ -124,15 +139,17 @@ public class ScriptUtilsUnitTests {
|
|||
splitScriptContainingComments(script, "--", "#", "^");
|
||||
}
|
||||
|
||||
private void splitScriptContainingComments(String script, String... commentPrefixes) throws Exception {
|
||||
private void splitScriptContainingComments(String script, String... commentPrefixes) {
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(null, script, ";", commentPrefixes, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
|
||||
DEFAULT_BLOCK_COMMENT_END_DELIMITER, 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)";
|
||||
// Statement 4 addresses the error described in SPR-9982.
|
||||
String statement4 = "INSERT INTO persons( person_id , name) VALUES( 1 , 'Name' )";
|
||||
|
||||
assertThat(statements).containsExactly(statement1, statement2, statement3, statement4);
|
||||
}
|
||||
|
||||
|
@ -141,9 +158,11 @@ public class ScriptUtilsUnitTests {
|
|||
String script = readScript("test-data-with-comments-and-leading-tabs.sql");
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, ';', statements);
|
||||
|
||||
String statement1 = "insert into customer (id, name) values (1, 'Sam Brannen')";
|
||||
String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2013-06-08', 1)";
|
||||
String statement3 = "insert into orders(id, order_date, customer_id) values (2, '2013-06-08', 1)";
|
||||
|
||||
assertThat(statements).containsExactly(statement1, statement2, statement3);
|
||||
}
|
||||
|
||||
|
@ -152,8 +171,10 @@ public class ScriptUtilsUnitTests {
|
|||
String script = readScript("test-data-with-multi-line-comments.sql");
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, ';', statements);
|
||||
|
||||
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller')";
|
||||
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Sam' , 'Brannen' )";
|
||||
|
||||
assertThat(statements).containsExactly(statement1, statement2);
|
||||
}
|
||||
|
||||
|
@ -162,8 +183,10 @@ public class ScriptUtilsUnitTests {
|
|||
String script = readScript("test-data-with-multi-line-nested-comments.sql");
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, ';', statements);
|
||||
|
||||
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller')";
|
||||
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Sam' , 'Brannen' )";
|
||||
|
||||
assertThat(statements).containsExactly(statement1, statement2);
|
||||
}
|
||||
|
||||
|
|
|
@ -45,6 +45,8 @@ import static org.springframework.r2dbc.connection.init.ScriptUtils.splitSqlScri
|
|||
* @author Chris Baldwin
|
||||
* @author Nicolas Debeissat
|
||||
* @author Mark Paluch
|
||||
* @since 5.3
|
||||
* @see ScriptUtilsIntegrationTests
|
||||
*/
|
||||
public class ScriptUtilsUnitTests {
|
||||
|
||||
|
@ -57,14 +59,13 @@ public class ScriptUtilsUnitTests {
|
|||
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)";
|
||||
|
||||
String script = Strings.join(rawStatement1, rawStatement2, rawStatement3).with(
|
||||
";");
|
||||
String delimiter = ";";
|
||||
String script = Strings.join(rawStatement1, rawStatement2, rawStatement3).with(delimiter);
|
||||
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, ";", statements);
|
||||
splitSqlScript(script, delimiter, statements);
|
||||
|
||||
assertThat(statements).hasSize(3).containsSequence(cleanedStatement1,
|
||||
cleanedStatement2, cleanedStatement3);
|
||||
assertThat(statements).containsExactly(cleanedStatement1, cleanedStatement2, cleanedStatement3);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -73,13 +74,13 @@ public class ScriptUtilsUnitTests {
|
|||
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)";
|
||||
|
||||
String script = Strings.join(statement1, statement2, statement3).with("\n");
|
||||
String delimiter = "\n";
|
||||
String script = Strings.join(statement1, statement2, statement3).with(delimiter);
|
||||
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, "\n", statements);
|
||||
splitSqlScript(script, delimiter, statements);
|
||||
|
||||
assertThat(statements).hasSize(3).containsSequence(statement1, statement2,
|
||||
statement3);
|
||||
assertThat(statements).containsExactly(statement1, statement2, statement3);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -87,57 +88,50 @@ public class ScriptUtilsUnitTests {
|
|||
String statement1 = "do something";
|
||||
String statement2 = "do something else";
|
||||
|
||||
char delim = '\n';
|
||||
String script = statement1 + delim + statement2 + delim;
|
||||
String script = Strings.join(statement1, statement2).with("\n");
|
||||
|
||||
List<String> statements = new ArrayList<>();
|
||||
|
||||
splitSqlScript(script, DEFAULT_STATEMENT_SEPARATOR,
|
||||
statements);
|
||||
splitSqlScript(script, DEFAULT_STATEMENT_SEPARATOR, statements);
|
||||
|
||||
assertThat(statements).hasSize(1).contains(script.replace('\n', ' '));
|
||||
assertThat(statements).as("stripped but not split statements").containsExactly(script.replace('\n', ' '));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test // SPR-13218
|
||||
public void splitScriptWithSingleQuotesNestedInsideDoubleQuotes() {
|
||||
String statement1 = "select '1' as \"Dogbert's owner's\" from dual";
|
||||
String statement2 = "select '2' as \"Dilbert's\" from dual";
|
||||
|
||||
char delim = ';';
|
||||
String script = statement1 + delim + statement2 + delim;
|
||||
String delimiter = ";";
|
||||
String script = Strings.join(statement1, statement2).with(delimiter);
|
||||
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, ';', statements);
|
||||
splitSqlScript(script, delimiter, statements);
|
||||
|
||||
assertThat(statements).hasSize(2).containsSequence(statement1, statement2);
|
||||
assertThat(statements).containsExactly(statement1, statement2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readAndSplitScriptWithMultipleNewlinesAsSeparator() {
|
||||
@Test // SPR-11560
|
||||
public void readAndSplitScriptWithMultipleNewlinesAsSeparator() throws Exception {
|
||||
String script = readScript("db-test-data-multi-newline.sql");
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, "\n\n", statements);
|
||||
|
||||
String statement1 = "insert into users (last_name) values ('Walter')";
|
||||
String statement2 = "insert into users (last_name) values ('Jesse')";
|
||||
String statement1 = "insert into T_TEST (NAME) values ('Keith')";
|
||||
String statement2 = "insert into T_TEST (NAME) values ('Dave')";
|
||||
|
||||
assertThat(statements.size()).as("wrong number of statements").isEqualTo(2);
|
||||
assertThat(statements.get(0)).as("statement 1 not split correctly").isEqualTo(
|
||||
statement1);
|
||||
assertThat(statements.get(1)).as("statement 2 not split correctly").isEqualTo(
|
||||
statement2);
|
||||
assertThat(statements).containsExactly(statement1, statement2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readAndSplitScriptContainingComments() {
|
||||
public void readAndSplitScriptContainingComments() throws Exception {
|
||||
String script = readScript("test-data-with-comments.sql");
|
||||
splitScriptContainingComments(script, DEFAULT_COMMENT_PREFIXES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readAndSplitScriptContainingCommentsWithWindowsLineEnding() {
|
||||
String script = readScript("test-data-with-comments.sql").replaceAll("\n",
|
||||
"\r\n");
|
||||
public void readAndSplitScriptContainingCommentsWithWindowsLineEnding() throws Exception {
|
||||
String script = readScript("test-data-with-comments.sql").replaceAll("\n", "\r\n");
|
||||
splitScriptContainingComments(script, DEFAULT_COMMENT_PREFIXES);
|
||||
}
|
||||
|
||||
|
@ -155,48 +149,47 @@ public class ScriptUtilsUnitTests {
|
|||
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)";
|
||||
// Statement 4 addresses the error described in SPR-9982.
|
||||
String statement4 = "INSERT INTO persons( person_id , name) VALUES( 1 , 'Name' )";
|
||||
|
||||
assertThat(statements).hasSize(4).containsSequence(statement1, statement2,
|
||||
statement3, statement4);
|
||||
assertThat(statements).containsExactly(statement1, statement2, statement3, statement4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readAndSplitScriptContainingCommentsWithLeadingTabs() {
|
||||
@Test // SPR-10330
|
||||
public void readAndSplitScriptContainingCommentsWithLeadingTabs() throws Exception {
|
||||
String script = readScript("test-data-with-comments-and-leading-tabs.sql");
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, ';', statements);
|
||||
|
||||
String statement1 = "insert into customer (id, name) values (1, 'Walter White')";
|
||||
String statement1 = "insert into customer (id, name) values (1, 'Sam Brannen')";
|
||||
String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2013-06-08', 1)";
|
||||
String statement3 = "insert into orders(id, order_date, customer_id) values (2, '2013-06-08', 1)";
|
||||
|
||||
assertThat(statements).hasSize(3).containsSequence(statement1, statement2,
|
||||
statement3);
|
||||
assertThat(statements).containsExactly(statement1, statement2, statement3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readAndSplitScriptContainingMultiLineComments() {
|
||||
@Test // SPR-9531
|
||||
public void readAndSplitScriptContainingMultiLineComments() throws Exception {
|
||||
String script = readScript("test-data-with-multi-line-comments.sql");
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, ';', statements);
|
||||
|
||||
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Walter', 'White')";
|
||||
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Jesse' , 'Pinkman' )";
|
||||
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller')";
|
||||
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Sam' , 'Brannen' )";
|
||||
|
||||
assertThat(statements).hasSize(2).containsSequence(statement1, statement2);
|
||||
assertThat(statements).containsExactly(statement1, statement2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readAndSplitScriptContainingMultiLineNestedComments() {
|
||||
public void readAndSplitScriptContainingMultiLineNestedComments() throws Exception {
|
||||
String script = readScript("test-data-with-multi-line-nested-comments.sql");
|
||||
List<String> statements = new ArrayList<>();
|
||||
splitSqlScript(script, ';', statements);
|
||||
|
||||
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Walter', 'White')";
|
||||
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Jesse' , 'Pinkman' )";
|
||||
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller')";
|
||||
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Sam' , 'Brannen' )";
|
||||
|
||||
assertThat(statements).hasSize(2).containsSequence(statement1, statement2);
|
||||
assertThat(statements).containsExactly(statement1, statement2);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
|
@ -220,9 +213,8 @@ public class ScriptUtilsUnitTests {
|
|||
assertThat(containsSqlScriptDelimiters(script, delimiter)).isEqualTo(expected);
|
||||
}
|
||||
|
||||
private String readScript(String path) {
|
||||
EncodedResource resource = new EncodedResource(
|
||||
new ClassPathResource(path, getClass()));
|
||||
private String readScript(String path) throws Exception {
|
||||
EncodedResource resource = new EncodedResource(new ClassPathResource(path, getClass()));
|
||||
return ScriptUtils.readScript(resource, DefaultDataBufferFactory.sharedInstance).block();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
insert into users (last_name)
|
||||
values ('Walter')
|
||||
insert into T_TEST (NAME)
|
||||
values ('Keith')
|
||||
|
||||
insert into users (last_name)
|
||||
values ('Jesse')
|
||||
insert into T_TEST (NAME)
|
||||
values ('Dave')
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
-- x, y, z...
|
||||
|
||||
insert into customer (id, name)
|
||||
values (1, 'Walter White');
|
||||
values (1, 'Sam Brannen');
|
||||
-- This is also a comment with a leading tab.
|
||||
insert into orders(id, order_date, customer_id) values (1, '2013-06-08', 1);
|
||||
-- This is also a comment with a leading tab, a space, and a tab.
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
/* This is a multi line comment
|
||||
* The next comment line has no text
|
||||
* The next comment line has no text
|
||||
|
||||
* The next comment line starts with a space.
|
||||
* x, y, z...
|
||||
* x, y, z...
|
||||
*/
|
||||
|
||||
INSERT INTO users(first_name, last_name) VALUES('Walter', 'White');
|
||||
INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller');
|
||||
-- This is also a comment.
|
||||
/*
|
||||
/*
|
||||
* Let's add another comment
|
||||
* that covers multiple lines
|
||||
*/INSERT INTO
|
||||
users(first_name, last_name)
|
||||
VALUES( 'Jesse' -- first_name
|
||||
, 'Pinkman' -- last_name
|
||||
);--
|
||||
VALUES( 'Sam' -- first_name
|
||||
, 'Brannen' -- last_name
|
||||
);--
|
|
@ -5,7 +5,7 @@
|
|||
* x, y, z...
|
||||
*/
|
||||
|
||||
INSERT INTO users(first_name, last_name) VALUES('Walter', 'White');
|
||||
INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller');
|
||||
-- This is also a comment.
|
||||
/*-------------------------------------------
|
||||
-- A fancy multi-line comments that puts
|
||||
|
@ -18,6 +18,6 @@ already inside a multi-line comment run.
|
|||
-------------------------------------------*/
|
||||
INSERT INTO
|
||||
users(first_name, last_name) -- This is a single line comment containing the block-end-comment sequence here */ but it's still a single-line comment
|
||||
VALUES( 'Jesse' -- first_name
|
||||
, 'Pinkman' -- last_name
|
||||
);--
|
||||
VALUES( 'Sam' -- first_name
|
||||
, 'Brannen' -- last_name
|
||||
);--
|
Loading…
Reference in New Issue