diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/DerbyEmbeddedDatabaseConfigurer.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/DerbyEmbeddedDatabaseConfigurer.java index 05ab67332de..be2270aa6de 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/DerbyEmbeddedDatabaseConfigurer.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/DerbyEmbeddedDatabaseConfigurer.java @@ -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 diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java index 3c51069ef99..2da28c039dc 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java @@ -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(); } - + } diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java index c015e9bf8fd..157794a02eb 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java @@ -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'); } diff --git a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilderTests.java b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilderTests.java index 51ab7882649..f0481095939 100644 --- a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilderTests.java +++ b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilderTests.java @@ -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())); diff --git a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java new file mode 100644 index 00000000000..8cb2bd5babb --- /dev/null +++ b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java @@ -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)); + } + +} \ No newline at end of file diff --git a/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/embedded/db-schema-comments.sql b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/embedded/db-schema-comments.sql new file mode 100644 index 00000000000..0b1c359fd9f --- /dev/null +++ b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/embedded/db-schema-comments.sql @@ -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); \ No newline at end of file diff --git a/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/embedded/db-schema-failed-drop-comments.sql b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/embedded/db-schema-failed-drop-comments.sql new file mode 100644 index 00000000000..e274ec0df89 --- /dev/null +++ b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/embedded/db-schema-failed-drop-comments.sql @@ -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); \ No newline at end of file diff --git a/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-schema-failed-drop-comments.sql b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-schema-failed-drop-comments.sql new file mode 100644 index 00000000000..e274ec0df89 --- /dev/null +++ b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-schema-failed-drop-comments.sql @@ -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); \ No newline at end of file diff --git a/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-test-data.sql b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-test-data.sql new file mode 100644 index 00000000000..51de08aac17 --- /dev/null +++ b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-test-data.sql @@ -0,0 +1 @@ +insert into T_TEST (NAME) values ('Keith'); \ No newline at end of file