SPR-6688: Add tests and explicit detection of \ in sql script extractor

This commit is contained in:
David Syer 2011-06-02 14:28:58 +00:00
parent e0ad6500be
commit 645631ad2e
5 changed files with 50 additions and 2 deletions

View File

@ -251,9 +251,21 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
private void splitSqlScript(String script, char delim, List<String> statements) {
StringBuilder sb = new StringBuilder();
boolean inLiteral = false;
boolean inEscape = false;
char[] content = script.toCharArray();
for (int i = 0; i < script.length(); i++) {
char c = content[i];
if (inEscape) {
inEscape = false;
sb.append(c);
continue;
}
// MySQL style escapes
if (c == '\\') {
inEscape = true;
sb.append(c);
continue;
}
if (c == '\'') {
inLiteral = !inLiteral;
}

View File

@ -23,7 +23,6 @@ import java.sql.Connection;
import javax.sql.DataSource;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.core.io.ClassRelativeResourceLoader;
import org.springframework.jdbc.core.JdbcTemplate;
@ -43,7 +42,11 @@ public class DatabasePopulatorTests {
private final JdbcTemplate jdbcTemplate = new JdbcTemplate(db);
private void assertTestDatabaseCreated() {
assertEquals("Keith", jdbcTemplate.queryForObject("select NAME from T_TEST", String.class));
assertTestDatabaseCreated("Keith");
}
private void assertTestDatabaseCreated(String name) {
assertEquals(name, jdbcTemplate.queryForObject("select NAME from T_TEST", String.class));
}
private void assertUsersDatabaseCreated(DataSource db) {
@ -71,6 +74,34 @@ public class DatabasePopulatorTests {
assertTestDatabaseCreated();
}
@Test
public void testBuildWithNormalEscapedLiteral() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
databasePopulator.addScript(resourceLoader.getResource("db-test-data-escaped-literal.sql"));
Connection connection = db.getConnection();
try {
databasePopulator.populate(connection);
} finally {
connection.close();
}
assertTestDatabaseCreated("'Keith'");
}
@Test
public void testBuildWithMySQLEscapedLiteral() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
databasePopulator.addScript(resourceLoader.getResource("db-test-data-mysql-escaped-literal.sql"));
Connection connection = db.getConnection();
try {
databasePopulator.populate(connection);
} finally {
connection.close();
}
assertTestDatabaseCreated("\\$Keith\\$");
}
@Test
public void scriptWithEolBetweenTokens() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("users-schema.sql"));

View File

@ -0,0 +1,3 @@
drop table T_TEST if exists;
create table T_TEST (NAME varchar(50) not null);

View File

@ -0,0 +1 @@
insert into T_TEST (NAME) values ('''Keith''');

View File

@ -0,0 +1 @@
insert into T_TEST (NAME) values ('\$Keith\$');