From c788be30fe8677daa21ab994069d5167f11d2bde Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 15 Jul 2019 19:03:40 +0200 Subject: [PATCH] Polish contribution See gh-23289 --- .../embedded/EmbeddedDatabaseBuilder.java | 13 ++++++++ .../init/ResourceDatabasePopulator.java | 20 ++++++------ .../jdbc/datasource/init/ScriptUtils.java | 31 ++++++++++--------- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java index 76bf7990cff..9e021a6e34a 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java @@ -201,12 +201,25 @@ public class EmbeddedDatabaseBuilder { * @param commentPrefix the prefix for single-line comments * @return {@code this}, to facilitate method chaining * @since 4.0.3 + * @see #setCommentPrefixes(String...) */ public EmbeddedDatabaseBuilder setCommentPrefix(String commentPrefix) { this.databasePopulator.setCommentPrefix(commentPrefix); return this; } + /** + * Specify the prefixes that identify single-line comments within all SQL scripts. + *

Defaults to {@code ["--"]}. + * @param commentPrefixes the prefixes for single-line comments + * @return {@code this}, to facilitate method chaining + * @since 5.2 + */ + public EmbeddedDatabaseBuilder setCommentPrefixes(String... commentPrefixes) { + this.databasePopulator.setCommentPrefixes(commentPrefixes); + return this; + } + /** * Specify the start delimiter for block comments in all SQL scripts. *

Defaults to {@code "/*"}. 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 50f19a1c411..8332c182f8b 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 @@ -47,6 +47,7 @@ import org.springframework.util.StringUtils; * @author Oliver Gierke * @author Sam Brannen * @author Chris Baldwin + * @author Phillip Webb * @since 3.0 * @see DatabasePopulatorUtils * @see ScriptUtils @@ -116,7 +117,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator { * @param script the path to an SQL script (never {@code null}) */ public void addScript(Resource script) { - Assert.notNull(script, "Script must not be null"); + Assert.notNull(script, "'script' must not be null"); this.scripts.add(script); } @@ -141,8 +142,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator { } private void assertContentsOfScriptArray(Resource... scripts) { - Assert.notNull(scripts, "Scripts array must not be null"); - Assert.noNullElements(scripts, "Scripts array must not contain null elements"); + Assert.notNull(scripts, "'scripts' must not be null"); + Assert.noNullElements(scripts, "'scripts' must not contain null elements"); } /** @@ -174,18 +175,19 @@ public class ResourceDatabasePopulator implements DatabasePopulator { * @see #setCommentPrefixes(String...) */ public void setCommentPrefix(String commentPrefix) { - Assert.hasText(commentPrefix, "CommentPrefix must not be null or empty"); + Assert.hasText(commentPrefix, "'commentPrefix' must not be null or empty"); this.commentPrefixes = new String[] { commentPrefix }; } /** * Set the prefixes that identify single-line comments within the SQL scripts. - *

Defaults to {@code "--"}. + *

Defaults to {@code ["--"]}. * @param commentPrefixes the prefixes for single-line comments * @since 5.2 */ public void setCommentPrefixes(String... commentPrefixes) { - Assert.notNull(commentPrefixes, "CommentPrefixes must not be null"); + Assert.notEmpty(commentPrefixes, "'commentPrefixes' must not be null or empty"); + Assert.noNullElements(commentPrefixes, "'commentPrefixes' must not contain null elements"); this.commentPrefixes = commentPrefixes; } @@ -199,7 +201,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator { * @see #setBlockCommentEndDelimiter */ public void setBlockCommentStartDelimiter(String blockCommentStartDelimiter) { - Assert.hasText(blockCommentStartDelimiter, "BlockCommentStartDelimiter must not be null or empty"); + Assert.hasText(blockCommentStartDelimiter, "'blockCommentStartDelimiter' must not be null or empty"); this.blockCommentStartDelimiter = blockCommentStartDelimiter; } @@ -213,7 +215,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator { * @see #setBlockCommentStartDelimiter */ public void setBlockCommentEndDelimiter(String blockCommentEndDelimiter) { - Assert.hasText(blockCommentEndDelimiter, "BlockCommentEndDelimiter must not be null or empty"); + Assert.hasText(blockCommentEndDelimiter, "'blockCommentEndDelimiter' must not be null or empty"); this.blockCommentEndDelimiter = blockCommentEndDelimiter; } @@ -245,7 +247,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator { */ @Override public void populate(Connection connection) throws ScriptException { - Assert.notNull(connection, "Connection must not be null"); + Assert.notNull(connection, "'connection' must not be null"); for (Resource script : this.scripts) { EncodedResource encodedScript = new EncodedResource(script, this.sqlScriptEncoding); ScriptUtils.executeSqlScript(connection, encodedScript, this.continueOnError, this.ignoreFailedDrops, diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java index 478ee0a3e0e..bb24ad95d0a 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java @@ -48,6 +48,7 @@ import org.springframework.util.StringUtils; * @author Oliver Gierke * @author Chris Baldwin * @author Nicolas Debeissat + * @author Phillip Webb * @since 4.0.3 */ public abstract class ScriptUtils { @@ -185,9 +186,9 @@ public abstract class ScriptUtils { * Split an SQL script into separate statements delimited by the provided * separator string. Each individual statement will be added to the provided * {@code List}. - *

Within the script, the provided {@code commentPrefix} will be honored: - * any text beginning with the comment prefix and extending to the end of the - * line will be omitted from the output. Similarly, the provided + *

Within the script, the provided {@code commentPrefixes} will be honored: + * any text beginning with one of the comment prefixes and extending to the + * end of the line will be omitted from the output. Similarly, the provided * {@code blockCommentStartDelimiter} and {@code blockCommentEndDelimiter} * delimiters will be honored: any text enclosed in a block comment will be * omitted from the output. In addition, multiple adjacent whitespace characters @@ -212,7 +213,7 @@ public abstract class ScriptUtils { Assert.hasText(script, "'script' must not be null or empty"); Assert.notNull(separator, "'separator' must not be null"); - Assert.notNull(commentPrefixes, "'commentPrefixes' must not be null"); + Assert.notEmpty(commentPrefixes, "'commentPrefixes' must not be null or empty"); for (int i = 0; i < commentPrefixes.length; i++) { Assert.hasText(commentPrefixes[i], "'commentPrefixes' must not contain null or empty elements"); } @@ -307,14 +308,14 @@ public abstract class ScriptUtils { } /** - * Read a script from the provided resource, using the supplied comment prefix + * Read a script from the provided resource, using the supplied comment prefixes * and statement separator, and build a {@code String} containing the lines. - *

Lines beginning with the comment prefix are excluded from the - * results; however, line comments anywhere else — for example, within - * a statement — will be included in the results. + *

Lines beginning with one of the comment prefixes are excluded + * from the results; however, line comments anywhere else — for example, + * within a statement — will be included in the results. * @param resource the {@code EncodedResource} containing the script * to be processed - * @param commentPrefixes the prefix that identifies comments in the SQL script + * @param commentPrefixes the prefixes that identify comments in the SQL script * (typically "--") * @param separator the statement separator in the SQL script (typically ";") * @param blockCommentEndDelimiter the end block comment delimiter @@ -358,11 +359,11 @@ public abstract class ScriptUtils { /** * Read a script from the provided {@code LineNumberReader}, using the supplied - * comment prefix and statement separator, and build a {@code String} containing + * comment prefixes and statement separator, and build a {@code String} containing * the lines. - *

Lines beginning with the comment prefix are excluded from the - * results; however, line comments anywhere else — for example, within - * a statement — will be included in the results. + *

Lines beginning with one of the comment prefixes are excluded + * from the results; however, line comments anywhere else — for example, + * within a statement — will be included in the results. * @param lineNumberReader the {@code LineNumberReader} containing the script * to be processed * @param lineCommentPrefixes the prefixes that identify comments in the SQL script @@ -407,9 +408,9 @@ public abstract class ScriptUtils { } } - private static boolean startsWithAny(String script, String[] prefixes, int toffset) { + private static boolean startsWithAny(String script, String[] prefixes, int offset) { for (String prefix : prefixes) { - if (script.startsWith(prefix, toffset)) { + if (script.startsWith(prefix, offset)) { return true; } }