From 60412131f3679b720bcaaaf3dea4be666cefea7a Mon Sep 17 00:00:00 2001 From: pascalschumacher Date: Sun, 11 Feb 2018 14:19:56 +0100 Subject: [PATCH] LANG-1364: ExceptionUtils#getRootCause(Throwable t) should return t if no lower level cause exists This makes the behavior of getRootCause consistent with getRootCauseMessage and getRootCauseStackTrace. --- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/exception/ExceptionUtils.java | 4 ++-- .../apache/commons/lang3/exception/ExceptionUtilsTest.java | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 73297cc24..61eca6880 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -46,6 +46,7 @@ The type attribute can be add,update,fix,remove. + ExceptionUtils#getRootCause(Throwable t) should return t if no lower level cause exists NumberUtils.isNumber assumes number starting with Zero defaultString(final String str) in StringUtils to reuse defaultString(final String str, final String defaultStr) Parsing Json Array failed diff --git a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java index 29f163e8a..bacb859d6 100644 --- a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java +++ b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java @@ -179,11 +179,11 @@ public class ExceptionUtils { * * @param throwable the throwable to get the root cause for, may be null * @return the root cause of the Throwable, - * null if none found or null throwable input + * null if null throwable input */ public static Throwable getRootCause(final Throwable throwable) { final List list = getThrowableList(throwable); - return list.size() < 2 ? null : list.get(list.size() - 1); + return list.isEmpty() ? null : list.get(list.size() - 1); } /** diff --git a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java index d6fb98a66..0af68a8fb 100644 --- a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java @@ -147,10 +147,10 @@ public class ExceptionUtilsTest { @Test public void testGetRootCause_Throwable() { assertSame(null, ExceptionUtils.getRootCause(null)); - assertSame(null, ExceptionUtils.getRootCause(withoutCause)); + assertSame(withoutCause, ExceptionUtils.getRootCause(withoutCause)); assertSame(withoutCause, ExceptionUtils.getRootCause(nested)); assertSame(withoutCause, ExceptionUtils.getRootCause(withCause)); - assertSame(null, ExceptionUtils.getRootCause(jdkNoCause)); + assertSame(jdkNoCause, ExceptionUtils.getRootCause(jdkNoCause)); assertSame(cyclicCause.getCause().getCause(), ExceptionUtils.getRootCause(cyclicCause)); }