|
1 | 1 | package com.opencastsoftware.yvette;
|
2 | 2 |
|
3 |
| -import java.io.ByteArrayOutputStream; |
4 |
| -import java.io.FileNotFoundException; |
5 |
| -import java.io.PrintStream; |
6 |
| -import java.io.UnsupportedEncodingException; |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.*; |
| 5 | + |
| 6 | +import java.io.*; |
7 | 7 | import java.nio.charset.StandardCharsets;
|
8 | 8 | import java.nio.file.AccessDeniedException;
|
9 | 9 | import java.util.concurrent.*;
|
10 |
| -import java.util.concurrent.atomic.AtomicBoolean; |
11 | 10 |
|
12 | 11 | import org.junit.jupiter.api.Test;
|
13 | 12 |
|
| 13 | +import com.opencastsoftware.yvette.handlers.ReportHandler; |
14 | 14 | import com.opencastsoftware.yvette.handlers.graphical.GraphicalReportHandler;
|
15 | 15 |
|
16 |
| -import static org.hamcrest.MatcherAssert.*; |
17 |
| -import static org.hamcrest.Matchers.*; |
18 |
| - |
19 | 16 | public class UncaughtExceptionHandlerTest {
|
20 | 17 |
|
21 | 18 | @Test
|
@@ -60,6 +57,51 @@ void replacesDefaultHandler() throws InterruptedException, UnsupportedEncodingEx
|
60 | 57 | assertThat(Thread.getDefaultUncaughtExceptionHandler(), is(nullValue()));
|
61 | 58 | }
|
62 | 59 |
|
| 60 | + @Test |
| 61 | + void handlesExceptionInHandler() throws InterruptedException, UnsupportedEncodingException { |
| 62 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 63 | + PrintStream printStream = new PrintStream(baos, true); |
| 64 | + |
| 65 | + ReportHandler handler = new ReportHandler() { |
| 66 | + @Override |
| 67 | + public void display(Diagnostic diagnostic, Appendable output) throws IOException { |
| 68 | + throw new IOException("Uh oh, this went very badly"); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + try { |
| 73 | + UncaughtExceptionHandler.install(handler, printStream); |
| 74 | + |
| 75 | + Thread throwingThread = new Thread(() -> { |
| 76 | + Throwable exc = new FileNotFoundException("Couldn't find the file BadFile.java"); |
| 77 | + exc.initCause(new AccessDeniedException("Access denied to file BadFile.java")); |
| 78 | + throw new RuntimeException("Whoops!", exc); |
| 79 | + }); |
| 80 | + |
| 81 | + throwingThread.start(); |
| 82 | + throwingThread.join(); |
| 83 | + |
| 84 | + String diagnosticOutput = baos.toString(StandardCharsets.UTF_8.name()); |
| 85 | + |
| 86 | + assertThat( |
| 87 | + diagnosticOutput, |
| 88 | + containsString("Uncaught exception in thread")); |
| 89 | + |
| 90 | + assertThat( |
| 91 | + diagnosticOutput, |
| 92 | + containsString("java.io.IOException: Uh oh, this went very badly")); |
| 93 | + |
| 94 | + assertThat( |
| 95 | + diagnosticOutput, |
| 96 | + containsString("Suppressed: java.lang.RuntimeException: Whoops!")); |
| 97 | + |
| 98 | + } finally { |
| 99 | + UncaughtExceptionHandler.uninstall(); |
| 100 | + } |
| 101 | + |
| 102 | + assertThat(Thread.getDefaultUncaughtExceptionHandler(), is(nullValue())); |
| 103 | + } |
| 104 | + |
63 | 105 | @Test
|
64 | 106 | void replacesThreadPoolHandler() throws InterruptedException, UnsupportedEncodingException {
|
65 | 107 | CountDownLatch excHandled = new CountDownLatch(1);
|
|
0 commit comments