You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2020/12/16 17:37:11 UTC

[GitHub] [commons-geometry] singhbaljit commented on a change in pull request #121: [GEOMETRY-105] - Replace the try-catch pattern with assertThrows()

singhbaljit commented on a change in pull request #121:
URL: https://github.com/apache/commons-geometry/pull/121#discussion_r544480779



##########
File path: commons-geometry-core/src/test/java/org/apache/commons/geometry/core/GeometryTestUtils.java
##########
@@ -50,35 +50,17 @@ public static void assertNegativeInfinity(final double value) {
         Assertions.assertTrue(value < 0, msg);
     }
 
-    /** Asserts that the given Runnable throws an exception of the given type.
-     * @param r the Runnable instance
-     * @param exceptionType the expected exception type
-     */
-    public static void assertThrows(final Runnable r, final Class<?> exceptionType) {
-        assertThrows(r, exceptionType, (String) null);
-    }
-
     /** Asserts that the given Runnable throws an exception of the given type. If
      * {@code message} is not null, the exception message is asserted to equal the
      * given value.
      * @param r the Runnable instance
      * @param exceptionType the expected exception type
      * @param message the expected exception message; ignored if null
      */
-    public static void assertThrows(final Runnable r, final Class<?> exceptionType, final String message) {
-        try {
-            r.run();
-            Assertions.fail("Operation should have thrown an exception");
-        } catch (final Exception exc) {
-            final Class<?> actualType = exc.getClass();
-
-            Assertions.assertTrue(exceptionType.isAssignableFrom(actualType),
-                    "Expected exception of type " + exceptionType.getName() + " but was " + actualType.getName());
-
-            if (message != null) {
-                Assertions.assertEquals(message, exc.getMessage());
-            }
-        }
+    public static <T extends Throwable> void assertThrowsWithMessage(final Runnable r, final Class<T> exceptionType, final String message) {

Review comment:
       It is safe to replace `Runnable` with `org.junit.jupiter.api.function.Executable`. Lambda signature is equivalent, so this is a non-breaking change. You can use pass the along the `r` instead of creating another lambda instance as `r::run`.

##########
File path: commons-geometry-core/src/test/java/org/apache/commons/geometry/core/GeometryTestUtils.java
##########
@@ -50,35 +50,17 @@ public static void assertNegativeInfinity(final double value) {
         Assertions.assertTrue(value < 0, msg);
     }
 
-    /** Asserts that the given Runnable throws an exception of the given type.
-     * @param r the Runnable instance
-     * @param exceptionType the expected exception type
-     */
-    public static void assertThrows(final Runnable r, final Class<?> exceptionType) {
-        assertThrows(r, exceptionType, (String) null);
-    }
-
     /** Asserts that the given Runnable throws an exception of the given type. If
      * {@code message} is not null, the exception message is asserted to equal the
      * given value.
      * @param r the Runnable instance
      * @param exceptionType the expected exception type
      * @param message the expected exception message; ignored if null
      */
-    public static void assertThrows(final Runnable r, final Class<?> exceptionType, final String message) {
-        try {
-            r.run();
-            Assertions.fail("Operation should have thrown an exception");
-        } catch (final Exception exc) {
-            final Class<?> actualType = exc.getClass();
-
-            Assertions.assertTrue(exceptionType.isAssignableFrom(actualType),
-                    "Expected exception of type " + exceptionType.getName() + " but was " + actualType.getName());
-
-            if (message != null) {
-                Assertions.assertEquals(message, exc.getMessage());
-            }
-        }
+    public static <T extends Throwable> void assertThrowsWithMessage(final Runnable r, final Class<T> exceptionType, final String message) {
+        final Throwable exc = Assertions.assertThrows(exceptionType, r::run);
+        Assertions.assertNotNull(message);

Review comment:
       This should not be asserted. `null` is a valid expected message.

##########
File path: commons-geometry-core/src/test/java/org/apache/commons/geometry/core/GeometryTestUtils.java
##########
@@ -88,23 +70,15 @@ public static void assertThrows(final Runnable r, final Class<?> exceptionType,
      * @param exceptionType the expected exception type
      * @param pattern regex pattern to match; ignored if null
      */
-    public static void assertThrows(final Runnable r, final Class<?> exceptionType, final Pattern pattern) {
-        try {
-            r.run();
-            Assertions.fail("Operation should have thrown an exception");
-        } catch (final Exception exc) {
-            final Class<?> actualType = exc.getClass();
-
-            Assertions.assertTrue(exceptionType.isAssignableFrom(actualType),
-                    "Expected exception of type " + exceptionType.getName() + " but was " + actualType.getName());
-
-            if (pattern != null) {
-                final String message = exc.getMessage();
-
-                final String err = "Expected exception message to match /" + pattern + "/ but was [" + message + "]";
-                Assertions.assertTrue(pattern.matcher(message).matches(), err);
-            }
-        }
+    public static <T extends Throwable> void assertThrowsWithMessage(final Runnable r, final Class<T> exceptionType, final Pattern pattern) {
+        final Throwable exc = Assertions.assertThrows(exceptionType, r::run);
+        Assertions.assertNotNull(pattern);
+        final Class<?> actualType = exc.getClass();
+        Assertions.assertTrue(exceptionType.isAssignableFrom(actualType),
+                "Expected exception of type " + exceptionType.getName() + " but was " + actualType.getName());
+        final String message = exc.getMessage();
+        final String err = "Expected exception message to match /" + pattern + "/ but was [" + message + "]";
+        Assertions.assertTrue(pattern.matcher(message).matches(), err);

Review comment:
       Simplified version (style choice):
   ```java
   Assertions.assertNotNull(pattern);
   
   final String message = Assertions.assertThrows(exceptionType, r).getMessage();
   Assertions.assertTrue(pattern.matcher(message).matches(), 
     "Expected exception message to match /" + pattern + "/ but was [" + message + "]");
   ```

##########
File path: commons-geometry-core/src/test/java/org/apache/commons/geometry/core/GeometryTestUtils.java
##########
@@ -50,35 +50,17 @@ public static void assertNegativeInfinity(final double value) {
         Assertions.assertTrue(value < 0, msg);
     }
 
-    /** Asserts that the given Runnable throws an exception of the given type.
-     * @param r the Runnable instance
-     * @param exceptionType the expected exception type
-     */
-    public static void assertThrows(final Runnable r, final Class<?> exceptionType) {
-        assertThrows(r, exceptionType, (String) null);
-    }
-
     /** Asserts that the given Runnable throws an exception of the given type. If
      * {@code message} is not null, the exception message is asserted to equal the
      * given value.
      * @param r the Runnable instance
      * @param exceptionType the expected exception type
      * @param message the expected exception message; ignored if null
      */
-    public static void assertThrows(final Runnable r, final Class<?> exceptionType, final String message) {
-        try {
-            r.run();
-            Assertions.fail("Operation should have thrown an exception");
-        } catch (final Exception exc) {
-            final Class<?> actualType = exc.getClass();
-
-            Assertions.assertTrue(exceptionType.isAssignableFrom(actualType),
-                    "Expected exception of type " + exceptionType.getName() + " but was " + actualType.getName());
-
-            if (message != null) {
-                Assertions.assertEquals(message, exc.getMessage());
-            }
-        }
+    public static <T extends Throwable> void assertThrowsWithMessage(final Runnable r, final Class<T> exceptionType, final String message) {

Review comment:
       This can be a one-liner (style choice):
   ```java
   Assertions.assertEquals(message, Assertions.assertThrows(exceptionType, r).getMessage())
   ```

##########
File path: commons-geometry-core/src/test/java/org/apache/commons/geometry/core/GeometryTestUtils.java
##########
@@ -88,23 +70,15 @@ public static void assertThrows(final Runnable r, final Class<?> exceptionType,
      * @param exceptionType the expected exception type
      * @param pattern regex pattern to match; ignored if null
      */
-    public static void assertThrows(final Runnable r, final Class<?> exceptionType, final Pattern pattern) {
-        try {
-            r.run();
-            Assertions.fail("Operation should have thrown an exception");
-        } catch (final Exception exc) {
-            final Class<?> actualType = exc.getClass();
-
-            Assertions.assertTrue(exceptionType.isAssignableFrom(actualType),
-                    "Expected exception of type " + exceptionType.getName() + " but was " + actualType.getName());
-
-            if (pattern != null) {
-                final String message = exc.getMessage();
-
-                final String err = "Expected exception message to match /" + pattern + "/ but was [" + message + "]";
-                Assertions.assertTrue(pattern.matcher(message).matches(), err);
-            }
-        }
+    public static <T extends Throwable> void assertThrowsWithMessage(final Runnable r, final Class<T> exceptionType, final Pattern pattern) {
+        final Throwable exc = Assertions.assertThrows(exceptionType, r::run);
+        Assertions.assertNotNull(pattern);
+        final Class<?> actualType = exc.getClass();
+        Assertions.assertTrue(exceptionType.isAssignableFrom(actualType),

Review comment:
       Unnecessary assertion. This will always pass.

##########
File path: commons-geometry-core/src/test/java/org/apache/commons/geometry/core/GeometryTestUtils.java
##########
@@ -88,23 +70,15 @@ public static void assertThrows(final Runnable r, final Class<?> exceptionType,
      * @param exceptionType the expected exception type
      * @param pattern regex pattern to match; ignored if null
      */
-    public static void assertThrows(final Runnable r, final Class<?> exceptionType, final Pattern pattern) {
-        try {
-            r.run();
-            Assertions.fail("Operation should have thrown an exception");
-        } catch (final Exception exc) {
-            final Class<?> actualType = exc.getClass();
-
-            Assertions.assertTrue(exceptionType.isAssignableFrom(actualType),
-                    "Expected exception of type " + exceptionType.getName() + " but was " + actualType.getName());
-
-            if (pattern != null) {
-                final String message = exc.getMessage();
-
-                final String err = "Expected exception message to match /" + pattern + "/ but was [" + message + "]";
-                Assertions.assertTrue(pattern.matcher(message).matches(), err);
-            }
-        }
+    public static <T extends Throwable> void assertThrowsWithMessage(final Runnable r, final Class<T> exceptionType, final Pattern pattern) {

Review comment:
       Same comment for `Runnable`.

##########
File path: commons-geometry-core/src/test/java/org/apache/commons/geometry/core/partitioning/AbstractConvexHyperplaneBoundedRegionTest.java
##########
@@ -33,6 +33,8 @@
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
+import static org.junit.jupiter.api.Assertions.assertThrows;

Review comment:
       Static imports are fine IMO, but stylistically, the library tests are written `Assertions.assert*`. So, we should keep it consistent.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org