public static Throwable unwrapCompletionException(Throwable ex) {
        if (ex instanceof CompletionException) {
            return unwrapCompletionException(ex.getCause());
        } else if (ex instanceof ExecutionException) {
            return unwrapCompletionException(ex.getCause());
        } else {
            return ex;
        }
    }
@Test
    public void testGetOriginalException() {
        CompletableFuture<Void> future = CompletableFuture.completedFuture(null);
        CompletableFuture<Void> exceptionFuture = future.thenAccept(__ -> {
            throw new IllegalStateException("Illegal state");
        });
        assertTrue(exceptionFuture.isCompletedExceptionally());
        try {
            exceptionFuture.get();
        } catch (InterruptedException | ExecutionException e) {
            Throwable originalException = FutureUtil.unwrapCompletionException(e);
            assertTrue(originalException instanceof IllegalStateException);
        }
        CompletableFuture<Object> exceptionFuture2 = new CompletableFuture<>();
        exceptionFuture2.completeExceptionally(new IllegalStateException("Completed exception"));
        final List<Throwable> future2Exception = Lists.newArrayList();
        exceptionFuture2.exceptionally(ex -> {
            future2Exception.add(FutureUtil.unwrapCompletionException(ex));
            return null;
        });
        Awaitility.await()
                .untilAsserted(() -> {
                    assertEquals(future2Exception.size(), 1);
                    assertTrue(future2Exception.get(0) instanceof IllegalStateException);
                });
        final List<Throwable> future3Exception = Lists.newArrayList();
        CompletableFuture.completedFuture(null)
                .thenAccept(__ -> {
                    throw new IllegalStateException("Throw illegal exception");
                })
                .exceptionally(ex -> {
                    future3Exception.add(FutureUtil.unwrapCompletionException(ex));
                    return null;
                });
        Awaitility.await()
                .untilAsserted(() -> {
                    assertEquals(future3Exception.size(), 1);
                    assertTrue(future3Exception.get(0) instanceof IllegalStateException);
                });
    }