Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertAll.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 java.util.Arrays;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Objects;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19
20 import org.junit.jupiter.api.function.Executable;
21 import org.junit.platform.commons.util.Preconditions;
22 import org.junit.platform.commons.util.UnrecoverableExceptions;
23 import org.opentest4j.MultipleFailuresError;
24
25 /**
26 * {@code AssertAll} is a collection of utility methods that support asserting
27 * multiple conditions in tests at once.
28 *
29 * @since 5.0
30 */
31 class AssertAll {
32
33 private AssertAll() {
34 /* no-op */
35 }
36
37 static void assertAll(Executable... executables) {
38 assertAll(null, executables);
39 }
40
41 static void assertAll(String heading, Executable... executables) {
42 Preconditions.notEmpty(executables, "executables array must not be null or empty");
43 Preconditions.containsNoNullElements(executables, "individual executables must not be null");
44 assertAll(heading, Arrays.stream(executables));
45 }
46
47 static void assertAll(Collection<Executable> executables) {
48 assertAll(null, executables);
49 }
50
51 static void assertAll(String heading, Collection<Executable> executables) {
52 Preconditions.notNull(executables, "executables collection must not be null");
53 Preconditions.containsNoNullElements(executables, "individual executables must not be null");
54 assertAll(heading, executables.stream());
55 }
56
57 static void assertAll(Stream<Executable> executables) {
58 assertAll(null, executables);
59 }
60
61 static void assertAll(String heading, Stream<Executable> executables) {
62 Preconditions.notNull(executables, "executables stream must not be null");
63
64 List<Throwable> failures = executables //
65 .map(executable -> {
66 Preconditions.notNull(executable, "individual executables must not be null");
67 try {
68 executable.execute();
69 return null;
70 }
71 catch (Throwable t) {
72 UnrecoverableExceptions.rethrowIfUnrecoverable(t);
73 return t;
74 }
75 }) //
76 .filter(Objects::nonNull) //
77 .collect(Collectors.toList());
78
79 if (!failures.isEmpty()) {
80 MultipleFailuresError multipleFailuresError = new MultipleFailuresError(heading, failures);
81 failures.forEach(multipleFailuresError::addSuppressed);
82 throw multipleFailuresError;
83 }
84 }
85
86 }