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