Add and use IOExceptionList(String, List).
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -154,6 +154,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-709" dev="ggregory" type="add" due-to="Boris Unckel, Gary Gregory">
Add null safe variants of isDirectory and isRegularFile.
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add and use IOExceptionList(String, List).
+ </action>
<!-- UPDATES -->
<action dev="ggregory" type="update" due-to="Dependabot">
Update junit-jupiter from 5.6.2 to 5.7.0 #153.

MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -326,7 +326,7 @@ public static void cleanDirectory(final File directory) throws IOException {
}
if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
+ throw new IOExceptionList(directory.toString(), causeList);
}
}
@@ -352,7 +352,7 @@ private static void cleanDirectoryOnExit(final File directory) throws IOExceptio
}
if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
+ throw new IOExceptionList(directory.toString(), causeList);
}
}

MODIFY
src/main/java/org/apache/commons/io/IOExceptionList.java
src/main/java/org/apache/commons/io/IOExceptionList.java
* @param causeList a list of cause exceptions.
*/
public IOExceptionList(final List<? extends Throwable> causeList) {
- super(String.format("%,d exceptions: %s", causeList == null ? 0 : causeList.size(), causeList),
- causeList == null ? null : causeList.get(0));
- this.causeList = causeList == null ? Collections.emptyList() : causeList;
+ this(String.format("%,d exceptions: %s", causeList == null ? 0 : causeList.size(), causeList), causeList);
}
/**
- * Gets the cause list.
+ * Creates a new exception caused by a list of exceptions.
*
- * @param <T> type of exception to return.
- * @return The list of causes.
+ * @param message The detail message, see {@link #getMessage()}.
+ * @param causeList a list of cause exceptions.
+ * @since 2.9.0
*/
- public <T extends Throwable> List<T> getCauseList() {
- return (List<T>) causeList;
+ public IOExceptionList(final String message, final List<? extends Throwable> causeList) {
+ super(message, causeList == null ? null : causeList.get(0));
+ this.causeList = causeList == null ? Collections.emptyList() : causeList;
}
/**
@@ -79,6 +79,16 @@ public IOExceptionList(final List<? extends Throwable> causeList) {
return (T) causeList.get(index);
}
+ /**
+ * Gets the cause list.
+ *
+ * @param <T> type of exception to return.
+ * @return The list of causes.
+ */
+ public <T extends Throwable> List<T> getCauseList() {
+ return (List<T>) causeList;
+ }
+
/**
* Works around Throwable and Generics, may fail at runtime depending on the argument value.
*

MODIFY
src/main/java/org/apache/commons/io/file/PathUtils.java
src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -940,7 +940,7 @@ public static Path setReadOnly(final Path path, final boolean readOnly, final Li
}
}
if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
+ throw new IOExceptionList(path.toString(), causeList);
}
throw new IOException(
String.format("No DosFileAttributeView or PosixFileAttributeView for '%s' (linkOptions=%s)", path,

MODIFY
src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java
src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java
@@ -88,7 +88,7 @@ public Writer append(final char c) throws IOException {
i++;
}
if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
+ throw new IOExceptionList("append", causeList);
}
return this;
}
@@ -108,7 +108,7 @@ public Writer append(final CharSequence csq) throws IOException {
i++;
}
if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
+ throw new IOExceptionList("append", causeList);
}
return this;
}
@@ -129,7 +129,7 @@ public Writer append(final CharSequence csq, final int start, final int end) thr
i++;
}
if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
+ throw new IOExceptionList("append", causeList);
}
return this;
}
@@ -149,7 +149,7 @@ public void close() throws IOException {
i++;
}
if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
+ throw new IOExceptionList("close", causeList);
}
}
@@ -174,7 +174,7 @@ public void flush() throws IOException {
i++;
}
if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
+ throw new IOExceptionList("flush", causeList);
}
}
@@ -203,7 +203,7 @@ public void write(final char[] cbuf, final int off, final int len) throws IOExce
i++;
}
if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
+ throw new IOExceptionList("write", causeList);
}
}
@@ -222,7 +222,7 @@ public void write(final char[] cbuf) throws IOException {
i++;
}
if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
+ throw new IOExceptionList("write", causeList);
}
}
@@ -246,7 +246,7 @@ public void write(final int c) throws IOException {
i++;
}
if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
+ throw new IOExceptionList("write", causeList);
}
}
@@ -265,7 +265,7 @@ public void write(final String str) throws IOException {
i++;
}
if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
+ throw new IOExceptionList("write", causeList);
}
}
@@ -294,7 +294,7 @@ public void write(final String str, final int off, final int len) throws IOExcep
i++;
}
if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
+ throw new IOExceptionList("write", causeList);
}
}

MODIFY
src/test/java/org/apache/commons/io/IOExceptionListTestCase.java
src/test/java/org/apache/commons/io/IOExceptionListTestCase.java
import org.junit.jupiter.api.Test;
+/**
+ * Tests {@link IOExceptionList}.
+ */
public class IOExceptionListTestCase {
@Test
@@ -46,6 +49,23 @@ public void testCause() {
assertEquals(list, causeList);
}
+ @Test
+ public void testMessageCause() {
+ final EOFException cause = new EOFException();
+ final List<EOFException> list = Collections.singletonList(cause);
+ final IOExceptionList sqlExceptionList = new IOExceptionList("Hello", list);
+ assertEquals("Hello", sqlExceptionList.getMessage());
+ //
+ assertEquals(cause, sqlExceptionList.getCause());
+ assertEquals(cause, sqlExceptionList.getCause(0));
+ assertEquals(list, sqlExceptionList.getCauseList());
+ assertEquals(list, sqlExceptionList.getCauseList(EOFException.class));
+ assertEquals(cause, sqlExceptionList.getCause(0, EOFException.class));
+ // No CCE:
+ final List<EOFException> causeList = sqlExceptionList.getCauseList();
+ assertEquals(list, causeList);
+ }
+
@Test
public void testNullCause() {
final IOExceptionList sqlExceptionList = new IOExceptionList(null);

Add and use IOExceptionList(String, List).