Introduce @SqlMergeMode for configuring @Sql annotation merging

Closes gh-1835
This commit is contained in:
Sam Brannen
2019-07-19 22:35:07 +02:00
parent ab8876219f
commit 89571ea236
7 changed files with 258 additions and 127 deletions

View File

@@ -1,54 +0,0 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
import static org.springframework.test.context.jdbc.Sql.MergeMode.OVERRIDE;
/**
* Transactional integration tests for {@link Sql @Sql} that verify proper
* overriding support for class-level and method-level declarations.
*
* @author Dmitry Semukhin
* @author Sam Brannen
* @since 5.2
*/
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
@Sql({ "schema.sql", "data-add-catbert.sql" })
@DirtiesContext
public class SqlMethodOverrideTests extends AbstractTransactionalJUnit4SpringContextTests {
@Test
@Sql(
scripts = { "schema.sql", "data.sql", "data-add-dogbert.sql", "data-add-catbert.sql" },
mergeMode = OVERRIDE
)
public void methodLevelSqlScriptsOverrideClassLevelScripts() {
assertNumUsers(3);
}
protected void assertNumUsers(int expected) {
assertEquals("Number of rows in the 'user' table.", expected, countRowsInTable("user"));
}
}

View File

@@ -14,38 +14,32 @@
* limitations under the License.
*/
package org.springframework.test.context.jdbc;
package org.springframework.test.context.jdbc.merging;
import org.junit.Test;
import java.util.List;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.jdbc.EmptyDatabaseConfig;
import org.springframework.test.context.jdbc.SqlMergeMode;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.context.jdbc.Sql.MergeMode.MERGE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD;
/**
* Transactional integration tests for {@link Sql @Sql} that verify proper
* merging support for class-level and method-level declarations.
* Abstract base class for tests involving {@link SqlMergeMode @SqlMergeMode}.
*
* @author Dmitry Semukhin
* @author Sam Brannen
* @since 5.2
*/
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
@Sql({ "schema.sql", "data-add-catbert.sql" })
@DirtiesContext
public class SqlMethodMergeTests extends AbstractTransactionalJUnit4SpringContextTests {
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
abstract class AbstractSqlMergeModeTests extends AbstractTransactionalJUnit4SpringContextTests {
@Test
@Sql(scripts = "data-add-dogbert.sql", mergeMode = MERGE)
public void testMerge() {
assertNumUsers(2);
}
protected void assertNumUsers(int expected) {
assertEquals("Number of rows in the 'user' table.", expected, countRowsInTable("user"));
protected void assertUsers(String... expectedUsers) {
List<String> actualUsers = super.jdbcTemplate.queryForList("select name from user", String.class);
assertThat(actualUsers).containsExactlyInAnyOrder(expectedUsers);
}
}

View File

@@ -0,0 +1,59 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.jdbc.merging;
import org.junit.Test;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlMergeMode;
import static org.springframework.test.context.jdbc.SqlMergeMode.MergeMode.MERGE;
import static org.springframework.test.context.jdbc.SqlMergeMode.MergeMode.OVERRIDE;
/**
* Transactional integration tests that verify proper merging and overriding support
* for class-level and method-level {@link Sql @Sql} declarations when
* {@link SqlMergeMode @SqlMergeMode} is declared at the class level with
* {@link SqlMergeMode.MergeMode#MERGE MERGE} mode.
*
* @author Sam Brannen
* @author Dmitry Semukhin
* @since 5.2
*/
@Sql({ "../schema.sql", "../data-add-catbert.sql" })
@SqlMergeMode(MERGE)
public class ClassLevelMergeSqlMergeModeTests extends AbstractSqlMergeModeTests {
@Test
public void classLevelScripts() {
assertUsers("Catbert");
}
@Test
@Sql("../data-add-dogbert.sql")
public void merged() {
assertUsers("Catbert", "Dogbert");
}
@Test
@Sql({ "../schema.sql", "../data.sql", "../data-add-dogbert.sql", "../data-add-catbert.sql" })
@SqlMergeMode(OVERRIDE)
public void overridden() {
assertUsers("Dilbert", "Dogbert", "Catbert");
}
}

View File

@@ -0,0 +1,59 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.jdbc.merging;
import org.junit.Test;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlMergeMode;
import static org.springframework.test.context.jdbc.SqlMergeMode.MergeMode.MERGE;
import static org.springframework.test.context.jdbc.SqlMergeMode.MergeMode.OVERRIDE;
/**
* Transactional integration tests that verify proper merging and overriding support
* for class-level and method-level {@link Sql @Sql} declarations when
* {@link SqlMergeMode @SqlMergeMode} is declared at the class level with
* {@link SqlMergeMode.MergeMode#OVERRIDE OVERRIDE} mode.
*
* @author Sam Brannen
* @author Dmitry Semukhin
* @since 5.2
*/
@Sql({ "../schema.sql", "../data-add-catbert.sql" })
@SqlMergeMode(OVERRIDE)
public class ClassLevelOverrideSqlMergeModeTests extends AbstractSqlMergeModeTests {
@Test
public void classLevelScripts() {
assertUsers("Catbert");
}
@Test
@Sql("../data-add-dogbert.sql")
@SqlMergeMode(MERGE)
public void merged() {
assertUsers("Catbert", "Dogbert");
}
@Test
@Sql({ "../schema.sql", "../data.sql", "../data-add-dogbert.sql", "../data-add-catbert.sql" })
public void overridden() {
assertUsers("Dilbert", "Dogbert", "Catbert");
}
}