Polishing

This commit is contained in:
Sam Brannen 2014-03-16 17:34:01 +01:00
parent bb67cd4657
commit cae50c3a2d
5 changed files with 45 additions and 42 deletions

View File

@ -62,14 +62,25 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
/**
* Construct a new {@code ResourceDatabasePopulator} with default settings.
* @since 4.0.3
*/
public ResourceDatabasePopulator() {
/* no-op */
}
/**
* Construct a new {@code ResourceDatabasePopulator} with default settings
* for the supplied scripts.
* @param scripts the scripts to execute to populate the database
* @since 4.0.3
*/
public ResourceDatabasePopulator(Resource... scripts) {
this();
this.scripts = Arrays.asList(scripts);
}
/**
* Construct a new {@code ResourceDatabasePopulator} with the supplied values.
*
* @param continueOnError flag to indicate that all failures in SQL should be
* logged but not cause a failure
* @param ignoreFailedDrops flag to indicate that a failed SQL {@code DROP}
@ -77,13 +88,14 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
* @param sqlScriptEncoding the encoding for the supplied SQL scripts, if
* different from the platform encoding; may be {@code null}
* @param scripts the scripts to execute to populate the database
* @since 4.0.3
*/
public ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops, String sqlScriptEncoding,
Resource... scripts) {
this(scripts);
this.continueOnError = continueOnError;
this.ignoreFailedDrops = ignoreFailedDrops;
this.sqlScriptEncoding = sqlScriptEncoding;
this.scripts = Arrays.asList(scripts);
}
/**

View File

@ -1,8 +1,6 @@
/**
*
* Provides extensible support for initializing databases through scripts.
*
*/
package org.springframework.jdbc.datasource.init;

View File

@ -26,6 +26,9 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* Abstract base class for integration tests involving database initialization.
*
@ -36,8 +39,9 @@ public abstract class AbstractDatabaseInitializationTests {
private final ClassRelativeResourceLoader resourceLoader = new ClassRelativeResourceLoader(getClass());
protected EmbeddedDatabase db;
protected JdbcTemplate jdbcTemplate;
EmbeddedDatabase db;
JdbcTemplate jdbcTemplate;
@Before
@ -55,10 +59,26 @@ public abstract class AbstractDatabaseInitializationTests {
db.shutdown();
}
protected abstract EmbeddedDatabaseType getEmbeddedDatabaseType();
abstract EmbeddedDatabaseType getEmbeddedDatabaseType();
protected Resource resource(String path) {
Resource resource(String path) {
return resourceLoader.getResource(path);
}
Resource defaultSchema() {
return resource("db-schema.sql");
}
Resource usersSchema() {
return resource("users-schema.sql");
}
void assertUsersDatabaseCreated(String... lastNames) {
for (String lastName : lastNames) {
assertThat("Did not find user with last name [" + lastName + "].",
jdbcTemplate.queryForObject("select count(0) from users where last_name = ?", Integer.class, lastName),
equalTo(1));
}
}
}

View File

@ -20,7 +20,6 @@ import java.sql.Connection;
import java.sql.SQLException;
import org.junit.Test;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@ -29,7 +28,8 @@ import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* Abstract base class for integration tests for {@link ResourceDatabasePopulator}.
* Abstract base class for integration tests for {@link ResourceDatabasePopulator}
* and {@link DatabasePopulatorUtils}.
*
* @author Dave Syer
* @author Sam Brannen
@ -141,7 +141,7 @@ public abstract class AbstractDatabasePopulatorTests extends AbstractDatabaseIni
@Test
public void constructorWithMultipleScriptResources() throws Exception {
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(false, false, null, usersSchema(),
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(usersSchema(),
resource("users-data-with-comments.sql"));
DatabasePopulatorUtils.execute(populator, db);
assertUsersDatabaseCreated("Brannen", "Hoeller");
@ -188,20 +188,4 @@ public abstract class AbstractDatabasePopulatorTests extends AbstractDatabaseIni
assertEquals(name, jdbcTemplate.queryForObject("select NAME from T_TEST", String.class));
}
private void assertUsersDatabaseCreated(String... lastNames) {
for (String lastName : lastNames) {
assertThat("Did not find user with last name [" + lastName + "].",
jdbcTemplate.queryForObject("select count(0) from users where last_name = ?", Integer.class, lastName),
equalTo(1));
}
}
private Resource defaultSchema() {
return resource("db-schema.sql");
}
private Resource usersSchema() {
return resource("users-schema.sql");
}
}

View File

@ -19,11 +19,9 @@ package org.springframework.jdbc.datasource.init;
import java.sql.SQLException;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.jdbc.datasource.init.ScriptUtils.*;
/**
* Integration tests for {@link ScriptUtils}.
@ -39,20 +37,11 @@ public class ScriptUtilsIntegrationTests extends AbstractDatabaseInitializationT
}
@Test
public void executeSqlScript() throws SQLException {
ScriptUtils.executeSqlScript(db.getConnection(), resource("users-schema.sql"));
ScriptUtils.executeSqlScript(db.getConnection(), resource("test-data-with-multi-line-comments.sql"));
public void executeSqlScriptContainingMuliLineComments() throws SQLException {
executeSqlScript(db.getConnection(), usersSchema());
executeSqlScript(db.getConnection(), resource("test-data-with-multi-line-comments.sql"));
assertUsersDatabaseCreated("Hoeller", "Brannen");
}
private void assertUsersDatabaseCreated(String... lastNames) {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(db);
for (String lastName : lastNames) {
assertThat("Did not find user with last name [" + lastName + "].",
jdbcTemplate.queryForObject("select count(0) from users where last_name = ?", Integer.class, lastName),
equalTo(1));
}
}
}