| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
package org.junit.jupiter.api.condition; |
| 12 |
|
| 13 |
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled; |
| 14 |
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation; |
| 15 |
|
| 16 |
import java.lang.annotation.Annotation; |
| 17 |
|
| 18 |
import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 19 |
import org.junit.jupiter.api.extension.ExecutionCondition; |
| 20 |
import org.junit.jupiter.api.extension.ExtensionContext; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
abstract class AbstractOsBasedExecutionCondition<A extends Annotation> implements ExecutionCondition { |
| 28 |
|
| 29 |
static final String CURRENT_ARCHITECTURE = System.getProperty("os.arch"); |
| 30 |
static final String CURRENT_OS = System.getProperty("os.name"); |
| 31 |
|
| 32 |
private final Class<A> annotationType; |
| 33 |
|
| 34 |
AbstractOsBasedExecutionCondition(Class<A> annotationType) { |
| 35 |
this.annotationType = annotationType; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
| 40 |
return findAnnotation(context.getElement(), this.annotationType) |
| 41 |
.map(this::evaluateExecutionCondition) |
| 42 |
.orElseGet(this::enabledByDefault); |
| 43 |
} |
| 44 |
|
| 45 |
abstract ConditionEvaluationResult evaluateExecutionCondition(A annotation); |
| 46 |
|
| 47 |
String createReason(boolean enabled, boolean osSpecified, boolean archSpecified) { |
| 48 |
StringBuilder reason = new StringBuilder() |
| 49 |
.append(enabled ? "Enabled" : "Disabled") |
| 50 |
.append(osSpecified ? " on operating system: " : " on architecture: "); |
| 51 |
|
| 52 |
if (osSpecified && archSpecified) { |
| 53 |
reason.append(String.format("%s (%s)", CURRENT_OS, CURRENT_ARCHITECTURE)); |
| 54 |
} |
| 55 |
else if (osSpecified) { |
| 56 |
reason.append(CURRENT_OS); |
| 57 |
} |
| 58 |
else { |
| 59 |
reason.append(CURRENT_ARCHITECTURE); |
| 60 |
} |
| 61 |
|
| 62 |
return reason.toString(); |
| 63 |
} |
| 64 |
|
| 65 |
private ConditionEvaluationResult enabledByDefault() { |
| 66 |
String reason = String.format("@%s is not present", this.annotationType.getSimpleName()); |
| 67 |
return enabled(reason); |
| 68 |
} |
| 69 |
|
| 70 |
} |