Polish ScriptUtils internals in spring-r2dbc

See gh-26947
This commit is contained in:
Sam Brannen
2021-05-17 16:12:13 +02:00
parent 101ed17b6f
commit fab7b1cebe
2 changed files with 32 additions and 49 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.r2dbc.connection.init;
import java.util.ArrayList;
import java.util.List;
import org.assertj.core.util.Strings;
@@ -60,8 +59,7 @@ public class ScriptUtilsUnitTests {
String delimiter = ";";
String script = Strings.join(rawStatement1, rawStatement2, rawStatement3).with(delimiter);
List<String> statements = new ArrayList<>();
splitSqlScript(script, delimiter, statements);
List<String> statements = splitSqlScript(script, delimiter);
assertThat(statements).containsExactly(cleanedStatement1, cleanedStatement2, cleanedStatement3);
}
@@ -75,8 +73,7 @@ public class ScriptUtilsUnitTests {
String delimiter = "\n";
String script = Strings.join(statement1, statement2, statement3).with(delimiter);
List<String> statements = new ArrayList<>();
splitSqlScript(script, delimiter, statements);
List<String> statements = splitSqlScript(script, delimiter);
assertThat(statements).containsExactly(statement1, statement2, statement3);
}
@@ -88,9 +85,7 @@ public class ScriptUtilsUnitTests {
String script = Strings.join(statement1, statement2).with("\n");
List<String> statements = new ArrayList<>();
splitSqlScript(script, DEFAULT_STATEMENT_SEPARATOR, statements);
List<String> statements = splitSqlScript(script, DEFAULT_STATEMENT_SEPARATOR);
assertThat(statements).as("stripped but not split statements").containsExactly(script.replace('\n', ' '));
}
@@ -103,8 +98,7 @@ public class ScriptUtilsUnitTests {
String delimiter = ";";
String script = Strings.join(statement1, statement2).with(delimiter);
List<String> statements = new ArrayList<>();
splitSqlScript(script, delimiter, statements);
List<String> statements = splitSqlScript(script, delimiter);
assertThat(statements).containsExactly(statement1, statement2);
}
@@ -112,8 +106,7 @@ public class ScriptUtilsUnitTests {
@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);
List<String> statements = splitSqlScript(script, "\n\n");
String statement1 = "insert into T_TEST (NAME) values ('Keith')";
String statement2 = "insert into T_TEST (NAME) values ('Dave')";
@@ -140,9 +133,8 @@ public class ScriptUtilsUnitTests {
}
private void splitScriptContainingComments(String script, String... commentPrefixes) {
List<String> statements = new ArrayList<>();
ScriptUtils.splitSqlScript(null, script, ";", commentPrefixes, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements);
List<String> statements = ScriptUtils.splitSqlScript(null, script, ";", commentPrefixes, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
DEFAULT_BLOCK_COMMENT_END_DELIMITER);
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)";
@@ -156,8 +148,7 @@ public class ScriptUtilsUnitTests {
@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);
List<String> statements = splitSqlScript(script, ";");
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)";
@@ -169,8 +160,7 @@ public class ScriptUtilsUnitTests {
@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);
List<String> statements = splitSqlScript(script, ";");
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller')";
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Sam' , 'Brannen' )";
@@ -181,8 +171,7 @@ public class ScriptUtilsUnitTests {
@Test
public void readAndSplitScriptContainingMultiLineNestedComments() throws Exception {
String script = readScript("test-data-with-multi-line-nested-comments.sql");
List<String> statements = new ArrayList<>();
splitSqlScript(script, ";", statements);
List<String> statements = splitSqlScript(script, ";");
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller')";
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Sam' , 'Brannen' )";
@@ -231,13 +220,12 @@ public class ScriptUtilsUnitTests {
private String readScript(String path) throws Exception {
EncodedResource resource = new EncodedResource(new ClassPathResource(path, getClass()));
return ScriptUtils.readScript(resource, DefaultDataBufferFactory.sharedInstance, DEFAULT_STATEMENT_SEPARATOR,
DEFAULT_COMMENT_PREFIXES, DEFAULT_BLOCK_COMMENT_END_DELIMITER).block();
return ScriptUtils.readScript(resource, DefaultDataBufferFactory.sharedInstance, DEFAULT_STATEMENT_SEPARATOR).block();
}
private static void splitSqlScript(String script, String separator, List<String> statements) throws ScriptException {
ScriptUtils.splitSqlScript(null, script, separator, DEFAULT_COMMENT_PREFIXES, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements);
private static List<String> splitSqlScript(String script, String separator) throws ScriptException {
return ScriptUtils.splitSqlScript(null, script, separator, DEFAULT_COMMENT_PREFIXES, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
DEFAULT_BLOCK_COMMENT_END_DELIMITER);
}
}