From 921f53c8b67dd9b9476305428aee8a35196f147b Mon Sep 17 00:00:00 2001 From: Gerald Unterrainer Date: Thu, 30 Sep 2021 14:58:45 +0200 Subject: [PATCH 1/3] update tests --- .../commons/jreutils/ExceptionsTests.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/test/java/info/unterrainer/commons/jreutils/ExceptionsTests.java b/src/test/java/info/unterrainer/commons/jreutils/ExceptionsTests.java index 3d6d3df..f4daf45 100644 --- a/src/test/java/info/unterrainer/commons/jreutils/ExceptionsTests.java +++ b/src/test/java/info/unterrainer/commons/jreutils/ExceptionsTests.java @@ -1,11 +1,14 @@ package info.unterrainer.commons.jreutils; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import org.junit.jupiter.api.Test; public class ExceptionsTests { @Test - public void BeforeTest() { + public void codeBeforeTest() { try { // Some method you don't have control over throwing an exception. throw new IllegalArgumentException(); @@ -15,9 +18,18 @@ public void BeforeTest() { } @Test - public void AfterTest() { + public void throwsExceptionWithoutSwallow() { + assertThrows(IllegalArgumentException.class, () -> { + // Some method you don't have control over throwing an exception. + throw new IllegalArgumentException(); + }); + } + + @Test + public void swallowsException() { Exceptions.swallow(() -> { throw new IllegalArgumentException(); }, IllegalArgumentException.class, NumberFormatException.class); + assertTrue(true); } } From a55e718c1d7bd6e0f7760726d4b54473bedbfbfd Mon Sep 17 00:00:00 2001 From: Gerald Unterrainer Date: Thu, 30 Sep 2021 16:01:43 +0200 Subject: [PATCH 2/3] update tests of Exceptions.swallow and change containsException logic to recursively scan causes as well --- .../commons/jreutils/Exceptions.java | 17 +++++++++++++---- .../commons/jreutils/ExceptionsTests.java | 18 +++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main/java/info/unterrainer/commons/jreutils/Exceptions.java b/src/main/java/info/unterrainer/commons/jreutils/Exceptions.java index 8e03047..de3b3a3 100644 --- a/src/main/java/info/unterrainer/commons/jreutils/Exceptions.java +++ b/src/main/java/info/unterrainer/commons/jreutils/Exceptions.java @@ -39,7 +39,7 @@ public static void swallow(final Runnable runnable, final Class... exceptions public static T swallowReturning(final Supplier supplier, final Class... exceptions) { try { return supplier.get(); - } catch (Exception e) { + } catch (Throwable e) { if (!containsException(e, exceptions)) throw e; return null; @@ -95,7 +95,7 @@ public static T retryReturning(final int times, final long backOffInMillis, for (int i = 0; i < times; i++) try { result = supplier.get(); - } catch (Exception e) { + } catch (Throwable e) { if (!containsException(e, exceptions) || i == times - 1) throw e; try { @@ -107,10 +107,19 @@ public static T retryReturning(final int times, final long backOffInMillis, return result; } - public static boolean containsException(final Exception e, final Class... exceptions) { + public static boolean containsException(final Throwable e, final Class... exceptions) { for (Class omit : exceptions) - if (omit.isAssignableFrom(e.getClass())) + if (equalsOrCauseEquals(e, omit)) return true; return false; } + + private static boolean equalsOrCauseEquals(final Throwable e, final Class omit) { + if (omit.isAssignableFrom(e.getClass())) + return true; + Throwable cause = e.getCause(); + if (cause != null && equalsOrCauseEquals(cause, omit)) + return true; + return false; + } } diff --git a/src/test/java/info/unterrainer/commons/jreutils/ExceptionsTests.java b/src/test/java/info/unterrainer/commons/jreutils/ExceptionsTests.java index f4daf45..3832858 100644 --- a/src/test/java/info/unterrainer/commons/jreutils/ExceptionsTests.java +++ b/src/test/java/info/unterrainer/commons/jreutils/ExceptionsTests.java @@ -26,10 +26,26 @@ public void throwsExceptionWithoutSwallow() { } @Test - public void swallowsException() { + public void swallowsWhenExceptionMatches() { Exceptions.swallow(() -> { throw new IllegalArgumentException(); }, IllegalArgumentException.class, NumberFormatException.class); assertTrue(true); } + + @Test + public void swallowsWhenCauseMatches() { + Exceptions.swallow(() -> { + throw new IllegalArgumentException("", new NumberFormatException()); + }, NumberFormatException.class); + assertTrue(true); + } + + @Test + public void swallowsWhenMultiLayeredCauseMatches() { + Exceptions.swallow(() -> { + throw new IllegalArgumentException("", new IllegalArgumentException("", new NumberFormatException())); + }, NumberFormatException.class); + assertTrue(true); + } } From dd0dc252b5c6cc1e999fd4b2f295100ae67e2a54 Mon Sep 17 00:00:00 2001 From: Gerald Unterrainer Date: Thu, 30 Sep 2021 16:02:13 +0200 Subject: [PATCH 3/3] bump pom version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e54caf3..9fa1460 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ 4.0.0 jre-utils - 0.2.4 + 0.2.5 JreUtils jar