changed example for finder of root cause exception
This commit is contained in:
parent
c223b669be
commit
c59d9dbf8f
@ -1,5 +1,8 @@
|
|||||||
package com.baeldung.exceptions;
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.Period;
|
||||||
|
import java.time.format.DateTimeParseException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -16,44 +19,75 @@ public class RootCauseFinder {
|
|||||||
return rootCause;
|
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 {
|
public static int calculateAge(String birthDate) throws CalculationException {
|
||||||
if (input == null || input.isEmpty()) {
|
if (birthDate == null || birthDate.isEmpty()) {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return new IntParser().stringToInt(input.trim());
|
return calculateDifference(birthDate).getYears();
|
||||||
} catch (NaNException ex) {
|
} catch (DateParseException ex) {
|
||||||
throw new InvalidNumber(input, ex);
|
throw new CalculationException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int stringToInt(String numberAsString) throws NaNException {
|
private static Period calculateDifference(String birthDateAsString) throws DateParseException {
|
||||||
|
|
||||||
|
LocalDate birthDate = null;
|
||||||
try {
|
try {
|
||||||
return Integer.valueOf(numberAsString);
|
birthDate = LocalDate.parse(birthDateAsString);
|
||||||
} catch (NumberFormatException ex) {
|
} catch (DateTimeParseException ex) {
|
||||||
throw new NaNException(numberAsString, 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) {
|
CalculationException(DateParseException ex) {
|
||||||
super("Invalid input for a number: " + input, thr);
|
super(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class NaNException extends Exception {
|
static class DateParseException extends Exception {
|
||||||
|
|
||||||
NaNException(String number, Throwable thr) {
|
DateParseException(String input) {
|
||||||
super(number + "is not a number", thr);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ import com.google.common.base.Throwables;
|
|||||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.time.format.DateTimeParseException;
|
||||||
|
|
||||||
import static com.baeldung.exceptions.RootCauseFinder.*;
|
import static com.baeldung.exceptions.RootCauseFinder.*;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
@ -13,38 +15,65 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||||||
public class RootCauseFinderTest {
|
public class RootCauseFinderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNestedException_whenFindingRootCauseUsingJava_thenRootCauseFound() {
|
public void givenWrongFormatDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
|
||||||
try {
|
try {
|
||||||
IntParser.parse("text");
|
AgeCalculator.calculateAge("010102");
|
||||||
} catch (InvalidNumber ex) {
|
} catch (CalculationException ex) {
|
||||||
assertTrue(findCauseUsingPlainJava(ex) instanceof NumberFormatException);
|
assertTrue(findCauseUsingPlainJava(ex) instanceof DateTimeParseException);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNonNestedException_whenFindingRootCauseUsingJava_thenRootCauseFound() {
|
public void givenOutOfRangeDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
|
||||||
try {
|
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) {
|
} catch (Exception ex) {
|
||||||
assertTrue(findCauseUsingPlainJava(ex) instanceof IllegalArgumentException);
|
assertTrue(findCauseUsingPlainJava(ex) instanceof IllegalArgumentException);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNestedException_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() {
|
public void givenWrongFormatDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() {
|
||||||
try {
|
try {
|
||||||
IntParser.parse("text");
|
AgeCalculator.calculateAge("010102");
|
||||||
} catch (InvalidNumber ex) {
|
} catch (CalculationException ex) {
|
||||||
assertTrue(ExceptionUtils.getRootCause(ex) instanceof NumberFormatException);
|
assertTrue(ExceptionUtils.getRootCause(ex) instanceof DateTimeParseException);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNestedException_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
|
public void givenOutOfRangeDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() {
|
||||||
try {
|
try {
|
||||||
IntParser.parse("text");
|
AgeCalculator.calculateAge("2020-04-04");
|
||||||
} catch (InvalidNumber ex) {
|
} catch (CalculationException ex) {
|
||||||
assertTrue(Throwables.getRootCause(ex) instanceof NumberFormatException);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user