Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 89dddae

Browse filesBrowse files
marcos-lgmikr
authored andcommitted
changed example for finder of root cause exception
1 parent 0d96085 commit 89dddae
Copy full SHA for 89dddae

File tree

Expand file treeCollapse file tree

2 files changed

+94
-31
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+94
-31
lines changed
Open diff view settings
Collapse file

‎core-java-modules/core-java-lang-2/src/test/java/com/baeldung/exceptions/RootCauseFinder.java‎

Copy file name to clipboardExpand all lines: core-java-modules/core-java-lang-2/src/test/java/com/baeldung/exceptions/RootCauseFinder.java
+51-17Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.baeldung.exceptions;
22

3+
import java.time.LocalDate;
4+
import java.time.Period;
5+
import java.time.format.DateTimeParseException;
36
import java.util.Objects;
47

58
/**
@@ -16,44 +19,75 @@ public static Throwable findCauseUsingPlainJava(Throwable throwable) {
1619
return rootCause;
1720
}
1821

19-
static class IntParser {
22+
/**
23+
* Calculates the age of a person from a given date.
24+
*/
25+
static class AgeCalculator {
2026

21-
private IntParser() {
27+
private AgeCalculator() {
2228
}
2329

24-
public static int parse(String input) throws InvalidNumber {
25-
if (input == null || input.isEmpty()) {
30+
public static int calculateAge(String birthDate) throws CalculationException {
31+
if (birthDate == null || birthDate.isEmpty()) {
2632
throw new IllegalArgumentException();
2733
}
2834

2935
try {
30-
return new IntParser().stringToInt(input.trim());
31-
} catch (NaNException ex) {
32-
throw new InvalidNumber(input, ex);
36+
return calculateDifference(birthDate).getYears();
37+
} catch (DateParseException ex) {
38+
throw new CalculationException(ex);
3339
}
3440
}
3541

36-
private int stringToInt(String numberAsString) throws NaNException {
42+
private static Period calculateDifference(String birthDateAsString) throws DateParseException {
43+
44+
LocalDate birthDate = null;
3745
try {
38-
return Integer.valueOf(numberAsString);
39-
} catch (NumberFormatException ex) {
40-
throw new NaNException(numberAsString, ex);
46+
birthDate = LocalDate.parse(birthDateAsString);
47+
} catch (DateTimeParseException ex) {
48+
throw new InvalidFormatException(birthDateAsString, ex);
49+
}
50+
51+
LocalDate today = LocalDate.now();
52+
53+
if (birthDate.isAfter(today)) {
54+
throw new DateOutOfRangeException(birthDateAsString);
4155
}
56+
57+
return Period.between(birthDate, today);
4258
}
4359

4460
}
4561

46-
static class InvalidNumber extends Exception {
62+
static class CalculationException extends Exception {
63+
64+
CalculationException(DateParseException ex) {
65+
super(ex);
66+
}
67+
}
68+
69+
static class DateParseException extends Exception {
70+
71+
DateParseException(String input) {
72+
super(input);
73+
}
74+
75+
DateParseException(String input, Throwable thr) {
76+
super(input, thr);
77+
}
78+
}
79+
80+
static class InvalidFormatException extends DateParseException {
4781

48-
InvalidNumber(String input, Throwable thr) {
49-
super("Invalid input for a number: " + input, thr);
82+
InvalidFormatException(String input, Throwable thr) {
83+
super("Invalid date format: " + input, thr);
5084
}
5185
}
5286

53-
static class NaNException extends Exception {
87+
static class DateOutOfRangeException extends DateParseException {
5488

55-
NaNException(String number, Throwable thr) {
56-
super(number + "is not a number", thr);
89+
DateOutOfRangeException(String date) {
90+
super("Date out of range: " + date);
5791
}
5892

5993
}
Collapse file

‎core-java-modules/core-java/src/test/java/com/baeldung/exceptions/RootCauseFinderTest.java‎

Copy file name to clipboardExpand all lines: core-java-modules/core-java/src/test/java/com/baeldung/exceptions/RootCauseFinderTest.java
+43-14Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.apache.commons.lang3.exception.ExceptionUtils;
55
import org.junit.jupiter.api.Test;
66

7+
import java.time.format.DateTimeParseException;
8+
79
import static com.baeldung.exceptions.RootCauseFinder.*;
810
import static org.junit.jupiter.api.Assertions.assertTrue;
911

@@ -13,38 +15,65 @@
1315
public class RootCauseFinderTest {
1416

1517
@Test
16-
public void givenNestedException_whenFindingRootCauseUsingJava_thenRootCauseFound() {
18+
public void givenWrongFormatDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
19+
try {
20+
AgeCalculator.calculateAge("010102");
21+
} catch (CalculationException ex) {
22+
assertTrue(findCauseUsingPlainJava(ex) instanceof DateTimeParseException);
23+
}
24+
}
25+
26+
@Test
27+
public void givenOutOfRangeDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
1728
try {
18-
IntParser.parse("text");
19-
} catch (InvalidNumber ex) {
20-
assertTrue(findCauseUsingPlainJava(ex) instanceof NumberFormatException);
29+
AgeCalculator.calculateAge("2020-04-04");
30+
} catch (CalculationException ex) {
31+
assertTrue(findCauseUsingPlainJava(ex) instanceof DateOutOfRangeException);
2132
}
2233
}
2334

2435
@Test
25-
public void givenNonNestedException_whenFindingRootCauseUsingJava_thenRootCauseFound() {
36+
public void givenNullDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
2637
try {
27-
IntParser.parse(null);
38+
AgeCalculator.calculateAge(null);
2839
} catch (Exception ex) {
2940
assertTrue(findCauseUsingPlainJava(ex) instanceof IllegalArgumentException);
3041
}
3142
}
3243

3344
@Test
34-
public void givenNestedException_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() {
45+
public void givenWrongFormatDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() {
46+
try {
47+
AgeCalculator.calculateAge("010102");
48+
} catch (CalculationException ex) {
49+
assertTrue(ExceptionUtils.getRootCause(ex) instanceof DateTimeParseException);
50+
}
51+
}
52+
53+
@Test
54+
public void givenOutOfRangeDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() {
55+
try {
56+
AgeCalculator.calculateAge("2020-04-04");
57+
} catch (CalculationException ex) {
58+
assertTrue(ExceptionUtils.getRootCause(ex) instanceof DateOutOfRangeException);
59+
}
60+
}
61+
62+
@Test
63+
public void givenWrongFormatDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
3564
try {
36-
IntParser.parse("text");
37-
} catch (InvalidNumber ex) {
38-
assertTrue(ExceptionUtils.getRootCause(ex) instanceof NumberFormatException);
65+
AgeCalculator.calculateAge("010102");
66+
} catch (CalculationException ex) {
67+
assertTrue(Throwables.getRootCause(ex) instanceof DateTimeParseException);
3968
}
4069
}
4170

4271
@Test
43-
public void givenNestedException_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
72+
public void givenOutOfRangeDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
4473
try {
45-
IntParser.parse("text");
46-
} catch (InvalidNumber ex) {
47-
assertTrue(Throwables.getRootCause(ex) instanceof NumberFormatException);
74+
AgeCalculator.calculateAge("2020-04-04");
75+
} catch (CalculationException ex) {
76+
assertTrue(Throwables.getRootCause(ex) instanceof DateOutOfRangeException);
4877
}
4978
}
5079

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.