From 5da2e78d1e255b880ddd3a732f76a1567a77ff25 Mon Sep 17 00:00:00 2001 From: Anatoliy Korovin Date: Sat, 20 Jul 2019 12:02:40 +1000 Subject: [PATCH 1/7] repeatable processing for the TestPropertySource annotation --- .../test/context/TestPropertySource.java | 2 + .../test/context/TestPropertySources.java | 26 +++++ .../support/TestPropertySourceUtils.java | 96 ++++++++----------- .../AnnotationWithTestProperty.java | 15 +++ .../ParentClassWithTestProperties.java | 7 ++ .../TestPropertySourceInheritTests.java | 46 +++++++++ ...ourceInheritedFromMetaAnnotationTests.java | 47 +++++++++ .../TestPropertySourceRepeatableTests.java | 48 ++++++++++ 8 files changed, 231 insertions(+), 56 deletions(-) create mode 100644 spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestProperty.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithTestProperties.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritTests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationTests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableTests.java diff --git a/spring-test/src/main/java/org/springframework/test/context/TestPropertySource.java b/spring-test/src/main/java/org/springframework/test/context/TestPropertySource.java index 150604e72b19..e71abcb294e0 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestPropertySource.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestPropertySource.java @@ -19,6 +19,7 @@ import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; +import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @@ -86,6 +87,7 @@ @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited +@Repeatable(TestPropertySources.class) public @interface TestPropertySource { /** diff --git a/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java b/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java new file mode 100644 index 000000000000..21a6933d9a00 --- /dev/null +++ b/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java @@ -0,0 +1,26 @@ +package org.springframework.test.context; + + +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; + + +/** + * Container for repeatable annotation {@link TestPropertySource} + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +public @interface TestPropertySources { + + /** + * array of annotation values + * @return value + */ + TestPropertySource[] value(); +} diff --git a/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java b/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java index 41574d9169a9..9d07f9e9a1d6 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java @@ -24,11 +24,13 @@ import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.stream.Collectors; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.annotation.MergedAnnotations; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.MapPropertySource; @@ -39,13 +41,10 @@ import org.springframework.core.io.support.ResourcePropertySource; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.util.TestContextResourceUtils; -import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; -import static org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptor; - /** * Utility methods for working with {@link TestPropertySource @TestPropertySource} * and adding test {@link PropertySource PropertySources} to the {@code Environment}. @@ -53,6 +52,7 @@ *

Primarily intended for use within the framework. * * @author Sam Brannen + * @author Anatoliy Korovin * @since 4.1 * @see TestPropertySource */ @@ -69,9 +69,10 @@ public abstract class TestPropertySourceUtils { static MergedTestPropertySources buildMergedTestPropertySources(Class testClass) { - Class annotationType = TestPropertySource.class; - AnnotationDescriptor descriptor = findAnnotationDescriptor(testClass, annotationType); - if (descriptor == null) { + + if (!MergedAnnotations.from(testClass, MergedAnnotations.SearchStrategy.EXHAUSTIVE) + .get(TestPropertySource.class) + .isPresent()) { return new MergedTestPropertySources(); } @@ -83,31 +84,11 @@ static MergedTestPropertySources buildMergedTestPropertySources(Class testCla private static List resolveTestPropertySourceAttributes(Class testClass) { Assert.notNull(testClass, "Class must not be null"); - List attributesList = new ArrayList<>(); - Class annotationType = TestPropertySource.class; - - AnnotationDescriptor descriptor = findAnnotationDescriptor(testClass, annotationType); - Assert.notNull(descriptor, String.format( - "Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]", - annotationType.getName(), testClass.getName())); - - while (descriptor != null) { - TestPropertySource testPropertySource = descriptor.synthesizeAnnotation(); - Class rootDeclaringClass = descriptor.getRootDeclaringClass(); - if (logger.isTraceEnabled()) { - logger.trace(String.format("Retrieved @TestPropertySource [%s] for declaring class [%s].", - testPropertySource, rootDeclaringClass.getName())); - } - TestPropertySourceAttributes attributes = - new TestPropertySourceAttributes(rootDeclaringClass, testPropertySource); - if (logger.isTraceEnabled()) { - logger.trace("Resolved TestPropertySource attributes: " + attributes); - } - attributesList.add(attributes); - descriptor = findAnnotationDescriptor(rootDeclaringClass.getSuperclass(), annotationType); - } - - return attributesList; + return MergedAnnotations.from(testClass, MergedAnnotations.SearchStrategy.EXHAUSTIVE) + .stream(TestPropertySource.class) + .map(a -> new TestPropertySourceAttributes((Class) a.getSource(), + a.synthesize())) + .collect(Collectors.toList()); } private static String[] mergeLocations(List attributesList) { @@ -148,15 +129,16 @@ private static String[] mergeProperties(List attri * to the {@link Environment} of the supplied {@code context}. *

This method simply delegates to * {@link #addPropertiesFilesToEnvironment(ConfigurableEnvironment, ResourceLoader, String...)}. - * @param context the application context whose environment should be updated; - * never {@code null} + * + * @param context the application context whose environment should be updated; + * never {@code null} * @param locations the resource locations of {@code Properties} files to add - * to the environment; potentially empty but never {@code null} + * to the environment; potentially empty but never {@code null} * @throws IllegalStateException if an error occurs while processing a properties file - * @since 4.1.5 * @see ResourcePropertySource * @see TestPropertySource#locations * @see #addPropertiesFilesToEnvironment(ConfigurableEnvironment, ResourceLoader, String...) + * @since 4.1.5 */ public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContext context, String... locations) { Assert.notNull(context, "'context' must not be null"); @@ -173,19 +155,20 @@ public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContex *

Each properties file will be converted to a {@link ResourcePropertySource} * that will be added to the {@link PropertySources} of the environment with * highest precedence. - * @param environment the environment to update; never {@code null} + * + * @param environment the environment to update; never {@code null} * @param resourceLoader the {@code ResourceLoader} to use to load each resource; - * never {@code null} - * @param locations the resource locations of {@code Properties} files to add - * to the environment; potentially empty but never {@code null} + * never {@code null} + * @param locations the resource locations of {@code Properties} files to add + * to the environment; potentially empty but never {@code null} * @throws IllegalStateException if an error occurs while processing a properties file - * @since 4.3 * @see ResourcePropertySource * @see TestPropertySource#locations * @see #addPropertiesFilesToEnvironment(ConfigurableApplicationContext, String...) + * @since 4.3 */ public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment environment, - ResourceLoader resourceLoader, String... locations) { + ResourceLoader resourceLoader, String... locations) { Assert.notNull(environment, "'environment' must not be null"); Assert.notNull(resourceLoader, "'resourceLoader' must not be null"); @@ -196,8 +179,7 @@ public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment envir Resource resource = resourceLoader.getResource(resolvedLocation); environment.getPropertySources().addFirst(new ResourcePropertySource(resource)); } - } - catch (IOException ex) { + } catch (IOException ex) { throw new IllegalStateException("Failed to add PropertySource to Environment", ex); } } @@ -207,13 +189,14 @@ public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment envir * supplied {@code context}. *

This method simply delegates to * {@link #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])}. - * @param context the application context whose environment should be updated; - * never {@code null} + * + * @param context the application context whose environment should be updated; + * never {@code null} * @param inlinedProperties the inlined properties to add to the environment; - * potentially empty but never {@code null} - * @since 4.1.5 + * potentially empty but never {@code null} * @see TestPropertySource#properties * @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[]) + * @since 4.1.5 */ public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationContext context, String... inlinedProperties) { Assert.notNull(context, "'context' must not be null"); @@ -228,14 +211,15 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationCont * single {@link MapPropertySource} with the highest precedence. *

For details on the parsing of inlined properties, consult the * Javadoc for {@link #convertInlinedPropertiesToMap}. - * @param environment the environment to update; never {@code null} + * + * @param environment the environment to update; never {@code null} * @param inlinedProperties the inlined properties to add to the environment; - * potentially empty but never {@code null} - * @since 4.1.5 + * potentially empty but never {@code null} * @see MapPropertySource * @see #INLINED_PROPERTIES_PROPERTY_SOURCE_NAME * @see TestPropertySource#properties * @see #convertInlinedPropertiesToMap + * @since 4.1.5 */ public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment environment, String... inlinedProperties) { Assert.notNull(environment, "'environment' must not be null"); @@ -243,7 +227,7 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment env if (!ObjectUtils.isEmpty(inlinedProperties)) { if (logger.isDebugEnabled()) { logger.debug("Adding inlined properties to environment: " + - ObjectUtils.nullSafeToString(inlinedProperties)); + ObjectUtils.nullSafeToString(inlinedProperties)); } MapPropertySource ps = (MapPropertySource) environment.getPropertySources().get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); @@ -264,13 +248,14 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment env * {@link Properties#load(java.io.Reader)} to parse each virtual file. *

For a full discussion of inlined properties, consult the Javadoc * for {@link TestPropertySource#properties}. + * * @param inlinedProperties the inlined properties to convert; potentially empty - * but never {@code null} + * but never {@code null} * @return a new, ordered map containing the converted properties * @throws IllegalStateException if a given key-value pair cannot be parsed, or if - * a given inlined property contains multiple key-value pairs - * @since 4.1.5 + * a given inlined property contains multiple key-value pairs * @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[]) + * @since 4.1.5 */ public static Map convertInlinedPropertiesToMap(String... inlinedProperties) { Assert.notNull(inlinedProperties, "'inlinedProperties' must not be null"); @@ -283,8 +268,7 @@ public static Map convertInlinedPropertiesToMap(String... inline } try { props.load(new StringReader(pair)); - } - catch (Exception ex) { + } catch (Exception ex) { throw new IllegalStateException("Failed to load test environment property from [" + pair + "]", ex); } Assert.state(props.size() == 1, () -> "Failed to load exactly one test environment property from [" + pair + "]"); diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestProperty.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestProperty.java new file mode 100644 index 000000000000..3e31cd0e5254 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestProperty.java @@ -0,0 +1,15 @@ +package org.springframework.test.context.env.repeatable; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.test.context.TestPropertySource; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@TestPropertySource(properties = "meta = value from meta-annotation") +public @interface AnnotationWithTestProperty { +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithTestProperties.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithTestProperties.java new file mode 100644 index 000000000000..f0801d57bc70 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithTestProperties.java @@ -0,0 +1,7 @@ +package org.springframework.test.context.env.repeatable; + +import org.springframework.test.context.TestPropertySource; + +@TestPropertySource(properties = "inherited = 12345") +public class ParentClassWithTestProperties { +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritTests.java new file mode 100644 index 000000000000..6fc13530ee9e --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritTests.java @@ -0,0 +1,46 @@ +package org.springframework.test.context.env.repeatable; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; + + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@TestPropertySource(properties = "key = 051187") +public class TestPropertySourceInheritTests extends ParentClassWithTestProperties { + + @Autowired + Environment env; + + @Value("${key}") + String key; + + @Value("${inherited}") + String inherited; + + + @Test + public void inlinePropertyFromParentClassAndFromLocalTestPropertySourceAnnotation() { + assertThat(env.getProperty("key")).isEqualTo("051187"); + assertThat(this.key).isEqualTo("051187"); + + assertThat(env.getProperty("inherited")).isEqualTo("12345"); + assertThat(inherited).isEqualTo("12345"); + } + + + @Configuration + static class Config { + } +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationTests.java new file mode 100644 index 000000000000..b3bf6dd5b363 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationTests.java @@ -0,0 +1,47 @@ +package org.springframework.test.context.env.repeatable; + + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@TestPropertySource(properties = "key = 051187") +@AnnotationWithTestProperty +public class TestPropertySourceInheritedFromMetaAnnotationTests { + + @Autowired + Environment env; + + @Value("${key}") + String key; + + @Value("${meta}") + String meta; + + + @Test + public void inlineLocalPropertyAndPropertyFromMetaAnnotation() { + // local inlined: + assertThat(env.getProperty("key")).isEqualTo("051187"); + assertThat(this.key).isEqualTo("051187"); + // inlined from meta-annotation: + assertThat(env.getProperty("meta")).isEqualTo("value from meta-annotation"); + assertThat(meta).isEqualTo("value from meta-annotation"); + } + + + @Configuration + static class Config { + } +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableTests.java new file mode 100644 index 000000000000..9e90b2f36748 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableTests.java @@ -0,0 +1,48 @@ +package org.springframework.test.context.env.repeatable; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@TestPropertySource(properties = "first = 1111") +@TestPropertySource(properties = "second = 2222") +public class TestPropertySourceRepeatableTests { + + @Autowired + Environment env; + + @Value("${first}") + String first; + + @Value("${second}") + String second; + + + @Test + public void inlinePropertyFromParentClassAndFromLocalTestPropertySourceAnnotation() { + assertPropertyValue("first", first, "1111"); + assertPropertyValue("second", second, "2222"); + } + + private void assertPropertyValue(String name, String value, String expectedValue) { + assertThat(env.getProperty(name)).isEqualTo(expectedValue); + assertThat(value).isEqualTo(expectedValue); + } + + + @Configuration + static class Config { + } +} From 899fb1d1868f3076bc58b5dd0c4228d5f8334650 Mon Sep 17 00:00:00 2001 From: Anatoliy Korovin Date: Sat, 20 Jul 2019 12:28:49 +1000 Subject: [PATCH 2/7] fix code style --- .../test/context/TestPropertySources.java | 16 ++++++++++++++++ .../repeatable/AnnotationWithTestProperty.java | 16 ++++++++++++++++ .../ParentClassWithTestProperties.java | 16 ++++++++++++++++ .../TestPropertySourceInheritTests.java | 16 ++++++++++++++++ ...tySourceInheritedFromMetaAnnotationTests.java | 16 ++++++++++++++++ .../TestPropertySourceRepeatableTests.java | 16 ++++++++++++++++ 6 files changed, 96 insertions(+) diff --git a/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java b/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java index 21a6933d9a00..21126447d6af 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java @@ -1,3 +1,19 @@ +/* + * 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; diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestProperty.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestProperty.java index 3e31cd0e5254..b46af3bd332e 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestProperty.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestProperty.java @@ -1,3 +1,19 @@ +/* + * 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.env.repeatable; diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithTestProperties.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithTestProperties.java index f0801d57bc70..6d66de2ec844 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithTestProperties.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithTestProperties.java @@ -1,3 +1,19 @@ +/* + * 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.env.repeatable; import org.springframework.test.context.TestPropertySource; diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritTests.java index 6fc13530ee9e..32882ddceeca 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritTests.java @@ -1,3 +1,19 @@ +/* + * 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.env.repeatable; import org.junit.Test; diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationTests.java index b3bf6dd5b363..adfb148a8617 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationTests.java @@ -1,3 +1,19 @@ +/* + * 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.env.repeatable; diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableTests.java index 9e90b2f36748..db443dfb310c 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableTests.java @@ -1,3 +1,19 @@ +/* + * 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.env.repeatable; import org.junit.Test; From aa3202e3f8aff31432a52c6fbad56967bb5a9e39 Mon Sep 17 00:00:00 2001 From: Anatoliy Korovin Date: Sat, 20 Jul 2019 16:46:32 +1000 Subject: [PATCH 3/7] revert noise after the spring.io format plugin --- .../test/context/TestPropertySources.java | 14 ++- .../support/TestPropertySourceUtils.java | 86 +++++++++++-------- 2 files changed, 59 insertions(+), 41 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java b/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java index 21126447d6af..316e7c7a5070 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java @@ -26,7 +26,14 @@ /** - * Container for repeatable annotation {@link TestPropertySource} + * {@code @TestPropertySources} is a container for one or more {@link TestPropertySource} + * declarations. + * + *

Note, however, that use of the {@code @TestPropertySources} container is completely + * optional since {@code @TestPropertySource} is a {@linkplain java.lang.annotation.Repeatable + * repeatable} annotation. + * + * @author Anatoliy Korovin */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @@ -35,8 +42,9 @@ public @interface TestPropertySources { /** - * array of annotation values - * @return value + * An array of one or more {@link TestPropertySource} declarations. + * + * @return array of {@link TestPropertySource} values. */ TestPropertySource[] value(); } diff --git a/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java b/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java index 9d07f9e9a1d6..c6c099617cec 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java @@ -67,28 +67,41 @@ public abstract class TestPropertySourceUtils { private static final Log logger = LogFactory.getLog(TestPropertySourceUtils.class); - static MergedTestPropertySources buildMergedTestPropertySources(Class testClass) { - if (!MergedAnnotations.from(testClass, MergedAnnotations.SearchStrategy.EXHAUSTIVE) - .get(TestPropertySource.class) - .isPresent()) { + if (!isPresentTestPropertySourceAnnotation(testClass)) { return new MergedTestPropertySources(); } + else { + return mergeTestPropertySources(testClass); + } + } + + private static boolean isPresentTestPropertySourceAnnotation(Class testClass) { + return MergedAnnotations + .from(testClass, MergedAnnotations.SearchStrategy.EXHAUSTIVE) + .get(TestPropertySource.class).isPresent(); + } + + private static MergedTestPropertySources mergeTestPropertySources(Class testClass) { + + List attributesList = resolveTestPropertySourceAttributes( + testClass); - List attributesList = resolveTestPropertySourceAttributes(testClass); String[] locations = mergeLocations(attributesList); String[] properties = mergeProperties(attributesList); + return new MergedTestPropertySources(locations, properties); } private static List resolveTestPropertySourceAttributes(Class testClass) { Assert.notNull(testClass, "Class must not be null"); - return MergedAnnotations.from(testClass, MergedAnnotations.SearchStrategy.EXHAUSTIVE) - .stream(TestPropertySource.class) - .map(a -> new TestPropertySourceAttributes((Class) a.getSource(), - a.synthesize())) - .collect(Collectors.toList()); + return MergedAnnotations + .from(testClass, MergedAnnotations.SearchStrategy.EXHAUSTIVE) + .stream(TestPropertySource.class) + .map(a -> new TestPropertySourceAttributes((Class) a.getSource(), + a.synthesize())) + .collect(Collectors.toList()); } private static String[] mergeLocations(List attributesList) { @@ -129,16 +142,15 @@ private static String[] mergeProperties(List attri * to the {@link Environment} of the supplied {@code context}. *

This method simply delegates to * {@link #addPropertiesFilesToEnvironment(ConfigurableEnvironment, ResourceLoader, String...)}. - * - * @param context the application context whose environment should be updated; - * never {@code null} + * @param context the application context whose environment should be updated; + * never {@code null} * @param locations the resource locations of {@code Properties} files to add - * to the environment; potentially empty but never {@code null} + * to the environment; potentially empty but never {@code null} * @throws IllegalStateException if an error occurs while processing a properties file + * @since 4.1.5 * @see ResourcePropertySource * @see TestPropertySource#locations * @see #addPropertiesFilesToEnvironment(ConfigurableEnvironment, ResourceLoader, String...) - * @since 4.1.5 */ public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContext context, String... locations) { Assert.notNull(context, "'context' must not be null"); @@ -155,20 +167,19 @@ public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContex *

Each properties file will be converted to a {@link ResourcePropertySource} * that will be added to the {@link PropertySources} of the environment with * highest precedence. - * - * @param environment the environment to update; never {@code null} + * @param environment the environment to update; never {@code null} * @param resourceLoader the {@code ResourceLoader} to use to load each resource; - * never {@code null} - * @param locations the resource locations of {@code Properties} files to add - * to the environment; potentially empty but never {@code null} + * never {@code null} + * @param locations the resource locations of {@code Properties} files to add + * to the environment; potentially empty but never {@code null} * @throws IllegalStateException if an error occurs while processing a properties file + * @since 4.3 * @see ResourcePropertySource * @see TestPropertySource#locations * @see #addPropertiesFilesToEnvironment(ConfigurableApplicationContext, String...) - * @since 4.3 */ public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment environment, - ResourceLoader resourceLoader, String... locations) { + ResourceLoader resourceLoader, String... locations) { Assert.notNull(environment, "'environment' must not be null"); Assert.notNull(resourceLoader, "'resourceLoader' must not be null"); @@ -179,7 +190,8 @@ public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment envir Resource resource = resourceLoader.getResource(resolvedLocation); environment.getPropertySources().addFirst(new ResourcePropertySource(resource)); } - } catch (IOException ex) { + } + catch (IOException ex) { throw new IllegalStateException("Failed to add PropertySource to Environment", ex); } } @@ -189,14 +201,13 @@ public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment envir * supplied {@code context}. *

This method simply delegates to * {@link #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])}. - * - * @param context the application context whose environment should be updated; - * never {@code null} + * @param context the application context whose environment should be updated; + * never {@code null} * @param inlinedProperties the inlined properties to add to the environment; - * potentially empty but never {@code null} + * potentially empty but never {@code null} + * @since 4.1.5 * @see TestPropertySource#properties * @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[]) - * @since 4.1.5 */ public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationContext context, String... inlinedProperties) { Assert.notNull(context, "'context' must not be null"); @@ -211,15 +222,14 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationCont * single {@link MapPropertySource} with the highest precedence. *

For details on the parsing of inlined properties, consult the * Javadoc for {@link #convertInlinedPropertiesToMap}. - * - * @param environment the environment to update; never {@code null} + * @param environment the environment to update; never {@code null} * @param inlinedProperties the inlined properties to add to the environment; - * potentially empty but never {@code null} + * potentially empty but never {@code null} + * @since 4.1.5 * @see MapPropertySource * @see #INLINED_PROPERTIES_PROPERTY_SOURCE_NAME * @see TestPropertySource#properties * @see #convertInlinedPropertiesToMap - * @since 4.1.5 */ public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment environment, String... inlinedProperties) { Assert.notNull(environment, "'environment' must not be null"); @@ -227,7 +237,7 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment env if (!ObjectUtils.isEmpty(inlinedProperties)) { if (logger.isDebugEnabled()) { logger.debug("Adding inlined properties to environment: " + - ObjectUtils.nullSafeToString(inlinedProperties)); + ObjectUtils.nullSafeToString(inlinedProperties)); } MapPropertySource ps = (MapPropertySource) environment.getPropertySources().get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); @@ -248,14 +258,13 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment env * {@link Properties#load(java.io.Reader)} to parse each virtual file. *

For a full discussion of inlined properties, consult the Javadoc * for {@link TestPropertySource#properties}. - * * @param inlinedProperties the inlined properties to convert; potentially empty - * but never {@code null} + * but never {@code null} * @return a new, ordered map containing the converted properties * @throws IllegalStateException if a given key-value pair cannot be parsed, or if - * a given inlined property contains multiple key-value pairs - * @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[]) + * a given inlined property contains multiple key-value pairs * @since 4.1.5 + * @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[]) */ public static Map convertInlinedPropertiesToMap(String... inlinedProperties) { Assert.notNull(inlinedProperties, "'inlinedProperties' must not be null"); @@ -268,7 +277,8 @@ public static Map convertInlinedPropertiesToMap(String... inline } try { props.load(new StringReader(pair)); - } catch (Exception ex) { + } + catch (Exception ex) { throw new IllegalStateException("Failed to load test environment property from [" + pair + "]", ex); } Assert.state(props.size() == 1, () -> "Failed to load exactly one test environment property from [" + pair + "]"); From 69b159c6d036adb53d7c31518ea4d95be7b99d84 Mon Sep 17 00:00:00 2001 From: Anatoliy Korovin Date: Sat, 20 Jul 2019 23:51:56 +1000 Subject: [PATCH 4/7] revert noise after the spring.io format plugin --- .../test/context/TestPropertySource.java | 2 +- .../test/context/TestPropertySources.java | 1 + .../support/TestPropertySourceUtils.java | 25 ++++++++++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/TestPropertySource.java b/spring-test/src/main/java/org/springframework/test/context/TestPropertySource.java index e71abcb294e0..a3c57b97e318 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestPropertySource.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestPropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 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. diff --git a/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java b/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java index 316e7c7a5070..c59e236823e4 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestPropertySources.java @@ -34,6 +34,7 @@ * repeatable} annotation. * * @author Anatoliy Korovin + * @since 5.2 */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) diff --git a/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java b/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java index c6c099617cec..268219495c0a 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java @@ -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. @@ -30,6 +30,7 @@ import org.apache.commons.logging.LogFactory; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.annotation.MergedAnnotation; import org.springframework.core.annotation.MergedAnnotations; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; @@ -99,11 +100,29 @@ private static List resolveTestPropertySourceAttri return MergedAnnotations .from(testClass, MergedAnnotations.SearchStrategy.EXHAUSTIVE) .stream(TestPropertySource.class) - .map(a -> new TestPropertySourceAttributes((Class) a.getSource(), - a.synthesize())) + .map(TestPropertySourceUtils::makeTestPropertySourceAttribute) .collect(Collectors.toList()); } + private static TestPropertySourceAttributes makeTestPropertySourceAttribute( + MergedAnnotation annotation) { + + TestPropertySource testPropertySource = annotation.synthesize(); + Class rootDeclaringClass = (Class) annotation.getSource(); + if (logger.isTraceEnabled()) { + logger.trace(String.format( + "Retrieved @TestPropertySource [%s] for declaring class [%s].", + testPropertySource, rootDeclaringClass.getName())); + } + + TestPropertySourceAttributes attributes = new TestPropertySourceAttributes( + rootDeclaringClass, testPropertySource); + if (logger.isTraceEnabled()) { + logger.trace("Resolved TestPropertySource attributes: " + attributes); + } + return attributes; + } + private static String[] mergeLocations(List attributesList) { List locations = new ArrayList<>(); for (TestPropertySourceAttributes attrs : attributesList) { From 8c59dc681248ef9d4f524bba6981492d0d439398 Mon Sep 17 00:00:00 2001 From: Anatoliy Korovin Date: Sun, 21 Jul 2019 09:50:29 +1000 Subject: [PATCH 5/7] add more tests for TestPropertySource overriding --- .../AnnotationWithTestProperty.java | 6 ++ ...ationWithTestPropertyInPropertiesFile.java | 37 +++++++++++ ...ParentClassWithMultipleTestProperties.java | 30 +++++++++ .../ParentClassWithTestProperties.java | 9 ++- ...omMetaAnnotationOverridesLocallyTests.java | 56 ++++++++++++++++ ...MetaAnnotationWithPropertiesFileTests.java | 64 +++++++++++++++++++ ...SourceOverridesInheritedPropertyTests.java | 55 ++++++++++++++++ ...artialOverridesInheritedPropertyTests.java | 55 ++++++++++++++++ ...eatableWithDefaultPropertiesFileTests.java | 63 ++++++++++++++++++ ...urceRepeatableWithPropertiesFileTests.java | 64 +++++++++++++++++++ ...eWithDefaultPropertiesFileTests.properties | 1 + .../context/env/repeatable/first.properties | 1 + .../context/env/repeatable/local.properties | 1 + .../context/env/repeatable/meta.properties | 1 + .../context/env/repeatable/second.properties | 1 + 15 files changed, 443 insertions(+), 1 deletion(-) create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestPropertyInPropertiesFile.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithMultipleTestProperties.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationOverridesLocallyTests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationWithPropertiesFileTests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceOverridesInheritedPropertyTests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourcePartialOverridesInheritedPropertyTests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithDefaultPropertiesFileTests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithPropertiesFileTests.java create mode 100644 spring-test/src/test/resources/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithDefaultPropertiesFileTests.properties create mode 100644 spring-test/src/test/resources/org/springframework/test/context/env/repeatable/first.properties create mode 100644 spring-test/src/test/resources/org/springframework/test/context/env/repeatable/local.properties create mode 100644 spring-test/src/test/resources/org/springframework/test/context/env/repeatable/meta.properties create mode 100644 spring-test/src/test/resources/org/springframework/test/context/env/repeatable/second.properties diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestProperty.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestProperty.java index b46af3bd332e..0b52c59c3bf4 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestProperty.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestProperty.java @@ -24,6 +24,12 @@ import org.springframework.test.context.TestPropertySource; +/** + * A custom annotation with properties defined by the {@link TestPropertySource}. + * + * @author Anatoliy Korovin + * @since 5.2 + */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @TestPropertySource(properties = "meta = value from meta-annotation") diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestPropertyInPropertiesFile.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestPropertyInPropertiesFile.java new file mode 100644 index 000000000000..c9a32a632055 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/AnnotationWithTestPropertyInPropertiesFile.java @@ -0,0 +1,37 @@ +/* + * 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.env.repeatable; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.test.context.TestPropertySource; + +/** + * A custom annotation which defined properties file in the {@link TestPropertySource}. + * + * @author Anatoliy Korovin + * @since 5.2 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@TestPropertySource("meta.properties") +public @interface AnnotationWithTestPropertyInPropertiesFile { +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithMultipleTestProperties.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithMultipleTestProperties.java new file mode 100644 index 000000000000..3a176827dcc6 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithMultipleTestProperties.java @@ -0,0 +1,30 @@ +/* + * 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.env.repeatable; + +import org.springframework.test.context.TestPropertySource; + +/** + * Abstract parent class with multiple properties definition for tests. + * + * @author Anatoliy Korovin + * @since 5.2 + */ +@TestPropertySource(properties = "first = value from parent class") +@TestPropertySource(properties = "second = value from parent class") +public abstract class ParentClassWithMultipleTestProperties { +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithTestProperties.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithTestProperties.java index 6d66de2ec844..86f792d979a1 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithTestProperties.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/ParentClassWithTestProperties.java @@ -18,6 +18,13 @@ import org.springframework.test.context.TestPropertySource; + +/** + * Base class which declare a property by the {@link TestPropertySource} annotation. + * + * @author Anatoliy Korovin + * @since 5.2 + */ @TestPropertySource(properties = "inherited = 12345") -public class ParentClassWithTestProperties { +public abstract class ParentClassWithTestProperties { } diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationOverridesLocallyTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationOverridesLocallyTests.java new file mode 100644 index 000000000000..4486f0b19aca --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationOverridesLocallyTests.java @@ -0,0 +1,56 @@ +/* + * 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.env.repeatable; + + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@TestPropertySource(properties = "meta = local value") +@AnnotationWithTestProperty +public class TestPropertySourceInheritedFromMetaAnnotationOverridesLocallyTests { + + @Autowired + Environment env; + + @Value("${meta}") + String meta; + + + @Test + public void inlineLocalPropertyAndPropertyFromMetaAnnotation() { + assertThat(env.getProperty("meta")).isEqualTo("local value"); + assertThat(meta).isEqualTo("local value"); + } + + + @Configuration + static class Config { + } +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationWithPropertiesFileTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationWithPropertiesFileTests.java new file mode 100644 index 000000000000..3acc5c270f4d --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationWithPropertiesFileTests.java @@ -0,0 +1,64 @@ +/* + * 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.env.repeatable; + + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@TestPropertySource("local.properties") +@AnnotationWithTestPropertyInPropertiesFile +public class TestPropertySourceInheritedFromMetaAnnotationWithPropertiesFileTests { + + @Autowired + Environment env; + + @Value("${key}") + String key; + + @Value("${meta}") + String meta; + + + @Test + public void inlinePropertyFromParentClassAndFromLocalTestPropertySourceAnnotation() { + assertPropertyValue("key", key, "local value"); + assertPropertyValue("meta", meta, "a value from file in the meta-annotation"); + } + + private void assertPropertyValue(String name, String value, String expectedValue) { + assertThat(env.getProperty(name)).isEqualTo(expectedValue); + assertThat(value).isEqualTo(expectedValue); + } + + + @Configuration + static class Config { + } +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceOverridesInheritedPropertyTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceOverridesInheritedPropertyTests.java new file mode 100644 index 000000000000..a9ba4b90b4d9 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceOverridesInheritedPropertyTests.java @@ -0,0 +1,55 @@ +/* + * 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.env.repeatable; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@TestPropertySource(properties = "inherited = local value") +public class TestPropertySourceOverridesInheritedPropertyTests extends ParentClassWithTestProperties { + + @Autowired + Environment env; + + @Value("${inherited}") + String inherited; + + + @Test + public void inlinePropertyFromParentClassAndFromLocalTestPropertySourceAnnotation() { + assertThat(env.getProperty("inherited")).isEqualTo("local value"); + assertThat(inherited).isEqualTo("local value"); + } + + + @Configuration + static class Config { + } +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourcePartialOverridesInheritedPropertyTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourcePartialOverridesInheritedPropertyTests.java new file mode 100644 index 000000000000..b84709b69b5a --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourcePartialOverridesInheritedPropertyTests.java @@ -0,0 +1,55 @@ +/* + * 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.env.repeatable; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@TestPropertySource(properties = "second = local value") +public class TestPropertySourcePartialOverridesInheritedPropertyTests extends ParentClassWithMultipleTestProperties { + + @Value("${first}") + String first; + + @Value("${second}") + String second; + + + @Test + public void inlinePropertyFromParentClassAndFromLocalTestPropertySourceAnnotation() { + assertThat(first).isEqualTo("value from parent class"); + assertThat(second).isEqualTo("local value"); + } + + + @Configuration + static class Config { + } +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithDefaultPropertiesFileTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithDefaultPropertiesFileTests.java new file mode 100644 index 000000000000..f4422dd0006b --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithDefaultPropertiesFileTests.java @@ -0,0 +1,63 @@ +/* + * 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.env.repeatable; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@TestPropertySource +@TestPropertySource("local.properties") +public class TestPropertySourceRepeatableWithDefaultPropertiesFileTests { + + @Autowired + Environment env; + + @Value("${key}") + String key; + + @Value("${default.value}") + String defaultValue; + + @Test + public void inlinePropertyFromParentClassAndFromLocalTestPropertySourceAnnotation() { + assertPropertyValue("key", key, "local value"); + assertPropertyValue("default.value", defaultValue, "a value from default properties file"); + } + + private void assertPropertyValue(String name, String value, String expectedValue) { + assertThat(env.getProperty(name)).isEqualTo(expectedValue); + assertThat(value).isEqualTo(expectedValue); + } + + + @Configuration + static class Config { + } +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithPropertiesFileTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithPropertiesFileTests.java new file mode 100644 index 000000000000..6a0789a3654f --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithPropertiesFileTests.java @@ -0,0 +1,64 @@ +/* + * 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.env.repeatable; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@TestPropertySource("first.properties") +@TestPropertySource("second.properties") +public class TestPropertySourceRepeatableWithPropertiesFileTests { + + @Autowired + Environment env; + + @Value("${first}") + String first; + + @Value("${second}") + String second; + + + @Test + public void inlinePropertyFromParentClassAndFromLocalTestPropertySourceAnnotation() { + assertPropertyValue("first", first, "1111"); + assertPropertyValue("second", second, "2222"); + } + + private void assertPropertyValue(String name, String value, String expectedValue) { + assertThat(env.getProperty(name)).isEqualTo(expectedValue); + assertThat(value).isEqualTo(expectedValue); + } + + + @Configuration + static class Config { + } +} diff --git a/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithDefaultPropertiesFileTests.properties b/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithDefaultPropertiesFileTests.properties new file mode 100644 index 000000000000..ca6ec5bbb483 --- /dev/null +++ b/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithDefaultPropertiesFileTests.properties @@ -0,0 +1 @@ +default.value = a value from default properties file \ No newline at end of file diff --git a/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/first.properties b/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/first.properties new file mode 100644 index 000000000000..2330dfc6148d --- /dev/null +++ b/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/first.properties @@ -0,0 +1 @@ +first = 1111 \ No newline at end of file diff --git a/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/local.properties b/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/local.properties new file mode 100644 index 000000000000..7bbf2a52d931 --- /dev/null +++ b/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/local.properties @@ -0,0 +1 @@ +key = local value \ No newline at end of file diff --git a/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/meta.properties b/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/meta.properties new file mode 100644 index 000000000000..ca8c37957726 --- /dev/null +++ b/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/meta.properties @@ -0,0 +1 @@ +meta = a value from file in the meta-annotation \ No newline at end of file diff --git a/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/second.properties b/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/second.properties new file mode 100644 index 000000000000..722f3912aac6 --- /dev/null +++ b/spring-test/src/test/resources/org/springframework/test/context/env/repeatable/second.properties @@ -0,0 +1 @@ +second = 2222 \ No newline at end of file From c170628e87910156352bae84d14dfc00e2ee07a8 Mon Sep 17 00:00:00 2001 From: Anatoliy Korovin Date: Sun, 21 Jul 2019 11:30:56 +1000 Subject: [PATCH 6/7] add documentation in tests --- .../repeatable/TestPropertySourceInheritTests.java | 11 ++++++++++- ...tedFromMetaAnnotationOverridesLocallyTests.java | 11 +++++++++++ ...ertySourceInheritedFromMetaAnnotationTests.java | 10 ++++++++++ ...dFromMetaAnnotationWithPropertiesFileTests.java | 12 ++++++++++++ ...pertySourceOverridesInheritedPropertyTests.java | 11 ++++++++++- ...urcePartialOverridesInheritedPropertyTests.java | 14 +++++++++++--- .../TestPropertySourceRepeatableTests.java | 11 ++++++++++- ...ceRepeatableWithDefaultPropertiesFileTests.java | 12 +++++++++++- ...rtySourceRepeatableWithPropertiesFileTests.java | 11 ++++++++++- 9 files changed, 95 insertions(+), 8 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritTests.java index 32882ddceeca..16b59c6d1144 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritTests.java @@ -30,7 +30,16 @@ import static org.assertj.core.api.Assertions.assertThat; - +/** + * Integration tests for support {@link TestPropertySource @TestPropertySource} as a + * repeatable annotation. + * + * Test a property definition by the using of {@link TestPropertySource} both in the + * parent class and locally. + * + * @author Anatoliy Korovin + * @since 5.2 + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @TestPropertySource(properties = "key = 051187") diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationOverridesLocallyTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationOverridesLocallyTests.java index 4486f0b19aca..6787bd8ad909 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationOverridesLocallyTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationOverridesLocallyTests.java @@ -30,6 +30,17 @@ import static org.assertj.core.api.Assertions.assertThat; +/** + * Integration tests for support {@link TestPropertySource @TestPropertySource} as a + * repeatable annotation. + * + * This test verifies an overriding of the property value which declared in the + * meta-annotation by the {@link TestPropertySource} when this property is also defined + * locally in {@link TestPropertySource}. + * + * @author Anatoliy Korovin + * @since 5.2 + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @TestPropertySource(properties = "meta = local value") diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationTests.java index adfb148a8617..69bbd72af56a 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationTests.java @@ -30,6 +30,16 @@ import static org.assertj.core.api.Assertions.assertThat; +/** + * Integration tests for support {@link TestPropertySource @TestPropertySource} as a + * repeatable annotation. + * + * This test verifies a declaration of properties by the {@link TestPropertySource} both + * locally and in the custom meta-annotation. + * + * @author Anatoliy Korovin + * @since 5.2 + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @TestPropertySource(properties = "key = 051187") diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationWithPropertiesFileTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationWithPropertiesFileTests.java index 3acc5c270f4d..7954d92cb482 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationWithPropertiesFileTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceInheritedFromMetaAnnotationWithPropertiesFileTests.java @@ -30,6 +30,18 @@ import static org.assertj.core.api.Assertions.assertThat; +/** + * Integration tests for support {@link TestPropertySource @TestPropertySource} as a + * repeatable annotation. + * + * Verify a property value defined both in the properties file which declared in the + * custom annotation {@link AnnotationWithTestPropertyInPropertiesFile} and a definition + * of property by the local usage of {@link TestPropertySource} with a properties file + * name. + * + * @author Anatoliy Korovin + * @since 5.2 + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @TestPropertySource("local.properties") diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceOverridesInheritedPropertyTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceOverridesInheritedPropertyTests.java index a9ba4b90b4d9..6d77ed737c5e 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceOverridesInheritedPropertyTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceOverridesInheritedPropertyTests.java @@ -29,7 +29,16 @@ import static org.assertj.core.api.Assertions.assertThat; - +/** + * Integration tests for support {@link TestPropertySource @TestPropertySource} as a + * repeatable annotation. + * + * Verify the overriding of property which defined both in the parent class and locally in + * the {@link TestPropertySource} annotation. + * + * @author Anatoliy Korovin + * @since 5.2 + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @TestPropertySource(properties = "inherited = local value") diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourcePartialOverridesInheritedPropertyTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourcePartialOverridesInheritedPropertyTests.java index b84709b69b5a..f59aa073f886 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourcePartialOverridesInheritedPropertyTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourcePartialOverridesInheritedPropertyTests.java @@ -19,17 +19,25 @@ import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.Environment; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.assertj.core.api.Assertions.assertThat; - +/** + * Integration tests for support {@link TestPropertySource @TestPropertySource} as a + * repeatable annotation. + * + * Verify the overriding of property which defined both in the parent class and locally in + * the {@link TestPropertySource} annotation. Also, verify that the value of not + * conflicted properties is applied from the parent class. + * + * @author Anatoliy Korovin + * @since 5.2 + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @TestPropertySource(properties = "second = local value") diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableTests.java index db443dfb310c..0e9b5f4165d1 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableTests.java @@ -29,7 +29,16 @@ import static org.assertj.core.api.Assertions.assertThat; - +/** + * Integration tests for support {@link TestPropertySource @TestPropertySource} as a + * repeatable annotation. + * + * Test multiple test property declarations by the using of {@link TestPropertySource} as + * a repeatable annotation. + * + * @author Anatoliy Korovin + * @since 5.2 + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @TestPropertySource(properties = "first = 1111") diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithDefaultPropertiesFileTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithDefaultPropertiesFileTests.java index f4422dd0006b..cac36f6b3fcd 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithDefaultPropertiesFileTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithDefaultPropertiesFileTests.java @@ -29,7 +29,17 @@ import static org.assertj.core.api.Assertions.assertThat; - +/** + * Integration tests for support {@link TestPropertySource @TestPropertySource} as a + * repeatable annotation. + * + * Verify a repeatable usage of {@link TestPropertySource} both with a default value of + * properties file and with a specified properties file name in the + * {@link TestPropertySource} annotation. + * + * @author Anatoliy Korovin + * @since 5.2 + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @TestPropertySource diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithPropertiesFileTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithPropertiesFileTests.java index 6a0789a3654f..c3932af07eb7 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithPropertiesFileTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableWithPropertiesFileTests.java @@ -29,7 +29,16 @@ import static org.assertj.core.api.Assertions.assertThat; - +/** + * Integration tests for support {@link TestPropertySource @TestPropertySource} as a + * repeatable annotation. + * + * Test multiple test properties file declarations by the using of {@link TestPropertySource} as + * a repeatable annotation. + * + * @author Anatoliy Korovin + * @since 5.2 + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @TestPropertySource("first.properties") From 5bef2fc44fe356caa21c2e5b19fe88c6de2b883e Mon Sep 17 00:00:00 2001 From: Anatoliy Korovin Date: Sun, 21 Jul 2019 12:16:47 +1000 Subject: [PATCH 7/7] Add one more test case of TestPropertySource overriding --- .../env/repeatable/FooTestProperty.java | 37 ++++++++++++ .../FooTestPropertyDeclaration.java | 29 ++++++++++ ...ropertySourceRepeatableOverridesTests.java | 58 +++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/FooTestProperty.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/FooTestPropertyDeclaration.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableOverridesTests.java diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/FooTestProperty.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/FooTestProperty.java new file mode 100644 index 000000000000..94a48f6507ef --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/FooTestProperty.java @@ -0,0 +1,37 @@ +/* + * 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.env.repeatable; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.test.context.TestPropertySource; + +/** + * A custom annotation with foo property defined by the {@link TestPropertySource}. + * + * @author Anatoliy Korovin + * @since 5.2 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@TestPropertySource(properties = "foo = value from meta-annotation") +public @interface FooTestProperty { +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/FooTestPropertyDeclaration.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/FooTestPropertyDeclaration.java new file mode 100644 index 000000000000..a5237c647e90 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/FooTestPropertyDeclaration.java @@ -0,0 +1,29 @@ +/* + * 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.env.repeatable; + +import org.springframework.test.context.TestPropertySource; + +/** + * Abstract parent class with foo property definition for tests. + * + * @author Anatoliy Korovin + * @since 5.2 + */ +@TestPropertySource(properties = "foo = value from parent class") +public abstract class FooTestPropertyDeclaration { +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableOverridesTests.java b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableOverridesTests.java new file mode 100644 index 000000000000..268238518103 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/repeatable/TestPropertySourceRepeatableOverridesTests.java @@ -0,0 +1,58 @@ +/* + * 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.env.repeatable; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration tests for support {@link TestPropertySource @TestPropertySource} as a + * repeatable annotation. + * + * Verify an overriding of a property value which defined both in custom annotation + * and in the parent class, when this property declares locally by the + * {@link TestPropertySource}. + * + * @author Anatoliy Korovin + * @since 5.2 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@FooTestProperty +@TestPropertySource(properties = "foo = local value") +public class TestPropertySourceRepeatableOverridesTests extends FooTestPropertyDeclaration { + + @Value("${foo}") + String foo; + + @Test + public void inlinePropertyFromParentClassAndFromLocalTestPropertySourceAnnotation() { + assertThat(foo).isEqualTo("local value"); + } + + @Configuration + static class Config { + } +}