1
0
mirror of https://github.com/square/okhttp.git synced 2025-11-05 14:10:40 +03:00

Dont System.exit the test runner (#3888)

* Dont System.exit

* Maven aware uncaught exception handling

* cleanup
This commit is contained in:
Yuri Schimke
2018-02-26 03:32:03 +00:00
committed by Jesse Wilson
parent 25d306ca76
commit adb2a82049

View File

@@ -17,6 +17,9 @@ package okhttp3.testing;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.internal.Throwables;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;
@@ -30,8 +33,9 @@ public class InstallUncaughtExceptionHandlerListener extends RunListener {
private Thread.UncaughtExceptionHandler oldDefaultUncaughtExceptionHandler;
private Description lastTestStarted;
private final Map<Throwable, String> exceptions = new LinkedHashMap<>();
@Override public void testRunStarted(Description description) throws Exception {
@Override public void testRunStarted(Description description) {
System.err.println("Installing aggressive uncaught exception handler");
oldDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@@ -48,17 +52,26 @@ public class InstallUncaughtExceptionHandlerListener extends RunListener {
errorText.append("\n");
}
System.err.print(errorText.toString());
System.exit(-1);
synchronized (exceptions) {
exceptions.put(throwable, lastTestStarted.getDisplayName());
}
}
});
}
@Override public void testStarted(Description description) throws Exception {
@Override public void testStarted(Description description) {
lastTestStarted = description;
}
@Override public void testRunFinished(Result result) throws Exception {
Thread.setDefaultUncaughtExceptionHandler(oldDefaultUncaughtExceptionHandler);
System.err.println("Uninstalled aggressive uncaught exception handler");
synchronized (exceptions) {
if (!exceptions.isEmpty()) {
throw Throwables.rethrowAsException(exceptions.keySet().iterator().next());
}
}
}
}