polish
This commit is contained in:
parent
0320445316
commit
4bfcd16130
|
|
@ -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);
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue