This is an automated email from the ASF dual-hosted git repository.

jermy pushed a commit to branch assert-throws-bugfix
in repository 
https://gitbox.apache.org/repos/asf/incubator-hugegraph-commons.git

commit d4c7daabc1807c689b12a9b463e684d2e79fe8d0
Author: jermy <[email protected]>
AuthorDate: Sun Nov 26 16:03:00 2023 +0800

    fix: Assert.assertThrows() should check result of exceptionConsumer
---
 .../java/org/apache/hugegraph/testutil/Assert.java | 26 +++++++----
 .../org/apache/hugegraph/util/ExceptionUtil.java   | 51 ++++++++++++++++++++++
 .../org/apache/hugegraph/testutil/AssertTest.java  | 27 ++++++++++--
 .../hugegraph/unit/util/ReflectionUtilTest.java    | 16 +++----
 4 files changed, 100 insertions(+), 20 deletions(-)

diff --git 
a/hugegraph-common/src/main/java/org/apache/hugegraph/testutil/Assert.java 
b/hugegraph-common/src/main/java/org/apache/hugegraph/testutil/Assert.java
index 218342e..60228c1 100644
--- a/hugegraph-common/src/main/java/org/apache/hugegraph/testutil/Assert.java
+++ b/hugegraph-common/src/main/java/org/apache/hugegraph/testutil/Assert.java
@@ -21,6 +21,7 @@ import java.util.concurrent.CompletableFuture;
 import java.util.function.Consumer;
 import java.util.function.Function;
 
+import org.apache.hugegraph.util.ExceptionUtil;
 import org.hamcrest.BaseMatcher;
 import org.hamcrest.CoreMatchers;
 import org.hamcrest.Description;
@@ -37,28 +38,29 @@ public class Assert extends org.junit.Assert {
         void accept(T t) throws Throwable;
     }
 
-    public static void assertThrows(Class<? extends Throwable> throwable,
-                                    ThrowableRunnable runnable) {
-        CompletableFuture<?> future = assertThrowsFuture(throwable, runnable);
+    public static Throwable assertThrows(Class<? extends Throwable> throwable,
+                                         ThrowableRunnable runnable) {
+        CompletableFuture<Throwable> future = assertThrowsFuture(throwable, 
runnable);
         future.thenAccept(System.err::println);
+        return ExceptionUtil.futureGet(future);
     }
 
     public static void assertThrows(Class<? extends Throwable> throwable,
                                     ThrowableRunnable runnable,
                                     Consumer<Throwable> exceptionConsumer) {
-        CompletableFuture<Throwable> future = assertThrowsFuture(throwable,
-                                                                 runnable);
-        future.thenAccept(exceptionConsumer);
+        CompletableFuture<Throwable> future = assertThrowsFuture(throwable, 
runnable);
+        CompletableFuture<Void> futureConsumer = 
future.thenAccept(exceptionConsumer);
+        ExceptionUtil.futureGet(futureConsumer);
     }
 
     public static CompletableFuture<Throwable> assertThrowsFuture(
                                                Class<? extends Throwable> 
clazz,
                                                ThrowableRunnable runnable) {
         CompletableFuture<Throwable> future = new CompletableFuture<>();
-        boolean fail = false;
+        boolean noException = false;
         try {
             runnable.run();
-            fail = true;
+            noException = true;
         } catch (Throwable e) {
             if (!clazz.isInstance(e)) {
                 Assert.fail(String.format(
@@ -67,7 +69,7 @@ public class Assert extends org.junit.Assert {
             }
             future.complete(e);
         }
-        if (fail) {
+        if (noException) {
             String msg = String.format("No exception was thrown(expected %s)",
                                        clazz.getName());
             future.completeExceptionally(new AssertionError(msg));
@@ -104,34 +106,40 @@ public class Assert extends org.junit.Assert {
         org.junit.Assert.assertEquals(expected, actual);
     }
 
+    @SuppressWarnings("deprecation")
     public static void assertGt(Number expected, Object actual) {
         org.junit.Assert.assertThat(actual, new NumberMatcher(expected, cmp -> 
{
             return cmp > 0;
         }, ">"));
     }
 
+    @SuppressWarnings("deprecation")
     public static void assertGte(Number expected, Object actual) {
         org.junit.Assert.assertThat(actual, new NumberMatcher(expected, cmp -> 
{
             return cmp >= 0;
         }, ">="));
     }
 
+    @SuppressWarnings("deprecation")
     public static void assertLt(Number expected, Object actual) {
         org.junit.Assert.assertThat(actual, new NumberMatcher(expected, cmp -> 
{
             return cmp < 0;
         }, "<"));
     }
 
+    @SuppressWarnings("deprecation")
     public static void assertLte(Number expected, Object actual) {
         org.junit.Assert.assertThat(actual, new NumberMatcher(expected, cmp -> 
{
             return cmp <= 0;
         }, "<="));
     }
 
+    @SuppressWarnings("deprecation")
     public static void assertContains(String sub, String actual) {
         org.junit.Assert.assertThat(actual, CoreMatchers.containsString(sub));
     }
 
+    @SuppressWarnings("deprecation")
     public static void assertInstanceOf(Class<?> clazz, Object object) {
         org.junit.Assert.assertThat(object, CoreMatchers.instanceOf(clazz));
     }
diff --git 
a/hugegraph-common/src/main/java/org/apache/hugegraph/util/ExceptionUtil.java 
b/hugegraph-common/src/main/java/org/apache/hugegraph/util/ExceptionUtil.java
new file mode 100644
index 0000000..20bc0b7
--- /dev/null
+++ 
b/hugegraph-common/src/main/java/org/apache/hugegraph/util/ExceptionUtil.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2017 HugeGraph Authors
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.util;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+
+public final class ExceptionUtil {
+
+    public static Throwable rootCause(Throwable e) {
+        Throwable cause = e;
+        while (cause.getCause() != null) {
+            cause = cause.getCause();
+        }
+        return cause;
+    }
+
+    public static RuntimeException transToRuntimeException(Throwable e) {
+        if (e instanceof RuntimeException) {
+            return (RuntimeException) e;
+        }
+        return new RuntimeException(rootCause(e).getMessage(), e);
+    }
+
+    public static <T> T futureGet(Future<T> future) {
+        try {
+            return future.get();
+        } catch (InterruptedException e) {
+            throw ExceptionUtil.transToRuntimeException(e);
+        } catch (ExecutionException e) {
+            throw ExceptionUtil.transToRuntimeException(e.getCause());
+        }
+    }
+}
diff --git 
a/hugegraph-common/src/test/java/org/apache/hugegraph/testutil/AssertTest.java 
b/hugegraph-common/src/test/java/org/apache/hugegraph/testutil/AssertTest.java
index cf08fda..7fdc9da 100644
--- 
a/hugegraph-common/src/test/java/org/apache/hugegraph/testutil/AssertTest.java
+++ 
b/hugegraph-common/src/test/java/org/apache/hugegraph/testutil/AssertTest.java
@@ -17,9 +17,8 @@
 
 package org.apache.hugegraph.testutil;
 
-import org.junit.Test;
-
 import org.apache.hugegraph.unit.BaseUnitTest;
+import org.junit.Test;
 
 public class AssertTest extends BaseUnitTest {
 
@@ -175,6 +174,12 @@ public class AssertTest extends BaseUnitTest {
             throw new RuntimeException();
         });
 
+        Throwable exception = Assert.assertThrows(RuntimeException.class, () 
-> {
+            throw new RuntimeException("fake-error");
+        });
+        Assert.assertInstanceOf(RuntimeException.class, exception);
+        Assert.assertEquals("fake-error", exception.getMessage());
+
         Assert.assertThrows(RuntimeException.class, () -> {
             throw new RuntimeException("fake-error");
         }, e -> {
@@ -183,7 +188,7 @@ public class AssertTest extends BaseUnitTest {
     }
 
     @Test
-    public void testAssertThrowsWithError() {
+    public void testAssertThrowsWithTypeError() {
         try {
             Assert.assertThrows(NullPointerException.class, () -> {
                 // pass
@@ -204,6 +209,22 @@ public class AssertTest extends BaseUnitTest {
         }
     }
 
+    @Test
+    public void testAssertThrowsWithMessageError() {
+        try {
+            Assert.assertThrows(RuntimeException.class, () -> {
+                throw new RuntimeException("fake-error");
+            }, e -> {
+                Assert.assertEquals("fake-error-typo", e.getMessage());
+            });
+            Assert.fail("Expect error");
+        } catch (Exception e) {
+            Assert.assertInstanceOf(RuntimeException.class, e); // not 
AssertionError
+            Assert.assertContains("expected:<fake-error[-typo]> but 
was:<fake-error[]>",
+                                  e.getMessage());
+        }
+    }
+
     @Test
     public void testAssertGt() {
         Assert.assertGt((byte) 1, Byte.valueOf("2"));
diff --git 
a/hugegraph-common/src/test/java/org/apache/hugegraph/unit/util/ReflectionUtilTest.java
 
b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/util/ReflectionUtilTest.java
index c83b2ad..f511ddf 100644
--- 
a/hugegraph-common/src/test/java/org/apache/hugegraph/unit/util/ReflectionUtilTest.java
+++ 
b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/util/ReflectionUtilTest.java
@@ -22,19 +22,19 @@ import java.lang.reflect.Method;
 import java.util.Comparator;
 import java.util.List;
 
-import org.junit.Test;
-
+import org.apache.commons.collections.IteratorUtils;
+import org.apache.hugegraph.perf.PerfUtil;
 import org.apache.hugegraph.testutil.Assert;
+import org.apache.hugegraph.unit.BaseUnitTest;
 import org.apache.hugegraph.unit.perf.testclass.TestClass;
 import org.apache.hugegraph.unit.perf.testclass.TestClass.Bar;
 import org.apache.hugegraph.unit.perf.testclass.TestClass.Base;
 import org.apache.hugegraph.unit.perf.testclass.TestClass.Foo;
 import org.apache.hugegraph.unit.perf.testclass.TestClass.ManuallyProfile;
 import org.apache.hugegraph.unit.perf.testclass.TestClass.Sub;
-import org.apache.hugegraph.perf.PerfUtil;
-import org.apache.hugegraph.unit.BaseUnitTest;
 import org.apache.hugegraph.util.ReflectionUtil;
-import org.apache.commons.collections.IteratorUtils;
+import org.junit.Test;
+
 import com.google.common.reflect.ClassPath.ClassInfo;
 
 import javassist.NotFoundException;
@@ -94,7 +94,7 @@ public class ReflectionUtilTest extends BaseUnitTest {
         @SuppressWarnings("unchecked")
         List<ClassInfo> classes = IteratorUtils.toList(ReflectionUtil.classes(
                                   "org.apache.hugegraph.util"));
-        Assert.assertEquals(18, classes.size());
+        Assert.assertEquals(19, classes.size());
         classes.sort(Comparator.comparing(ClassInfo::getName));
         Assert.assertEquals("org.apache.hugegraph.util.Bytes",
                             classes.get(0).getName());
@@ -102,8 +102,8 @@ public class ReflectionUtilTest extends BaseUnitTest {
                             classes.get(1).getName());
         Assert.assertEquals("org.apache.hugegraph.util.CollectionUtil",
                             classes.get(2).getName());
-        Assert.assertEquals("org.apache.hugegraph.util.VersionUtil",
-                            classes.get(17).getName());
+        Assert.assertEquals("org.apache.hugegraph.util.DateUtil",
+                            classes.get(3).getName());
     }
 
     @Test

Reply via email to