|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2013 the original author or authors. |
|
|
|
* Copyright 2002-2016 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -26,8 +26,6 @@ import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.ObjectUtils; |
|
|
|
import org.springframework.util.ObjectUtils; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
|
|
|
|
|
|
import static org.springframework.core.annotation.AnnotationUtils.*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* General utility methods for working with <em>profile values</em>. |
|
|
|
* General utility methods for working with <em>profile values</em>. |
|
|
|
* |
|
|
|
* |
|
|
|
@ -49,12 +47,10 @@ public abstract class ProfileValueUtils { |
|
|
|
* {@link ProfileValueSourceConfiguration |
|
|
|
* {@link ProfileValueSourceConfiguration |
|
|
|
* @ProfileValueSourceConfiguration} annotation and instantiates a new |
|
|
|
* @ProfileValueSourceConfiguration} annotation and instantiates a new |
|
|
|
* instance of that type. |
|
|
|
* instance of that type. |
|
|
|
* <p> |
|
|
|
* <p>If {@link ProfileValueSourceConfiguration |
|
|
|
* If {@link ProfileValueSourceConfiguration |
|
|
|
|
|
|
|
* @ProfileValueSourceConfiguration} is not present on the specified |
|
|
|
* @ProfileValueSourceConfiguration} is not present on the specified |
|
|
|
* class or if a custom {@link ProfileValueSource} is not declared, the |
|
|
|
* class or if a custom {@link ProfileValueSource} is not declared, the |
|
|
|
* default {@link SystemProfileValueSource} will be returned instead. |
|
|
|
* default {@link SystemProfileValueSource} will be returned instead. |
|
|
|
* |
|
|
|
|
|
|
|
* @param testClass The test class for which the ProfileValueSource should |
|
|
|
* @param testClass The test class for which the ProfileValueSource should |
|
|
|
* be retrieved |
|
|
|
* be retrieved |
|
|
|
* @return the configured (or default) ProfileValueSource for the specified |
|
|
|
* @return the configured (or default) ProfileValueSource for the specified |
|
|
|
@ -66,10 +62,10 @@ public abstract class ProfileValueUtils { |
|
|
|
Assert.notNull(testClass, "testClass must not be null"); |
|
|
|
Assert.notNull(testClass, "testClass must not be null"); |
|
|
|
|
|
|
|
|
|
|
|
Class<ProfileValueSourceConfiguration> annotationType = ProfileValueSourceConfiguration.class; |
|
|
|
Class<ProfileValueSourceConfiguration> annotationType = ProfileValueSourceConfiguration.class; |
|
|
|
ProfileValueSourceConfiguration config = findAnnotation(testClass, annotationType); |
|
|
|
ProfileValueSourceConfiguration config = AnnotationUtils.findAnnotation(testClass, annotationType); |
|
|
|
if (logger.isDebugEnabled()) { |
|
|
|
if (logger.isDebugEnabled()) { |
|
|
|
logger.debug("Retrieved @ProfileValueSourceConfiguration [" + config + "] for test class [" |
|
|
|
logger.debug("Retrieved @ProfileValueSourceConfiguration [" + config + "] for test class [" + |
|
|
|
+ testClass.getName() + "]"); |
|
|
|
testClass.getName() + "]"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Class<? extends ProfileValueSource> profileValueSourceType; |
|
|
|
Class<? extends ProfileValueSource> profileValueSourceType; |
|
|
|
@ -80,8 +76,8 @@ public abstract class ProfileValueUtils { |
|
|
|
profileValueSourceType = (Class<? extends ProfileValueSource>) AnnotationUtils.getDefaultValue(annotationType); |
|
|
|
profileValueSourceType = (Class<? extends ProfileValueSource>) AnnotationUtils.getDefaultValue(annotationType); |
|
|
|
} |
|
|
|
} |
|
|
|
if (logger.isDebugEnabled()) { |
|
|
|
if (logger.isDebugEnabled()) { |
|
|
|
logger.debug("Retrieved ProfileValueSource type [" + profileValueSourceType + "] for class [" |
|
|
|
logger.debug("Retrieved ProfileValueSource type [" + profileValueSourceType + "] for class [" + |
|
|
|
+ testClass.getName() + "]"); |
|
|
|
testClass.getName() + "]"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ProfileValueSource profileValueSource; |
|
|
|
ProfileValueSource profileValueSource; |
|
|
|
@ -92,10 +88,10 @@ public abstract class ProfileValueUtils { |
|
|
|
try { |
|
|
|
try { |
|
|
|
profileValueSource = profileValueSourceType.newInstance(); |
|
|
|
profileValueSource = profileValueSourceType.newInstance(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception e) { |
|
|
|
catch (Exception ex) { |
|
|
|
if (logger.isWarnEnabled()) { |
|
|
|
if (logger.isWarnEnabled()) { |
|
|
|
logger.warn("Could not instantiate a ProfileValueSource of type [" + profileValueSourceType |
|
|
|
logger.warn("Could not instantiate a ProfileValueSource of type [" + profileValueSourceType + |
|
|
|
+ "] for class [" + testClass.getName() + "]: using default.", e); |
|
|
|
"] for class [" + testClass.getName() + "]: using default.", ex); |
|
|
|
} |
|
|
|
} |
|
|
|
profileValueSource = SystemProfileValueSource.getInstance(); |
|
|
|
profileValueSource = SystemProfileValueSource.getInstance(); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -108,16 +104,14 @@ public abstract class ProfileValueUtils { |
|
|
|
* Determine if the supplied {@code testClass} is <em>enabled</em> in |
|
|
|
* Determine if the supplied {@code testClass} is <em>enabled</em> in |
|
|
|
* the current environment, as specified by the {@link IfProfileValue |
|
|
|
* the current environment, as specified by the {@link IfProfileValue |
|
|
|
* @IfProfileValue} annotation at the class level. |
|
|
|
* @IfProfileValue} annotation at the class level. |
|
|
|
* <p> |
|
|
|
* <p>Defaults to {@code true} if no {@link IfProfileValue |
|
|
|
* Defaults to {@code true} if no {@link IfProfileValue |
|
|
|
|
|
|
|
* @IfProfileValue} annotation is declared. |
|
|
|
* @IfProfileValue} annotation is declared. |
|
|
|
* |
|
|
|
|
|
|
|
* @param testClass the test class
|
|
|
|
* @param testClass the test class
|
|
|
|
* @return {@code true} if the test is <em>enabled</em> in the current |
|
|
|
* @return {@code true} if the test is <em>enabled</em> in the current |
|
|
|
* environment |
|
|
|
* environment |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean isTestEnabledInThisEnvironment(Class<?> testClass) { |
|
|
|
public static boolean isTestEnabledInThisEnvironment(Class<?> testClass) { |
|
|
|
IfProfileValue ifProfileValue = findAnnotation(testClass, IfProfileValue.class); |
|
|
|
IfProfileValue ifProfileValue = AnnotationUtils.findAnnotation(testClass, IfProfileValue.class); |
|
|
|
return isTestEnabledInThisEnvironment(retrieveProfileValueSource(testClass), ifProfileValue); |
|
|
|
return isTestEnabledInThisEnvironment(retrieveProfileValueSource(testClass), ifProfileValue); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -127,10 +121,8 @@ public abstract class ProfileValueUtils { |
|
|
|
* @IfProfileValue} annotation, which may be declared on the test |
|
|
|
* @IfProfileValue} annotation, which may be declared on the test |
|
|
|
* method itself or at the class level. Class-level usage overrides |
|
|
|
* method itself or at the class level. Class-level usage overrides |
|
|
|
* method-level usage. |
|
|
|
* method-level usage. |
|
|
|
* <p> |
|
|
|
* <p>Defaults to {@code true} if no {@link IfProfileValue |
|
|
|
* Defaults to {@code true} if no {@link IfProfileValue |
|
|
|
|
|
|
|
* @IfProfileValue} annotation is declared. |
|
|
|
* @IfProfileValue} annotation is declared. |
|
|
|
* |
|
|
|
|
|
|
|
* @param testMethod the test method |
|
|
|
* @param testMethod the test method |
|
|
|
* @param testClass the test class
|
|
|
|
* @param testClass the test class
|
|
|
|
* @return {@code true} if the test is <em>enabled</em> in the current |
|
|
|
* @return {@code true} if the test is <em>enabled</em> in the current |
|
|
|
@ -146,10 +138,8 @@ public abstract class ProfileValueUtils { |
|
|
|
* @IfProfileValue} annotation, which may be declared on the test |
|
|
|
* @IfProfileValue} annotation, which may be declared on the test |
|
|
|
* method itself or at the class level. Class-level usage overrides |
|
|
|
* method itself or at the class level. Class-level usage overrides |
|
|
|
* method-level usage. |
|
|
|
* method-level usage. |
|
|
|
* <p> |
|
|
|
* <p>Defaults to {@code true} if no {@link IfProfileValue |
|
|
|
* Defaults to {@code true} if no {@link IfProfileValue |
|
|
|
|
|
|
|
* @IfProfileValue} annotation is declared. |
|
|
|
* @IfProfileValue} annotation is declared. |
|
|
|
* |
|
|
|
|
|
|
|
* @param profileValueSource the ProfileValueSource to use to determine if |
|
|
|
* @param profileValueSource the ProfileValueSource to use to determine if |
|
|
|
* the test is enabled |
|
|
|
* the test is enabled |
|
|
|
* @param testMethod the test method |
|
|
|
* @param testMethod the test method |
|
|
|
@ -160,11 +150,11 @@ public abstract class ProfileValueUtils { |
|
|
|
public static boolean isTestEnabledInThisEnvironment(ProfileValueSource profileValueSource, Method testMethod, |
|
|
|
public static boolean isTestEnabledInThisEnvironment(ProfileValueSource profileValueSource, Method testMethod, |
|
|
|
Class<?> testClass) { |
|
|
|
Class<?> testClass) { |
|
|
|
|
|
|
|
|
|
|
|
IfProfileValue ifProfileValue = findAnnotation(testClass, IfProfileValue.class); |
|
|
|
IfProfileValue ifProfileValue = AnnotationUtils.findAnnotation(testClass, IfProfileValue.class); |
|
|
|
boolean classLevelEnabled = isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue); |
|
|
|
boolean classLevelEnabled = isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue); |
|
|
|
|
|
|
|
|
|
|
|
if (classLevelEnabled) { |
|
|
|
if (classLevelEnabled) { |
|
|
|
ifProfileValue = findAnnotation(testMethod, IfProfileValue.class); |
|
|
|
ifProfileValue = AnnotationUtils.findAnnotation(testMethod, IfProfileValue.class); |
|
|
|
return isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue); |
|
|
|
return isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -175,7 +165,6 @@ public abstract class ProfileValueUtils { |
|
|
|
* Determine if the {@code value} (or one of the {@code values}) |
|
|
|
* Determine if the {@code value} (or one of the {@code values}) |
|
|
|
* in the supplied {@link IfProfileValue @IfProfileValue} annotation is |
|
|
|
* in the supplied {@link IfProfileValue @IfProfileValue} annotation is |
|
|
|
* <em>enabled</em> in the current environment. |
|
|
|
* <em>enabled</em> in the current environment. |
|
|
|
* |
|
|
|
|
|
|
|
* @param profileValueSource the ProfileValueSource to use to determine if |
|
|
|
* @param profileValueSource the ProfileValueSource to use to determine if |
|
|
|
* the test is enabled |
|
|
|
* the test is enabled |
|
|
|
* @param ifProfileValue the annotation to introspect; may be |
|
|
|
* @param ifProfileValue the annotation to introspect; may be |
|
|
|
@ -195,8 +184,8 @@ public abstract class ProfileValueUtils { |
|
|
|
String[] annotatedValues = ifProfileValue.values(); |
|
|
|
String[] annotatedValues = ifProfileValue.values(); |
|
|
|
if (StringUtils.hasLength(ifProfileValue.value())) { |
|
|
|
if (StringUtils.hasLength(ifProfileValue.value())) { |
|
|
|
if (annotatedValues.length > 0) { |
|
|
|
if (annotatedValues.length > 0) { |
|
|
|
throw new IllegalArgumentException("Setting both the 'value' and 'values' attributes " |
|
|
|
throw new IllegalArgumentException("Setting both the 'value' and 'values' attributes " + |
|
|
|
+ "of @IfProfileValue is not allowed: choose one or the other."); |
|
|
|
"of @IfProfileValue is not allowed: choose one or the other."); |
|
|
|
} |
|
|
|
} |
|
|
|
annotatedValues = new String[] { ifProfileValue.value() }; |
|
|
|
annotatedValues = new String[] { ifProfileValue.value() }; |
|
|
|
} |
|
|
|
} |
|
|
|
|