|
| 1 | +/* |
| 2 | + * Copyright 2015-2025 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 static org.assertj.core.api.Assertions.assertThat; |
| 14 | + |
| 15 | +import java.lang.annotation.Retention; |
| 16 | +import java.lang.annotation.RetentionPolicy; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 20 | +import org.junit.jupiter.params.ParameterizedTest; |
| 21 | +import org.junit.jupiter.params.provider.NullSource; |
| 22 | +import org.junit.jupiter.params.provider.ValueSource; |
| 23 | + |
| 24 | +/** |
| 25 | + * Unit tests for {@link ConditionEvaluationResult}. |
| 26 | + * |
| 27 | + * @since 5.13.3 |
| 28 | + */ |
| 29 | +class ConditionEvaluationResultTests { |
| 30 | + |
| 31 | + @Test |
| 32 | + void enabledWithReason() { |
| 33 | + var result = ConditionEvaluationResult.enabled("reason"); |
| 34 | + |
| 35 | + assertThat(result.isDisabled()).isFalse(); |
| 36 | + assertThat(result.getReason()).contains("reason"); |
| 37 | + assertThat(result).asString()// |
| 38 | + .isEqualTo("ConditionEvaluationResult [enabled = true, reason = 'reason']"); |
| 39 | + } |
| 40 | + |
| 41 | + @EmptyReasonsTest |
| 42 | + void enabledWithInvalidReason(String reason) { |
| 43 | + @SuppressWarnings("NullAway") |
| 44 | + var result = ConditionEvaluationResult.enabled(reason); |
| 45 | + |
| 46 | + assertThat(result.isDisabled()).isFalse(); |
| 47 | + |
| 48 | + if (reason == null) { |
| 49 | + assertThat(result.getReason()).isEmpty(); |
| 50 | + assertThat(result).asString()// |
| 51 | + .isEqualTo("ConditionEvaluationResult [enabled = true, reason = '<unknown>']"); |
| 52 | + } |
| 53 | + // TODO Remove else-block once issues are addressed. |
| 54 | + else { |
| 55 | + assertThat(result.getReason()).contains(reason); |
| 56 | + assertThat(result).asString()// |
| 57 | + .isEqualTo("ConditionEvaluationResult [enabled = true, reason = '%s']", reason); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void disabledWithDefaultReason() { |
| 63 | + var result = ConditionEvaluationResult.disabled("default"); |
| 64 | + |
| 65 | + assertThat(result.isDisabled()).isTrue(); |
| 66 | + assertThat(result.getReason()).contains("default"); |
| 67 | + assertThat(result).asString()// |
| 68 | + .isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'default']"); |
| 69 | + } |
| 70 | + |
| 71 | + @EmptyReasonsTest |
| 72 | + void disabledWithInvalidDefaultReason(String reason) { |
| 73 | + @SuppressWarnings("NullAway") |
| 74 | + var result = ConditionEvaluationResult.disabled(reason); |
| 75 | + |
| 76 | + assertThat(result.isDisabled()).isTrue(); |
| 77 | + |
| 78 | + if (reason == null) { |
| 79 | + assertThat(result.getReason()).isEmpty(); |
| 80 | + assertThat(result).asString()// |
| 81 | + .isEqualTo("ConditionEvaluationResult [enabled = false, reason = '<unknown>']"); |
| 82 | + } |
| 83 | + // TODO Remove else-block once issues are addressed. |
| 84 | + else { |
| 85 | + assertThat(result.getReason()).contains(reason); |
| 86 | + assertThat(result).asString()// |
| 87 | + .isEqualTo("ConditionEvaluationResult [enabled = false, reason = '%s']", reason); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @EmptyReasonsTest |
| 92 | + void disabledWithValidDefaultReasonAndInvalidCustomReason(String customReason) { |
| 93 | + @SuppressWarnings("NullAway") |
| 94 | + var result = ConditionEvaluationResult.disabled("default", customReason); |
| 95 | + |
| 96 | + assertThat(result.isDisabled()).isTrue(); |
| 97 | + assertThat(result.getReason()).contains("default"); |
| 98 | + assertThat(result).asString()// |
| 99 | + .isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'default']"); |
| 100 | + } |
| 101 | + |
| 102 | + @EmptyReasonsTest |
| 103 | + void disabledWithInvalidDefaultReasonAndValidCustomReason(String reason) { |
| 104 | + @SuppressWarnings("NullAway") |
| 105 | + var result = ConditionEvaluationResult.disabled(reason, "custom"); |
| 106 | + |
| 107 | + assertThat(result.isDisabled()).isTrue(); |
| 108 | + |
| 109 | + // TODO Convert to single assertion once issues are addressed. |
| 110 | + // The following should hold for all null/blank default reasons. |
| 111 | + // assertThat(result).asString().isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'custom']"); |
| 112 | + |
| 113 | + if (reason == null) { |
| 114 | + assertThat(result.getReason()).contains("null ==> custom"); |
| 115 | + assertThat(result).asString()// |
| 116 | + .isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'null ==> custom']"); |
| 117 | + } |
| 118 | + else { |
| 119 | + var generatedReason = reason + " ==> custom"; |
| 120 | + assertThat(result.getReason()).contains(generatedReason); |
| 121 | + assertThat(result).asString()// |
| 122 | + .isEqualTo("ConditionEvaluationResult [enabled = false, reason = '%s']", generatedReason); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @EmptyReasonsTest |
| 127 | + void disabledWithInvalidDefaultReasonAndInvalidCustomReason(String reason) { |
| 128 | + // We intentionally use the reason as both the default and custom reason. |
| 129 | + @SuppressWarnings("NullAway") |
| 130 | + var result = ConditionEvaluationResult.disabled(reason, reason); |
| 131 | + |
| 132 | + assertThat(result.isDisabled()).isTrue(); |
| 133 | + |
| 134 | + if (reason == null) { |
| 135 | + assertThat(result.getReason()).isEmpty(); |
| 136 | + assertThat(result).asString()// |
| 137 | + .isEqualTo("ConditionEvaluationResult [enabled = false, reason = '<unknown>']"); |
| 138 | + } |
| 139 | + // TODO Remove else-block once issues are addressed. |
| 140 | + else { |
| 141 | + assertThat(result.getReason()).contains(reason); |
| 142 | + assertThat(result).asString()// |
| 143 | + .isEqualTo("ConditionEvaluationResult [enabled = false, reason = '%s']", reason); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void disabledWithValidDefaultReasonAndCustomReason() { |
| 149 | + var result = ConditionEvaluationResult.disabled("default", "custom"); |
| 150 | + |
| 151 | + assertThat(result.isDisabled()).isTrue(); |
| 152 | + assertThat(result.getReason()).contains("default ==> custom"); |
| 153 | + assertThat(result).asString()// |
| 154 | + .isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'default ==> custom']"); |
| 155 | + } |
| 156 | + |
| 157 | + @Retention(RetentionPolicy.RUNTIME) |
| 158 | + @ParameterizedTest |
| 159 | + @NullSource |
| 160 | + @ValueSource(strings = { "", " ", " ", "\t", "\n" }) |
| 161 | + @interface EmptyReasonsTest { |
| 162 | + } |
| 163 | + |
| 164 | +} |
0 commit comments