Skip to content
Permalink
Browse files
GEOMETRY-105 - Replace the try-catch pattern with assertThrows()
  • Loading branch information
arturobernalg committed Dec 17, 2020
1 parent ef65dd7 commit 5403a8d96e01d5bf8846d36955db35e618a139f7
Show file tree
Hide file tree
Showing 68 changed files with 711 additions and 1,220 deletions.
@@ -23,6 +23,7 @@
import java.util.regex.Pattern;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.function.Executable;

/** Class containing various geometry-related test utilities.
*/
@@ -50,61 +51,28 @@ 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
/** Asserts that the given Executable 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 executable the Executable 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 Executable executable, final Class<T> exceptionType, final String message) {
Assertions.assertEquals(message, Assertions.assertThrows(exceptionType, executable).getMessage());
}

/** Asserts that the given Runnable throws an exception of the given type. If
/** Asserts that the given Executable throws an exception of the given type. If
* {@code pattern} is not null, the exception message is asserted to match the
* given regex.
* @param r the Runnable instance
* @param executable the Executable instance
* @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 Executable executable, final Class<T> exceptionType, final Pattern pattern) {
final String message = Assertions.assertThrows(exceptionType, executable).getMessage();
Assertions.assertTrue(pattern.matcher(message).matches(),
"Expected exception message to match /" + pattern + "/ but was [" + message + "]");
}

/** Assert that a string contains a given substring value.
@@ -26,7 +26,6 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class IteratorTransformTest {

@@ -50,7 +49,7 @@ public void testThrowsNoSuchElement() {

// act/assert
Assertions.assertFalse(it.hasNext());
assertThrows(NoSuchElementException.class, it::next);
Assertions.assertThrows(NoSuchElementException.class, it::next);
}

private static <T> List<T> toList(final Iterator<T> it) {
@@ -33,6 +33,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;


public class AbstractConvexHyperplaneBoundedRegionTest {

@Test
@@ -41,9 +42,7 @@ public void testBoundaries_areUnmodifiable() {
final StubRegion region = new StubRegion(new ArrayList<>());

// act/assert
GeometryTestUtils.assertThrows(() -> {
region.getBoundaries().add(TestLine.X_AXIS.span());
}, UnsupportedOperationException.class);
Assertions.assertThrows(UnsupportedOperationException.class, () -> region.getBoundaries().add(TestLine.X_AXIS.span()));
}

@Test
@@ -631,17 +630,13 @@ public void testConvexRegionBoundaryBuilder_duplicateLines() {
@Test
public void testConvexRegionBoundaryBuilder() {
// act/assert
GeometryTestUtils.assertThrows(() -> {
StubRegion.fromBounds(Arrays.asList(TestLine.X_AXIS, TestLine.X_AXIS.reverse()));
}, IllegalArgumentException.class);
Assertions.assertThrows(IllegalArgumentException.class, () -> StubRegion.fromBounds(Arrays.asList(TestLine.X_AXIS, TestLine.X_AXIS.reverse())));
Assertions.assertThrows(IllegalArgumentException.class, () -> StubRegion.fromBounds(Arrays.asList(
TestLine.X_AXIS,
TestLine.Y_AXIS,
new TestLine(new TestPoint2D(1, 0), new TestPoint2D(0, -1)),
new TestLine(new TestPoint2D(1, 0), new TestPoint2D(0, -2)))));

GeometryTestUtils.assertThrows(() -> {
StubRegion.fromBounds(Arrays.asList(
TestLine.X_AXIS,
TestLine.Y_AXIS,
new TestLine(new TestPoint2D(1, 0), new TestPoint2D(0, -1)),
new TestLine(new TestPoint2D(1, 0), new TestPoint2D(0, -2))));
}, IllegalArgumentException.class);
}

@Test
@@ -38,7 +38,7 @@ public void testCtor_invalidTree() {
final TestRegionBSPTree tree = new TestRegionBSPTree(true);

// act/assert
GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
new TestRegionBuilder(tree);
}, IllegalArgumentException.class, "Tree must be empty");
}
@@ -65,7 +65,7 @@ public void testInsertPartition_cannotInsertAfterBoundary() {
builder.insertBoundary(new TestLineSegment(new TestPoint2D(0, 0), new TestPoint2D(1, 0)));

// act/assert
GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
builder.insertPartition(new TestLine(new TestPoint2D(0, 0), new TestPoint2D(1, 0)).span());
}, IllegalStateException.class, "Cannot insert partitions after boundaries have been inserted");
}
@@ -317,9 +317,9 @@ public void testSetLocation_noChange_doesNotInvalidateTree() {
@Test
public void testSetLocation_invalidArgs() {
// act/assert
GeometryTestUtils.assertThrows(() -> root.setLocation(null),
GeometryTestUtils.assertThrowsWithMessage(() -> root.setLocation(null),
IllegalArgumentException.class, "Invalid node location: null");
GeometryTestUtils.assertThrows(() -> root.setLocation(RegionLocation.BOUNDARY),
GeometryTestUtils.assertThrowsWithMessage(() -> root.setLocation(RegionLocation.BOUNDARY),
IllegalArgumentException.class, "Invalid node location: BOUNDARY");
}

@@ -20,6 +20,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;


public class EpsilonDoublePrecisionContextTest {

@Test
@@ -38,19 +39,17 @@ public void testGetters() {
@Test
public void testInvalidEpsilonValues() {
// act/assert
GeometryTestUtils.assertThrows(() -> {
new EpsilonDoublePrecisionContext(-1.0);
}, IllegalArgumentException.class);
Assertions.assertThrows(IllegalArgumentException.class, () -> new EpsilonDoublePrecisionContext(-1.0));

GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
new EpsilonDoublePrecisionContext(Double.NaN);
}, IllegalArgumentException.class, "Invalid epsilon value: NaN");

GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
new EpsilonDoublePrecisionContext(Double.POSITIVE_INFINITY);
}, IllegalArgumentException.class, "Invalid epsilon value: Infinity");

GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
new EpsilonDoublePrecisionContext(Double.NEGATIVE_INFINITY);
}, IllegalArgumentException.class, "Invalid epsilon value: -Infinity");
}
@@ -27,7 +27,6 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class EnclosingBallTest {

@@ -81,7 +80,7 @@ public void testGetSupport_listCannotBeModified() {
final EnclosingBall<Vector2D> ball = new EnclosingBall<>(Vector2D.of(1, 1), 4, support);

// act/assert
assertThrows(UnsupportedOperationException.class, () -> ball.getSupport().add(Vector2D.Unit.PLUS_X));
Assertions.assertThrows(UnsupportedOperationException.class, () -> ball.getSupport().add(Vector2D.Unit.PLUS_X));
}

@Test
@@ -46,11 +46,11 @@ public void testNoPoints() {
final String msg = "Unable to generate enclosing ball: no points given";

// act/assert
GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
encloser.enclose(null);
}, IllegalArgumentException.class, msg);

GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
encloser.enclose(new ArrayList<Vector3D>());
}, IllegalArgumentException.class, msg);
}
@@ -46,11 +46,11 @@ public void testNoPoints() {
final String msg = "Unable to generate enclosing ball: no points given";

// act/assert
GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
encloser.enclose(null);
}, IllegalArgumentException.class, msg);

GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
encloser.enclose(new ArrayList<Vector2D>());
}, IllegalArgumentException.class, msg);
}
@@ -92,19 +92,19 @@ public void testCheckDeterminantForInverse() {
@Test
public void testCheckDeterminantForInverse_invalid() {
// act/assert
GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
Matrices.checkDeterminantForInverse(0);
}, IllegalStateException.class, "Matrix is not invertible; matrix determinant is 0.0");

GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
Matrices.checkDeterminantForInverse(Double.NaN);
}, IllegalStateException.class, "Matrix is not invertible; matrix determinant is NaN");

GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
Matrices.checkDeterminantForInverse(Double.POSITIVE_INFINITY);
}, IllegalStateException.class, "Matrix is not invertible; matrix determinant is Infinity");

GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
Matrices.checkDeterminantForInverse(Double.NEGATIVE_INFINITY);
}, IllegalStateException.class, "Matrix is not invertible; matrix determinant is -Infinity");
}
@@ -121,15 +121,15 @@ public void testCheckElementForInverse() {
@Test
public void testCheckElementForInverse_invalid() {
// act/assert
GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
Matrices.checkElementForInverse(Double.NaN);
}, IllegalStateException.class, "Matrix is not invertible; invalid matrix element: NaN");

GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
Matrices.checkElementForInverse(Double.POSITIVE_INFINITY);
}, IllegalStateException.class, "Matrix is not invertible; invalid matrix element: Infinity");

GeometryTestUtils.assertThrows(() -> {
GeometryTestUtils.assertThrowsWithMessage(() -> {
Matrices.checkElementForInverse(Double.NEGATIVE_INFINITY);
}, IllegalStateException.class, "Matrix is not invertible; invalid matrix element: -Infinity");
}
@@ -53,13 +53,13 @@ public void testCheckedNorm_normArg() {
Assertions.assertEquals(-23.12, Vectors.checkedNorm(-23.12), EPS);
Assertions.assertEquals(-2e-12, Vectors.checkedNorm(-2e-12), EPS);

GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(0.0),
GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(0.0),
IllegalArgumentException.class, "Illegal norm: 0.0");
GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Double.NaN),
GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Double.NaN),
IllegalArgumentException.class, "Illegal norm: NaN");
GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Double.POSITIVE_INFINITY),
GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Double.POSITIVE_INFINITY),
IllegalArgumentException.class, "Illegal norm: Infinity");
GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Double.NEGATIVE_INFINITY),
GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Double.NEGATIVE_INFINITY),
IllegalArgumentException.class, "Illegal norm: -Infinity");
}

@@ -74,13 +74,13 @@ public void testCheckedNorm_vectorArg() {
Assertions.assertEquals(23.12, Vectors.checkedNorm(Vector1D.of(-23.12)), EPS);
Assertions.assertEquals(2e-12, Vectors.checkedNorm(Vector1D.of(-2e-12)), EPS);

GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Vector3D.ZERO),
GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Vector3D.ZERO),
IllegalArgumentException.class, "Illegal norm: 0.0");
GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Vector3D.NaN),
GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Vector3D.NaN),
IllegalArgumentException.class, "Illegal norm: NaN");
GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Vector3D.POSITIVE_INFINITY),
GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Vector3D.POSITIVE_INFINITY),
IllegalArgumentException.class, "Illegal norm: NaN");
GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Vector3D.NEGATIVE_INFINITY),
GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Vector3D.NEGATIVE_INFINITY),
IllegalArgumentException.class, "Illegal norm: NaN");
}

0 comments on commit 5403a8d

Please sign in to comment.