提交 534f8a47 编写于 作者: D David Syer

RESOLVED - issue SPR-6345: ResourceDatabasePopulator does not handle comments...

RESOLVED - issue SPR-6345: ResourceDatabasePopulator does not handle comments properly when ignoring failures 
http://jira.springframework.org/browse/SPR-6345
上级 aac9107f
...@@ -106,7 +106,7 @@ final class DerbyEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigure ...@@ -106,7 +106,7 @@ final class DerbyEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigure
* Returns an {@link OutputStream} that ignores all data given to it. * Returns an {@link OutputStream} that ignores all data given to it.
* Used by {@link #getInstance()} to prevent writing to Derby.log file. * Used by {@link #getInstance()} to prevent writing to Derby.log file.
*/ */
static OutputStream getNoopOutputStream() { public static OutputStream getNoopOutputStream() {
return new OutputStream() { return new OutputStream() {
public void write(int b) throws IOException { public void write(int b) throws IOException {
// ignore the output // ignore the output
......
...@@ -32,17 +32,17 @@ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; ...@@ -32,17 +32,17 @@ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
* *
* @author Keith Donald * @author Keith Donald
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Dave Syer
* @since 3.0 * @since 3.0
*/ */
public class EmbeddedDatabaseBuilder { public class EmbeddedDatabaseBuilder {
private final EmbeddedDatabaseFactory databaseFactory; private final EmbeddedDatabaseFactory databaseFactory;
private final ResourceDatabasePopulator databasePopulator; private final ResourceDatabasePopulator databasePopulator;
private final ResourceLoader resourceLoader; private final ResourceLoader resourceLoader;
/** /**
* Create a new embedded database builder. * Create a new embedded database builder.
*/ */
...@@ -51,7 +51,7 @@ public class EmbeddedDatabaseBuilder { ...@@ -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 * @param resourceLoader the ResourceLoader to delegate to
*/ */
public EmbeddedDatabaseBuilder(ResourceLoader resourceLoader) { public EmbeddedDatabaseBuilder(ResourceLoader resourceLoader) {
...@@ -61,7 +61,6 @@ public class EmbeddedDatabaseBuilder { ...@@ -61,7 +61,6 @@ public class EmbeddedDatabaseBuilder {
this.resourceLoader = resourceLoader; this.resourceLoader = resourceLoader;
} }
/** /**
* Sets the name of the embedded database * Sets the name of the embedded database
* Defaults to 'testdb' if not called. * Defaults to 'testdb' if not called.
...@@ -73,6 +72,30 @@ public class EmbeddedDatabaseBuilder { ...@@ -73,6 +72,30 @@ public class EmbeddedDatabaseBuilder {
return this; 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. * Sets the type of embedded database.
* Defaults to HSQL if not called. * Defaults to HSQL if not called.
...@@ -83,7 +106,7 @@ public class EmbeddedDatabaseBuilder { ...@@ -83,7 +106,7 @@ public class EmbeddedDatabaseBuilder {
this.databaseFactory.setDatabaseType(databaseType); this.databaseFactory.setDatabaseType(databaseType);
return this; return this;
} }
/** /**
* Adds a SQL script to execute to populate the database. * Adds a SQL script to execute to populate the database.
* @param sqlResource the sql resource location * @param sqlResource the sql resource location
...@@ -112,5 +135,5 @@ public class EmbeddedDatabaseBuilder { ...@@ -112,5 +135,5 @@ public class EmbeddedDatabaseBuilder {
public EmbeddedDatabase build() { public EmbeddedDatabase build() {
return this.databaseFactory.getDatabase(); return this.databaseFactory.getDatabase();
} }
} }
...@@ -54,6 +54,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator { ...@@ -54,6 +54,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
private boolean ignoreFailedDrops = false; private boolean ignoreFailedDrops = false;
private static String COMMENT_PREFIX = "--";
/** /**
* Add a script to execute to populate the database. * Add a script to execute to populate the database.
* @param script the path to a SQL script * @param script the path to a SQL script
...@@ -185,7 +187,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator { ...@@ -185,7 +187,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
String currentStatement = lnr.readLine(); String currentStatement = lnr.readLine();
StringBuilder scriptBuilder = new StringBuilder(); StringBuilder scriptBuilder = new StringBuilder();
while (currentStatement != null) { while (currentStatement != null) {
if (StringUtils.hasText(currentStatement)) { if (StringUtils.hasText(currentStatement) && !currentStatement.startsWith(COMMENT_PREFIX)) {
if (scriptBuilder.length() > 0) { if (scriptBuilder.length() > 0) {
scriptBuilder.append('\n'); scriptBuilder.append('\n');
} }
......
...@@ -26,6 +26,20 @@ public class EmbeddedDatabaseBuilderTests { ...@@ -26,6 +26,20 @@ public class EmbeddedDatabaseBuilderTests {
assertDatabaseCreatedAndShutdown(db); 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 @Test
public void testBuildH2() { public void testBuildH2() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass())); EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()));
......
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
-- 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
-- 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
-- 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
insert into T_TEST (NAME) values ('Keith');
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册