Browse Source
Prior to these commits method-level @Sql declarations always overrode class-level @Sql declarations, which required developers to redeclare class-level @Sql declarations on test methods (e.g., via copy-and-paste) in order to combine them with method-level @Sql declarations. These commits provide developers the ability to optionally merge class-level and method-level @Sql declarations via a new @SqlMergeMode annotation that can be applied at the class level or method level, with method-level declarations of @SqlMergeMode overriding class-level declarations. For example, @SqlMergeMode(MERGE) switches from the default OVERRIDE behavior to merging behavior, with class-level SQL scripts and statements executed before method-level SQL scripts and statements. Closes gh-1835pull/23329/head
7 changed files with 402 additions and 41 deletions
@ -0,0 +1,82 @@
@@ -0,0 +1,82 @@
|
||||
/* |
||||
* 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 java.lang.annotation.Documented; |
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Inherited; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
/** |
||||
* {@code @SqlMergeMode} is used to annotate a test class or test method to |
||||
* configure whether method-level {@code @Sql} declarations are merged with |
||||
* class-level {@code @Sql} declarations. |
||||
* |
||||
* <p>A method-level {@code @SqlMergeMode} declaration overrides a class-level |
||||
* declaration. |
||||
* |
||||
* <p>If {@code @SqlMergeMode} is not declared on a test class or test method, |
||||
* {@link MergeMode#OVERRIDE} will be used by default. |
||||
* |
||||
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom |
||||
* <em>composed annotations</em> with attribute overrides. |
||||
* |
||||
* @author Sam Brannen |
||||
* @author Dmitry Semukhin |
||||
* @since 5.2 |
||||
* @see Sql |
||||
* @see MergeMode#MERGE |
||||
* @see MergeMode#OVERRIDE |
||||
*/ |
||||
@Target({ElementType.TYPE, ElementType.METHOD}) |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@Documented |
||||
@Inherited |
||||
public @interface SqlMergeMode { |
||||
|
||||
/** |
||||
* Indicates whether method-level {@code @Sql} annotations should be merged |
||||
* with class-level {@code @Sql} annotations or override them. |
||||
*/ |
||||
MergeMode value(); |
||||
|
||||
|
||||
/** |
||||
* Enumeration of <em>modes</em> that dictate whether method-level {@code @Sql} |
||||
* declarations are merged with class-level {@code @Sql} declarations. |
||||
*/ |
||||
enum MergeMode { |
||||
|
||||
/** |
||||
* Indicates that method-level {@code @Sql} declarations should be merged |
||||
* with class-level {@code @Sql} declarations, with class-level SQL |
||||
* scripts and statements executed before method-level scripts and |
||||
* statements. |
||||
*/ |
||||
MERGE, |
||||
|
||||
/** |
||||
* Indicates that method-level {@code @Sql} declarations should override |
||||
* class-level {@code @Sql} declarations. |
||||
*/ |
||||
OVERRIDE |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,45 @@
@@ -0,0 +1,45 @@
|
||||
/* |
||||
* 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 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.assertj.core.api.Assertions.assertThat; |
||||
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD; |
||||
|
||||
/** |
||||
* Abstract base class for tests involving {@link SqlMergeMode @SqlMergeMode}. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 5.2 |
||||
*/ |
||||
@ContextConfiguration(classes = EmptyDatabaseConfig.class) |
||||
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) |
||||
abstract class AbstractSqlMergeModeTests extends AbstractTransactionalJUnit4SpringContextTests { |
||||
|
||||
protected void assertUsers(String... expectedUsers) { |
||||
List<String> actualUsers = super.jdbcTemplate.queryForList("select name from user", String.class); |
||||
assertThat(actualUsers).containsExactlyInAnyOrder(expectedUsers); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,59 @@
@@ -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"); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,59 @@
@@ -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"); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue