aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorGary Gregory <garydgregory@gmail.com>2022-06-18 13:23:48 -0400
committerGary Gregory <garydgregory@gmail.com>2022-06-18 13:23:48 -0400
commit70b2250f80ac098e3fbb136bcd765ad90e8d3c0e (patch)
treeff343ea24f0bb64ad1dae17b70aa2b907710db2f /src/test
parent7912894eb8545ea076732a95dc0124d6426b3e76 (diff)
downloadapache-commons-lang-70b2250f80ac098e3fbb136bcd765ad90e8d3c0e.tar.gz
Add ExceptionUtils.forEach(Throwable, Consumer<Throwable>)
Add ExceptionUtils.stream(Throwable).
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java99
1 files changed, 98 insertions, 1 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 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));
@@ -588,6 +641,50 @@ public class ExceptionUtilsTest {
}
@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();
final Exception actual = assertThrows(Exception.class, () -> ExceptionUtils.rethrow(expected));