changed example for finder of root cause exception

This commit is contained in:
Marcos Lopez Gonzalez 2019-04-29 19:58:51 +02:00 committed by mikr
parent c223b669be
commit c59d9dbf8f
2 changed files with 94 additions and 31 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}