diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DatabasePopulator.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DatabasePopulator.java index b6b9f2d2056..d72ee0cab3b 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DatabasePopulator.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DatabasePopulator.java @@ -40,7 +40,7 @@ public interface DatabasePopulator { * {@link ResourceDatabasePopulator} and {@link DatabasePopulatorUtils} wrap * all {@code SQLExceptions} in {@code ScriptExceptions}. * @param connection the JDBC connection to use to populate the db; already - * configured and ready to use + * configured and ready to use; never {@code null} * @throws SQLException if an unrecoverable data access exception occurs * during database population * @throws ScriptException in all other error cases 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 8451c985f9a..8bd50ebc8e3 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,7 +16,6 @@ package org.springframework.jdbc.datasource.init; - import java.sql.Connection; import java.util.ArrayList; import java.util.Arrays; @@ -26,6 +25,7 @@ import javax.sql.DataSource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.EncodedResource; +import org.springframework.util.Assert; /** * Populates, initializes, or cleans up a database using SQL scripts defined in @@ -85,7 +85,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator { */ public ResourceDatabasePopulator(Resource... scripts) { this(); - this.scripts = Arrays.asList(scripts); + setScripts(scripts); } /** @@ -96,7 +96,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator { * statement can be ignored * @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 initialize or populate the database + * @param scripts the scripts to execute to initialize or populate the database; + * never {@code null} * @since 4.0.3 */ public ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops, String sqlScriptEncoding, @@ -109,26 +110,29 @@ public class ResourceDatabasePopulator implements DatabasePopulator { /** * Add a script to execute to initialize or populate the database. - * @param script the path to an SQL script + * @param script the path to an SQL script; never {@code null} */ public void addScript(Resource script) { - this.scripts.add(script); + Assert.notNull(script, "script must not be null"); + getScripts().add(script); } /** * Add multiple scripts to execute to initialize or populate the database. - * @param scripts the scripts to execute + * @param scripts the scripts to execute; never {@code null} */ public void addScripts(Resource... scripts) { - this.scripts.addAll(Arrays.asList(scripts)); + assertContentsOfScriptArray(scripts); + getScripts().addAll(Arrays.asList(scripts)); } /** * Set the scripts to execute to initialize or populate the database, * replacing any previously added scripts. - * @param scripts the scripts to execute + * @param scripts the scripts to execute; never {@code null} */ public void setScripts(Resource... scripts) { + assertContentsOfScriptArray(scripts); this.scripts = Arrays.asList(scripts); } @@ -165,11 +169,13 @@ public class ResourceDatabasePopulator implements DatabasePopulator { * Set the start delimiter that identifies block comments within the SQL * scripts. *
Defaults to {@code "/*"}. - * @param blockCommentStartDelimiter the start delimiter for block comments + * @param blockCommentStartDelimiter the start delimiter for block comments; + * never {@code null} or empty * @since 4.0.3 * @see #setBlockCommentEndDelimiter */ public void setBlockCommentStartDelimiter(String blockCommentStartDelimiter) { + Assert.hasText(blockCommentStartDelimiter, "blockCommentStartDelimiter must not be null or empty"); this.blockCommentStartDelimiter = blockCommentStartDelimiter; } @@ -177,11 +183,13 @@ public class ResourceDatabasePopulator implements DatabasePopulator { * Set the end delimiter that identifies block comments within the SQL * scripts. *
Defaults to "*/".
- * @param blockCommentEndDelimiter the end delimiter for block comments
+ * @param blockCommentEndDelimiter the end delimiter for block comments;
+ * never {@code null} or empty
* @since 4.0.3
* @see #setBlockCommentStartDelimiter
*/
public void setBlockCommentEndDelimiter(String blockCommentEndDelimiter) {
+ Assert.hasText(blockCommentEndDelimiter, "blockCommentEndDelimiter must not be null or empty");
this.blockCommentEndDelimiter = blockCommentEndDelimiter;
}
@@ -212,7 +220,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
*/
@Override
public void populate(Connection connection) throws ScriptException {
- for (Resource script : this.scripts) {
+ Assert.notNull(connection, "Connection must not be null");
+ for (Resource script : getScripts()) {
ScriptUtils.executeSqlScript(connection, encodeScript(script), this.continueOnError,
this.ignoreFailedDrops, this.commentPrefix, this.separator, this.blockCommentStartDelimiter,
this.blockCommentEndDelimiter);
@@ -223,23 +232,34 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
* Execute this {@code ResourceDatabasePopulator} against the given
* {@link DataSource}.
*
Delegates to {@link DatabasePopulatorUtils#execute}.
- * @param dataSource the {@code DataSource} to execute against
+ * @param dataSource the {@code DataSource} to execute against; never {@code null}
* @throws ScriptException if an error occurs
* @since 4.1
* @see #populate(Connection)
*/
public void execute(DataSource dataSource) throws ScriptException {
+ Assert.notNull(dataSource, "DataSource must not be null");
DatabasePopulatorUtils.execute(this, dataSource);
}
+ final List