/* * 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.support; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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; import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.ResourceUtils; import org.springframework.util.StringUtils; /** * {@code TestPropertySourceAttributes} encapsulates attributes declared * via {@link TestPropertySource @TestPropertySource} annotations. * *
In addition to encapsulating declared attributes,
* {@code TestPropertySourceAttributes} also enforces configuration rules.
*
* @author Sam Brannen
* @author Phillip Webb
* @since 4.1
* @see TestPropertySource
* @see MergedTestPropertySources
*/
class TestPropertySourceAttributes {
private static final Log logger = LogFactory.getLog(TestPropertySourceAttributes.class);
private final int aggregateIndex;
private final Class> declaringClass;
private final MergedAnnotation> rootAnnotation;
private final List This method effectively checks that two annotations are declared at
* the same level in the type hierarchy (i.e., have the same
* {@linkplain MergedAnnotation#getAggregateIndex() aggregate index}).
* @since 5.2
* @see #mergeWith(MergedAnnotation)
*/
boolean canMergeWith(MergedAnnotation This method should only be invoked if {@link #canMergeWith(MergedAnnotation)}
* returns {@code true}.
* @since 5.2
* @see #canMergeWith(MergedAnnotation)
*/
void mergeWith(MergedAnnotation If the {@code prepend} flag is {@code false}, the elements will appended
* to the list.
* @param prepend whether the elements should be prepended to the list
* @param list the list to which to add the elements
* @param elements the elements to add to the list
*/
private void addAll(boolean prepend, List Note: The returned value may represent a detected default
* or merged locations that do not match the original value declared via a
* single {@code @TestPropertySource} annotation.
* @return the resource locations; potentially empty
* @see TestPropertySource#value
* @see TestPropertySource#locations
*/
String[] getLocations() {
return StringUtils.toStringArray(this.locations);
}
/**
* Get the {@code inheritLocations} flag that was declared via {@code @TestPropertySource}.
* @return the {@code inheritLocations} flag
* @see TestPropertySource#inheritLocations
*/
boolean isInheritLocations() {
return this.inheritLocations;
}
/**
* Get the inlined properties that were declared via {@code @TestPropertySource}.
* Note: The returned value may represent merged properties that do not
* match the original value declared via a single {@code @TestPropertySource}
* annotation.
* @return the inlined properties; potentially empty
* @see TestPropertySource#properties
*/
String[] getProperties() {
return StringUtils.toStringArray(this.properties);
}
/**
* Get the {@code inheritProperties} flag that was declared via {@code @TestPropertySource}.
* @return the {@code inheritProperties} flag
* @see TestPropertySource#inheritProperties
*/
boolean isInheritProperties() {
return this.inheritProperties;
}
/**
* Provide a String representation of the {@code @TestPropertySource}
* attributes and declaring class.
*/
@Override
public String toString() {
return new ToStringCreator(this)
.append("declaringClass", this.declaringClass.getName())
.append("locations", this.locations)
.append("inheritLocations", this.inheritLocations)
.append("properties", this.properties)
.append("inheritProperties", this.inheritProperties)
.toString();
}
private static Class> declaringClass(MergedAnnotation> mergedAnnotation) {
Object source = mergedAnnotation.getSource();
Assert.state(source instanceof Class, "No source class available");
return (Class>) source;
}
}