diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java index 700f9e2a3e4..d2c9d4791cb 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java @@ -16,22 +16,28 @@ package org.springframework.jdbc.datasource.init; + import java.sql.Connection; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import javax.sql.DataSource; + import org.springframework.core.io.Resource; import org.springframework.core.io.support.EncodedResource; /** - * Populates or initializes a database from SQL scripts defined in external - * resources. + * Populates, initializes, or cleans up a database using SQL scripts defined in + * external resources. * - *
Call {@link #addScript(Resource)} to add a single SQL script location. - * Call {@link #addScripts(Resource...)} to add multiple SQL script locations. - * Call {@link #setSqlScriptEncoding(String)} to set the encoding for all added - * scripts. + *
Delegates to {@link DatabasePopulatorUtils#execute}. + * @param dataSource the {@code DataSource} to execute against + * @throws ScriptException if an error occurs + * @since 4.1 + * @see #populate(Connection) + */ + public void execute(DataSource dataSource) throws ScriptException { + DatabasePopulatorUtils.execute(this, dataSource); + } + + /** + * {@link EncodedResource} is not a sub-type of {@link Resource}. Thus we always need + * to wrap each script resource in an encoded resource. */ private EncodedResource encodeScript(Resource script) { return new EncodedResource(script, this.sqlScriptEncoding); diff --git a/spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java b/spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java index 7ab768effd3..da5bbb02796 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java @@ -23,8 +23,6 @@ import org.springframework.context.ApplicationContext; import org.springframework.core.io.Resource; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.init.DatabasePopulator; -import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; @@ -180,14 +178,11 @@ public abstract class AbstractTransactionalJUnit4SpringContextTests extends Abst * exception in the event of an error * @throws DataAccessException if there is an error executing a statement * @see ResourceDatabasePopulator - * @see DatabasePopulatorUtils * @see #setSqlScriptEncoding */ protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException { Resource resource = this.applicationContext.getResource(sqlResourcePath); - DatabasePopulator databasePopulator = new ResourceDatabasePopulator(continueOnError, false, - this.sqlScriptEncoding, resource); - DatabasePopulatorUtils.execute(databasePopulator, jdbcTemplate.getDataSource()); + new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(jdbcTemplate.getDataSource()); } } diff --git a/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java b/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java index a768c29dbfd..4228e06aada 100644 --- a/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java +++ b/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java @@ -23,8 +23,6 @@ import org.springframework.context.ApplicationContext; import org.springframework.core.io.Resource; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.init.DatabasePopulator; -import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.transaction.TransactionalTestExecutionListener; @@ -171,14 +169,11 @@ public abstract class AbstractTransactionalTestNGSpringContextTests extends Abst * exception in the event of an error * @throws DataAccessException if there is an error executing a statement * @see ResourceDatabasePopulator - * @see DatabasePopulatorUtils * @see #setSqlScriptEncoding */ protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException { Resource resource = this.applicationContext.getResource(sqlResourcePath); - DatabasePopulator databasePopulator = new ResourceDatabasePopulator(continueOnError, false, - this.sqlScriptEncoding, resource); - DatabasePopulatorUtils.execute(databasePopulator, jdbcTemplate.getDataSource()); + new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(jdbcTemplate.getDataSource()); } } diff --git a/spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java b/spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java index 2e491e4d4a6..7abc58d1386 100644 --- a/spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java +++ b/spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java @@ -29,8 +29,6 @@ import org.springframework.core.io.support.EncodedResource; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.SqlParameterValue; -import org.springframework.jdbc.datasource.init.DatabasePopulator; -import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.jdbc.datasource.init.ScriptUtils; import org.springframework.util.StringUtils; @@ -164,7 +162,6 @@ public class JdbcTestUtils { * @throws DataAccessException if there is an error executing a statement * and {@code continueOnError} is {@code false} * @see ResourceDatabasePopulator - * @see DatabasePopulatorUtils * @see #executeSqlScript(JdbcTemplate, Resource, boolean) * @deprecated as of Spring 4.0.3, in favor of using * {@link org.springframework.jdbc.datasource.init.ScriptUtils#executeSqlScript} @@ -192,7 +189,6 @@ public class JdbcTestUtils { * @throws DataAccessException if there is an error executing a statement * and {@code continueOnError} is {@code false} * @see ResourceDatabasePopulator - * @see DatabasePopulatorUtils * @see #executeSqlScript(JdbcTemplate, EncodedResource, boolean) * @deprecated as of Spring 4.0.3, in favor of using * {@link org.springframework.jdbc.datasource.init.ScriptUtils#executeSqlScript} @@ -217,7 +213,6 @@ public class JdbcTestUtils { * @throws DataAccessException if there is an error executing a statement * and {@code continueOnError} is {@code false} * @see ResourceDatabasePopulator - * @see DatabasePopulatorUtils * @deprecated as of Spring 4.0.3, in favor of using * {@link org.springframework.jdbc.datasource.init.ScriptUtils#executeSqlScript} * or {@link org.springframework.jdbc.datasource.init.ResourceDatabasePopulator}. @@ -225,9 +220,7 @@ public class JdbcTestUtils { @Deprecated public static void executeSqlScript(JdbcTemplate jdbcTemplate, EncodedResource resource, boolean continueOnError) throws DataAccessException { - DatabasePopulator databasePopulator = new ResourceDatabasePopulator(continueOnError, false, - resource.getEncoding(), resource.getResource()); - DatabasePopulatorUtils.execute(databasePopulator, jdbcTemplate.getDataSource()); + new ResourceDatabasePopulator(continueOnError, false, resource.getEncoding(), resource.getResource()).execute(jdbcTemplate.getDataSource()); } /**