test/jdk/jdk/jfr/api/consumer/recordingstream/TestUtils.java
author egahlin
Fri, 27 Sep 2019 13:01:55 +0200
branchJEP-349-branch
changeset 58370 9b4841a568cb
child 58371 a6d44d9bf80b
permissions -rw-r--r--
Add missing TestUtils class for testing onError
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58370
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
     1
package jdk.jfr.api.consumer.recordingstream;
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
     2
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
     3
import java.util.concurrent.CountDownLatch;
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
     4
import java.util.concurrent.atomic.AtomicBoolean;
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
     5
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
     6
public class TestUtils {
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
     7
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
     8
    public static final class TestError extends Error {
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
     9
        private static final long serialVersionUID = 1L;
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    10
    }
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    11
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    12
    public static final class TestException extends Exception {
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    13
        private static final long serialVersionUID = 1L;
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    14
        private volatile boolean printed;
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    15
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    16
        @Override
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    17
        public void printStackTrace() {
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    18
            super.printStackTrace();
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    19
            printed = true;
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    20
        }
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    21
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    22
        public boolean isPrinted() {
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    23
            return printed;
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    24
        }
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    25
    }
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    26
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    27
    // Can throw checked exception as unchecked.
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    28
    @SuppressWarnings("unchecked")
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    29
    public static <T extends Throwable> void throwUnchecked(Throwable e) throws T {
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    30
        throw (T) e;
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    31
    }
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    32
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    33
    public static void installUncaughtException(CountDownLatch receivedError, Throwable expected) {
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    34
        Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> {
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    35
            if (throwable == expected) {
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    36
                System.out.println("Received uncaught exception " + expected.getClass());
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    37
                receivedError.countDown();
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    38
            }
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    39
        });
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    40
    }
9b4841a568cb Add missing TestUtils class for testing onError
egahlin
parents:
diff changeset
    41
}