Skip to content

Commit 59db78a

Browse files
Add test for exception in UncaughtExceptionHandler
1 parent 131b26a commit 59db78a

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

src/test/java/com/opencastsoftware/yvette/UncaughtExceptionHandlerTest.java

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package com.opencastsoftware.yvette;
22

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.*;
77
import java.nio.charset.StandardCharsets;
88
import java.nio.file.AccessDeniedException;
99
import java.util.concurrent.*;
10-
import java.util.concurrent.atomic.AtomicBoolean;
1110

1211
import org.junit.jupiter.api.Test;
1312

13+
import com.opencastsoftware.yvette.handlers.ReportHandler;
1414
import com.opencastsoftware.yvette.handlers.graphical.GraphicalReportHandler;
1515

16-
import static org.hamcrest.MatcherAssert.*;
17-
import static org.hamcrest.Matchers.*;
18-
1916
public class UncaughtExceptionHandlerTest {
2017

2118
@Test
@@ -60,6 +57,51 @@ void replacesDefaultHandler() throws InterruptedException, UnsupportedEncodingEx
6057
assertThat(Thread.getDefaultUncaughtExceptionHandler(), is(nullValue()));
6158
}
6259

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+
63105
@Test
64106
void replacesThreadPoolHandler() throws InterruptedException, UnsupportedEncodingException {
65107
CountDownLatch excHandled = new CountDownLatch(1);

0 commit comments

Comments
 (0)