| 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.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 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class EnabledIfEnvironmentVariableCondition |
| 28 |
extends AbstractRepeatableAnnotationCondition<EnabledIfEnvironmentVariable> { |
| 29 |
|
| 30 |
private static final ConditionEvaluationResult ENABLED = ConditionEvaluationResult.enabled( |
| 31 |
"No @EnabledIfEnvironmentVariable conditions resulting in 'disabled' execution encountered"); |
| 32 |
|
| 33 |
EnabledIfEnvironmentVariableCondition() { |
| 34 |
super(EnabledIfEnvironmentVariable.class); |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
protected ConditionEvaluationResult getNoDisabledConditionsEncounteredResult() { |
| 39 |
return ENABLED; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
protected ConditionEvaluationResult evaluate(EnabledIfEnvironmentVariable annotation) { |
| 44 |
|
| 45 |
String name = annotation.named().trim(); |
| 46 |
String regex = annotation.matches(); |
| 47 |
Preconditions.notBlank(name, () -> "The 'named' attribute must not be blank in " + annotation); |
| 48 |
Preconditions.notBlank(regex, () -> "The 'matches' attribute must not be blank in " + annotation); |
| 49 |
String actual = getEnvironmentVariable(name); |
| 50 |
|
| 51 |
|
| 52 |
if (actual == null) { |
| 53 |
return disabled(format("Environment variable [%s] does not exist", name), annotation.disabledReason()); |
| 54 |
} |
| 55 |
if (actual.matches(regex)) { |
| 56 |
return enabled(format("Environment variable [%s] with value [%s] matches regular expression [%s]", name, |
| 57 |
actual, regex)); |
| 58 |
} |
| 59 |
return disabled(format("Environment variable [%s] with value [%s] does not match regular expression [%s]", name, |
| 60 |
actual, regex), annotation.disabledReason()); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
protected String getEnvironmentVariable(String name) { |
| 71 |
return System.getenv(name); |
| 72 |
} |
| 73 |
|
| 74 |
} |