Support @EnabledIf & @DisabledIf w/o loading ApplicationContext
Prior to this commit, when using @EnabledIf or @DisabledIf in Spring's JUnit Jupiter support, the test's ApplicationContext was always eagerly loaded, even if the ApplicationContext would never be used (i.e., the test was disabled). This behavior can lead to undesirable side effects -- for example, attempting to load an application context that requires services only available on the CI server when the test is not actually running on the CI server. This commit addresses this issue by introducing new boolean `loadContext` flags in @EnabledIf and @DisabledIf. By default these flags are set to false which means that the user's test application context will not be loaded to evaluate the expression. On the contrary, a dummy application context will be loaded instead, and expressions will be evaluated against that dummy context. Consequently, if the user wishes to interact with properties from the Spring Environment or with beans from the test application context, the `loadContext` must be explicitly set to true. In addition, expressions which evaluate to a String must now evaluate to exactly "true" or "false" (ignoring case and surrounding whitespace). Issue: SPR-14649
This commit is contained in:
@@ -34,6 +34,7 @@ import org.springframework.beans.factory.config.BeanExpressionResolver;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -75,6 +76,8 @@ abstract class AbstractExpressionEvaluatingCondition implements ContainerExecuti
|
||||
* the annotation
|
||||
* @param reasonExtractor a function that extracts the reason from the
|
||||
* annotation
|
||||
* @param loadContextExtractor a function that extracts the {@code loadContext}
|
||||
* flag from the annotation
|
||||
* @param enabledOnTrue indicates whether the returned {@code ConditionEvaluationResult}
|
||||
* should be {@link ConditionEvaluationResult#enabled enabled} if the expression
|
||||
* evaluates to {@code true}
|
||||
@@ -83,8 +86,8 @@ abstract class AbstractExpressionEvaluatingCondition implements ContainerExecuti
|
||||
* or test should be enabled; otherwise {@link ConditionEvaluationResult#disabled disabled}
|
||||
*/
|
||||
protected <A extends Annotation> ConditionEvaluationResult evaluateAnnotation(Class<A> annotationType,
|
||||
Function<A, String> expressionExtractor, Function<A, String> reasonExtractor, boolean enabledOnTrue,
|
||||
ExtensionContext context) {
|
||||
Function<A, String> expressionExtractor, Function<A, String> reasonExtractor,
|
||||
Function<A, Boolean> loadContextExtractor, boolean enabledOnTrue, ExtensionContext context) {
|
||||
|
||||
AnnotatedElement element = context.getElement().get();
|
||||
Optional<A> annotation = findMergedAnnotation(element, annotationType);
|
||||
@@ -104,7 +107,8 @@ abstract class AbstractExpressionEvaluatingCondition implements ContainerExecuti
|
||||
"The expression in @%s on [%s] must not be blank", annotationType.getSimpleName(), element)));
|
||||
// @formatter:on
|
||||
|
||||
boolean evaluatedToTrue = evaluateExpression(expression, annotationType, context);
|
||||
boolean loadContext = annotation.map(loadContextExtractor).get();
|
||||
boolean evaluatedToTrue = evaluateExpression(expression, loadContext, annotationType, context);
|
||||
|
||||
if (evaluatedToTrue) {
|
||||
String adjective = (enabledOnTrue ? "enabled" : "disabled");
|
||||
@@ -129,17 +133,27 @@ abstract class AbstractExpressionEvaluatingCondition implements ContainerExecuti
|
||||
}
|
||||
}
|
||||
|
||||
private <A extends Annotation> boolean evaluateExpression(String expression, Class<A> annotationType,
|
||||
ExtensionContext extensionContext) {
|
||||
private <A extends Annotation> boolean evaluateExpression(String expression, boolean loadContext,
|
||||
Class<A> annotationType, ExtensionContext extensionContext) {
|
||||
|
||||
ApplicationContext applicationContext = SpringExtension.getApplicationContext(extensionContext);
|
||||
AnnotatedElement element = extensionContext.getElement().get();
|
||||
GenericApplicationContext gac = null;
|
||||
ApplicationContext applicationContext;
|
||||
|
||||
if (loadContext) {
|
||||
applicationContext = SpringExtension.getApplicationContext(extensionContext);
|
||||
} else {
|
||||
gac = new GenericApplicationContext();
|
||||
gac.refresh();
|
||||
applicationContext = gac;
|
||||
}
|
||||
|
||||
if (!(applicationContext instanceof ConfigurableApplicationContext)) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
String contextType = (applicationContext != null ? applicationContext.getClass().getName() : "null");
|
||||
logger.warn(String.format("@%s(\"%s\") could not be evaluated on [%s] since the test " +
|
||||
"ApplicationContext [%s] is not a ConfigurableApplicationContext",
|
||||
annotationType.getSimpleName(), expression, extensionContext.getElement(), contextType));
|
||||
annotationType.getSimpleName(), expression, element, contextType));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -151,12 +165,29 @@ abstract class AbstractExpressionEvaluatingCondition implements ContainerExecuti
|
||||
Object result = expressionResolver.evaluate(configurableBeanFactory.resolveEmbeddedValue(expression),
|
||||
beanExpressionContext);
|
||||
|
||||
Assert.state((result instanceof Boolean || result instanceof String),
|
||||
() -> String.format("@%s(\"%s\") must evaluate to a String or a Boolean, not %s",
|
||||
annotationType.getSimpleName(), expression, (result != null ? result.getClass().getName() : "null")));
|
||||
if (gac != null) {
|
||||
gac.close();
|
||||
}
|
||||
|
||||
return (result instanceof Boolean && ((Boolean) result).booleanValue())
|
||||
|| (result instanceof String && Boolean.parseBoolean((String) result));
|
||||
if (result instanceof Boolean) {
|
||||
return ((Boolean) result).booleanValue();
|
||||
}
|
||||
else if (result instanceof String) {
|
||||
String str = ((String) result).trim().toLowerCase();
|
||||
if ("true".equals(str)) {
|
||||
return true;
|
||||
}
|
||||
Assert.state("false".equals(str),
|
||||
() -> String.format("@%s(\"%s\") on %s must evaluate to \"true\" or \"false\", not \"%s\"",
|
||||
annotationType.getSimpleName(), expression, element, result));
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
String message = String.format("@%s(\"%s\") on %s must evaluate to a String or a Boolean, not %s",
|
||||
annotationType.getSimpleName(), expression, element,
|
||||
(result != null ? result.getClass().getName() : "null"));
|
||||
throw new IllegalStateException(message);
|
||||
}
|
||||
}
|
||||
|
||||
private static <A extends Annotation> Optional<A> findMergedAnnotation(AnnotatedElement element,
|
||||
|
||||
@@ -64,8 +64,8 @@ import org.springframework.core.annotation.AliasFor;
|
||||
public @interface DisabledIf {
|
||||
|
||||
/**
|
||||
* Alias for {@link #expression}; only intended to be used if an
|
||||
* explicit {@link #reason} is not provided.
|
||||
* Alias for {@link #expression}; only intended to be used if {@link #reason}
|
||||
* and {@link #loadContext} are not specified.
|
||||
*
|
||||
* @see #expression
|
||||
*/
|
||||
@@ -97,6 +97,7 @@ public @interface DisabledIf {
|
||||
* and {@code @DisabledIf("false")} is logically meaningless.
|
||||
*
|
||||
* @see #reason
|
||||
* @see #loadContext
|
||||
* @see #value
|
||||
*/
|
||||
@AliasFor("value")
|
||||
@@ -109,4 +110,19 @@ public @interface DisabledIf {
|
||||
*/
|
||||
String reason() default "";
|
||||
|
||||
/**
|
||||
* Whether the {@code ApplicationContext} associated with the current test
|
||||
* should be eagerly loaded in order to evaluate the {@link #expression}.
|
||||
*
|
||||
* <p>Defaults to {@code false} so that test application contexts are not
|
||||
* eagerly loaded unnecessarily. If an expression is based solely on system
|
||||
* properties or environment variables or does not interact with beans in
|
||||
* the test's application context, there is no need to load the context
|
||||
* prematurely since doing so would be a waste of time if the test ends up
|
||||
* being disabled.
|
||||
*
|
||||
* @see #expression
|
||||
*/
|
||||
boolean loadContext() default false;
|
||||
|
||||
}
|
||||
|
||||
@@ -61,7 +61,8 @@ public class DisabledIfCondition extends AbstractExpressionEvaluatingCondition {
|
||||
}
|
||||
|
||||
private ConditionEvaluationResult evaluateDisabledIf(ExtensionContext context) {
|
||||
return evaluateAnnotation(DisabledIf.class, DisabledIf::expression, DisabledIf::reason, false, context);
|
||||
return evaluateAnnotation(DisabledIf.class, DisabledIf::expression, DisabledIf::reason,
|
||||
DisabledIf::loadContext, false, context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -63,8 +63,8 @@ import org.springframework.core.annotation.AliasFor;
|
||||
public @interface EnabledIf {
|
||||
|
||||
/**
|
||||
* Alias for {@link #expression}; only intended to be used if an
|
||||
* explicit {@link #reason} is not provided.
|
||||
* Alias for {@link #expression}; only intended to be used if {@link #reason}
|
||||
* and {@link #loadContext} are not specified.
|
||||
*
|
||||
* @see #expression
|
||||
*/
|
||||
@@ -96,6 +96,7 @@ public @interface EnabledIf {
|
||||
* and {@code @EnabledIf("true")} is logically meaningless.
|
||||
*
|
||||
* @see #reason
|
||||
* @see #loadContext
|
||||
* @see #value
|
||||
*/
|
||||
@AliasFor("value")
|
||||
@@ -108,4 +109,19 @@ public @interface EnabledIf {
|
||||
*/
|
||||
String reason() default "";
|
||||
|
||||
/**
|
||||
* Whether the {@code ApplicationContext} associated with the current test
|
||||
* should be eagerly loaded in order to evaluate the {@link #expression}.
|
||||
*
|
||||
* <p>Defaults to {@code false} so that test application contexts are not
|
||||
* eagerly loaded unnecessarily. If an expression is based solely on system
|
||||
* properties or environment variables or does not interact with beans in
|
||||
* the test's application context, there is no need to load the context
|
||||
* prematurely since doing so would be a waste of time if the test ends up
|
||||
* being disabled.
|
||||
*
|
||||
* @see #expression
|
||||
*/
|
||||
boolean loadContext() default false;
|
||||
|
||||
}
|
||||
|
||||
@@ -60,7 +60,8 @@ public class EnabledIfCondition extends AbstractExpressionEvaluatingCondition {
|
||||
}
|
||||
|
||||
private ConditionEvaluationResult evaluateEnabledIf(ExtensionContext context) {
|
||||
return evaluateAnnotation(EnabledIf.class, EnabledIf::expression, EnabledIf::reason, true, context);
|
||||
return evaluateAnnotation(EnabledIf.class, EnabledIf::expression, EnabledIf::reason,
|
||||
EnabledIf::loadContext, true, context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user