| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
package org.junit.jupiter.api.condition; |
| 12 |
|
| 13 |
import static java.lang.String.format; |
| 14 |
import static org.junit.platform.commons.util.AnnotationUtils.findRepeatableAnnotations; |
| 15 |
|
| 16 |
import java.lang.annotation.Annotation; |
| 17 |
import java.lang.annotation.Repeatable; |
| 18 |
import java.lang.reflect.AnnotatedElement; |
| 19 |
import java.util.Optional; |
| 20 |
|
| 21 |
import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 22 |
import org.junit.jupiter.api.extension.ExecutionCondition; |
| 23 |
import org.junit.jupiter.api.extension.ExtensionContext; |
| 24 |
import org.junit.platform.commons.logging.Logger; |
| 25 |
import org.junit.platform.commons.logging.LoggerFactory; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
abstract class AbstractRepeatableAnnotationCondition<A extends Annotation> implements ExecutionCondition { |
| 35 |
|
| 36 |
private final Logger logger = LoggerFactory.getLogger(getClass()); |
| 37 |
|
| 38 |
private final Class<A> annotationType; |
| 39 |
|
| 40 |
AbstractRepeatableAnnotationCondition(Class<A> annotationType) { |
| 41 |
this.annotationType = annotationType; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public final ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
| 46 |
Optional<AnnotatedElement> optionalElement = context.getElement(); |
| 47 |
if (optionalElement.isPresent()) { |
| 48 |
AnnotatedElement annotatedElement = optionalElement.get(); |
| 49 |
|
| 50 |
return findRepeatableAnnotations(annotatedElement, this.annotationType).stream() |
| 51 |
.map(annotation -> { |
| 52 |
ConditionEvaluationResult result = evaluate(annotation); |
| 53 |
logResult(annotation, annotatedElement, result); |
| 54 |
return result; |
| 55 |
}) |
| 56 |
.filter(ConditionEvaluationResult::isDisabled) |
| 57 |
.findFirst() |
| 58 |
.orElse(getNoDisabledConditionsEncounteredResult()); |
| 59 |
|
| 60 |
} |
| 61 |
return getNoDisabledConditionsEncounteredResult(); |
| 62 |
} |
| 63 |
|
| 64 |
protected abstract ConditionEvaluationResult evaluate(A annotation); |
| 65 |
|
| 66 |
protected abstract ConditionEvaluationResult getNoDisabledConditionsEncounteredResult(); |
| 67 |
|
| 68 |
private void logResult(A annotation, AnnotatedElement annotatedElement, ConditionEvaluationResult result) { |
| 69 |
logger.trace(() -> format("Evaluation of %s on [%s] resulted in: %s", annotation, annotatedElement, result)); |
| 70 |
} |
| 71 |
|
| 72 |
} |