Skip to content

Commit

Permalink
Test for multi-character delimiter
Browse files Browse the repository at this point in the history
Issue: SPR-14808
(cherry picked from commit 71d8338)
  • Loading branch information
jhoeller committed Oct 31, 2016
1 parent fe19cfd commit 5578a2e
Showing 1 changed file with 19 additions and 29 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
Expand Down Expand Up @@ -50,7 +50,7 @@ public void splitSqlScriptDelimitedWithSemicolon() {
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;
List<String> statements = new ArrayList<String>();
List<String> statements = new ArrayList<>();
splitSqlScript(script, delim, statements);
assertEquals("wrong number of statements", 3, statements.size());
assertEquals("statement 1 not split correctly", cleanedStatement1, statements.get(0));
Expand All @@ -65,7 +65,7 @@ public void splitSqlScriptDelimitedWithNewLine() {
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>();
List<String> statements = new ArrayList<>();
splitSqlScript(script, delim, statements);
assertEquals("wrong number of statements", 3, statements.size());
assertEquals("statement 1 not split correctly", statement1, statements.get(0));
Expand All @@ -79,36 +79,30 @@ public void splitSqlScriptDelimitedWithNewLineButDefaultDelimiterSpecified() {
String statement2 = "do something else";
char delim = '\n';
String script = statement1 + delim + statement2 + delim;
List<String> statements = new ArrayList<String>();
List<String> statements = new ArrayList<>();
splitSqlScript(script, DEFAULT_STATEMENT_SEPARATOR, statements);
assertEquals("wrong number of statements", 1, statements.size());
assertEquals("script should have been 'stripped' but not actually 'split'", script.replace('\n', ' '),
statements.get(0));
}

/**
* See <a href="https://jira.spring.io/browse/SPR-13218">SPR-13218</a>
*/
@Test
@Test // SPR-13218
public void splitScriptWithSingleQuotesNestedInsideDoubleQuotes() throws Exception {
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;
List<String> statements = new ArrayList<String>();
List<String> statements = new ArrayList<>();
splitSqlScript(script, ';', statements);
assertEquals("wrong number of statements", 2, statements.size());
assertEquals("statement 1 not split correctly", statement1, statements.get(0));
assertEquals("statement 2 not split correctly", statement2, statements.get(1));
}

/**
* See <a href="https://jira.spring.io/browse/SPR-11560">SPR-11560</a>
*/
@Test
@Test // SPR-11560
public void readAndSplitScriptWithMultipleNewlinesAsSeparator() throws Exception {
String script = readScript("db-test-data-multi-newline.sql");
List<String> statements = new ArrayList<String>();
List<String> statements = new ArrayList<>();
splitSqlScript(script, "\n\n", statements);

String statement1 = "insert into T_TEST (NAME) values ('Keith')";
Expand All @@ -122,7 +116,7 @@ public void readAndSplitScriptWithMultipleNewlinesAsSeparator() throws Exception
@Test
public void readAndSplitScriptContainingComments() throws Exception {
String script = readScript("test-data-with-comments.sql");
List<String> statements = new ArrayList<String>();
List<String> statements = new ArrayList<>();
splitSqlScript(script, ';', statements);

String statement1 = "insert into customer (id, name) values (1, 'Rod; Johnson'), (2, 'Adrian Collier')";
Expand All @@ -138,13 +132,10 @@ public void readAndSplitScriptContainingComments() throws Exception {
assertEquals("statement 4 not split correctly", statement4, statements.get(3));
}

/**
* See <a href="https://jira.spring.io/browse/SPR-10330">SPR-10330</a>
*/
@Test
@Test // SPR-10330
public void readAndSplitScriptContainingCommentsWithLeadingTabs() throws Exception {
String script = readScript("test-data-with-comments-and-leading-tabs.sql");
List<String> statements = new ArrayList<String>();
List<String> statements = new ArrayList<>();
splitSqlScript(script, ';', statements);

String statement1 = "insert into customer (id, name) values (1, 'Sam Brannen')";
Expand All @@ -157,13 +148,10 @@ public void readAndSplitScriptContainingCommentsWithLeadingTabs() throws Excepti
assertEquals("statement 3 not split correctly", statement3, statements.get(2));
}

/**
* See <a href="https://jira.spring.io/browse/SPR-9531">SPR-9531</a>
*/
@Test
@Test // SPR-9531
public void readAndSplitScriptContainingMuliLineComments() throws Exception {
String script = readScript("test-data-with-multi-line-comments.sql");
List<String> statements = new ArrayList<String>();
List<String> statements = new ArrayList<>();
splitSqlScript(script, ';', statements);

String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Juergen', 'Hoeller')";
Expand All @@ -176,10 +164,12 @@ public void readAndSplitScriptContainingMuliLineComments() throws Exception {

@Test
public void containsDelimiters() {
assertTrue("test with ';' is wrong", !containsSqlScriptDelimiters("select 1\n select ';'", ";"));
assertTrue("test with delimiter ; is wrong", containsSqlScriptDelimiters("select 1; select 2", ";"));
assertTrue("test with '\\n' is wrong", !containsSqlScriptDelimiters("select 1; select '\\n\n';", "\n"));
assertTrue("test with delimiter \\n is wrong", containsSqlScriptDelimiters("select 1\n select 2", "\n"));
assertFalse(containsSqlScriptDelimiters("select 1\n select ';'", ";"));
assertTrue(containsSqlScriptDelimiters("select 1; select 2", ";"));
assertFalse(containsSqlScriptDelimiters("select 1; select '\\n\n';", "\n"));
assertTrue(containsSqlScriptDelimiters("select 1\n select 2", "\n"));
assertFalse(containsSqlScriptDelimiters("select 1\n select 2", "\n\n"));
assertTrue(containsSqlScriptDelimiters("select 1\n\n select 2", "\n\n"));
}

private String readScript(String path) throws Exception {
Expand Down

0 comments on commit 5578a2e

Please sign in to comment.