From 4e39597c28461e79aa6c156595c5aa5a80faa95c Mon Sep 17 00:00:00 2001 From: asympro Date: Fri, 25 May 2018 16:33:41 +0300 Subject: [PATCH 1/8] SPR-16021 implemented --- .../jdbc/SqlScriptsTestExecutionListener.java | 22 +++++++++---------- .../jdbc/DataSourceOnlySqlScriptsTests.java | 4 ++-- .../jdbc/NonTransactionalSqlScriptsTests.java | 2 +- ...epeatableSqlAnnotationSqlScriptsTests.java | 4 +--- ...ionalInlinedStatementsSqlScriptsTests.java | 6 ++--- .../jdbc/TransactionalSqlScriptsTests.java | 4 ++-- ...ransactionalSqlScriptsSpringRuleTests.java | 4 ++-- 7 files changed, 21 insertions(+), 25 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java index d34562c24e74..f93732551af7 100644 --- a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java @@ -16,6 +16,7 @@ package org.springframework.test.context.jdbc; +import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; import java.util.List; import java.util.Set; @@ -126,20 +127,19 @@ public void afterTestMethod(TestContext testContext) throws Exception { * {@link TestContext} and {@link ExecutionPhase}. */ private void executeSqlScripts(TestContext testContext, ExecutionPhase executionPhase) throws Exception { - boolean classLevel = false; + executeSqlScriptsByElement(testContext, executionPhase, testContext.getTestClass()); + executeSqlScriptsByElement(testContext, executionPhase, testContext.getTestMethod()); + } + /** + * Execute SQL scripts configured via {@link Sql @Sql} for the supplied + * {@link TestContext}, {@link ExecutionPhase} and {@link AnnotatedElement}. + */ + private void executeSqlScriptsByElement(TestContext testContext, ExecutionPhase executionPhase, AnnotatedElement annotatedElement) throws Exception { Set sqlAnnotations = AnnotatedElementUtils.getMergedRepeatableAnnotations( - testContext.getTestMethod(), Sql.class, SqlGroup.class); - if (sqlAnnotations.isEmpty()) { - sqlAnnotations = AnnotatedElementUtils.getMergedRepeatableAnnotations( - testContext.getTestClass(), Sql.class, SqlGroup.class); - if (!sqlAnnotations.isEmpty()) { - classLevel = true; - } - } - + annotatedElement, Sql.class, SqlGroup.class); for (Sql sql : sqlAnnotations) { - executeSqlScripts(sql, executionPhase, testContext, classLevel); + executeSqlScripts(sql, executionPhase, testContext, annotatedElement instanceof Class); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/DataSourceOnlySqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/DataSourceOnlySqlScriptsTests.java index 175df3b7baf7..d9a9ee658373 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/DataSourceOnlySqlScriptsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/DataSourceOnlySqlScriptsTests.java @@ -46,7 +46,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @ContextConfiguration -@Sql({ "schema.sql", "data.sql" }) +@Sql({"drop-schema.sql", "schema.sql", "data.sql" }) @DirtiesContext public class DataSourceOnlySqlScriptsTests { @@ -66,7 +66,7 @@ public void test01_classLevelScripts() { } @Test - @Sql({ "drop-schema.sql", "schema.sql", "data.sql", "data-add-dogbert.sql" }) + @Sql({"data-add-dogbert.sql" }) // test##_ prefix is required for @FixMethodOrder. public void test02_methodLevelScripts() { assertInTransaction(false); diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/NonTransactionalSqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/NonTransactionalSqlScriptsTests.java index 52740b29644c..a26e4178bc32 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/NonTransactionalSqlScriptsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/NonTransactionalSqlScriptsTests.java @@ -42,7 +42,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @ContextConfiguration(classes = EmptyDatabaseConfig.class) -@Sql({ "schema.sql", "data.sql" }) +@Sql({"drop-schema.sql", "schema.sql", "data.sql" }) @DirtiesContext public class NonTransactionalSqlScriptsTests { diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/RepeatableSqlAnnotationSqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/RepeatableSqlAnnotationSqlScriptsTests.java index 6d23fccef418..f8dfa5fd1b84 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/RepeatableSqlAnnotationSqlScriptsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/RepeatableSqlAnnotationSqlScriptsTests.java @@ -37,6 +37,7 @@ */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) @ContextConfiguration(classes = EmptyDatabaseConfig.class) +@Sql("drop-schema.sql") @Sql("schema.sql") @Sql("data.sql") @DirtiesContext @@ -49,9 +50,6 @@ public void test01_classLevelScripts() { } @Test - @Sql("drop-schema.sql") - @Sql("schema.sql") - @Sql("data.sql") @Sql("data-add-dogbert.sql") // test##_ prefix is required for @FixMethodOrder. public void test02_methodLevelScripts() { diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalInlinedStatementsSqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalInlinedStatementsSqlScriptsTests.java index 6a97aea54143..c4d46262fabc 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalInlinedStatementsSqlScriptsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalInlinedStatementsSqlScriptsTests.java @@ -46,7 +46,7 @@ @ContextConfiguration(classes = EmptyDatabaseConfig.class) @Transactional @Sql( - scripts = "schema.sql", + scripts = {"drop-schema.sql", "schema.sql"}, statements = "INSERT INTO user VALUES('Dilbert')" ) @DirtiesContext @@ -67,9 +67,7 @@ public void test01_classLevelScripts() { } @Test - @Sql(statements = "DROP TABLE user IF EXISTS") - @Sql("schema.sql") - @Sql(statements = "INSERT INTO user VALUES ('Dilbert'), ('Dogbert'), ('Catbert')") + @Sql(statements = "INSERT INTO user VALUES ('Dogbert'), ('Catbert')") // test##_ prefix is required for @FixMethodOrder. public void test02_methodLevelScripts() { assertNumUsers(3); diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java index 47d17895472e..017f8e61873e 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java @@ -43,7 +43,7 @@ @FixMethodOrder(MethodSorters.NAME_ASCENDING) @ContextConfiguration(classes = EmptyDatabaseConfig.class) @Transactional -@Sql({ "schema.sql", "data.sql" }) +@Sql({ "drop-schema.sql", "schema.sql", "data.sql" }) @DirtiesContext public class TransactionalSqlScriptsTests { @@ -62,7 +62,7 @@ public void test01_classLevelScripts() { } @Test - @Sql({ "drop-schema.sql", "schema.sql", "data.sql", "data-add-dogbert.sql" }) + @Sql({ "data-add-dogbert.sql" }) // test##_ prefix is required for @FixMethodOrder. public void test02_methodLevelScripts() { assertNumUsers(2); diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/rules/TransactionalSqlScriptsSpringRuleTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/rules/TransactionalSqlScriptsSpringRuleTests.java index 75371b718935..688a19ae0ea5 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/rules/TransactionalSqlScriptsSpringRuleTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/rules/TransactionalSqlScriptsSpringRuleTests.java @@ -42,7 +42,7 @@ // Note: @FixMethodOrder is NOT @Inherited. @FixMethodOrder(MethodSorters.NAME_ASCENDING) // Overriding @Sql declaration to reference scripts using relative path. -@Sql({ "../../jdbc/schema.sql", "../../jdbc/data.sql" }) +@Sql({ "../../jdbc/drop-schema.sql", "../../jdbc/schema.sql", "../../jdbc/data.sql" }) public class TransactionalSqlScriptsSpringRuleTests extends TransactionalSqlScriptsTests { @ClassRule @@ -69,7 +69,7 @@ public void test01_classLevelScripts() { * Overriding {@code @Sql} declaration to reference scripts using relative path. */ @Test - @Sql({ "../../jdbc/drop-schema.sql", "../../jdbc/schema.sql", "../../jdbc/data.sql", "../../jdbc/data-add-dogbert.sql" }) + @Sql({ "../../jdbc/data-add-dogbert.sql" }) @Override // test##_ prefix is required for @FixMethodOrder. public void test02_methodLevelScripts() { From 6e47d1fb7ec0f83ad652b65e63562a364194b3de Mon Sep 17 00:00:00 2001 From: asympro Date: Sun, 3 Jun 2018 13:41:33 +0300 Subject: [PATCH 2/8] Tests reverted as proposed --- .../test/context/jdbc/DataSourceOnlySqlScriptsTests.java | 4 ++-- .../test/context/jdbc/NonTransactionalSqlScriptsTests.java | 2 +- .../jdbc/RepeatableSqlAnnotationSqlScriptsTests.java | 4 +++- .../jdbc/TransactionalInlinedStatementsSqlScriptsTests.java | 6 ++++-- .../test/context/jdbc/TransactionalSqlScriptsTests.java | 4 ++-- .../rules/TransactionalSqlScriptsSpringRuleTests.java | 4 ++-- 6 files changed, 14 insertions(+), 10 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/DataSourceOnlySqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/DataSourceOnlySqlScriptsTests.java index d9a9ee658373..175df3b7baf7 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/DataSourceOnlySqlScriptsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/DataSourceOnlySqlScriptsTests.java @@ -46,7 +46,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @ContextConfiguration -@Sql({"drop-schema.sql", "schema.sql", "data.sql" }) +@Sql({ "schema.sql", "data.sql" }) @DirtiesContext public class DataSourceOnlySqlScriptsTests { @@ -66,7 +66,7 @@ public void test01_classLevelScripts() { } @Test - @Sql({"data-add-dogbert.sql" }) + @Sql({ "drop-schema.sql", "schema.sql", "data.sql", "data-add-dogbert.sql" }) // test##_ prefix is required for @FixMethodOrder. public void test02_methodLevelScripts() { assertInTransaction(false); diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/NonTransactionalSqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/NonTransactionalSqlScriptsTests.java index a26e4178bc32..52740b29644c 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/NonTransactionalSqlScriptsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/NonTransactionalSqlScriptsTests.java @@ -42,7 +42,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @ContextConfiguration(classes = EmptyDatabaseConfig.class) -@Sql({"drop-schema.sql", "schema.sql", "data.sql" }) +@Sql({ "schema.sql", "data.sql" }) @DirtiesContext public class NonTransactionalSqlScriptsTests { diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/RepeatableSqlAnnotationSqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/RepeatableSqlAnnotationSqlScriptsTests.java index f8dfa5fd1b84..6d23fccef418 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/RepeatableSqlAnnotationSqlScriptsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/RepeatableSqlAnnotationSqlScriptsTests.java @@ -37,7 +37,6 @@ */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) @ContextConfiguration(classes = EmptyDatabaseConfig.class) -@Sql("drop-schema.sql") @Sql("schema.sql") @Sql("data.sql") @DirtiesContext @@ -50,6 +49,9 @@ public void test01_classLevelScripts() { } @Test + @Sql("drop-schema.sql") + @Sql("schema.sql") + @Sql("data.sql") @Sql("data-add-dogbert.sql") // test##_ prefix is required for @FixMethodOrder. public void test02_methodLevelScripts() { diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalInlinedStatementsSqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalInlinedStatementsSqlScriptsTests.java index c4d46262fabc..6a97aea54143 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalInlinedStatementsSqlScriptsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalInlinedStatementsSqlScriptsTests.java @@ -46,7 +46,7 @@ @ContextConfiguration(classes = EmptyDatabaseConfig.class) @Transactional @Sql( - scripts = {"drop-schema.sql", "schema.sql"}, + scripts = "schema.sql", statements = "INSERT INTO user VALUES('Dilbert')" ) @DirtiesContext @@ -67,7 +67,9 @@ public void test01_classLevelScripts() { } @Test - @Sql(statements = "INSERT INTO user VALUES ('Dogbert'), ('Catbert')") + @Sql(statements = "DROP TABLE user IF EXISTS") + @Sql("schema.sql") + @Sql(statements = "INSERT INTO user VALUES ('Dilbert'), ('Dogbert'), ('Catbert')") // test##_ prefix is required for @FixMethodOrder. public void test02_methodLevelScripts() { assertNumUsers(3); diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java index 017f8e61873e..47d17895472e 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java @@ -43,7 +43,7 @@ @FixMethodOrder(MethodSorters.NAME_ASCENDING) @ContextConfiguration(classes = EmptyDatabaseConfig.class) @Transactional -@Sql({ "drop-schema.sql", "schema.sql", "data.sql" }) +@Sql({ "schema.sql", "data.sql" }) @DirtiesContext public class TransactionalSqlScriptsTests { @@ -62,7 +62,7 @@ public void test01_classLevelScripts() { } @Test - @Sql({ "data-add-dogbert.sql" }) + @Sql({ "drop-schema.sql", "schema.sql", "data.sql", "data-add-dogbert.sql" }) // test##_ prefix is required for @FixMethodOrder. public void test02_methodLevelScripts() { assertNumUsers(2); diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/rules/TransactionalSqlScriptsSpringRuleTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/rules/TransactionalSqlScriptsSpringRuleTests.java index 688a19ae0ea5..75371b718935 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/rules/TransactionalSqlScriptsSpringRuleTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/rules/TransactionalSqlScriptsSpringRuleTests.java @@ -42,7 +42,7 @@ // Note: @FixMethodOrder is NOT @Inherited. @FixMethodOrder(MethodSorters.NAME_ASCENDING) // Overriding @Sql declaration to reference scripts using relative path. -@Sql({ "../../jdbc/drop-schema.sql", "../../jdbc/schema.sql", "../../jdbc/data.sql" }) +@Sql({ "../../jdbc/schema.sql", "../../jdbc/data.sql" }) public class TransactionalSqlScriptsSpringRuleTests extends TransactionalSqlScriptsTests { @ClassRule @@ -69,7 +69,7 @@ public void test01_classLevelScripts() { * Overriding {@code @Sql} declaration to reference scripts using relative path. */ @Test - @Sql({ "../../jdbc/data-add-dogbert.sql" }) + @Sql({ "../../jdbc/drop-schema.sql", "../../jdbc/schema.sql", "../../jdbc/data.sql", "../../jdbc/data-add-dogbert.sql" }) @Override // test##_ prefix is required for @FixMethodOrder. public void test02_methodLevelScripts() { From 313689d95fb61be133a1af2c274593eead0d9c56 Mon Sep 17 00:00:00 2001 From: asympro Date: Sun, 3 Jun 2018 13:42:22 +0300 Subject: [PATCH 3/8] Sql.mergeMode added --- .../test/context/jdbc/Sql.java | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/jdbc/Sql.java b/spring-test/src/main/java/org/springframework/test/context/jdbc/Sql.java index 5794dde30f17..de9de4922075 100644 --- a/spring-test/src/main/java/org/springframework/test/context/jdbc/Sql.java +++ b/spring-test/src/main/java/org/springframework/test/context/jdbc/Sql.java @@ -31,7 +31,8 @@ * SQL {@link #scripts} and {@link #statements} to be executed against a given * database during integration tests. * - *

Method-level declarations override class-level declarations. + *

Method-level declarations override class-level declarations by default. + * This behaviour can be adjusted via {@link MergeMode} * *

Script execution is performed by the {@link SqlScriptsTestExecutionListener}, * which is enabled by default. @@ -146,6 +147,13 @@ */ SqlConfig config() default @SqlConfig(); + /** + * Indicates whether this annotation should be merged with upper-level annotations + * or override them. + *

Defaults to {@link MergeMode#OVERRIDE}. + */ + MergeMode mergeMode() default MergeMode.OVERRIDE; + /** * Enumeration of phases that dictate when SQL scripts are executed. @@ -165,4 +173,23 @@ enum ExecutionPhase { AFTER_TEST_METHOD } + /** + * Enumeration of modes that dictate whether or not + * declared SQL {@link #scripts} and {@link #statements} are merged + * with the upper-level annotations. + */ + enum MergeMode { + + /** + * Indicates that locally declared SQL {@link #scripts} and {@link #statements} + * should override the upper-level (e.g. Class-level) annotations. + */ + OVERRIDE, + + /** + * Indicates that locally declared SQL {@link #scripts} and {@link #statements} + * should be merged the upper-level (e.g. Class-level) annotations. + */ + MERGE + } } From 4d03a9190d0108ca43fed860b10068bd1c4ce0e9 Mon Sep 17 00:00:00 2001 From: asympro Date: Mon, 6 Aug 2018 20:01:10 +0300 Subject: [PATCH 4/8] Sql.mergeMode added --- .../jdbc/SqlScriptsTestExecutionListener.java | 36 ++++++++++++++----- ...epeatableSqlAnnotationSqlScriptsTests.java | 5 +++ .../test/context/jdbc/SqlMergeTest.java | 30 ++++++++++++++++ 3 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMergeTest.java diff --git a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java index f93732551af7..da694ca03422 100644 --- a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java @@ -20,6 +20,7 @@ import java.lang.reflect.Method; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import javax.sql.DataSource; import org.apache.commons.logging.Log; @@ -127,19 +128,36 @@ public void afterTestMethod(TestContext testContext) throws Exception { * {@link TestContext} and {@link ExecutionPhase}. */ private void executeSqlScripts(TestContext testContext, ExecutionPhase executionPhase) throws Exception { - executeSqlScriptsByElement(testContext, executionPhase, testContext.getTestClass()); - executeSqlScriptsByElement(testContext, executionPhase, testContext.getTestMethod()); + Set methodLevelSqls = getScriptsFromElement(testContext.getTestMethod()); + List methodLevelOverrides = methodLevelSqls.stream() +// .filter(s -> s.executionPhase() == executionPhase) + .filter(s -> s.mergeMode() == Sql.MergeMode.OVERRIDE) + .collect(Collectors.toList()); + if (methodLevelOverrides.isEmpty()) { + executeScripts(getScriptsFromElement(testContext.getTestClass()), testContext, executionPhase, true); + executeScripts(methodLevelSqls, testContext, executionPhase, false); + } else { + executeScripts(methodLevelOverrides, testContext, executionPhase, false); + } } /** - * Execute SQL scripts configured via {@link Sql @Sql} for the supplied - * {@link TestContext}, {@link ExecutionPhase} and {@link AnnotatedElement}. + * Get SQL scripts configured via {@link Sql @Sql} for the supplied + * {@link AnnotatedElement}. */ - private void executeSqlScriptsByElement(TestContext testContext, ExecutionPhase executionPhase, AnnotatedElement annotatedElement) throws Exception { - Set sqlAnnotations = AnnotatedElementUtils.getMergedRepeatableAnnotations( - annotatedElement, Sql.class, SqlGroup.class); - for (Sql sql : sqlAnnotations) { - executeSqlScripts(sql, executionPhase, testContext, annotatedElement instanceof Class); + private Set getScriptsFromElement(AnnotatedElement annotatedElement) throws Exception { + return AnnotatedElementUtils.getMergedRepeatableAnnotations(annotatedElement, Sql.class, SqlGroup.class); + } + + /** + * Execute given {@link Sql @Sql} scripts. + * {@link AnnotatedElement}. + */ + private void executeScripts(Iterable scripts, TestContext testContext, ExecutionPhase executionPhase, + boolean classLevel) + throws Exception { + for (Sql sql : scripts) { + executeSqlScripts(sql, executionPhase, testContext, classLevel); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/RepeatableSqlAnnotationSqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/RepeatableSqlAnnotationSqlScriptsTests.java index 6d23fccef418..c2943bee382c 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/RepeatableSqlAnnotationSqlScriptsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/RepeatableSqlAnnotationSqlScriptsTests.java @@ -25,6 +25,7 @@ import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; +import org.springframework.test.jdbc.JdbcTestUtils; import static org.junit.Assert.*; @@ -58,6 +59,10 @@ public void test02_methodLevelScripts() { assertNumUsers(2); } + protected int countRowsInTable(String tableName) { + return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName); + } + protected void assertNumUsers(int expected) { assertEquals("Number of rows in the 'user' table.", expected, countRowsInTable("user")); } diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMergeTest.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMergeTest.java new file mode 100644 index 000000000000..7e773047ee02 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMergeTest.java @@ -0,0 +1,30 @@ +package org.springframework.test.context.jdbc; + +import org.junit.Test; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; + +import static org.junit.Assert.assertEquals; + +/** + * Integration tests that verify support for default SQL script detection. + * + * @author Dmitry Semukhin + */ +@ContextConfiguration(classes = EmptyDatabaseConfig.class) +@Sql(value = {"schema.sql", "data-add-catbert.sql"}, mergeMode = Sql.MergeMode.MERGE) +@DirtiesContext +public class SqlMergeTest extends AbstractTransactionalJUnit4SpringContextTests { + + @Test + @Sql(value = "data-add-dogbert.sql", mergeMode = Sql.MergeMode.MERGE) + public void testMerge() { + assertNumUsers(2); + } + + protected void assertNumUsers(int expected) { + assertEquals("Number of rows in the 'user' table.", expected, countRowsInTable("user")); + } + +} From e8b0efee653568453c6f908652c6080213be01dd Mon Sep 17 00:00:00 2001 From: asympro Date: Mon, 6 Aug 2018 21:39:24 +0300 Subject: [PATCH 5/8] slight refactoring --- .../jdbc/SqlScriptsTestExecutionListener.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java index da694ca03422..41e925799838 100644 --- a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java @@ -26,6 +26,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.jetbrains.annotations.NotNull; import org.springframework.context.ApplicationContext; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.io.ByteArrayResource; @@ -130,7 +131,7 @@ public void afterTestMethod(TestContext testContext) throws Exception { private void executeSqlScripts(TestContext testContext, ExecutionPhase executionPhase) throws Exception { Set methodLevelSqls = getScriptsFromElement(testContext.getTestMethod()); List methodLevelOverrides = methodLevelSqls.stream() -// .filter(s -> s.executionPhase() == executionPhase) + .filter(s -> s.executionPhase() == executionPhase) .filter(s -> s.mergeMode() == Sql.MergeMode.OVERRIDE) .collect(Collectors.toList()); if (methodLevelOverrides.isEmpty()) { @@ -184,14 +185,7 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte mergedSqlConfig, executionPhase, testContext)); } - final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); - populator.setSqlScriptEncoding(mergedSqlConfig.getEncoding()); - populator.setSeparator(mergedSqlConfig.getSeparator()); - populator.setCommentPrefix(mergedSqlConfig.getCommentPrefix()); - populator.setBlockCommentStartDelimiter(mergedSqlConfig.getBlockCommentStartDelimiter()); - populator.setBlockCommentEndDelimiter(mergedSqlConfig.getBlockCommentEndDelimiter()); - populator.setContinueOnError(mergedSqlConfig.getErrorMode() == ErrorMode.CONTINUE_ON_ERROR); - populator.setIgnoreFailedDrops(mergedSqlConfig.getErrorMode() == ErrorMode.IGNORE_FAILED_DROPS); + final ResourceDatabasePopulator populator = configurePopulator(mergedSqlConfig); String[] scripts = getScripts(sql, testContext, classLevel); scripts = TestContextResourceUtils.convertToClasspathResourcePaths(testContext.getTestClass(), scripts); @@ -250,6 +244,19 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte } } + @NotNull + private ResourceDatabasePopulator configurePopulator(MergedSqlConfig mergedSqlConfig) { + final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); + populator.setSqlScriptEncoding(mergedSqlConfig.getEncoding()); + populator.setSeparator(mergedSqlConfig.getSeparator()); + populator.setCommentPrefix(mergedSqlConfig.getCommentPrefix()); + populator.setBlockCommentStartDelimiter(mergedSqlConfig.getBlockCommentStartDelimiter()); + populator.setBlockCommentEndDelimiter(mergedSqlConfig.getBlockCommentEndDelimiter()); + populator.setContinueOnError(mergedSqlConfig.getErrorMode() == ErrorMode.CONTINUE_ON_ERROR); + populator.setIgnoreFailedDrops(mergedSqlConfig.getErrorMode() == ErrorMode.IGNORE_FAILED_DROPS); + return populator; + } + @Nullable private DataSource getDataSourceFromTransactionManager(PlatformTransactionManager transactionManager) { try { From 0c3a2673b9e54907b6c168bc664927b698d39c2f Mon Sep 17 00:00:00 2001 From: asympro Date: Tue, 7 Aug 2018 00:28:52 +0300 Subject: [PATCH 6/8] more tests --- ...MergeTest.java => SqlMethodMergeTest.java} | 5 +-- .../context/jdbc/SqlMethodOverrideTest.java | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) rename spring-test/src/test/java/org/springframework/test/context/jdbc/{SqlMergeTest.java => SqlMethodMergeTest.java} (80%) create mode 100644 spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodOverrideTest.java diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMergeTest.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodMergeTest.java similarity index 80% rename from spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMergeTest.java rename to spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodMergeTest.java index 7e773047ee02..2dc8a148549e 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMergeTest.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodMergeTest.java @@ -13,12 +13,13 @@ * @author Dmitry Semukhin */ @ContextConfiguration(classes = EmptyDatabaseConfig.class) -@Sql(value = {"schema.sql", "data-add-catbert.sql"}, mergeMode = Sql.MergeMode.MERGE) +@Sql(value = {"schema.sql", "data-add-catbert.sql"}) @DirtiesContext -public class SqlMergeTest extends AbstractTransactionalJUnit4SpringContextTests { +public class SqlMethodMergeTest extends AbstractTransactionalJUnit4SpringContextTests { @Test @Sql(value = "data-add-dogbert.sql", mergeMode = Sql.MergeMode.MERGE) + // test##_ prefix is required for @FixMethodOrder. public void testMerge() { assertNumUsers(2); } diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodOverrideTest.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodOverrideTest.java new file mode 100644 index 000000000000..99f06a107667 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodOverrideTest.java @@ -0,0 +1,31 @@ +package org.springframework.test.context.jdbc; + +import org.junit.Test; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; + +import static org.junit.Assert.assertEquals; + +/** + * Integration tests that verify support for default SQL script detection. + * + * @author Dmitry Semukhin + */ +@ContextConfiguration(classes = EmptyDatabaseConfig.class) +@Sql(value = {"schema.sql", "data-add-catbert.sql"}) +@DirtiesContext +public class SqlMethodOverrideTest extends AbstractTransactionalJUnit4SpringContextTests { + + @Test + @Sql(value = {"schema.sql", "data.sql", "data-add-dogbert.sql", "data-add-catbert.sql"}, mergeMode = Sql.MergeMode.OVERRIDE) + // test##_ prefix is required for @FixMethodOrder. + public void testMerge() { + assertNumUsers(3); + } + + protected void assertNumUsers(int expected) { + assertEquals("Number of rows in the 'user' table.", expected, countRowsInTable("user")); + } + +} From e5b92f6678a6e710a36532e66c25ce7426925767 Mon Sep 17 00:00:00 2001 From: asympro Date: Tue, 7 Aug 2018 00:31:08 +0300 Subject: [PATCH 7/8] remove redundant comments --- .../springframework/test/context/jdbc/SqlMethodMergeTest.java | 1 - .../springframework/test/context/jdbc/SqlMethodOverrideTest.java | 1 - 2 files changed, 2 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodMergeTest.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodMergeTest.java index 2dc8a148549e..c2f9bc65bc58 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodMergeTest.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodMergeTest.java @@ -19,7 +19,6 @@ public class SqlMethodMergeTest extends AbstractTransactionalJUnit4SpringContext @Test @Sql(value = "data-add-dogbert.sql", mergeMode = Sql.MergeMode.MERGE) - // test##_ prefix is required for @FixMethodOrder. public void testMerge() { assertNumUsers(2); } diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodOverrideTest.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodOverrideTest.java index 99f06a107667..58e5493f8335 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodOverrideTest.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodOverrideTest.java @@ -19,7 +19,6 @@ public class SqlMethodOverrideTest extends AbstractTransactionalJUnit4SpringCont @Test @Sql(value = {"schema.sql", "data.sql", "data-add-dogbert.sql", "data-add-catbert.sql"}, mergeMode = Sql.MergeMode.OVERRIDE) - // test##_ prefix is required for @FixMethodOrder. public void testMerge() { assertNumUsers(3); } From 20cdc3086bc204c7b285260bebefc3d14806bafb Mon Sep 17 00:00:00 2001 From: asympro Date: Tue, 7 Aug 2018 00:32:12 +0300 Subject: [PATCH 8/8] updated javadocs --- .../springframework/test/context/jdbc/SqlMethodMergeTest.java | 2 +- .../test/context/jdbc/SqlMethodOverrideTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodMergeTest.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodMergeTest.java index c2f9bc65bc58..3a23a9db8780 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodMergeTest.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodMergeTest.java @@ -8,7 +8,7 @@ import static org.junit.Assert.assertEquals; /** - * Integration tests that verify support for default SQL script detection. + * Test to verify method level merge of @Sql annotations. * * @author Dmitry Semukhin */ diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodOverrideTest.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodOverrideTest.java index 58e5493f8335..6f4d8023c490 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodOverrideTest.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlMethodOverrideTest.java @@ -8,7 +8,7 @@ import static org.junit.Assert.assertEquals; /** - * Integration tests that verify support for default SQL script detection. + * Test to verify method level override of @Sql annotations. * * @author Dmitry Semukhin */