RESOLVED - issue SPR-6345: ResourceDatabasePopulator does not handle comments properly when ignoring failures

http://jira.springframework.org/browse/SPR-6345
This commit is contained in:
David Syer 2009-11-14 09:02:03 +00:00
parent aac9107f6b
commit 534f8a4705
9 changed files with 97 additions and 8 deletions

View File

@ -106,7 +106,7 @@ final class DerbyEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigure
* Returns an {@link OutputStream} that ignores all data given to it.
* Used by {@link #getInstance()} to prevent writing to Derby.log file.
*/
static OutputStream getNoopOutputStream() {
public static OutputStream getNoopOutputStream() {
return new OutputStream() {
public void write(int b) throws IOException {
// ignore the output

View File

@ -32,17 +32,17 @@ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
*
* @author Keith Donald
* @author Juergen Hoeller
* @author Dave Syer
* @since 3.0
*/
public class EmbeddedDatabaseBuilder {
private final EmbeddedDatabaseFactory databaseFactory;
private final ResourceDatabasePopulator databasePopulator;
private final ResourceLoader resourceLoader;
/**
* Create a new embedded database builder.
*/
@ -51,7 +51,7 @@ public class EmbeddedDatabaseBuilder {
}
/**
* Create a new embedded database builder withfor the given ResourceLoader.
* Create a new embedded database builder with the given ResourceLoader.
* @param resourceLoader the ResourceLoader to delegate to
*/
public EmbeddedDatabaseBuilder(ResourceLoader resourceLoader) {
@ -61,7 +61,6 @@ public class EmbeddedDatabaseBuilder {
this.resourceLoader = resourceLoader;
}
/**
* Sets the name of the embedded database
* Defaults to 'testdb' if not called.
@ -73,6 +72,30 @@ public class EmbeddedDatabaseBuilder {
return this;
}
/**
* Sets a flag to say that the database populator should continue on
* errors in the scripts provided (if any).
*
* @param continueOnError the flag value
* @return this, for fluent call chaining
*/
public EmbeddedDatabaseBuilder continueOnError(boolean continueOnError) {
this.databasePopulator.setContinueOnError(continueOnError);
return this;
}
/**
* Sets a flag to say that the database populator should continue on
* errors in DROP statements in the scripts provided (if any).
*
* @param ignoreFailedDrops the flag value
* @return this, for fluent call chaining
*/
public EmbeddedDatabaseBuilder ignoreFailedDrops(boolean ignoreFailedDrops) {
this.databasePopulator.setIgnoreFailedDrops(ignoreFailedDrops);
return this;
}
/**
* Sets the type of embedded database.
* Defaults to HSQL if not called.
@ -83,7 +106,7 @@ public class EmbeddedDatabaseBuilder {
this.databaseFactory.setDatabaseType(databaseType);
return this;
}
/**
* Adds a SQL script to execute to populate the database.
* @param sqlResource the sql resource location
@ -112,5 +135,5 @@ public class EmbeddedDatabaseBuilder {
public EmbeddedDatabase build() {
return this.databaseFactory.getDatabase();
}
}

View File

@ -54,6 +54,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
private boolean ignoreFailedDrops = false;
private static String COMMENT_PREFIX = "--";
/**
* Add a script to execute to populate the database.
* @param script the path to a SQL script
@ -185,7 +187,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
String currentStatement = lnr.readLine();
StringBuilder scriptBuilder = new StringBuilder();
while (currentStatement != null) {
if (StringUtils.hasText(currentStatement)) {
if (StringUtils.hasText(currentStatement) && !currentStatement.startsWith(COMMENT_PREFIX)) {
if (scriptBuilder.length() > 0) {
scriptBuilder.append('\n');
}

View File

@ -26,6 +26,20 @@ public class EmbeddedDatabaseBuilderTests {
assertDatabaseCreatedAndShutdown(db);
}
@Test
public void testBuildWithComments() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()));
EmbeddedDatabase db = builder.addScript("db-schema-comments.sql").addScript("db-test-data.sql").build();
assertDatabaseCreatedAndShutdown(db);
}
@Test
public void testBuildWithCommentsAndFailedDrop() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()));
EmbeddedDatabase db = builder.ignoreFailedDrops(true).addScript("db-schema-failed-drop-comments.sql").addScript("db-test-data.sql").build();
assertDatabaseCreatedAndShutdown(db);
}
@Test
public void testBuildH2() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()));

View File

@ -0,0 +1,34 @@
package org.springframework.jdbc.datasource.init;
import static org.junit.Assert.assertEquals;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.core.io.ClassRelativeResourceLoader;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
public class DatabasePopulatorTests {
@Test
public void testBuildWithCommentsAndFailedDrop() throws Exception {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder.build();
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
ClassRelativeResourceLoader resourceLoader = new ClassRelativeResourceLoader(getClass());
databasePopulator.addScript(resourceLoader.getResource("db-schema-failed-drop-comments.sql"));
databasePopulator.addScript(resourceLoader.getResource("db-test-data.sql"));
databasePopulator.setIgnoreFailedDrops(true);
databasePopulator.populate(db.getConnection());
assertDatabaseCreated(db);
db.shutdown();
}
private void assertDatabaseCreated(DataSource db) {
JdbcTemplate template = new JdbcTemplate(db);
assertEquals("Keith", template.queryForObject("select NAME from T_TEST", String.class));
}
}

View File

@ -0,0 +1,5 @@
-- Failed DROP can be ignored if necessary
drop table T_TEST if exists;
-- Create the test table
create table T_TEST (NAME varchar(50) not null);

View File

@ -0,0 +1,5 @@
-- Failed DROP can be ignored if necessary
drop table T_TEST;
-- Create the test table
create table T_TEST (NAME varchar(50) not null);

View File

@ -0,0 +1,5 @@
-- Failed DROP can be ignored if necessary
drop table T_TEST;
-- Create the test table
create table T_TEST (NAME varchar(50) not null);

View File

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