From c223b669be5cc7d913aac5821bd9af034a7a543f Mon Sep 17 00:00:00 2001 From: Marcos Lopez Gonzalez Date: Mon, 15 Apr 2019 20:47:23 +0200 Subject: [PATCH] root cause finder --- .../baeldung/exceptions/RootCauseFinder.java | 61 +++++++++++++++++++ .../exceptions/RootCauseFinderTest.java | 51 ++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/exceptions/RootCauseFinder.java create mode 100644 core-java-modules/core-java/src/test/java/com/baeldung/exceptions/RootCauseFinderTest.java diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/exceptions/RootCauseFinder.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/exceptions/RootCauseFinder.java new file mode 100644 index 0000000000..a7963c35a8 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/exceptions/RootCauseFinder.java @@ -0,0 +1,61 @@ +package com.baeldung.exceptions; + +import java.util.Objects; + +/** + * Utility class to find root cause exceptions. + */ +public class RootCauseFinder { + + public static Throwable findCauseUsingPlainJava(Throwable throwable) { + Objects.requireNonNull(throwable); + Throwable rootCause = throwable; + while (rootCause.getCause() != null) { + rootCause = rootCause.getCause(); + } + return rootCause; + } + + static class IntParser { + + private IntParser() { + } + + public static int parse(String input) throws InvalidNumber { + if (input == null || input.isEmpty()) { + throw new IllegalArgumentException(); + } + + try { + return new IntParser().stringToInt(input.trim()); + } catch (NaNException ex) { + throw new InvalidNumber(input, ex); + } + } + + private int stringToInt(String numberAsString) throws NaNException { + try { + return Integer.valueOf(numberAsString); + } catch (NumberFormatException ex) { + throw new NaNException(numberAsString, ex); + } + } + + } + + static class InvalidNumber extends Exception { + + InvalidNumber(String input, Throwable thr) { + super("Invalid input for a number: " + input, thr); + } + } + + static class NaNException extends Exception { + + NaNException(String number, Throwable thr) { + super(number + "is not a number", thr); + } + + } + +} diff --git a/core-java-modules/core-java/src/test/java/com/baeldung/exceptions/RootCauseFinderTest.java b/core-java-modules/core-java/src/test/java/com/baeldung/exceptions/RootCauseFinderTest.java new file mode 100644 index 0000000000..1e58e3f602 --- /dev/null +++ b/core-java-modules/core-java/src/test/java/com/baeldung/exceptions/RootCauseFinderTest.java @@ -0,0 +1,51 @@ +package com.baeldung.exceptions; + +import com.google.common.base.Throwables; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.junit.jupiter.api.Test; + +import static com.baeldung.exceptions.RootCauseFinder.*; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests the {@link RootCauseFinder}. + */ +public class RootCauseFinderTest { + + @Test + public void givenNestedException_whenFindingRootCauseUsingJava_thenRootCauseFound() { + try { + IntParser.parse("text"); + } catch (InvalidNumber ex) { + assertTrue(findCauseUsingPlainJava(ex) instanceof NumberFormatException); + } + } + + @Test + public void givenNonNestedException_whenFindingRootCauseUsingJava_thenRootCauseFound() { + try { + IntParser.parse(null); + } catch (Exception ex) { + assertTrue(findCauseUsingPlainJava(ex) instanceof IllegalArgumentException); + } + } + + @Test + public void givenNestedException_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() { + try { + IntParser.parse("text"); + } catch (InvalidNumber ex) { + assertTrue(ExceptionUtils.getRootCause(ex) instanceof NumberFormatException); + } + } + + @Test + public void givenNestedException_whenFindingRootCauseUsingGuava_thenRootCauseFound() { + try { + IntParser.parse("text"); + } catch (InvalidNumber ex) { + assertTrue(Throwables.getRootCause(ex) instanceof NumberFormatException); + } + } + +}