"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ScriptUtilsUnitTests.java" between
spring-framework-5.3.7.tar.gz and spring-framework-5.3.8.tar.gz

About: Spring Framework is an application framework for the Java platform and .NET Framework. Community edition.

ScriptUtilsUnitTests.java  (spring-framework-5.3.7):ScriptUtilsUnitTests.java  (spring-framework-5.3.8)
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2021 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.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* https://www.apache.org/licenses/LICENSE-2.0 * https://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.jdbc.datasource.init; package org.springframework.jdbc.datasource.init;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.assertj.core.util.Strings;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.EncodedResource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_BLOCK _COMMENT_END_DELIMITER; import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_BLOCK _COMMENT_END_DELIMITER;
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_BLOCK _COMMENT_START_DELIMITER; import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_BLOCK _COMMENT_START_DELIMITER;
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_COMME NT_PREFIXES; import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_COMME NT_PREFIXES;
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_STATE MENT_SEPARATOR; import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_STATE MENT_SEPARATOR;
import static org.springframework.jdbc.datasource.init.ScriptUtils.containsSqlSc riptDelimiters; import static org.springframework.jdbc.datasource.init.ScriptUtils.containsSqlSc riptDelimiters;
skipping to change at line 49 skipping to change at line 52
* @author Sam Brannen * @author Sam Brannen
* @author Phillip Webb * @author Phillip Webb
* @author Chris Baldwin * @author Chris Baldwin
* @author Nicolas Debeissat * @author Nicolas Debeissat
* @since 4.0.3 * @since 4.0.3
* @see ScriptUtilsIntegrationTests * @see ScriptUtilsIntegrationTests
*/ */
public class ScriptUtilsUnitTests { public class ScriptUtilsUnitTests {
@Test @Test
@SuppressWarnings("deprecation")
public void splitSqlScriptDelimitedWithSemicolon() { public void splitSqlScriptDelimitedWithSemicolon() {
String rawStatement1 = "insert into customer (id, name)\nvalues ( 1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')"; String rawStatement1 = "insert into customer (id, name)\nvalues ( 1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')";
String cleanedStatement1 = "insert into customer (id, name) value s (1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')"; String cleanedStatement1 = "insert into customer (id, name) value s (1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')";
String rawStatement2 = "insert into orders(id, order_date, custom er_id)\nvalues (1, '2008-01-02', 2)"; String rawStatement2 = "insert into orders(id, order_date, custom er_id)\nvalues (1, '2008-01-02', 2)";
String cleanedStatement2 = "insert into orders(id, order_date, cu stomer_id) values (1, '2008-01-02', 2)"; String cleanedStatement2 = "insert into orders(id, order_date, cu stomer_id) values (1, '2008-01-02', 2)";
String rawStatement3 = "insert into orders(id, order_date, custom er_id) values (1, '2008-01-02', 2)"; String rawStatement3 = "insert into orders(id, order_date, custom er_id) values (1, '2008-01-02', 2)";
String cleanedStatement3 = "insert into orders(id, order_date, cu stomer_id) values (1, '2008-01-02', 2)"; String cleanedStatement3 = "insert into orders(id, order_date, cu stomer_id) values (1, '2008-01-02', 2)";
char delim = ';';
String script = rawStatement1 + delim + rawStatement2 + delim + r String delimiter = ";";
awStatement3 + delim; String script = Strings.join(rawStatement1, rawStatement2, rawSta
tement3).with(delimiter);
List<String> statements = new ArrayList<>(); List<String> statements = new ArrayList<>();
splitSqlScript(script, delim, statements); splitSqlScript(script, delimiter, statements);
assertThat(statements).containsExactly(cleanedStatement1, cleaned Statement2, cleanedStatement3); assertThat(statements).containsExactly(cleanedStatement1, cleaned Statement2, cleanedStatement3);
} }
@Test @Test
@SuppressWarnings("deprecation")
public void splitSqlScriptDelimitedWithNewLine() { public void splitSqlScriptDelimitedWithNewLine() {
String statement1 = "insert into customer (id, name) values (1, ' Rod ; Johnson'), (2, 'Adrian \n Collier')"; 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 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 statement3 = "insert into orders(id, order_date, customer_ id) values (1, '2008-01-02', 2)";
char delim = '\n';
String script = statement1 + delim + statement2 + delim + stateme String delimiter = "\n";
nt3 + delim; String script = Strings.join(statement1, statement2, statement3).
with(delimiter);
List<String> statements = new ArrayList<>(); List<String> statements = new ArrayList<>();
splitSqlScript(script, delim, statements); splitSqlScript(script, delimiter, statements);
assertThat(statements).containsExactly(statement1, statement2, st atement3); assertThat(statements).containsExactly(statement1, statement2, st atement3);
} }
@Test @Test
@SuppressWarnings("deprecation")
public void splitSqlScriptDelimitedWithNewLineButDefaultDelimiterSpecifie d() { public void splitSqlScriptDelimitedWithNewLineButDefaultDelimiterSpecifie d() {
String statement1 = "do something"; String statement1 = "do something";
String statement2 = "do something else"; 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<>(); List<String> statements = new ArrayList<>();
splitSqlScript(script, DEFAULT_STATEMENT_SEPARATOR, statements); splitSqlScript(script, DEFAULT_STATEMENT_SEPARATOR, statements);
assertThat(statements).as("stripped but not split statements").co ntainsExactly(script.replace('\n', ' ')); assertThat(statements).as("stripped but not split statements").co ntainsExactly(script.replace('\n', ' '));
} }
@Test // SPR-13218 @Test // SPR-13218
public void splitScriptWithSingleQuotesNestedInsideDoubleQuotes() throws @SuppressWarnings("deprecation")
Exception { public void splitScriptWithSingleQuotesNestedInsideDoubleQuotes() {
String statement1 = "select '1' as \"Dogbert's owner's\" from dua l"; String statement1 = "select '1' as \"Dogbert's owner's\" from dua l";
String statement2 = "select '2' as \"Dilbert'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(delimit
er);
List<String> statements = new ArrayList<>(); List<String> statements = new ArrayList<>();
splitSqlScript(script, ';', statements); splitSqlScript(script, delimiter, statements);
assertThat(statements).containsExactly(statement1, statement2); assertThat(statements).containsExactly(statement1, statement2);
} }
@Test // SPR-11560 @Test // SPR-11560
@SuppressWarnings("deprecation")
public void readAndSplitScriptWithMultipleNewlinesAsSeparator() throws Ex ception { public void readAndSplitScriptWithMultipleNewlinesAsSeparator() throws Ex ception {
String script = readScript("db-test-data-multi-newline.sql"); String script = readScript("db-test-data-multi-newline.sql");
List<String> statements = new ArrayList<>(); List<String> statements = new ArrayList<>();
splitSqlScript(script, "\n\n", statements); splitSqlScript(script, "\n\n", statements);
String statement1 = "insert into T_TEST (NAME) values ('Keith')"; String statement1 = "insert into T_TEST (NAME) values ('Keith')";
String statement2 = "insert into T_TEST (NAME) values ('Dave')"; String statement2 = "insert into T_TEST (NAME) values ('Dave')";
assertThat(statements).containsExactly(statement1, statement2); assertThat(statements).containsExactly(statement1, statement2);
} }
@Test @Test
public void readAndSplitScriptContainingComments() throws Exception { public void readAndSplitScriptContainingComments() throws Exception {
String script = readScript("test-data-with-comments.sql"); String script = readScript("test-data-with-comments.sql");
splitScriptContainingComments(script, DEFAULT_COMMENT_PREFIXES); splitScriptContainingComments(script, DEFAULT_COMMENT_PREFIXES);
} }
@Test @Test
skipping to change at line 125 skipping to change at line 147
String script = readScript("test-data-with-comments.sql").replace All("\n", "\r\n"); String script = readScript("test-data-with-comments.sql").replace All("\n", "\r\n");
splitScriptContainingComments(script, DEFAULT_COMMENT_PREFIXES); splitScriptContainingComments(script, DEFAULT_COMMENT_PREFIXES);
} }
@Test @Test
public void readAndSplitScriptContainingCommentsWithMultiplePrefixes() th rows Exception { public void readAndSplitScriptContainingCommentsWithMultiplePrefixes() th rows Exception {
String script = readScript("test-data-with-multi-prefix-comments. sql"); String script = readScript("test-data-with-multi-prefix-comments. sql");
splitScriptContainingComments(script, "--", "#", "^"); splitScriptContainingComments(script, "--", "#", "^");
} }
private void splitScriptContainingComments(String script, String... comme @SuppressWarnings("deprecation")
ntPrefixes) throws Exception { private void splitScriptContainingComments(String script, String... comme
ntPrefixes) {
List<String> statements = new ArrayList<>(); List<String> statements = new ArrayList<>();
splitSqlScript(null, script, ";", commentPrefixes, DEFAULT_BLOCK_ COMMENT_START_DELIMITER, splitSqlScript(null, script, ";", commentPrefixes, DEFAULT_BLOCK_ COMMENT_START_DELIMITER,
DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements); DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements);
String statement1 = "insert into customer (id, name) values (1, ' Rod; Johnson'), (2, 'Adrian Collier')"; 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 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 statement3 = "insert into orders(id, order_date, customer_ id) values (1, '2008-01-02', 2)";
// Statement 4 addresses the error described in SPR-9982. // Statement 4 addresses the error described in SPR-9982.
String statement4 = "INSERT INTO persons( person_id , name) VALUE S( 1 , 'Name' )"; String statement4 = "INSERT INTO persons( person_id , name) VALUE S( 1 , 'Name' )";
assertThat(statements).containsExactly(statement1, statement2, st atement3, statement4); assertThat(statements).containsExactly(statement1, statement2, st atement3, statement4);
} }
@Test // SPR-10330 @Test // SPR-10330
@SuppressWarnings("deprecation")
public void readAndSplitScriptContainingCommentsWithLeadingTabs() throws Exception { public void readAndSplitScriptContainingCommentsWithLeadingTabs() throws Exception {
String script = readScript("test-data-with-comments-and-leading-t abs.sql"); String script = readScript("test-data-with-comments-and-leading-t abs.sql");
List<String> statements = new ArrayList<>(); List<String> statements = new ArrayList<>();
splitSqlScript(script, ';', statements); splitSqlScript(script, ';', statements);
String statement1 = "insert into customer (id, name) values (1, ' Sam Brannen')"; 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 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)"; String statement3 = "insert into orders(id, order_date, customer_ id) values (2, '2013-06-08', 1)";
assertThat(statements).containsExactly(statement1, statement2, st atement3); assertThat(statements).containsExactly(statement1, statement2, st atement3);
} }
@Test // SPR-9531 @Test // SPR-9531
@SuppressWarnings("deprecation")
public void readAndSplitScriptContainingMultiLineComments() throws Except ion { public void readAndSplitScriptContainingMultiLineComments() throws Except ion {
String script = readScript("test-data-with-multi-line-comments.sq l"); String script = readScript("test-data-with-multi-line-comments.sq l");
List<String> statements = new ArrayList<>(); List<String> statements = new ArrayList<>();
splitSqlScript(script, ';', statements); splitSqlScript(script, ';', statements);
String statement1 = "INSERT INTO users(first_name, last_name) VAL UES('Juergen', 'Hoeller')"; String statement1 = "INSERT INTO users(first_name, last_name) VAL UES('Juergen', 'Hoeller')";
String statement2 = "INSERT INTO users(first_name, last_name) VAL UES( 'Sam' , 'Brannen' )"; String statement2 = "INSERT INTO users(first_name, last_name) VAL UES( 'Sam' , 'Brannen' )";
assertThat(statements).containsExactly(statement1, statement2); assertThat(statements).containsExactly(statement1, statement2);
} }
@Test @Test
@SuppressWarnings("deprecation")
public void readAndSplitScriptContainingMultiLineNestedComments() throws Exception { public void readAndSplitScriptContainingMultiLineNestedComments() throws Exception {
String script = readScript("test-data-with-multi-line-nested-comm ents.sql"); String script = readScript("test-data-with-multi-line-nested-comm ents.sql");
List<String> statements = new ArrayList<>(); List<String> statements = new ArrayList<>();
splitSqlScript(script, ';', statements); splitSqlScript(script, ';', statements);
String statement1 = "INSERT INTO users(first_name, last_name) VAL UES('Juergen', 'Hoeller')"; String statement1 = "INSERT INTO users(first_name, last_name) VAL UES('Juergen', 'Hoeller')";
String statement2 = "INSERT INTO users(first_name, last_name) VAL UES( 'Sam' , 'Brannen' )"; String statement2 = "INSERT INTO users(first_name, last_name) VAL UES( 'Sam' , 'Brannen' )";
assertThat(statements).containsExactly(statement1, statement2); assertThat(statements).containsExactly(statement1, statement2);
} }
@Test @ParameterizedTest
public void containsDelimiters() { @CsvSource(delimiter = '#', value = {
assertThat(containsSqlScriptDelimiters("select 1\n select ';'", " // semicolon
;")).isFalse(); "'select 1\n select '';'''
assertThat(containsSqlScriptDelimiters("select 1; select 2", ";") # ; # false",
).isTrue(); "'select 1\n select \";\"'
assertThat(containsSqlScriptDelimiters("select 1; select '\\n\n'; # ; # false",
", "\n")).isFalse(); "'select 1; select 2'
assertThat(containsSqlScriptDelimiters("select 1\n select 2", "\n # ; # true",
")).isTrue(); // newline
assertThat(containsSqlScriptDelimiters("select 1\n select 2", "\n "'select 1; select ''\n'''
\n")).isFalse(); # '\n' # false",
assertThat(containsSqlScriptDelimiters("select 1\n\n select 2", " "'select 1; select \"\n\"'
\n\n")).isTrue(); # '\n' # false",
// MySQL style escapes '\\' "'select 1\n select 2'
assertThat(containsSqlScriptDelimiters("insert into users(first_n # '\n' # true",
ame, last_name)\nvalues('a\\\\', 'b;')", ";")).isFalse(); // double newline
assertThat(containsSqlScriptDelimiters("insert into users(first_n "'select 1\n select 2'
ame, last_name)\nvalues('Charles', 'd\\'Artagnan'); select 1;", ";")).isTrue(); # '\n\n' # false",
"'select 1\n\n select 2'
# '\n\n' # true",
// semicolon with MySQL style escapes '\\'
"'insert into users(first, last)\nvalues(''a\\\\'', ''b;'')'
# ; # false",
"'insert into users(first, last)\nvalues(''Charles'', ''d\\''Arta
gnan''); select 1' # ; # true",
// semicolon inside comments
"'-- a;b;c\ninsert into colors(color_num) values(42);'
# ; # true",
"'/* a;b;c */\ninsert into colors(color_num) values(42);'
# ; # true",
"'-- a;b;c\ninsert into colors(color_num) values(42)'
# ; # false",
"'/* a;b;c */\ninsert into colors(color_num) values(42)'
# ; # false",
// single quotes inside comments
"'-- What\\''s your favorite color?\ninsert into colors(color_num
) values(42);' # ; # true",
"'-- What''s your favorite color?\ninsert into colors(color_num)
values(42);' # ; # true",
"'/* What\\''s your favorite color? */\ninsert into colors(color_
num) values(42);' # ; # true",
"'/* What''s your favorite color? */\ninsert into colors(color_nu
m) values(42);' # ; # true",
// double quotes inside comments
"'-- double \" quotes\ninsert into colors(color_num) values(42);'
# ; # true",
"'-- double \\\" quotes\ninsert into colors(color_num) values(42)
;' # ; # true",
"'/* double \" quotes */\ninsert into colors(color_num) values(42
);' # ; # true",
"'/* double \\\" quotes */\ninsert into colors(color_num) values(
42);' # ; # true"
})
@SuppressWarnings("deprecation")
public void containsStatementSeparator(String script, String delimiter, b
oolean expected) {
// Indirectly tests ScriptUtils.containsStatementSeparator(Encode
dResource, String, String, String[], String, String).
assertThat(containsSqlScriptDelimiters(script, delimiter)).isEqua
lTo(expected);
} }
private String readScript(String path) throws Exception { private String readScript(String path) throws Exception {
EncodedResource resource = new EncodedResource(new ClassPathResou rce(path, getClass())); EncodedResource resource = new EncodedResource(new ClassPathResou rce(path, getClass()));
return ScriptUtils.readScript(resource); return ScriptUtils.readScript(resource, DEFAULT_STATEMENT_SEPARAT
OR, DEFAULT_COMMENT_PREFIXES,
DEFAULT_BLOCK_COMMENT_END_DELIMITER);
} }
} }
 End of changes. 33 change blocks. 
38 lines changed or deleted 116 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)