| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
package org.junit.jupiter.api.condition; |
| 12 |
|
| 13 |
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled; |
| 14 |
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled; |
| 15 |
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation; |
| 16 |
|
| 17 |
import java.lang.annotation.Annotation; |
| 18 |
import java.util.function.Function; |
| 19 |
|
| 20 |
import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 21 |
import org.junit.jupiter.api.extension.ExecutionCondition; |
| 22 |
import org.junit.jupiter.api.extension.ExtensionContext; |
| 23 |
|
| 24 |
abstract class BooleanExecutionCondition<A extends Annotation> implements ExecutionCondition { |
| 25 |
|
| 26 |
private final Class<A> annotationType; |
| 27 |
private final String enabledReason; |
| 28 |
private final String disabledReason; |
| 29 |
private final Function<A, String> customDisabledReason; |
| 30 |
|
| 31 |
BooleanExecutionCondition(Class<A> annotationType, String enabledReason, String disabledReason, |
| 32 |
Function<A, String> customDisabledReason) { |
| 33 |
this.annotationType = annotationType; |
| 34 |
this.enabledReason = enabledReason; |
| 35 |
this.disabledReason = disabledReason; |
| 36 |
this.customDisabledReason = customDisabledReason; |
| 37 |
} |
| 38 |
|
| 39 |
abstract boolean isEnabled(A annotation); |
| 40 |
|
| 41 |
@Override |
| 42 |
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
| 43 |
return findAnnotation(context.getElement(), annotationType) |
| 44 |
.map(annotation -> isEnabled(annotation) ? enabled(enabledReason) |
| 45 |
: disabled(disabledReason, customDisabledReason.apply(annotation))) |
| 46 |
.orElseGet(this::enabledByDefault); |
| 47 |
} |
| 48 |
|
| 49 |
private ConditionEvaluationResult enabledByDefault() { |
| 50 |
String reason = String.format("@%s is not present", annotationType.getSimpleName()); |
| 51 |
return enabled(reason); |
| 52 |
} |
| 53 |
|
| 54 |
} |