Skip to content

Merge class-level and method-level @Sql declarations #1835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Sql> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
@Sql("drop-schema.sql")
@Sql("schema.sql")
@Sql("data.sql")
@DirtiesContext
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down