Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/AbstractRepeatableAnnotationCondition.java

raw symbols SCIP
1 /*
2 * Copyright 2015-2024 the original author or authors.
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v2.0 which
6 * accompanies this distribution and is available at
7 *
8 * https://www.eclipse.org/legal/epl-v20.html
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 * Abstract base class for {@link ExecutionCondition} implementations that support
29 * {@linkplain Repeatable repeatable} annotations.
30 *
31 * @param <A> the type of repeatable annotation supported by this {@code ExecutionCondition}
32 * @since 5.6
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 // @formatter:off
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 // @formatter:on
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 }