Browse Source

Introduce execute(DataSource) in ResrcDbPopulator

To simplify common use cases, this commit introduces a new
execute(DataSource) method in ResourceDatabasePopulator that complements
the existing populate(Connection) method.

Issue: SPR-11629
pull/503/head
Sam Brannen 11 years ago
parent
commit
5d049e0de8
  1. 35
      spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java
  2. 7
      spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java
  3. 7
      spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java
  4. 9
      spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java

35
spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java

@ -16,22 +16,28 @@ @@ -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.
*
* <p>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.
* <ul>
* <li>Call {@link #addScript} to add a single SQL script location.
* <li>Call {@link #addScripts} to add multiple SQL script locations.
* <li>Consult the setter methods in this class for further configuration options.
* <li>Call {@link #populate} or {@link #execute} to initialize or clean up the
* database using the configured scripts.
* </ul>
*
* @author Keith Donald
* @author Dave Syer
@ -199,6 +205,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator { @@ -199,6 +205,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
/**
* {@inheritDoc}
* @see #execute(DataSource)
*/
@Override
public void populate(Connection connection) throws ScriptException {
@ -210,8 +217,20 @@ public class ResourceDatabasePopulator implements DatabasePopulator { @@ -210,8 +217,20 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
}
/**
* {@link EncodedResource} is not a sub-type of {@link Resource}. Thus we
* always need to wrap each script resource in an encoded resource.
* Execute this {@code DatabasePopulator} against the given {@link DataSource}.
* <p>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);

7
spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java

@ -23,8 +23,6 @@ import org.springframework.context.ApplicationContext; @@ -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 @@ -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());
}
}

7
spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java

@ -23,8 +23,6 @@ import org.springframework.context.ApplicationContext; @@ -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 @@ -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());
}
}

9
spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java

@ -29,8 +29,6 @@ import org.springframework.core.io.support.EncodedResource; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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());
}
/**

Loading…
Cancel
Save