Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/Assumptions.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;
12
13 import static org.apiguardian.api.API.Status.STABLE;
14
15 import java.util.function.BooleanSupplier;
16 import java.util.function.Supplier;
17
18 import org.apiguardian.api.API;
19 import org.junit.jupiter.api.function.Executable;
20 import org.junit.platform.commons.util.ExceptionUtils;
21 import org.junit.platform.commons.util.StringUtils;
22 import org.opentest4j.TestAbortedException;
23
24 /**
25 * {@code Assumptions} is a collection of utility methods that support
26 * conditional test execution based on <em>assumptions</em>.
27 *
28 * <p>In direct contrast to failed {@linkplain Assertions assertions},
29 * failed assumptions do not result in a test <em>failure</em>; rather,
30 * a failed assumption results in a test being <em>aborted</em>.
31 *
32 * <p>Assumptions are typically used whenever it does not make sense to
33 * continue execution of a given test method &mdash; for example, if the
34 * test depends on something that does not exist in the current runtime
35 * environment.
36 *
37 * <p>Although it is technically possible to extend this class, extension is
38 * strongly discouraged. The JUnit Team highly recommends that the methods
39 * defined in this class be used via <em>static imports</em>.
40 *
41 * @since 5.0
42 * @see TestAbortedException
43 * @see Assertions
44 */
45 @API(status = STABLE, since = "5.0")
46 public class Assumptions {
47
48 /**
49 * Protected constructor allowing subclassing but not direct instantiation.
50 *
51 * @since 5.3
52 */
53 protected Assumptions() {
54 /* no-op */
55 }
56
57 // --- assumeTrue ----------------------------------------------------------
58
59 /**
60 * Validate the given assumption.
61 *
62 * @param assumption the assumption to validate
63 * @throws TestAbortedException if the assumption is not {@code true}
64 */
65 public static void assumeTrue(boolean assumption) throws TestAbortedException {
66 assumeTrue(assumption, "assumption is not true");
67 }
68
69 /**
70 * Validate the given assumption.
71 *
72 * @param assumptionSupplier the supplier of the assumption to validate
73 * @throws TestAbortedException if the assumption is not {@code true}
74 */
75 public static void assumeTrue(BooleanSupplier assumptionSupplier) throws TestAbortedException {
76 assumeTrue(assumptionSupplier.getAsBoolean(), "assumption is not true");
77 }
78
79 /**
80 * Validate the given assumption.
81 *
82 * @param assumptionSupplier the supplier of the assumption to validate
83 * @param message the message to be included in the {@code TestAbortedException}
84 * if the assumption is invalid
85 * @throws TestAbortedException if the assumption is not {@code true}
86 */
87 public static void assumeTrue(BooleanSupplier assumptionSupplier, String message) throws TestAbortedException {
88 assumeTrue(assumptionSupplier.getAsBoolean(), message);
89 }
90
91 /**
92 * Validate the given assumption.
93 *
94 * @param assumption the assumption to validate
95 * @param messageSupplier the supplier of the message to be included in
96 * the {@code TestAbortedException} if the assumption is invalid
97 * @throws TestAbortedException if the assumption is not {@code true}
98 */
99 public static void assumeTrue(boolean assumption, Supplier<String> messageSupplier) throws TestAbortedException {
100 if (!assumption) {
101 throwAssumptionFailed(messageSupplier.get());
102 }
103 }
104
105 /**
106 * Validate the given assumption.
107 *
108 * @param assumption the assumption to validate
109 * @param message the message to be included in the {@code TestAbortedException}
110 * if the assumption is invalid
111 * @throws TestAbortedException if the assumption is not {@code true}
112 */
113 public static void assumeTrue(boolean assumption, String message) throws TestAbortedException {
114 if (!assumption) {
115 throwAssumptionFailed(message);
116 }
117 }
118
119 /**
120 * Validate the given assumption.
121 *
122 * @param assumptionSupplier the supplier of the assumption to validate
123 * @param messageSupplier the supplier of the message to be included in
124 * the {@code TestAbortedException} if the assumption is invalid
125 * @throws TestAbortedException if the assumption is not {@code true}
126 */
127 public static void assumeTrue(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier)
128 throws TestAbortedException {
129
130 assumeTrue(assumptionSupplier.getAsBoolean(), messageSupplier);
131 }
132
133 // --- assumeFalse ---------------------------------------------------------
134
135 /**
136 * Validate the given assumption.
137 *
138 * @param assumption the assumption to validate
139 * @throws TestAbortedException if the assumption is not {@code false}
140 */
141 public static void assumeFalse(boolean assumption) throws TestAbortedException {
142 assumeFalse(assumption, "assumption is not false");
143 }
144
145 /**
146 * Validate the given assumption.
147 *
148 * @param assumptionSupplier the supplier of the assumption to validate
149 * @throws TestAbortedException if the assumption is not {@code false}
150 */
151 public static void assumeFalse(BooleanSupplier assumptionSupplier) throws TestAbortedException {
152 assumeFalse(assumptionSupplier.getAsBoolean(), "assumption is not false");
153 }
154
155 /**
156 * Validate the given assumption.
157 *
158 * @param assumptionSupplier the supplier of the assumption to validate
159 * @param message the message to be included in the {@code TestAbortedException}
160 * if the assumption is invalid
161 * @throws TestAbortedException if the assumption is not {@code false}
162 */
163 public static void assumeFalse(BooleanSupplier assumptionSupplier, String message) throws TestAbortedException {
164 assumeFalse(assumptionSupplier.getAsBoolean(), message);
165 }
166
167 /**
168 * Validate the given assumption.
169 *
170 * @param assumption the assumption to validate
171 * @param messageSupplier the supplier of the message to be included in
172 * the {@code TestAbortedException} if the assumption is invalid
173 * @throws TestAbortedException if the assumption is not {@code false}
174 */
175 public static void assumeFalse(boolean assumption, Supplier<String> messageSupplier) throws TestAbortedException {
176 if (assumption) {
177 throwAssumptionFailed(messageSupplier.get());
178 }
179 }
180
181 /**
182 * Validate the given assumption.
183 *
184 * @param assumption the assumption to validate
185 * @param message the message to be included in the {@code TestAbortedException}
186 * if the assumption is invalid
187 * @throws TestAbortedException if the assumption is not {@code false}
188 */
189 public static void assumeFalse(boolean assumption, String message) throws TestAbortedException {
190 if (assumption) {
191 throwAssumptionFailed(message);
192 }
193 }
194
195 /**
196 * Validate the given assumption.
197 *
198 * @param assumptionSupplier the supplier of the assumption to validate
199 * @param messageSupplier the supplier of the message to be included in
200 * the {@code TestAbortedException} if the assumption is invalid
201 * @throws TestAbortedException if the assumption is not {@code false}
202 */
203 public static void assumeFalse(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier)
204 throws TestAbortedException {
205
206 assumeFalse(assumptionSupplier.getAsBoolean(), messageSupplier);
207 }
208
209 // --- assumingThat --------------------------------------------------------
210
211 /**
212 * Execute the supplied {@link Executable}, but only if the supplied
213 * assumption is valid.
214 *
215 * <p>Unlike the other assumption methods, this method will not abort the test.
216 * If the assumption is invalid, this method does nothing. If the assumption is
217 * valid and the {@code executable} throws an exception, it will be treated like
218 * a regular test <em>failure</em>. That exception will be rethrown <em>as is</em>
219 * but {@link ExceptionUtils#throwAsUncheckedException masked} as an unchecked
220 * exception.
221 *
222 * @param assumptionSupplier the supplier of the assumption to validate
223 * @param executable the block of code to execute if the assumption is valid
224 * @see #assumingThat(boolean, Executable)
225 */
226 public static void assumingThat(BooleanSupplier assumptionSupplier, Executable executable) {
227 assumingThat(assumptionSupplier.getAsBoolean(), executable);
228 }
229
230 /**
231 * Execute the supplied {@link Executable}, but only if the supplied
232 * assumption is valid.
233 *
234 * <p>Unlike the other assumption methods, this method will not abort the test.
235 * If the assumption is invalid, this method does nothing. If the assumption is
236 * valid and the {@code executable} throws an exception, it will be treated like
237 * a regular test <em>failure</em>. That exception will be rethrown <em>as is</em>
238 * but {@link ExceptionUtils#throwAsUncheckedException masked} as an unchecked
239 * exception.
240 *
241 * @param assumption the assumption to validate
242 * @param executable the block of code to execute if the assumption is valid
243 * @see #assumingThat(BooleanSupplier, Executable)
244 */
245 public static void assumingThat(boolean assumption, Executable executable) {
246 if (assumption) {
247 try {
248 executable.execute();
249 }
250 catch (Throwable t) {
251 ExceptionUtils.throwAsUncheckedException(t);
252 }
253 }
254 }
255
256 // --- abort ---------------------------------------------------------------
257
258 /**
259 * <em>Abort</em> the test <em>without</em> a message.
260 *
261 * <p>Although aborting with an explicit message is recommended, this may be
262 * useful when maintaining legacy code.
263 *
264 * <p>See Javadoc for {@link #abort(String)} for an explanation of this
265 * method's generic return type {@code V}.
266 *
267 * @throws TestAbortedException always
268 * @since 5.9
269 */
270 @API(status = STABLE, since = "5.9")
271 public static <V> V abort() {
272 throw new TestAbortedException();
273 }
274
275 /**
276 * <em>Abort</em> the test with the given {@code message}.
277 *
278 * <p>The generic return type {@code V} allows this method to be used
279 * directly as a single-statement lambda expression, thereby avoiding the
280 * need to implement a code block with an explicit return value. Since this
281 * method throws a {@link TestAbortedException} before its return statement,
282 * this method never actually returns a value to its caller. The following
283 * example demonstrates how this may be used in practice.
284 *
285 * <pre>{@code
286 * Stream.of().map(entry -> abort("assumption not met"));
287 * }</pre>
288 *
289 * @param message the message to be included in the {@code TestAbortedException}
290 * @throws TestAbortedException always
291 * @since 5.9
292 */
293 @API(status = STABLE, since = "5.9")
294 public static <V> V abort(String message) {
295 throw new TestAbortedException(message);
296 }
297
298 /**
299 * <em>Abort</em> the test with the supplied message.
300 *
301 * <p>See Javadoc for {@link #abort(String)} for an explanation of this
302 * method's generic return type {@code V}.
303 *
304 * @param messageSupplier the supplier of the message to be included in the
305 * {@code TestAbortedException}
306 * @throws TestAbortedException always
307 * @since 5.9
308 */
309 @API(status = STABLE, since = "5.9")
310 public static <V> V abort(Supplier<String> messageSupplier) {
311 throw new TestAbortedException(messageSupplier.get());
312 }
313
314 private static void throwAssumptionFailed(String message) {
315 throw new TestAbortedException(
316 StringUtils.isNotBlank(message) ? "Assumption failed: " + message : "Assumption failed");
317 }
318
319 }