Skip to content

Commit 2e476ca

Browse files
antkorwinsbrannen
authored andcommitted
Support @TestPropertySource as a repeatable annotation
Prior to this commit, @TestPropertySource could not be declared as a repeatable annotation. In addition, a local declaration of @TestPropertySource would silently override a meta-present @TestPropertySource. This commit addresses this issue by introducing @TestPropertySources as a container for @TestPropertySource. This commit also updates the search and algorithms within TestPropertySourceUtils. Closes gh-23320
1 parent 8574f97 commit 2e476ca

24 files changed

+992
-30
lines changed

spring-test/src/main/java/org/springframework/test/context/TestPropertySource.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.lang.annotation.Documented;
2020
import java.lang.annotation.ElementType;
2121
import java.lang.annotation.Inherited;
22+
import java.lang.annotation.Repeatable;
2223
import java.lang.annotation.Retention;
2324
import java.lang.annotation.RetentionPolicy;
2425
import java.lang.annotation.Target;
@@ -86,6 +87,7 @@
8687
@Retention(RetentionPolicy.RUNTIME)
8788
@Documented
8889
@Inherited
90+
@Repeatable(TestPropertySources.class)
8991
public @interface TestPropertySource {
9092

9193
/**
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context;
18+
19+
20+
import java.lang.annotation.Documented;
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Inherited;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
26+
27+
28+
/**
29+
* {@code @TestPropertySources} is a container for one or more {@link TestPropertySource}
30+
* declarations.
31+
*
32+
* <p>Note, however, that use of the {@code @TestPropertySources} container is completely
33+
* optional since {@code @TestPropertySource} is a {@linkplain java.lang.annotation.Repeatable
34+
* repeatable} annotation.
35+
*
36+
* @author Anatoliy Korovin
37+
* @since 5.2
38+
*/
39+
@Target(ElementType.TYPE)
40+
@Retention(RetentionPolicy.RUNTIME)
41+
@Documented
42+
@Inherited
43+
public @interface TestPropertySources {
44+
45+
/**
46+
* An array of one or more {@link TestPropertySource} declarations.
47+
*
48+
* @return array of {@link TestPropertySource} values.
49+
*/
50+
TestPropertySource[] value();
51+
}

spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,11 +24,14 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Properties;
27+
import java.util.stream.Collectors;
2728

2829
import org.apache.commons.logging.Log;
2930
import org.apache.commons.logging.LogFactory;
3031

3132
import org.springframework.context.ConfigurableApplicationContext;
33+
import org.springframework.core.annotation.MergedAnnotation;
34+
import org.springframework.core.annotation.MergedAnnotations;
3235
import org.springframework.core.env.ConfigurableEnvironment;
3336
import org.springframework.core.env.Environment;
3437
import org.springframework.core.env.MapPropertySource;
@@ -39,20 +42,18 @@
3942
import org.springframework.core.io.support.ResourcePropertySource;
4043
import org.springframework.test.context.TestPropertySource;
4144
import org.springframework.test.context.util.TestContextResourceUtils;
42-
import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor;
4345
import org.springframework.util.Assert;
4446
import org.springframework.util.ObjectUtils;
4547
import org.springframework.util.StringUtils;
4648

47-
import static org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptor;
48-
4949
/**
5050
* Utility methods for working with {@link TestPropertySource @TestPropertySource}
5151
* and adding test {@link PropertySource PropertySources} to the {@code Environment}.
5252
*
5353
* <p>Primarily intended for use within the framework.
5454
*
5555
* @author Sam Brannen
56+
* @author Anatoliy Korovin
5657
* @since 4.1
5758
* @see TestPropertySource
5859
*/
@@ -67,47 +68,59 @@ public abstract class TestPropertySourceUtils {
6768

6869
private static final Log logger = LogFactory.getLog(TestPropertySourceUtils.class);
6970

70-
7171
static MergedTestPropertySources buildMergedTestPropertySources(Class<?> testClass) {
72-
Class<TestPropertySource> annotationType = TestPropertySource.class;
73-
AnnotationDescriptor<TestPropertySource> descriptor = findAnnotationDescriptor(testClass, annotationType);
74-
if (descriptor == null) {
72+
73+
if (!isPresentTestPropertySourceAnnotation(testClass)) {
7574
return new MergedTestPropertySources();
7675
}
76+
else {
77+
return mergeTestPropertySources(testClass);
78+
}
79+
}
80+
81+
private static boolean isPresentTestPropertySourceAnnotation(Class<?> testClass) {
82+
return MergedAnnotations
83+
.from(testClass, MergedAnnotations.SearchStrategy.EXHAUSTIVE)
84+
.get(TestPropertySource.class).isPresent();
85+
}
86+
87+
private static MergedTestPropertySources mergeTestPropertySources(Class<?> testClass) {
88+
89+
List<TestPropertySourceAttributes> attributesList = resolveTestPropertySourceAttributes(
90+
testClass);
7791

78-
List<TestPropertySourceAttributes> attributesList = resolveTestPropertySourceAttributes(testClass);
7992
String[] locations = mergeLocations(attributesList);
8093
String[] properties = mergeProperties(attributesList);
94+
8195
return new MergedTestPropertySources(locations, properties);
8296
}
8397

8498
private static List<TestPropertySourceAttributes> resolveTestPropertySourceAttributes(Class<?> testClass) {
8599
Assert.notNull(testClass, "Class must not be null");
86-
List<TestPropertySourceAttributes> attributesList = new ArrayList<>();
87-
Class<TestPropertySource> annotationType = TestPropertySource.class;
100+
return MergedAnnotations
101+
.from(testClass, MergedAnnotations.SearchStrategy.EXHAUSTIVE)
102+
.stream(TestPropertySource.class)
103+
.map(TestPropertySourceUtils::makeTestPropertySourceAttribute)
104+
.collect(Collectors.toList());
105+
}
88106

89-
AnnotationDescriptor<TestPropertySource> descriptor = findAnnotationDescriptor(testClass, annotationType);
90-
Assert.notNull(descriptor, String.format(
91-
"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
92-
annotationType.getName(), testClass.getName()));
107+
private static TestPropertySourceAttributes makeTestPropertySourceAttribute(
108+
MergedAnnotation<TestPropertySource> annotation) {
93109

94-
while (descriptor != null) {
95-
TestPropertySource testPropertySource = descriptor.synthesizeAnnotation();
96-
Class<?> rootDeclaringClass = descriptor.getRootDeclaringClass();
97-
if (logger.isTraceEnabled()) {
98-
logger.trace(String.format("Retrieved @TestPropertySource [%s] for declaring class [%s].",
110+
TestPropertySource testPropertySource = annotation.synthesize();
111+
Class<?> rootDeclaringClass = (Class<?>) annotation.getSource();
112+
if (logger.isTraceEnabled()) {
113+
logger.trace(String.format(
114+
"Retrieved @TestPropertySource [%s] for declaring class [%s].",
99115
testPropertySource, rootDeclaringClass.getName()));
100-
}
101-
TestPropertySourceAttributes attributes =
102-
new TestPropertySourceAttributes(rootDeclaringClass, testPropertySource);
103-
if (logger.isTraceEnabled()) {
104-
logger.trace("Resolved TestPropertySource attributes: " + attributes);
105-
}
106-
attributesList.add(attributes);
107-
descriptor = findAnnotationDescriptor(rootDeclaringClass.getSuperclass(), annotationType);
108116
}
109117

110-
return attributesList;
118+
TestPropertySourceAttributes attributes = new TestPropertySourceAttributes(
119+
rootDeclaringClass, testPropertySource);
120+
if (logger.isTraceEnabled()) {
121+
logger.trace("Resolved TestPropertySource attributes: " + attributes);
122+
}
123+
return attributes;
111124
}
112125

113126
private static String[] mergeLocations(List<TestPropertySourceAttributes> attributesList) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.env.repeatable;
18+
19+
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.test.context.TestPropertySource;
26+
27+
/**
28+
* A custom annotation with properties defined by the {@link TestPropertySource}.
29+
*
30+
* @author Anatoliy Korovin
31+
* @since 5.2
32+
*/
33+
@Target(ElementType.TYPE)
34+
@Retention(RetentionPolicy.RUNTIME)
35+
@TestPropertySource(properties = "meta = value from meta-annotation")
36+
public @interface AnnotationWithTestProperty {
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.env.repeatable;
18+
19+
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.test.context.TestPropertySource;
26+
27+
/**
28+
* A custom annotation which defined properties file in the {@link TestPropertySource}.
29+
*
30+
* @author Anatoliy Korovin
31+
* @since 5.2
32+
*/
33+
@Target(ElementType.TYPE)
34+
@Retention(RetentionPolicy.RUNTIME)
35+
@TestPropertySource("meta.properties")
36+
public @interface AnnotationWithTestPropertyInPropertiesFile {
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.env.repeatable;
18+
19+
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.test.context.TestPropertySource;
26+
27+
/**
28+
* A custom annotation with foo property defined by the {@link TestPropertySource}.
29+
*
30+
* @author Anatoliy Korovin
31+
* @since 5.2
32+
*/
33+
@Target(ElementType.TYPE)
34+
@Retention(RetentionPolicy.RUNTIME)
35+
@TestPropertySource(properties = "foo = value from meta-annotation")
36+
public @interface FooTestProperty {
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.env.repeatable;
18+
19+
import org.springframework.test.context.TestPropertySource;
20+
21+
/**
22+
* Abstract parent class with foo property definition for tests.
23+
*
24+
* @author Anatoliy Korovin
25+
* @since 5.2
26+
*/
27+
@TestPropertySource(properties = "foo = value from parent class")
28+
public abstract class FooTestPropertyDeclaration {
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.env.repeatable;
18+
19+
import org.springframework.test.context.TestPropertySource;
20+
21+
/**
22+
* Abstract parent class with multiple properties definition for tests.
23+
*
24+
* @author Anatoliy Korovin
25+
* @since 5.2
26+
*/
27+
@TestPropertySource(properties = "first = value from parent class")
28+
@TestPropertySource(properties = "second = value from parent class")
29+
public abstract class ParentClassWithMultipleTestProperties {
30+
}

0 commit comments

Comments
 (0)