1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.io;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertNull;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 import java.io.EOFException;
27 import java.io.PrintWriter;
28 import java.io.StringWriter;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32
33 import org.junit.jupiter.api.Test;
34
35
36
37
38 public class IOExceptionListTest {
39
40 @Test
41 public void testCause() {
42 final EOFException cause = new EOFException();
43 final List<EOFException> list = Collections.singletonList(cause);
44 final IOExceptionList sqlExceptionList = new IOExceptionList(list);
45 assertEquals(cause, sqlExceptionList.getCause());
46 assertEquals(cause, sqlExceptionList.getCause(0));
47 assertEquals(list, sqlExceptionList.getCauseList());
48 assertEquals(list, sqlExceptionList.getCauseList(EOFException.class));
49 assertEquals(cause, sqlExceptionList.getCause(0, EOFException.class));
50
51 final List<EOFException> causeList = sqlExceptionList.getCauseList();
52 assertEquals(list, causeList);
53 }
54
55 @Test
56 public void testCheckEmpty() throws IOExceptionList {
57 IOExceptionList.checkEmpty(null, "");
58 IOExceptionList.checkEmpty(null, null);
59 IOExceptionList.checkEmpty(Collections.emptyList(), "");
60 IOExceptionList.checkEmpty(Collections.emptyList(), null);
61 assertThrows(IOExceptionList.class, () -> IOExceptionList.checkEmpty(Collections.singletonList(new Exception()), ""));
62 assertThrows(IOExceptionList.class, () -> IOExceptionList.checkEmpty(Collections.singletonList(new Exception()), null));
63 }
64
65 @Test
66 public void testEmptyList() {
67 new IOExceptionList(Collections.emptyList());
68 new IOExceptionList("foo", Collections.emptyList());
69 }
70
71 @Test
72 public void testIterable() {
73 final EOFException cause = new EOFException();
74 final List<EOFException> list = Collections.singletonList(cause);
75 final IOExceptionList sqlExceptionList = new IOExceptionList("Hello", list);
76
77 assertEquals(list, sqlExceptionList.getCauseList());
78
79 final List<EOFException> causeList = sqlExceptionList.getCauseList();
80 assertEquals(list, causeList);
81
82 final List<Throwable> list2 = new ArrayList<>();
83 sqlExceptionList.forEach(list2::add);
84 assertEquals(list2, causeList);
85 }
86
87 @Test
88 public void testMessageCause() {
89 final EOFException cause = new EOFException();
90 final List<EOFException> list = Collections.singletonList(cause);
91 final IOExceptionList sqlExceptionList = new IOExceptionList("Hello", list);
92 assertEquals("Hello", sqlExceptionList.getMessage());
93
94 assertEquals(cause, sqlExceptionList.getCause());
95 assertEquals(cause, sqlExceptionList.getCause(0));
96 assertEquals(list, sqlExceptionList.getCauseList());
97 assertEquals(list, sqlExceptionList.getCauseList(EOFException.class));
98 assertEquals(cause, sqlExceptionList.getCause(0, EOFException.class));
99
100 final List<EOFException> causeList = sqlExceptionList.getCauseList();
101 assertEquals(list, causeList);
102 }
103
104 @Test
105 public void testNullCause() {
106 final IOExceptionList sqlExceptionList = new IOExceptionList(null);
107 assertNull(sqlExceptionList.getCause());
108 assertTrue(sqlExceptionList.getCauseList().isEmpty());
109 }
110
111 @Test
112 public void testNullMessageArg() {
113 assertNotNull(new IOExceptionList(null, Collections.emptyList()).getMessage());
114 assertNotNull(new IOExceptionList(null, null).getMessage());
115 assertEquals("A", new IOExceptionList("A", Collections.emptyList()).getMessage());
116 assertEquals("A", new IOExceptionList("A", null).getMessage());
117 }
118
119 @Test
120 public void testPrintStackTrace() {
121 final EOFException cause = new EOFException();
122 final List<EOFException> list = Collections.singletonList(cause);
123 final IOExceptionList sqlExceptionList = new IOExceptionList(list);
124 final StringWriter sw = new StringWriter();
125 final PrintWriter pw = new PrintWriter(sw);
126 sqlExceptionList.printStackTrace(pw);
127 final String st = sw.toString();
128 assertTrue(st.startsWith("org.apache.commons.io.IOExceptionList: 1 exception(s): [java.io.EOFException]"));
129 assertTrue(st.contains("Caused by: java.io.EOFException"));
130 }
131 }