Skip to content

Commit

Permalink
support assert-throws return future (#102)
Browse files Browse the repository at this point in the history
Change-Id: I8fe115a7518dd84e22c9e28f777515e74e48d5d2
  • Loading branch information
javeme authored May 27, 2022
1 parent db84121 commit 589ee2c
Showing 1 changed file with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.baidu.hugegraph.testutil;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;

Expand All @@ -40,31 +41,43 @@ public interface ThrowableConsumer<T> {

public static void assertThrows(Class<? extends Throwable> throwable,
ThrowableRunnable runnable) {
assertThrows(throwable, runnable, e -> {
CompletableFuture<?> future = assertThrowsFuture(throwable, runnable);
future.thenAccept(e -> {
System.err.println(e);
});
}

public static void assertThrows(Class<? extends Throwable> throwable,
ThrowableRunnable runnable,
Consumer<Throwable> exceptionConsumer) {
CompletableFuture<Throwable> future = assertThrowsFuture(throwable,
runnable);
future.thenAccept(exceptionConsumer);
}

public static CompletableFuture<Throwable> assertThrowsFuture(
Class<? extends Throwable> clazz,
ThrowableRunnable runnable) {
CompletableFuture<Throwable> future = new CompletableFuture<>();
boolean fail = false;
try {
runnable.run();
fail = true;
} catch (Throwable e) {
if (!throwable.isInstance(e)) {
if (!clazz.isInstance(e)) {
Assert.fail(String.format(
"Bad exception type %s(expected %s)",
e.getClass().getName(), throwable.getName()));
e.getClass().getName(), clazz.getName()));
}
exceptionConsumer.accept(e);
future.complete(e);
}
if (fail) {
Assert.fail(String.format(
"No exception was thrown(expected %s)",
throwable.getName()));
String msg = String.format("No exception was thrown(expected %s)",
clazz.getName());
future.completeExceptionally(new AssertionError(msg));
Assert.fail(msg);
}
return future;
}

public static void assertEquals(byte expected, Object actual) {
Expand Down

0 comments on commit 589ee2c

Please sign in to comment.