Skip to content

Commit

Permalink
Remove validation from TestPropertySourceAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Oct 12, 2020
1 parent b963d49 commit e9d0dd1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.log.LogMessage;
import org.springframework.core.style.ToStringCreator;
import org.springframework.test.context.TestPropertySource;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -54,8 +53,6 @@ class TestPropertySourceAttributes {

private final Class<?> declaringClass;

private final MergedAnnotation<?> rootAnnotation;

private final List<String> locations = new ArrayList<>();

private final boolean inheritLocations;
Expand All @@ -67,48 +64,11 @@ class TestPropertySourceAttributes {

TestPropertySourceAttributes(MergedAnnotation<TestPropertySource> annotation) {
this.declaringClass = declaringClass(annotation);
this.rootAnnotation = annotation.getRoot();
this.inheritLocations = annotation.getBoolean("inheritLocations");
this.inheritProperties = annotation.getBoolean("inheritProperties");
mergePropertiesAndLocations(annotation);
}

/**
* Merge this {@code TestPropertySourceAttributes} instance with the
* supplied {@code annotation}, asserting that the two sets of test property
* source attributes have identical values for the
* {@link TestPropertySource#inheritLocations} and
* {@link TestPropertySource#inheritProperties} flags and that the two
* underlying annotations were declared on the same class.
* <p>This method should only be invoked if {@link #canMergeWith(MergedAnnotation)}
* returns {@code true}.
* @since 5.2
* @see #canMergeWith(MergedAnnotation)
*/
void mergeWith(MergedAnnotation<TestPropertySource> annotation) {
Class<?> source = declaringClass(annotation);
Assert.state(source == this.declaringClass,
() -> "Detected @TestPropertySource declarations within an aggregate index "
+ "with different sources: " + this.declaringClass.getName() + " and "
+ source.getName());
logger.trace(LogMessage.format("Retrieved %s for declaring class [%s].",
annotation, this.declaringClass.getName()));
assertSameBooleanAttribute(this.inheritLocations, annotation, "inheritLocations");
assertSameBooleanAttribute(this.inheritProperties, annotation, "inheritProperties");
mergePropertiesAndLocations(annotation);
}

private void assertSameBooleanAttribute(boolean expected, MergedAnnotation<TestPropertySource> annotation,
String attribute) {

Assert.isTrue(expected == annotation.getBoolean(attribute), () -> String.format(
"@%s on %s and @%s on %s must declare the same value for '%s' as other " +
"directly present or meta-present @TestPropertySource annotations",
this.rootAnnotation.getType().getSimpleName(), this.declaringClass.getSimpleName(),
annotation.getRoot().getType().getSimpleName(), declaringClass(annotation).getSimpleName(),
attribute));
}

private void mergePropertiesAndLocations(MergedAnnotation<TestPropertySource> annotation) {
String[] locations = annotation.getStringArray("locations");
String[] properties = annotation.getStringArray("properties");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ public static Map<String, Object> convertInlinedPropertiesToMap(String... inline
return map;
}

private static <T extends Annotation> List<MergedAnnotation<T>> findRepeatableAnnotations(Class<?> clazz, Class<T> annotationType) {
private static <T extends Annotation> List<MergedAnnotation<T>> findRepeatableAnnotations(
Class<?> clazz, Class<T> annotationType) {

List<List<MergedAnnotation<T>>> listOfLists = new ArrayList<>();
findRepeatableAnnotations(clazz, annotationType, listOfLists, new int[] {0});
return listOfLists.stream().flatMap(List::stream).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void extendedEmptyAnnotation() {
}

@Test
@Disabled("Validation for repeated @TestPropertySource annotations is currently not working")
@Disabled("Validation for repeated @TestPropertySource annotations has been removed")
void repeatedTestPropertySourcesWithConflictingInheritLocationsFlags() {
assertThatIllegalArgumentException()
.isThrownBy(() -> buildMergedTestPropertySources(RepeatedPropertySourcesWithConflictingInheritLocationsFlags.class))
Expand All @@ -91,7 +91,7 @@ void repeatedTestPropertySourcesWithConflictingInheritLocationsFlags() {
}

@Test
@Disabled("Validation for repeated @TestPropertySource annotations is currently not working")
@Disabled("Validation for repeated @TestPropertySource annotations has been removed")
void repeatedTestPropertySourcesWithConflictingInheritPropertiesFlags() {
assertThatIllegalArgumentException()
.isThrownBy(() -> buildMergedTestPropertySources(RepeatedPropertySourcesWithConflictingInheritPropertiesFlags.class))
Expand Down

0 comments on commit e9d0dd1

Please sign in to comment.