|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context.junit.jupiter; |
| 18 | + |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | +import java.lang.reflect.AnnotatedElement; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import org.apache.commons.logging.Log; |
| 24 | +import org.apache.commons.logging.LogFactory; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 27 | +import org.junit.jupiter.api.extension.ContainerExecutionCondition; |
| 28 | +import org.junit.jupiter.api.extension.ContainerExtensionContext; |
| 29 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 30 | +import org.junit.jupiter.api.extension.TestExecutionCondition; |
| 31 | +import org.junit.jupiter.api.extension.TestExtensionContext; |
| 32 | + |
| 33 | +import org.springframework.beans.factory.config.BeanExpressionContext; |
| 34 | +import org.springframework.beans.factory.config.BeanExpressionResolver; |
| 35 | +import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
| 36 | +import org.springframework.context.ApplicationContext; |
| 37 | +import org.springframework.context.ConfigurableApplicationContext; |
| 38 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 39 | +import org.springframework.util.Assert; |
| 40 | +import org.springframework.util.StringUtils; |
| 41 | + |
| 42 | +/** |
| 43 | + * {@code DisabledIfCondition} is a composite {@link ContainerExecutionCondition} |
| 44 | + * and {@link TestExecutionCondition} that supports the {@link DisabledIf @DisabledIf} |
| 45 | + * annotation when using the <em>Spring TestContext Framework</em> in conjunction |
| 46 | + * with JUnit 5's <em>Jupiter</em> programming model. |
| 47 | + * |
| 48 | + * <p>Any attempt to use {@code DisabledIfCondition} without the presence of |
| 49 | + * {@link DisabledIf @DisabledIf} will result in an {@link IllegalStateException}. |
| 50 | + * |
| 51 | + * @author Sam Brannen |
| 52 | + * @author Tadaya Tsuyukubo |
| 53 | + * @since 5.0 |
| 54 | + * @see org.springframework.test.context.junit.jupiter.DisabledIf |
| 55 | + * @see org.springframework.test.context.junit.jupiter.SpringExtension |
| 56 | + */ |
| 57 | +public class DisabledIfCondition implements ContainerExecutionCondition, TestExecutionCondition { |
| 58 | + |
| 59 | + private static final Log logger = LogFactory.getLog(DisabledIfCondition.class); |
| 60 | + |
| 61 | + |
| 62 | + /** |
| 63 | + * Containers are disabled if {@code @DisabledIf} is present on the test class |
| 64 | + * and the configured expression evaluates to {@code true}. |
| 65 | + */ |
| 66 | + @Override |
| 67 | + public ConditionEvaluationResult evaluate(ContainerExtensionContext context) { |
| 68 | + return evaluateDisabledIf(context); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Tests are disabled if {@code @DisabledIf} is present on the test method |
| 73 | + * and the configured expression evaluates to {@code true}. |
| 74 | + */ |
| 75 | + @Override |
| 76 | + public ConditionEvaluationResult evaluate(TestExtensionContext context) { |
| 77 | + return evaluateDisabledIf(context); |
| 78 | + } |
| 79 | + |
| 80 | + private ConditionEvaluationResult evaluateDisabledIf(ExtensionContext extensionContext) { |
| 81 | + AnnotatedElement element = extensionContext.getElement().get(); |
| 82 | + Optional<DisabledIf> disabledIf = findMergedAnnotation(element, DisabledIf.class); |
| 83 | + Assert.state(disabledIf.isPresent(), () -> "@DisabledIf must be present on " + element); |
| 84 | + |
| 85 | + String expression = disabledIf.get().expression().trim(); |
| 86 | + |
| 87 | + if (isDisabled(expression, extensionContext)) { |
| 88 | + String reason = disabledIf.map(DisabledIf::reason).filter(StringUtils::hasText).orElseGet( |
| 89 | + () -> String.format("%s is disabled because @DisabledIf(\"%s\") evaluated to true", element, |
| 90 | + expression)); |
| 91 | + logger.info(reason); |
| 92 | + return ConditionEvaluationResult.disabled(reason); |
| 93 | + } |
| 94 | + else { |
| 95 | + String reason = String.format("%s is enabled because @DisabledIf(\"%s\") did not evaluate to true", |
| 96 | + element, expression); |
| 97 | + logger.debug(reason); |
| 98 | + return ConditionEvaluationResult.enabled(reason); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private boolean isDisabled(String expression, ExtensionContext extensionContext) { |
| 103 | + ApplicationContext applicationContext = SpringExtension.getApplicationContext(extensionContext); |
| 104 | + |
| 105 | + if (!(applicationContext instanceof ConfigurableApplicationContext)) { |
| 106 | + if (logger.isWarnEnabled()) { |
| 107 | + String contextType = (applicationContext != null ? applicationContext.getClass().getName() : "null"); |
| 108 | + logger.warn(String.format("@DisabledIf(\"%s\") could not be evaluated on [%s] since the test " + |
| 109 | + "ApplicationContext [%s] is not a ConfigurableApplicationContext", |
| 110 | + expression, extensionContext.getElement(), contextType)); |
| 111 | + } |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + ConfigurableBeanFactory configurableBeanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory(); |
| 116 | + BeanExpressionResolver expressionResolver = configurableBeanFactory.getBeanExpressionResolver(); |
| 117 | + BeanExpressionContext beanExpressionContext = new BeanExpressionContext(configurableBeanFactory, null); |
| 118 | + |
| 119 | + Object result = expressionResolver.evaluate(configurableBeanFactory.resolveEmbeddedValue(expression), |
| 120 | + beanExpressionContext); |
| 121 | + |
| 122 | + Assert.state((result instanceof Boolean || result instanceof String), () -> |
| 123 | + String.format("@DisabledIf(\"%s\") must evaluate to a String or a Boolean, not %s", expression, |
| 124 | + (result != null ? result.getClass().getName() : "null"))); |
| 125 | + |
| 126 | + boolean disabled = (result instanceof Boolean && ((Boolean) result).booleanValue()) || |
| 127 | + (result instanceof String && Boolean.parseBoolean((String) result)); |
| 128 | + |
| 129 | + return disabled; |
| 130 | + } |
| 131 | + |
| 132 | + private static <A extends Annotation> Optional<A> findMergedAnnotation(AnnotatedElement element, |
| 133 | + Class<A> annotationType) { |
| 134 | + return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(element, annotationType)); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments