| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class AssertDoesNotThrow { |
| 30 |
|
| 31 |
private AssertDoesNotThrow() { |
| 32 |
|
| 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 |
} |