From c59d9dbf8f886a80d1b1dd2a7b1e3cc2593099e6 Mon Sep 17 00:00:00 2001 From: Marcos Lopez Gonzalez Date: Mon, 29 Apr 2019 19:58:51 +0200 Subject: [PATCH] changed example for finder of root cause exception --- .../baeldung/exceptions/RootCauseFinder.java | 68 ++++++++++++++----- .../exceptions/RootCauseFinderTest.java | 57 ++++++++++++---- 2 files changed, 94 insertions(+), 31 deletions(-) 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 index a7963c35a8..11f29a2cf0 100644 --- 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 @@ -1,5 +1,8 @@ package com.baeldung.exceptions; +import java.time.LocalDate; +import java.time.Period; +import java.time.format.DateTimeParseException; import java.util.Objects; /** @@ -16,44 +19,75 @@ public class RootCauseFinder { return rootCause; } - static class IntParser { + /** + * Calculates the age of a person from a given date. + */ + static class AgeCalculator { - private IntParser() { + private AgeCalculator() { } - public static int parse(String input) throws InvalidNumber { - if (input == null || input.isEmpty()) { + public static int calculateAge(String birthDate) throws CalculationException { + if (birthDate == null || birthDate.isEmpty()) { throw new IllegalArgumentException(); } try { - return new IntParser().stringToInt(input.trim()); - } catch (NaNException ex) { - throw new InvalidNumber(input, ex); + return calculateDifference(birthDate).getYears(); + } catch (DateParseException ex) { + throw new CalculationException(ex); } } - private int stringToInt(String numberAsString) throws NaNException { + private static Period calculateDifference(String birthDateAsString) throws DateParseException { + + LocalDate birthDate = null; try { - return Integer.valueOf(numberAsString); - } catch (NumberFormatException ex) { - throw new NaNException(numberAsString, ex); + birthDate = LocalDate.parse(birthDateAsString); + } catch (DateTimeParseException ex) { + throw new InvalidFormatException(birthDateAsString, ex); } + + LocalDate today = LocalDate.now(); + + if (birthDate.isAfter(today)) { + throw new DateOutOfRangeException(birthDateAsString); + } + + return Period.between(birthDate, today); } } - static class InvalidNumber extends Exception { + static class CalculationException extends Exception { - InvalidNumber(String input, Throwable thr) { - super("Invalid input for a number: " + input, thr); + CalculationException(DateParseException ex) { + super(ex); } } - static class NaNException extends Exception { + static class DateParseException extends Exception { - NaNException(String number, Throwable thr) { - super(number + "is not a number", thr); + DateParseException(String input) { + super(input); + } + + DateParseException(String input, Throwable thr) { + super(input, thr); + } + } + + static class InvalidFormatException extends DateParseException { + + InvalidFormatException(String input, Throwable thr) { + super("Invalid date format: " + input, thr); + } + } + + static class DateOutOfRangeException extends DateParseException { + + DateOutOfRangeException(String date) { + super("Date out of range: " + date); } } 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 index 1e58e3f602..c333a7ac26 100644 --- 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 @@ -4,6 +4,8 @@ import com.google.common.base.Throwables; import org.apache.commons.lang3.exception.ExceptionUtils; import org.junit.jupiter.api.Test; +import java.time.format.DateTimeParseException; + import static com.baeldung.exceptions.RootCauseFinder.*; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -13,38 +15,65 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class RootCauseFinderTest { @Test - public void givenNestedException_whenFindingRootCauseUsingJava_thenRootCauseFound() { + public void givenWrongFormatDate_whenFindingRootCauseUsingJava_thenRootCauseFound() { try { - IntParser.parse("text"); - } catch (InvalidNumber ex) { - assertTrue(findCauseUsingPlainJava(ex) instanceof NumberFormatException); + AgeCalculator.calculateAge("010102"); + } catch (CalculationException ex) { + assertTrue(findCauseUsingPlainJava(ex) instanceof DateTimeParseException); } } @Test - public void givenNonNestedException_whenFindingRootCauseUsingJava_thenRootCauseFound() { + public void givenOutOfRangeDate_whenFindingRootCauseUsingJava_thenRootCauseFound() { try { - IntParser.parse(null); + AgeCalculator.calculateAge("2020-04-04"); + } catch (CalculationException ex) { + assertTrue(findCauseUsingPlainJava(ex) instanceof DateOutOfRangeException); + } + } + + @Test + public void givenNullDate_whenFindingRootCauseUsingJava_thenRootCauseFound() { + try { + AgeCalculator.calculateAge(null); } catch (Exception ex) { assertTrue(findCauseUsingPlainJava(ex) instanceof IllegalArgumentException); } } @Test - public void givenNestedException_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() { + public void givenWrongFormatDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() { try { - IntParser.parse("text"); - } catch (InvalidNumber ex) { - assertTrue(ExceptionUtils.getRootCause(ex) instanceof NumberFormatException); + AgeCalculator.calculateAge("010102"); + } catch (CalculationException ex) { + assertTrue(ExceptionUtils.getRootCause(ex) instanceof DateTimeParseException); } } @Test - public void givenNestedException_whenFindingRootCauseUsingGuava_thenRootCauseFound() { + public void givenOutOfRangeDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() { try { - IntParser.parse("text"); - } catch (InvalidNumber ex) { - assertTrue(Throwables.getRootCause(ex) instanceof NumberFormatException); + AgeCalculator.calculateAge("2020-04-04"); + } catch (CalculationException ex) { + assertTrue(ExceptionUtils.getRootCause(ex) instanceof DateOutOfRangeException); + } + } + + @Test + public void givenWrongFormatDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() { + try { + AgeCalculator.calculateAge("010102"); + } catch (CalculationException ex) { + assertTrue(Throwables.getRootCause(ex) instanceof DateTimeParseException); + } + } + + @Test + public void givenOutOfRangeDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() { + try { + AgeCalculator.calculateAge("2020-04-04"); + } catch (CalculationException ex) { + assertTrue(Throwables.getRootCause(ex) instanceof DateOutOfRangeException); } }