Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ConditionEvaluationResult.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.extension;
12
13 import static org.apiguardian.api.API.Status.STABLE;
14
15 import java.util.Optional;
16
17 import org.apiguardian.api.API;
18 import org.junit.platform.commons.util.StringUtils;
19 import org.junit.platform.commons.util.ToStringBuilder;
20
21 /**
22 * The result of evaluating an {@link ExecutionCondition}.
23 *
24 * @since 5.0
25 */
26 @API(status = STABLE, since = "5.0")
27 public class ConditionEvaluationResult {
28
29 /**
30 * Factory for creating <em>enabled</em> results.
31 *
32 * @param reason the reason why the container or test should be enabled
33 * @return an enabled {@code ConditionEvaluationResult} with the given reason
34 */
35 public static ConditionEvaluationResult enabled(String reason) {
36 return new ConditionEvaluationResult(true, reason);
37 }
38
39 /**
40 * Factory for creating <em>disabled</em> results.
41 *
42 * @param reason the reason why the container or test should be disabled
43 * @return a disabled {@code ConditionEvaluationResult} with the given reason
44 */
45 public static ConditionEvaluationResult disabled(String reason) {
46 return new ConditionEvaluationResult(false, reason);
47 }
48
49 /**
50 * Factory for creating <em>disabled</em> results with custom reasons
51 * added by the user.
52 *
53 * @param reason the default reason why the container or test should be disabled
54 * @param customReason the custom reason why the container or test should be disabled
55 * @return a disabled {@code ConditionEvaluationResult} with the given reasons
56 * @since 5.7
57 */
58 @API(status = STABLE, since = "5.7")
59 public static ConditionEvaluationResult disabled(String reason, String customReason) {
60 if (StringUtils.isBlank(customReason)) {
61 return disabled(reason);
62 }
63 return disabled(String.format("%s ==> %s", reason, customReason));
64 }
65
66 private final boolean enabled;
67
68 private final Optional<String> reason;
69
70 private ConditionEvaluationResult(boolean enabled, String reason) {
71 this.enabled = enabled;
72 this.reason = Optional.ofNullable(reason);
73 }
74
75 /**
76 * Whether the container or test should be disabled.
77 *
78 * @return {@code true} if the container or test should be disabled
79 */
80 public boolean isDisabled() {
81 return !this.enabled;
82 }
83
84 /**
85 * Get the reason why the container or test should be enabled or disabled,
86 * if available.
87 */
88 public Optional<String> getReason() {
89 return this.reason;
90 }
91
92 @Override
93 public String toString() {
94 // @formatter:off
95 return new ToStringBuilder(this)
96 .append("enabled", this.enabled)
97 .append("reason", this.reason.orElse("<unknown>"))
98 .toString();
99 // @formatter:on
100 }
101
102 }