Skip to content

Commit e4ce8f5

Browse files
ppfeilerrnorth
authored andcommitted
Fix ScriptUtils splitting with Windows EOF (#1467, #1465)
1 parent 03f8caa commit e4ce8f5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ else if (script.startsWith(blockCommentStartDelimiter, i)) {
206206
blockCommentEndDelimiter), resource);
207207
}
208208
}
209-
else if (c == ' ' || c == '\n' || c == '\t') {
209+
else if (c == ' ' || c == '\n' || c == '\t' || c == '\r') {
210210
// avoid multiple adjacent whitespace characters
211211
if (sb.length() > 0 && sb.charAt(sb.length() - 1) != ' ') {
212212
c = ' ';

modules/database-commons/src/test/java/org/testcontainers/ext/ScriptUtilsTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,21 @@ public void testSplit() throws IOException {
2828
assertEquals("SELECT * from `bar`", statements.get(4));
2929
assertEquals("INSERT INTO bar (foo) VALUES ('hello world')", statements.get(6));
3030
}
31+
32+
/*
33+
* Test ScriptUtils script splitting with some ugly/hard-to-split cases and linux line endings
34+
*/
35+
@Test
36+
public void testSplitWithWidnwosLineEnding() throws IOException {
37+
final String script = Resources.toString(Resources.getResource("splittable.sql"), Charsets.UTF_8);
38+
final String scriptWithWindowsLineEndings = script.replaceAll("\n", "\r\n");
39+
final List<String> statements = new ArrayList<>();
40+
ScriptUtils.splitSqlScript("resourcename", scriptWithWindowsLineEndings, ";", "--", "/*", "*/", statements);
41+
42+
assertEquals(7, statements.size());
43+
assertEquals("SELECT \"a /* string literal containing comment characters like -- here\"", statements.get(2));
44+
assertEquals("SELECT \"a 'quoting' \\\"scenario ` involving BEGIN keyword\\\" here\"", statements.get(3));
45+
assertEquals("SELECT * from `bar`", statements.get(4));
46+
assertEquals("INSERT INTO bar (foo) VALUES ('hello world')", statements.get(6));
47+
}
3148
}

0 commit comments

Comments
 (0)