Add multi-prefix comment support for @SqlConfig

gh-23289 introduced support for multiple single-line comment prefixes
for ScriptUtils, ResourceDatabasePopulator, and EmbeddedDatabaseBuilder.

This commit adds the same support for @SqlConfig in the TestContext
Framework. Specifically, @SqlConfig has a new `commentPrefixes`
attribute for setting multiple single-line comment prefixes.

Closes gh-23331
This commit is contained in:
Sam Brannen
2019-07-23 15:23:44 +02:00
parent a3c7ae2c58
commit c3c152f806
9 changed files with 394 additions and 95 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,6 +27,6 @@ import org.springframework.test.context.jdbc.SqlConfig;
*/
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
@DirtiesContext
@SqlConfig(commentPrefix = "`", blockCommentStartDelimiter = "#$", blockCommentEndDelimiter = "$#", separator = "@@")
@SqlConfig(commentPrefixes = { "`", "%%" }, blockCommentStartDelimiter = "#$", blockCommentEndDelimiter = "$#", separator = "@@")
interface SqlConfigTestInterface {
}

View File

@@ -38,7 +38,7 @@ public class CustomScriptSyntaxSqlScriptsTests extends AbstractTransactionalJUni
@Test
@Sql("schema.sql")
@Sql(scripts = "data-add-users-with-custom-script-syntax.sql",//
config = @SqlConfig(commentPrefix = "`", blockCommentStartDelimiter = "#$", blockCommentEndDelimiter = "$#", separator = "@@"))
config = @SqlConfig(commentPrefixes = { "`", "%%" }, blockCommentStartDelimiter = "#$", blockCommentEndDelimiter = "$#", separator = "@@"))
public void methodLevelScripts() {
assertNumUsers(3);
}

View File

@@ -33,7 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
@DirtiesContext
@SqlConfig(commentPrefix = "`", blockCommentStartDelimiter = "#$", blockCommentEndDelimiter = "$#", separator = "@@")
@SqlConfig(commentPrefixes = { "`", "%%" }, blockCommentStartDelimiter = "#$", blockCommentEndDelimiter = "$#", separator = "@@")
public class GlobalCustomScriptSyntaxSqlScriptsTests extends AbstractTransactionalJUnit4SpringContextTests {
@Test

View File

@@ -21,9 +21,11 @@ import java.lang.reflect.Method;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_BLOCK_COMMENT_END_DELIMITER;
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER;
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_COMMENT_PREFIX;
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_COMMENT_PREFIXES;
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_STATEMENT_SEPARATOR;
import static org.springframework.test.context.jdbc.SqlConfig.ErrorMode.CONTINUE_ON_ERROR;
import static org.springframework.test.context.jdbc.SqlConfig.ErrorMode.FAIL_ON_ERROR;
@@ -39,17 +41,50 @@ import static org.springframework.test.context.jdbc.SqlConfig.TransactionMode.IS
*/
public class MergedSqlConfigTests {
private void assertDefaults(MergedSqlConfig cfg) {
assertThat(cfg).isNotNull();
assertThat(cfg.getDataSource()).as("dataSource").isEqualTo("");
assertThat(cfg.getTransactionManager()).as("transactionManager").isEqualTo("");
assertThat(cfg.getTransactionMode()).as("transactionMode").isEqualTo(INFERRED);
assertThat(cfg.getEncoding()).as("encoding").isEqualTo("");
assertThat(cfg.getSeparator()).as("separator").isEqualTo(DEFAULT_STATEMENT_SEPARATOR);
assertThat(cfg.getCommentPrefix()).as("commentPrefix").isEqualTo(DEFAULT_COMMENT_PREFIX);
assertThat(cfg.getBlockCommentStartDelimiter()).as("blockCommentStartDelimiter").isEqualTo(DEFAULT_BLOCK_COMMENT_START_DELIMITER);
assertThat(cfg.getBlockCommentEndDelimiter()).as("blockCommentEndDelimiter").isEqualTo(DEFAULT_BLOCK_COMMENT_END_DELIMITER);
assertThat(cfg.getErrorMode()).as("errorMode").isEqualTo(FAIL_ON_ERROR);
@Test
public void nullLocalSqlConfig() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new MergedSqlConfig(null, getClass()))
.withMessage("Local @SqlConfig must not be null");
}
@Test
public void nullTestClass() {
SqlConfig sqlConfig = GlobalConfigClass.class.getAnnotation(SqlConfig.class);
assertThatIllegalArgumentException()
.isThrownBy(() -> new MergedSqlConfig(sqlConfig, null))
.withMessage("testClass must not be null");
}
@Test
public void localConfigWithEmptyCommentPrefix() throws Exception {
Method method = getClass().getMethod("localConfigMethodWithEmptyCommentPrefix");
SqlConfig sqlConfig = method.getAnnotation(Sql.class).config();
assertThatIllegalArgumentException()
.isThrownBy(() -> new MergedSqlConfig(sqlConfig, getClass()))
.withMessage("@SqlConfig(commentPrefix) must contain text");
}
@Test
public void localConfigWithEmptyCommentPrefixes() throws Exception {
Method method = getClass().getMethod("localConfigMethodWithEmptyCommentPrefixes");
SqlConfig sqlConfig = method.getAnnotation(Sql.class).config();
assertThatIllegalArgumentException()
.isThrownBy(() -> new MergedSqlConfig(sqlConfig, getClass()))
.withMessage("@SqlConfig(commentPrefixes) must not contain empty prefixes");
}
@Test
public void localConfigWithDuplicatedCommentPrefixes() throws Exception {
Method method = getClass().getMethod("localConfigMethodWithDuplicatedCommentPrefixes");
SqlConfig sqlConfig = method.getAnnotation(Sql.class).config();
assertThatIllegalArgumentException()
.isThrownBy(() -> new MergedSqlConfig(sqlConfig, getClass()))
.withMessage("You may declare the 'commentPrefix' or 'commentPrefixes' attribute in @SqlConfig but not both");
}
@Test
@@ -60,29 +95,44 @@ public class MergedSqlConfigTests {
assertDefaults(cfg);
}
@Test
public void globalConfigWithDefaults() throws Exception {
Method method = GlobalConfigWithDefaultsClass.class.getMethod("globalConfigMethod");
SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, GlobalConfigWithDefaultsClass.class);
assertDefaults(cfg);
}
@Test
public void localConfigWithCustomValues() throws Exception {
Method method = getClass().getMethod("localConfigMethodWithCustomValues");
SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, getClass());
assertSoftly(softly -> {
softly.assertThat(cfg).isNotNull();
softly.assertThat(cfg.getDataSource()).as("dataSource").isEqualTo("ds");
softly.assertThat(cfg.getTransactionManager()).as("transactionManager").isEqualTo("txMgr");
softly.assertThat(cfg.getTransactionMode()).as("transactionMode").isEqualTo(ISOLATED);
softly.assertThat(cfg.getEncoding()).as("encoding").isEqualTo("enigma");
softly.assertThat(cfg.getSeparator()).as("separator").isEqualTo("\n");
softly.assertThat(cfg.getCommentPrefixes()).as("commentPrefixes").isEqualTo(array("`"));
softly.assertThat(cfg.getBlockCommentStartDelimiter()).as("blockCommentStartDelimiter").isEqualTo("<<");
softly.assertThat(cfg.getBlockCommentEndDelimiter()).as("blockCommentEndDelimiter").isEqualTo(">>");
softly.assertThat(cfg.getErrorMode()).as("errorMode").isEqualTo(IGNORE_FAILED_DROPS);
});
}
@Test
public void localConfigWithCustomCommentPrefixes() throws Exception {
Method method = getClass().getMethod("localConfigMethodWithCustomCommentPrefixes");
SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, getClass());
assertThat(cfg).isNotNull();
assertThat(cfg.getDataSource()).as("dataSource").isEqualTo("ds");
assertThat(cfg.getTransactionManager()).as("transactionManager").isEqualTo("txMgr");
assertThat(cfg.getTransactionMode()).as("transactionMode").isEqualTo(ISOLATED);
assertThat(cfg.getEncoding()).as("encoding").isEqualTo("enigma");
assertThat(cfg.getSeparator()).as("separator").isEqualTo("\n");
assertThat(cfg.getCommentPrefix()).as("commentPrefix").isEqualTo("`");
assertThat(cfg.getBlockCommentStartDelimiter()).as("blockCommentStartDelimiter").isEqualTo("<<");
assertThat(cfg.getBlockCommentEndDelimiter()).as("blockCommentEndDelimiter").isEqualTo(">>");
assertThat(cfg.getErrorMode()).as("errorMode").isEqualTo(IGNORE_FAILED_DROPS);
assertThat(cfg.getCommentPrefixes()).as("commentPrefixes").isEqualTo(array("`"));
}
@Test
public void localConfigWithMultipleCommentPrefixes() throws Exception {
Method method = getClass().getMethod("localConfigMethodWithMultipleCommentPrefixes");
SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, getClass());
assertThat(cfg).isNotNull();
assertThat(cfg.getCommentPrefixes()).as("commentPrefixes").isEqualTo(array("`", "--"));
}
@Test
@@ -90,6 +140,7 @@ public class MergedSqlConfigTests {
Method method = getClass().getMethod("localConfigMethodWithContinueOnError");
SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, getClass());
assertThat(cfg).isNotNull();
assertThat(cfg.getErrorMode()).as("errorMode").isEqualTo(CONTINUE_ON_ERROR);
}
@@ -99,25 +150,64 @@ public class MergedSqlConfigTests {
Method method = getClass().getMethod("localConfigMethodWithIgnoreFailedDrops");
SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, getClass());
assertThat(cfg).isNotNull();
assertThat(cfg.getErrorMode()).as("errorMode").isEqualTo(IGNORE_FAILED_DROPS);
}
@Test
public void globalConfigWithEmptyCommentPrefix() throws Exception {
SqlConfig sqlConfig = GlobalConfigWithWithEmptyCommentPrefixClass.class.getAnnotation(SqlConfig.class);
assertThatIllegalArgumentException()
.isThrownBy(() -> new MergedSqlConfig(sqlConfig, getClass()))
.withMessage("@SqlConfig(commentPrefix) must contain text");
}
@Test
public void globalConfigWithEmptyCommentPrefixes() throws Exception {
SqlConfig sqlConfig = GlobalConfigWithWithEmptyCommentPrefixesClass.class.getAnnotation(SqlConfig.class);
assertThatIllegalArgumentException()
.isThrownBy(() -> new MergedSqlConfig(sqlConfig, getClass()))
.withMessage("@SqlConfig(commentPrefixes) must not contain empty prefixes");
}
@Test
public void globalConfigWithDuplicatedCommentPrefixes() throws Exception {
SqlConfig sqlConfig = GlobalConfigWithWithDuplicatedCommentPrefixesClass.class.getAnnotation(SqlConfig.class);
assertThatIllegalArgumentException()
.isThrownBy(() -> new MergedSqlConfig(sqlConfig, getClass()))
.withMessage("You may declare the 'commentPrefix' or 'commentPrefixes' attribute in @SqlConfig but not both");
}
@Test
public void globalConfigWithDefaults() throws Exception {
Method method = GlobalConfigWithDefaultsClass.class.getMethod("globalConfigMethod");
SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, GlobalConfigWithDefaultsClass.class);
assertDefaults(cfg);
}
@Test
public void globalConfig() throws Exception {
Method method = GlobalConfigClass.class.getMethod("globalConfigMethod");
SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, GlobalConfigClass.class);
assertThat(cfg).isNotNull();
assertThat(cfg.getDataSource()).as("dataSource").isEqualTo("");
assertThat(cfg.getTransactionManager()).as("transactionManager").isEqualTo("");
assertThat(cfg.getTransactionMode()).as("transactionMode").isEqualTo(INFERRED);
assertThat(cfg.getEncoding()).as("encoding").isEqualTo("global");
assertThat(cfg.getSeparator()).as("separator").isEqualTo("\n");
assertThat(cfg.getCommentPrefix()).as("commentPrefix").isEqualTo(DEFAULT_COMMENT_PREFIX);
assertThat(cfg.getBlockCommentStartDelimiter()).as("blockCommentStartDelimiter").isEqualTo(DEFAULT_BLOCK_COMMENT_START_DELIMITER);
assertThat(cfg.getBlockCommentEndDelimiter()).as("blockCommentEndDelimiter").isEqualTo(DEFAULT_BLOCK_COMMENT_END_DELIMITER);
assertThat(cfg.getErrorMode()).as("errorMode").isEqualTo(IGNORE_FAILED_DROPS);
assertSoftly(softly -> {
softly.assertThat(cfg).isNotNull();
softly.assertThat(cfg.getDataSource()).as("dataSource").isEqualTo("");
softly.assertThat(cfg.getTransactionManager()).as("transactionManager").isEqualTo("");
softly.assertThat(cfg.getTransactionMode()).as("transactionMode").isEqualTo(INFERRED);
softly.assertThat(cfg.getEncoding()).as("encoding").isEqualTo("global");
softly.assertThat(cfg.getSeparator()).as("separator").isEqualTo("\n");
softly.assertThat(cfg.getCommentPrefixes()).as("commentPrefixes").isEqualTo(array("`", "--"));
softly.assertThat(cfg.getBlockCommentStartDelimiter()).as("blockCommentStartDelimiter").isEqualTo(DEFAULT_BLOCK_COMMENT_START_DELIMITER);
softly.assertThat(cfg.getBlockCommentEndDelimiter()).as("blockCommentEndDelimiter").isEqualTo(DEFAULT_BLOCK_COMMENT_END_DELIMITER);
softly.assertThat(cfg.getErrorMode()).as("errorMode").isEqualTo(IGNORE_FAILED_DROPS);
});
}
@Test
@@ -126,20 +216,79 @@ public class MergedSqlConfigTests {
SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, GlobalConfigClass.class);
assertThat(cfg).isNotNull();
assertThat(cfg.getDataSource()).as("dataSource").isEqualTo("");
assertThat(cfg.getTransactionManager()).as("transactionManager").isEqualTo("");
assertThat(cfg.getTransactionMode()).as("transactionMode").isEqualTo(INFERRED);
assertThat(cfg.getEncoding()).as("encoding").isEqualTo("local");
assertThat(cfg.getSeparator()).as("separator").isEqualTo("@@");
assertThat(cfg.getCommentPrefix()).as("commentPrefix").isEqualTo(DEFAULT_COMMENT_PREFIX);
assertThat(cfg.getBlockCommentStartDelimiter()).as("blockCommentStartDelimiter").isEqualTo(DEFAULT_BLOCK_COMMENT_START_DELIMITER);
assertThat(cfg.getBlockCommentEndDelimiter()).as("blockCommentEndDelimiter").isEqualTo(DEFAULT_BLOCK_COMMENT_END_DELIMITER);
assertThat(cfg.getErrorMode()).as("errorMode").isEqualTo(CONTINUE_ON_ERROR);
assertSoftly(softly -> {
softly.assertThat(cfg).isNotNull();
softly.assertThat(cfg.getDataSource()).as("dataSource").isEqualTo("");
softly.assertThat(cfg.getTransactionManager()).as("transactionManager").isEqualTo("");
softly.assertThat(cfg.getTransactionMode()).as("transactionMode").isEqualTo(INFERRED);
softly.assertThat(cfg.getEncoding()).as("encoding").isEqualTo("local");
softly.assertThat(cfg.getSeparator()).as("separator").isEqualTo("@@");
softly.assertThat(cfg.getCommentPrefixes()).as("commentPrefixes").isEqualTo(array("#"));
softly.assertThat(cfg.getBlockCommentStartDelimiter()).as("blockCommentStartDelimiter").isEqualTo(DEFAULT_BLOCK_COMMENT_START_DELIMITER);
softly.assertThat(cfg.getBlockCommentEndDelimiter()).as("blockCommentEndDelimiter").isEqualTo(DEFAULT_BLOCK_COMMENT_END_DELIMITER);
softly.assertThat(cfg.getErrorMode()).as("errorMode").isEqualTo(CONTINUE_ON_ERROR);
});
}
@Test
public void globalConfigWithCommentPrefixAndLocalOverrides() throws Exception {
Class<?> testClass = GlobalConfigWithPrefixClass.class;
Method method = testClass.getMethod("commentPrefixesOverrideCommentPrefix");
SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, testClass);
assertThat(cfg.getCommentPrefixes()).as("commentPrefixes").isEqualTo(array("#", "@"));
method = testClass.getMethod("commentPrefixOverridesCommentPrefix");
localSqlConfig = method.getAnnotation(Sql.class).config();
cfg = new MergedSqlConfig(localSqlConfig, testClass);
assertThat(cfg.getCommentPrefixes()).as("commentPrefixes").isEqualTo(array("#"));
}
@Test
public void globalConfigWithCommentPrefixesAndLocalOverrides() throws Exception {
Class<?> testClass = GlobalConfigWithPrefixesClass.class;
Method method = testClass.getMethod("commentPrefixesOverrideCommentPrefixes");
SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, testClass);
assertThat(cfg.getCommentPrefixes()).as("commentPrefixes").isEqualTo(array("#", "@"));
method = testClass.getMethod("commentPrefixOverridesCommentPrefixes");
localSqlConfig = method.getAnnotation(Sql.class).config();
cfg = new MergedSqlConfig(localSqlConfig, testClass);
assertThat(cfg.getCommentPrefixes()).as("commentPrefixes").isEqualTo(array("#"));
}
private void assertDefaults(MergedSqlConfig cfg) {
assertSoftly(softly -> {
softly.assertThat(cfg).isNotNull();
softly.assertThat(cfg.getDataSource()).as("dataSource").isEqualTo("");
softly.assertThat(cfg.getTransactionManager()).as("transactionManager").isEqualTo("");
softly.assertThat(cfg.getTransactionMode()).as("transactionMode").isEqualTo(INFERRED);
softly.assertThat(cfg.getEncoding()).as("encoding").isEqualTo("");
softly.assertThat(cfg.getSeparator()).as("separator").isEqualTo(DEFAULT_STATEMENT_SEPARATOR);
softly.assertThat(cfg.getCommentPrefixes()).as("commentPrefixes").isEqualTo(DEFAULT_COMMENT_PREFIXES);
softly.assertThat(cfg.getBlockCommentStartDelimiter()).as("blockCommentStartDelimiter").isEqualTo(DEFAULT_BLOCK_COMMENT_START_DELIMITER);
softly.assertThat(cfg.getBlockCommentEndDelimiter()).as("blockCommentEndDelimiter").isEqualTo(DEFAULT_BLOCK_COMMENT_END_DELIMITER);
softly.assertThat(cfg.getErrorMode()).as("errorMode").isEqualTo(FAIL_ON_ERROR);
});
}
private static String[] array(String... elements) {
return elements;
}
// -------------------------------------------------------------------------
@Sql(config = @SqlConfig(commentPrefix = "#", commentPrefixes = "#" ))
public static void localConfigMethodWithDuplicatedCommentPrefixes() {
}
@Sql
public static void localConfigMethodWithDefaults() {
}
@@ -148,6 +297,22 @@ public class MergedSqlConfigTests {
public static void localConfigMethodWithCustomValues() {
}
@Sql(config = @SqlConfig(commentPrefix = " " ))
public static void localConfigMethodWithEmptyCommentPrefix() {
}
@Sql(config = @SqlConfig(commentPrefixes = { "--", " " }))
public static void localConfigMethodWithEmptyCommentPrefixes() {
}
@Sql(config = @SqlConfig(commentPrefixes = "`"))
public static void localConfigMethodWithCustomCommentPrefixes() {
}
@Sql(config = @SqlConfig(commentPrefixes = { "`", "--" }))
public static void localConfigMethodWithMultipleCommentPrefixes() {
}
@Sql(config = @SqlConfig(errorMode = CONTINUE_ON_ERROR))
public static void localConfigMethodWithContinueOnError() {
}
@@ -156,25 +321,60 @@ public class MergedSqlConfigTests {
public static void localConfigMethodWithIgnoreFailedDrops() {
}
@SqlConfig(commentPrefix = " ")
public static class GlobalConfigWithWithEmptyCommentPrefixClass {
}
@SqlConfig(commentPrefixes = { "--", " " })
public static class GlobalConfigWithWithEmptyCommentPrefixesClass {
}
@SqlConfig(commentPrefix = "#", commentPrefixes = "#")
public static class GlobalConfigWithWithDuplicatedCommentPrefixesClass {
}
@SqlConfig
public static class GlobalConfigWithDefaultsClass {
@Sql("foo.sql")
@Sql
public void globalConfigMethod() {
}
}
@SqlConfig(encoding = "global", separator = "\n", errorMode = IGNORE_FAILED_DROPS)
@SqlConfig(encoding = "global", separator = "\n", commentPrefixes = { "`", "--" }, errorMode = IGNORE_FAILED_DROPS)
public static class GlobalConfigClass {
@Sql("foo.sql")
@Sql
public void globalConfigMethod() {
}
@Sql(scripts = "foo.sql", config = @SqlConfig(encoding = "local", separator = "@@", errorMode = CONTINUE_ON_ERROR))
@Sql(config = @SqlConfig(encoding = "local", separator = "@@", commentPrefix = "#", errorMode = CONTINUE_ON_ERROR))
public void globalConfigWithLocalOverridesMethod() {
}
}
@SqlConfig(commentPrefix = "`")
public static class GlobalConfigWithPrefixClass {
@Sql(config = @SqlConfig(commentPrefixes = { "#", "@" }))
public void commentPrefixesOverrideCommentPrefix() {
}
@Sql(config = @SqlConfig(commentPrefix = "#"))
public void commentPrefixOverridesCommentPrefix() {
}
}
@SqlConfig(commentPrefixes = { "`", "--" })
public static class GlobalConfigWithPrefixesClass {
@Sql(config = @SqlConfig(commentPrefixes = { "#", "@" }))
public void commentPrefixesOverrideCommentPrefixes() {
}
@Sql(config = @SqlConfig(commentPrefix = "#"))
public void commentPrefixOverridesCommentPrefixes() {
}
}
}