Simplify implementation of DefaultActiveProfilesResolver

This commit is contained in:
Sam Brannen
2020-10-26 13:53:23 +01:00
parent 9a4a593c7f
commit 9287c15583

View File

@@ -16,9 +16,6 @@
package org.springframework.test.context.support;
import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -26,7 +23,6 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ActiveProfilesResolver;
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import static org.springframework.test.context.TestContextAnnotationUtils.findAnnotationDescriptor;
@@ -43,6 +39,8 @@ import static org.springframework.test.context.TestContextAnnotationUtils.findAn
*/
public class DefaultActiveProfilesResolver implements ActiveProfilesResolver {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private static final Log logger = LogFactory.getLog(DefaultActiveProfilesResolver.class);
@@ -58,36 +56,24 @@ public class DefaultActiveProfilesResolver implements ActiveProfilesResolver {
@Override
public String[] resolve(Class<?> testClass) {
Assert.notNull(testClass, "Class must not be null");
Set<String> activeProfiles = new TreeSet<>();
Class<ActiveProfiles> annotationType = ActiveProfiles.class;
AnnotationDescriptor<ActiveProfiles> descriptor = findAnnotationDescriptor(testClass, annotationType);
AnnotationDescriptor<ActiveProfiles> descriptor = findAnnotationDescriptor(testClass, ActiveProfiles.class);
if (descriptor == null) {
if (logger.isDebugEnabled()) {
logger.debug(String.format(
"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
annotationType.getName(), testClass.getName()));
ActiveProfiles.class.getName(), testClass.getName()));
}
return EMPTY_STRING_ARRAY;
}
else {
Class<?> declaringClass = descriptor.getDeclaringClass();
ActiveProfiles annotation = descriptor.synthesizeAnnotation();
if (logger.isTraceEnabled()) {
logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s].", annotation,
declaringClass.getName()));
}
for (String profile : annotation.profiles()) {
if (StringUtils.hasText(profile)) {
activeProfiles.add(profile.trim());
}
descriptor.getDeclaringClass().getName()));
}
return annotation.profiles();
}
return StringUtils.toStringArray(activeProfiles);
}
}