This commit is contained in:
Keith Donald 2009-05-06 09:14:25 +00:00
parent 0320445316
commit 4bfcd16130
2 changed files with 16 additions and 4 deletions

View File

@ -21,6 +21,7 @@ import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -52,6 +53,14 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
scripts.add(script); scripts.add(script);
} }
/**
* Set the scripts to execute to populate the database.
* @param scripts the scripts to execute
*/
public void setScripts(Resource[] scripts) {
this.scripts = Arrays.asList(scripts);
}
/** /**
* Specify the encoding for SQL scripts, if different from the platform encoding. * Specify the encoding for SQL scripts, if different from the platform encoding.
* Note setting this property has no effect on added scripts that are already {@link EncodedResource encoded resources}. * Note setting this property has no effect on added scripts that are already {@link EncodedResource encoded resources}.
@ -60,7 +69,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
public void setSqlScriptEncoding(String sqlScriptEncoding) { public void setSqlScriptEncoding(String sqlScriptEncoding) {
this.sqlScriptEncoding = sqlScriptEncoding; this.sqlScriptEncoding = sqlScriptEncoding;
} }
public void populate(Connection connection) throws SQLException { public void populate(Connection connection) throws SQLException {
for (Resource script : scripts) { for (Resource script : scripts) {
executeSqlScript(connection, applyEncodingIfNecessary(script), false); executeSqlScript(connection, applyEncodingIfNecessary(script), false);

View File

@ -6,16 +6,19 @@ import javax.sql.DataSource;
import org.junit.Test; import org.junit.Test;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
public class EmbeddedDatabaseFactoryBeanTests { public class EmbeddedDatabaseFactoryBeanTests {
@Test @Test
public void testFactoryBeanLifecycle() throws Exception { public void testFactoryBeanLifecycle() throws Exception {
EmbeddedDatabaseFactoryBean bean = new EmbeddedDatabaseFactoryBean(); EmbeddedDatabaseFactoryBean bean = new EmbeddedDatabaseFactoryBean();
ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.addScript(new ClassPathResource("db-schema.sql", getClass())); populator.setScripts(new Resource[] {
populator.addScript(new ClassPathResource("db-test-data.sql", getClass())); new ClassPathResource("db-schema.sql", getClass()),
new ClassPathResource("db-test-data.sql", getClass())
});
bean.setDatabasePopulator(populator); bean.setDatabasePopulator(populator);
bean.afterPropertiesSet(); bean.afterPropertiesSet();
DataSource ds = bean.getObject(); DataSource ds = bean.getObject();