Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/DisabledOnOsCondition.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 java.util.Arrays;
14
15 import org.junit.jupiter.api.extension.ConditionEvaluationResult;
16 import org.junit.jupiter.api.extension.ExecutionCondition;
17 import org.junit.platform.commons.util.Preconditions;
18
19 /**
20 * {@link ExecutionCondition} for {@link DisabledOnOs @DisabledOnOs}.
21 *
22 * @since 5.1
23 * @see DisabledOnOs
24 */
25 class DisabledOnOsCondition extends AbstractOsBasedExecutionCondition<DisabledOnOs> {
26
27 DisabledOnOsCondition() {
28 super(DisabledOnOs.class);
29 }
30
31 @Override
32 ConditionEvaluationResult evaluateExecutionCondition(DisabledOnOs annotation) {
33 boolean osSpecified = annotation.value().length > 0;
34 boolean archSpecified = annotation.architectures().length > 0;
35 Preconditions.condition(osSpecified || archSpecified,
36 "You must declare at least one OS or architecture in @DisabledOnOs");
37
38 boolean enabled = isEnabledBasedOnOs(annotation) || isEnabledBasedOnArchitecture(annotation);
39 String reason = createReason(enabled, osSpecified, archSpecified);
40
41 return enabled ? ConditionEvaluationResult.enabled(reason)
42 : ConditionEvaluationResult.disabled(reason, annotation.disabledReason());
43 }
44
45 private boolean isEnabledBasedOnOs(DisabledOnOs annotation) {
46 OS[] operatingSystems = annotation.value();
47 if (operatingSystems.length == 0) {
48 return false;
49 }
50 return Arrays.stream(operatingSystems).noneMatch(OS::isCurrentOs);
51 }
52
53 private boolean isEnabledBasedOnArchitecture(DisabledOnOs annotation) {
54 String[] architectures = annotation.architectures();
55 if (architectures.length == 0) {
56 return false;
57 }
58 return Arrays.stream(architectures).noneMatch(CURRENT_ARCHITECTURE::equalsIgnoreCase);
59 }
60
61 }