Add missing TestUtils class for testing onError JEP-349-branch
authoregahlin
Fri, 27 Sep 2019 13:01:55 +0200
branchJEP-349-branch
changeset 58370 9b4841a568cb
parent 58369 d55d1bd947c2
child 58371 a6d44d9bf80b
Add missing TestUtils class for testing onError
test/jdk/jdk/jfr/api/consumer/recordingstream/TestUtils.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/jdk/jfr/api/consumer/recordingstream/TestUtils.java	Fri Sep 27 13:01:55 2019 +0200
@@ -0,0 +1,41 @@
+package jdk.jfr.api.consumer.recordingstream;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+public class TestUtils {
+
+    public static final class TestError extends Error {
+        private static final long serialVersionUID = 1L;
+    }
+
+    public static final class TestException extends Exception {
+        private static final long serialVersionUID = 1L;
+        private volatile boolean printed;
+
+        @Override
+        public void printStackTrace() {
+            super.printStackTrace();
+            printed = true;
+        }
+
+        public boolean isPrinted() {
+            return printed;
+        }
+    }
+
+    // Can throw checked exception as unchecked.
+    @SuppressWarnings("unchecked")
+    public static <T extends Throwable> void throwUnchecked(Throwable e) throws T {
+        throw (T) e;
+    }
+
+    public static void installUncaughtException(CountDownLatch receivedError, Throwable expected) {
+        Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> {
+            if (throwable == expected) {
+                System.out.println("Received uncaught exception " + expected.getClass());
+                receivedError.countDown();
+            }
+        });
+    }
+}