Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/DisabledIfEnvironmentVariableCondition.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.jupiter.api.extension.ConditionEvaluationResult.disabled;
15 import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled;
16
17 import org.junit.jupiter.api.extension.ConditionEvaluationResult;
18 import org.junit.jupiter.api.extension.ExecutionCondition;
19 import org.junit.platform.commons.util.Preconditions;
20
21 /**
22 * {@link ExecutionCondition} for {@link DisabledIfEnvironmentVariable @DisabledIfEnvironmentVariable}.
23 *
24 * @since 5.1
25 * @see DisabledIfEnvironmentVariable
26 */
27 class DisabledIfEnvironmentVariableCondition
28 extends AbstractRepeatableAnnotationCondition<DisabledIfEnvironmentVariable> {
29
30 private static final ConditionEvaluationResult ENABLED = ConditionEvaluationResult.enabled(
31 "No @DisabledIfEnvironmentVariable conditions resulting in 'disabled' execution encountered");
32
33 DisabledIfEnvironmentVariableCondition() {
34 super(DisabledIfEnvironmentVariable.class);
35 }
36
37 @Override
38 protected ConditionEvaluationResult getNoDisabledConditionsEncounteredResult() {
39 return ENABLED;
40 }
41
42 @Override
43 protected ConditionEvaluationResult evaluate(DisabledIfEnvironmentVariable annotation) {
44 String name = annotation.named().trim();
45 String regex = annotation.matches();
46 Preconditions.notBlank(name, () -> "The 'named' attribute must not be blank in " + annotation);
47 Preconditions.notBlank(regex, () -> "The 'matches' attribute must not be blank in " + annotation);
48 String actual = getEnvironmentVariable(name);
49
50 // Nothing to match against?
51 if (actual == null) {
52 return enabled(format("Environment variable [%s] does not exist", name));
53 }
54
55 if (actual.matches(regex)) {
56 return disabled(format("Environment variable [%s] with value [%s] matches regular expression [%s]", name,
57 actual, regex), annotation.disabledReason());
58 }
59 // else
60 return enabled(format("Environment variable [%s] with value [%s] does not match regular expression [%s]", name,
61 actual, regex));
62 }
63
64 /**
65 * Get the value of the named environment variable.
66 *
67 * <p>The default implementation delegates to
68 * {@link System#getenv(String)}. Can be overridden in a subclass for
69 * testing purposes.
70 */
71 protected String getEnvironmentVariable(String name) {
72 return System.getenv(name);
73 }
74
75 }