| 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 EnabledIfSystemPropertyCondition extends AbstractRepeatableAnnotationCondition<EnabledIfSystemProperty> { |
| 28 |
|
| 29 |
private static final ConditionEvaluationResult ENABLED = ConditionEvaluationResult.enabled( |
| 30 |
"No @EnabledIfSystemProperty conditions resulting in 'disabled' execution encountered"); |
| 31 |
|
| 32 |
EnabledIfSystemPropertyCondition() { |
| 33 |
super(EnabledIfSystemProperty.class); |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
protected ConditionEvaluationResult getNoDisabledConditionsEncounteredResult() { |
| 38 |
return ENABLED; |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
protected ConditionEvaluationResult evaluate(EnabledIfSystemProperty annotation) { |
| 43 |
|
| 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 = System.getProperty(name); |
| 49 |
|
| 50 |
|
| 51 |
if (actual == null) { |
| 52 |
return disabled(format("System property [%s] does not exist", name), annotation.disabledReason()); |
| 53 |
} |
| 54 |
if (actual.matches(regex)) { |
| 55 |
return enabled( |
| 56 |
format("System property [%s] with value [%s] matches regular expression [%s]", name, actual, regex)); |
| 57 |
} |
| 58 |
return disabled( |
| 59 |
format("System property [%s] with value [%s] does not match regular expression [%s]", name, actual, regex), |
| 60 |
annotation.disabledReason()); |
| 61 |
} |
| 62 |
|
| 63 |
} |