Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertDoesNotThrow.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.junit.jupiter.api.AssertionFailureBuilder.assertionFailure;
14
15 import java.util.function.Supplier;
16
17 import org.junit.jupiter.api.function.Executable;
18 import org.junit.jupiter.api.function.ThrowingSupplier;
19 import org.junit.platform.commons.util.StringUtils;
20 import org.junit.platform.commons.util.UnrecoverableExceptions;
21 import org.opentest4j.AssertionFailedError;
22
23 /**
24 * {@code AssertDoesNotThrow} is a collection of utility methods that support
25 * explicitly asserting that a given code block does not throw an exception.
26 *
27 * @since 5.2
28 */
29 class AssertDoesNotThrow {
30
31 private AssertDoesNotThrow() {
32 /* no-op */
33 }
34
35 static void assertDoesNotThrow(Executable executable) {
36 assertDoesNotThrow(executable, (Object) null);
37 }
38
39 static void assertDoesNotThrow(Executable executable, String message) {
40 assertDoesNotThrow(executable, (Object) message);
41 }
42
43 static void assertDoesNotThrow(Executable executable, Supplier<String> messageSupplier) {
44 assertDoesNotThrow(executable, (Object) messageSupplier);
45 }
46
47 private static void assertDoesNotThrow(Executable executable, Object messageOrSupplier) {
48 try {
49 executable.execute();
50 }
51 catch (Throwable t) {
52 UnrecoverableExceptions.rethrowIfUnrecoverable(t);
53 throw createAssertionFailedError(messageOrSupplier, t);
54 }
55 }
56
57 static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier) {
58 return assertDoesNotThrow(supplier, (Object) null);
59 }
60
61 static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier, String message) {
62 return assertDoesNotThrow(supplier, (Object) message);
63 }
64
65 static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier, Supplier<String> messageSupplier) {
66 return assertDoesNotThrow(supplier, (Object) messageSupplier);
67 }
68
69 private static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier, Object messageOrSupplier) {
70 try {
71 return supplier.get();
72 }
73 catch (Throwable t) {
74 UnrecoverableExceptions.rethrowIfUnrecoverable(t);
75 throw createAssertionFailedError(messageOrSupplier, t);
76 }
77 }
78
79 private static AssertionFailedError createAssertionFailedError(Object messageOrSupplier, Throwable t) {
80 return assertionFailure() //
81 .message(messageOrSupplier) //
82 .reason("Unexpected exception thrown: " + t.getClass().getName() + buildSuffix(t.getMessage())) //
83 .cause(t) //
84 .build();
85 }
86
87 private static String buildSuffix(String message) {
88 return StringUtils.isNotBlank(message) ? ": " + message : "";
89 }
90
91 }