You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/06/18 17:41:48 UTC

[commons-lang] branch master updated (df0296f7c -> 911fbb935)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


    from df0296f7c Format tweaks
     new 7912894eb Sort members
     new 70b2250f8 Add ExceptionUtils.forEach(Throwable, Consumer<Throwable>)
     new c6cfdc614 Format tweaks
     new 01f8edc18 In-line some local vars
     new 911fbb935 Add ExceptionUtils.getRootCauseStackTraceList(Throwable)

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |   3 +
 .../commons/lang3/exception/ExceptionUtils.java    |  81 +++++++--
 .../lang3/exception/ExceptionUtilsTest.java        | 198 +++++++++++++++++----
 3 files changed, 232 insertions(+), 50 deletions(-)


[commons-lang] 01/05: Sort members

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit 7912894eb8545ea076732a95dc0124d6426b3e76
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 18 13:02:50 2022 -0400

    Sort members
---
 .../lang3/exception/ExceptionUtilsTest.java        | 68 +++++++++++-----------
 1 file changed, 34 insertions(+), 34 deletions(-)

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 767e3d9d9..38b6127d1 100644
--- a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
@@ -286,6 +286,39 @@ public class ExceptionUtilsTest {
         assertFalse(match);
     }
 
+    @Test
+    @DisplayName("getStackFrames returns empty string array when the argument is null")
+    public void testgetStackFramesHappyPath() {
+        final String[] actual = ExceptionUtils.getStackFrames(new Throwable() {
+            private static final long serialVersionUID = 1L;
+
+            // provide static stack trace to make test stable
+            @Override
+            public void printStackTrace(final PrintWriter s) {
+                s.write("org.apache.commons.lang3.exception.ExceptionUtilsTest$1\n" +
+                    "\tat org.apache.commons.lang3.exception.ExceptionUtilsTest.testgetStackFramesGappyPath(ExceptionUtilsTest.java:706)\n" +
+                    "\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
+                    "\tat com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)\n" +
+                    "\tat com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)\n");
+            }
+        });
+
+        assertArrayEquals(new String[]{
+            "org.apache.commons.lang3.exception.ExceptionUtilsTest$1",
+            "\tat org.apache.commons.lang3.exception.ExceptionUtilsTest.testgetStackFramesGappyPath(ExceptionUtilsTest.java:706)",
+            "\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
+            "\tat com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)",
+            "\tat com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)"
+        }, actual);
+    }
+
+    @Test
+    @DisplayName("getStackFrames returns the string array of the stack frames when there is a real exception")
+    public void testgetStackFramesNullArg() {
+        final String[] actual = ExceptionUtils.getStackFrames((Throwable) null);
+        assertEquals(0, actual.length);
+    }
+
     @Test
     public void testGetThrowableCount_Throwable() {
         assertEquals(0, ExceptionUtils.getThrowableCount(null));
@@ -464,6 +497,7 @@ public class ExceptionUtilsTest {
         assertEquals(0, ExceptionUtils.indexOfType(withCause, Throwable.class));
     }
 
+
     @Test
     public void testIndexOfType_ThrowableClassInt() {
         assertEquals(-1, ExceptionUtils.indexOfType(null, null, 0));
@@ -500,7 +534,6 @@ public class ExceptionUtilsTest {
         // internally this method calls stream method anyway
     }
 
-
     @Test
     public void testPrintRootCauseStackTrace_ThrowableStream() {
         ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
@@ -690,37 +723,4 @@ public class ExceptionUtilsTest {
         final Throwable t = assertThrows(Throwable.class, () -> ExceptionUtils.wrapAndThrow(new TestThrowable()));
         assertTrue(ExceptionUtils.hasCause(t, TestThrowable.class));
     }
-
-    @Test
-    @DisplayName("getStackFrames returns the string array of the stack frames when there is a real exception")
-    public void testgetStackFramesNullArg() {
-        final String[] actual = ExceptionUtils.getStackFrames((Throwable) null);
-        assertEquals(0, actual.length);
-    }
-
-    @Test
-    @DisplayName("getStackFrames returns empty string array when the argument is null")
-    public void testgetStackFramesHappyPath() {
-        final String[] actual = ExceptionUtils.getStackFrames(new Throwable() {
-            private static final long serialVersionUID = 1L;
-
-            // provide static stack trace to make test stable
-            @Override
-            public void printStackTrace(final PrintWriter s) {
-                s.write("org.apache.commons.lang3.exception.ExceptionUtilsTest$1\n" +
-                    "\tat org.apache.commons.lang3.exception.ExceptionUtilsTest.testgetStackFramesGappyPath(ExceptionUtilsTest.java:706)\n" +
-                    "\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
-                    "\tat com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)\n" +
-                    "\tat com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)\n");
-            }
-        });
-
-        assertArrayEquals(new String[]{
-            "org.apache.commons.lang3.exception.ExceptionUtilsTest$1",
-            "\tat org.apache.commons.lang3.exception.ExceptionUtilsTest.testgetStackFramesGappyPath(ExceptionUtilsTest.java:706)",
-            "\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
-            "\tat com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)",
-            "\tat com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)"
-        }, actual);
-    }
 }


[commons-lang] 02/05: Add ExceptionUtils.forEach(Throwable, Consumer)

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit 70b2250f80ac098e3fbb136bcd765ad90e8d3c0e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 18 13:23:48 2022 -0400

    Add ExceptionUtils.forEach(Throwable, Consumer<Throwable>)
    
    Add ExceptionUtils.stream(Throwable).
---
 src/changes/changes.xml                            |  2 +
 .../commons/lang3/exception/ExceptionUtils.java    | 44 ++++++++++
 .../lang3/exception/ExceptionUtilsTest.java        | 99 +++++++++++++++++++++-
 3 files changed, 144 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 33d7b7234..ddfbc2534 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -147,6 +147,8 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add coverage.yml.</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.since(Temporal).</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.of(FailableConsumer|FailableRunnbale).</action>
+    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add ExceptionUtils.forEach(Throwable, Consumer&lt;Throwable&gt;).</action>
+    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add ExceptionUtils.stream(Throwable).</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.4 #742, #752, #764, #833, #867.</action>
     <action                   type="update" dev="ggregory" due-to="Dependabot">Bump actions/checkout from 2 to 3 #819, #825, #859.</action>
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 986fd0bf2..35cde8021 100644
--- a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
+++ b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
@@ -26,6 +26,8 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
 import java.util.StringTokenizer;
+import java.util.function.Consumer;
+import java.util.stream.Stream;
 
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.ClassUtils;
@@ -427,6 +429,26 @@ public class ExceptionUtils {
         return list;
     }
 
+    /**
+     * Performs an action for each Throwable causes of the given Throwable.
+     * <p>
+     * A throwable without cause will return a stream containing one element - the input throwable. A throwable with one cause
+     * will return a stream containing two elements. - the input throwable and the cause throwable. A {@code null} throwable
+     * will return a stream of count zero.
+     * </p>
+     *
+     * <p>
+     * This method handles recursive cause structures that might otherwise cause infinite loops. The cause chain is
+     * processed until the end is reached, or until the next item in the chain is already in the result set.
+     * </p>
+     * @param throwable The Throwable to traverse.
+     * @param consumer a non-interfering action to perform on the elements.
+     * @since 3.13.0
+     */
+    public static void forEach(final Throwable throwable, final Consumer<Throwable> consumer) {
+        stream(throwable).forEach(consumer);
+    }
+
     /**
      * Gets the list of {@code Throwable} objects in the
      * exception chain.
@@ -770,6 +792,28 @@ public class ExceptionUtils {
         return ExceptionUtils.<R, RuntimeException>eraseType(throwable);
     }
 
+    /**
+     * Streams causes of a Throwable.
+     * <p>
+     * A throwable without cause will return a stream containing one element - the input throwable. A throwable with one cause
+     * will return a stream containing two elements. - the input throwable and the cause throwable. A {@code null} throwable
+     * will return a stream of count zero.
+     * </p>
+     *
+     * <p>
+     * This method handles recursive cause structures that might otherwise cause infinite loops. The cause chain is
+     * processed until the end is reached, or until the next item in the chain is already in the result set.
+     * </p>
+     *
+     * @param throwable The Throwable to traverse
+     * @return A new Stream of Throwable causes.
+     * @since 3.13.0
+     */
+    public static Stream<Throwable> stream(final Throwable throwable) {
+        // No point building a custom Iterable as it would keep track of visited elements to avoid infinite loops
+        return getThrowableList(throwable).stream();
+    }
+
     /**
      * Worker method for the {@code throwableOfType} methods.
      *
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 38b6127d1..27a2495b1 100644
--- a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
@@ -32,7 +32,9 @@ import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Modifier;
+import java.util.ArrayList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 import org.apache.commons.lang3.test.NotVisibleExceptionFactory;
 import org.junit.jupiter.api.AfterEach;
@@ -216,6 +218,58 @@ public class ExceptionUtilsTest {
         assertFalse(Modifier.isFinal(ExceptionUtils.class.getModifiers()));
     }
 
+    @Test
+    public void testForEach_jdkNoCause() {
+        final List<Throwable> throwables = new ArrayList<>();
+        ExceptionUtils.forEach(jdkNoCause, throwables::add);
+        assertEquals(1, throwables.size());
+        assertSame(jdkNoCause, throwables.get(0));
+    }
+
+    @Test
+    public void testForEach_nested() {
+        final List<Throwable> throwables = new ArrayList<>();
+        ExceptionUtils.forEach(nested, throwables::add);
+        assertEquals(2, throwables.size());
+        assertSame(nested, throwables.get(0));
+        assertSame(withoutCause, throwables.get(1));
+    }
+
+    @Test
+    public void testForEach_null() {
+        final List<Throwable> throwables = new ArrayList<>();
+        ExceptionUtils.forEach(null, throwables::add);
+        assertEquals(0, throwables.size());
+    }
+
+    @Test
+    public void testForEach_recursiveCause() {
+        final List<Throwable> throwables = new ArrayList<>();
+        ExceptionUtils.forEach(cyclicCause, throwables::add);
+        assertEquals(3, throwables.size());
+        assertSame(cyclicCause, throwables.get(0));
+        assertSame(cyclicCause.getCause(), throwables.get(1));
+        assertSame(cyclicCause.getCause().getCause(), throwables.get(2));
+    }
+
+    @Test
+    public void testForEach_withCause() {
+        final List<Throwable> throwables = new ArrayList<>();
+        ExceptionUtils.forEach(withCause, throwables::add);
+        assertEquals(3, throwables.size());
+        assertSame(withCause, throwables.get(0));
+        assertSame(nested, throwables.get(1));
+        assertSame(withoutCause, throwables.get(2));
+    }
+
+    @Test
+    public void testForEach_withoutCause() {
+        final List<Throwable> throwables = new ArrayList<>();
+        ExceptionUtils.forEach(withoutCause, throwables::add);
+        assertEquals(1, throwables.size());
+        assertSame(withoutCause, throwables.get(0));
+    }
+
     @SuppressWarnings("deprecation") // Specifically tests the deprecated methods
     @Test
     public void testGetCause_Throwable() {
@@ -497,7 +551,6 @@ public class ExceptionUtilsTest {
         assertEquals(0, ExceptionUtils.indexOfType(withCause, Throwable.class));
     }
 
-
     @Test
     public void testIndexOfType_ThrowableClassInt() {
         assertEquals(-1, ExceptionUtils.indexOfType(null, null, 0));
@@ -587,6 +640,50 @@ public class ExceptionUtilsTest {
         assertThrows(IllegalArgumentException.class, () -> ExceptionUtils.removeCommonFrames(null, null));
     }
 
+    @Test
+    public void testStream_jdkNoCause() {
+        assertEquals(1, ExceptionUtils.stream(jdkNoCause).count());
+        assertSame(jdkNoCause, ExceptionUtils.stream(jdkNoCause).toArray()[0]);
+    }
+
+    @Test
+    public void testStream_nested() {
+        assertEquals(2, ExceptionUtils.stream(nested).count());
+        final Object[] array = ExceptionUtils.stream(nested).toArray();
+        assertSame(nested, array[0]);
+        assertSame(withoutCause, array[1]);
+    }
+
+    @Test
+    public void testStream_null() {
+        assertEquals(0, ExceptionUtils.stream(null).count());
+    }
+
+    @Test
+    public void testStream_recursiveCause() {
+        final List<?> throwables = ExceptionUtils.stream(cyclicCause).collect(Collectors.toList());
+        assertEquals(3, throwables.size());
+        assertSame(cyclicCause, throwables.get(0));
+        assertSame(cyclicCause.getCause(), throwables.get(1));
+        assertSame(cyclicCause.getCause().getCause(), throwables.get(2));
+    }
+
+    @Test
+    public void testStream_withCause() {
+        final List<?> throwables = ExceptionUtils.stream(withCause).collect(Collectors.toList());
+        assertEquals(3, throwables.size());
+        assertSame(withCause, throwables.get(0));
+        assertSame(nested, throwables.get(1));
+        assertSame(withoutCause, throwables.get(2));
+    }
+
+    @Test
+    public void testStream_withoutCause() {
+        final List<?> throwables = ExceptionUtils.stream(withoutCause).collect(Collectors.toList());
+        assertEquals(1, throwables.size());
+        assertSame(withoutCause, throwables.get(0));
+    }
+
     @Test
     public void testThrow() {
         final Exception expected = new InterruptedException();


[commons-lang] 03/05: Format tweaks

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit c6cfdc6149430e1d4c0f0cda03e7fb4d0e465802
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 18 13:26:13 2022 -0400

    Format tweaks
---
 .../org/apache/commons/lang3/exception/ExceptionUtilsTest.java     | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

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 27a2495b1..47ff55750 100644
--- a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
@@ -77,6 +77,7 @@ public class ExceptionUtilsTest {
             this.cause = cause;
         }
     }
+
     /**
      * Provides a method with a well known chained/nested exception
      * name which does not match the full signature (e.g. lacks a
@@ -90,6 +91,7 @@ public class ExceptionUtilsTest {
             // noop
         }
     }
+
     // Temporary classes to allow the nested exception code to be removed
     // prior to a rewrite of this test class.
     private static class NestableException extends Exception {
@@ -103,12 +105,15 @@ public class ExceptionUtilsTest {
             super(t);
         }
     }
+
     public static class TestThrowable extends Throwable {
         private static final long serialVersionUID = 1L;
     }
+
     private static int redeclareCheckedException() {
         return throwsCheckedException();
     }
+
     private static int throwsCheckedException() {
         try {
             throw new IOException();
@@ -117,7 +122,6 @@ public class ExceptionUtilsTest {
         }
     }
 
-
     private NestableException nested;
 
 
@@ -127,7 +131,6 @@ public class ExceptionUtilsTest {
 
     private Throwable jdkNoCause;
 
-
     private ExceptionWithCause cyclicCause;
 
     private Throwable notVisibleException;


[commons-lang] 05/05: Add ExceptionUtils.getRootCauseStackTraceList(Throwable)

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit 911fbb9352f029dd02971eebdbf7d9cffc7f9175
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 18 13:41:40 2022 -0400

    Add ExceptionUtils.getRootCauseStackTraceList(Throwable)
---
 src/changes/changes.xml                            |  1 +
 .../commons/lang3/exception/ExceptionUtils.java    | 31 +++++++++++++++-------
 .../lang3/exception/ExceptionUtilsTest.java        | 26 ++++++++++++++++++
 3 files changed, 48 insertions(+), 10 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ddfbc2534..0946e7e68 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -149,6 +149,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.of(FailableConsumer|FailableRunnbale).</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add ExceptionUtils.forEach(Throwable, Consumer&lt;Throwable&gt;).</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add ExceptionUtils.stream(Throwable).</action>
+    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add ExceptionUtils.getRootCauseStackTraceList(Throwable).</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.4 #742, #752, #764, #833, #867.</action>
     <action                   type="update" dev="ggregory" due-to="Dependabot">Bump actions/checkout from 2 to 3 #819, #825, #859.</action>
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 33333d58b..89316cb1b 100644
--- a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
+++ b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
@@ -23,6 +23,7 @@ import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.UndeclaredThrowableException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 import java.util.StringTokenizer;
@@ -266,8 +267,24 @@ public class ExceptionUtils {
      * @since 2.0
      */
     public static String[] getRootCauseStackTrace(final Throwable throwable) {
+        return getRootCauseStackTraceList(throwable).toArray(ArrayUtils.EMPTY_STRING_ARRAY);
+    }
+
+    /**
+     * Gets a compact stack trace for the root cause of the supplied {@code Throwable}.
+     *
+     * <p>
+     * The output of this method is consistent across JDK versions. It consists of the root exception followed by each of
+     * its wrapping exceptions separated by '[wrapped]'. Note that this is the opposite order to the JDK1.4 display.
+     * </p>
+     *
+     * @param throwable the throwable to examine, may be null
+     * @return a list of stack trace frames, never null
+     * @since 3.13.0
+     */
+    public static List<String> getRootCauseStackTraceList(final Throwable throwable) {
         if (throwable == null) {
-            return ArrayUtils.EMPTY_STRING_ARRAY;
+            return Collections.emptyList();
         }
         final Throwable[] throwables = getThrowables(throwable);
         final int count = throwables.length;
@@ -286,7 +303,7 @@ public class ExceptionUtils {
             }
             frames.addAll(trace);
         }
-        return frames.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
+        return frames;
     }
 
     /**
@@ -661,10 +678,7 @@ public class ExceptionUtils {
             return;
         }
         Objects.requireNonNull(printStream, "printStream");
-        final String[] trace = getRootCauseStackTrace(throwable);
-        for (final String element : trace) {
-            printStream.println(element);
-        }
+        getRootCauseStackTraceList(throwable).forEach(printStream::println);
         printStream.flush();
     }
 
@@ -693,10 +707,7 @@ public class ExceptionUtils {
             return;
         }
         Objects.requireNonNull(printWriter, "printWriter");
-        final String[] trace = getRootCauseStackTrace(throwable);
-        for (final String element : trace) {
-            printWriter.println(element);
-        }
+        getRootCauseStackTraceList(throwable).forEach(printWriter::println);
         printWriter.flush();
     }
 
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 47ff55750..ffe83c625 100644
--- a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
@@ -343,6 +343,32 @@ public class ExceptionUtilsTest {
         assertFalse(match);
     }
 
+    @Test
+    public void testGetRootCauseStackTraceList_Throwable() {
+        assertEquals(0, ExceptionUtils.getRootCauseStackTraceList(null).size());
+
+        final Throwable cause = createExceptionWithCause();
+        List<String> stackTrace = ExceptionUtils.getRootCauseStackTraceList(cause);
+        boolean match = false;
+        for (final String element : stackTrace) {
+            if (element.startsWith(ExceptionUtils.WRAPPED_MARKER)) {
+                match = true;
+                break;
+            }
+        }
+        assertTrue(match);
+
+        stackTrace = ExceptionUtils.getRootCauseStackTraceList(withoutCause);
+        match = false;
+        for (final String element : stackTrace) {
+            if (element.startsWith(ExceptionUtils.WRAPPED_MARKER)) {
+                match = true;
+                break;
+            }
+        }
+        assertFalse(match);
+    }
+
     @Test
     @DisplayName("getStackFrames returns empty string array when the argument is null")
     public void testgetStackFramesHappyPath() {


[commons-lang] 04/05: In-line some local vars

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit 01f8edc18b1941dfa79d23c9a63724b1595a84f6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 18 13:27:48 2022 -0400

    In-line some local vars
---
 .../java/org/apache/commons/lang3/exception/ExceptionUtils.java     | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

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 35cde8021..33333d58b 100644
--- a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
+++ b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
@@ -210,8 +210,7 @@ public class ExceptionUtils {
             return StringUtils.EMPTY;
         }
         final String clsName = ClassUtils.getShortClassName(th, null);
-        final String msg = th.getMessage();
-        return clsName + ": " + StringUtils.defaultString(msg);
+        return clsName + ": " + StringUtils.defaultString(th.getMessage());
     }
 
     /**
@@ -469,8 +468,7 @@ public class ExceptionUtils {
      * @return the array of throwables, never null
      */
     public static Throwable[] getThrowables(final Throwable throwable) {
-        final List<Throwable> list = getThrowableList(throwable);
-        return list.toArray(ArrayUtils.EMPTY_THROWABLE_ARRAY);
+        return getThrowableList(throwable).toArray(ArrayUtils.EMPTY_THROWABLE_ARRAY);
     }
 
     /**