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-io.git
The following commit(s) were added to refs/heads/master by this push: new af58d18 Add and use IOExceptionList(String, List). af58d18 is described below commit af58d18cb3c0553d8c3c0961d4f68a0851f349ab Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Thu Jan 28 11:47:19 2021 -0500 Add and use IOExceptionList(String, List). --- src/changes/changes.xml | 3 +++ src/main/java/org/apache/commons/io/FileUtils.java | 4 ++-- .../org/apache/commons/io/IOExceptionList.java | 26 +++++++++++++++------- .../java/org/apache/commons/io/file/PathUtils.java | 2 +- .../commons/io/output/FilterCollectionWriter.java | 20 ++++++++--------- .../apache/commons/io/IOExceptionListTestCase.java | 20 +++++++++++++++++ 6 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 06e42ad..7ced17b 100644 --- a/src/changes/changes.xml +++ b/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. diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java index 56f0530..7fe1a3d 100644 --- a/src/main/java/org/apache/commons/io/FileUtils.java +++ b/src/main/java/org/apache/commons/io/FileUtils.java @@ -326,7 +326,7 @@ public class FileUtils { } if (!causeList.isEmpty()) { - throw new IOExceptionList(causeList); + throw new IOExceptionList(directory.toString(), causeList); } } @@ -352,7 +352,7 @@ public class FileUtils { } if (!causeList.isEmpty()) { - throw new IOExceptionList(causeList); + throw new IOExceptionList(directory.toString(), causeList); } } diff --git a/src/main/java/org/apache/commons/io/IOExceptionList.java b/src/main/java/org/apache/commons/io/IOExceptionList.java index 1d27d88..288c105 100644 --- a/src/main/java/org/apache/commons/io/IOExceptionList.java +++ b/src/main/java/org/apache/commons/io/IOExceptionList.java @@ -41,19 +41,19 @@ public class IOExceptionList extends IOException { * @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; } /** @@ -80,6 +80,16 @@ public class IOExceptionList extends IOException { } /** + * 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. * * @param <T> type of exception to return. diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java b/src/main/java/org/apache/commons/io/file/PathUtils.java index 97b6653..e5b4ad4 100644 --- a/src/main/java/org/apache/commons/io/file/PathUtils.java +++ b/src/main/java/org/apache/commons/io/file/PathUtils.java @@ -940,7 +940,7 @@ public final class PathUtils { } } 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, diff --git a/src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java b/src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java index acf1291..aa830d2 100644 --- a/src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java +++ b/src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java @@ -88,7 +88,7 @@ public class FilterCollectionWriter extends Writer { i++; } if (!causeList.isEmpty()) { - throw new IOExceptionList(causeList); + throw new IOExceptionList("append", causeList); } return this; } @@ -108,7 +108,7 @@ public class FilterCollectionWriter extends Writer { i++; } if (!causeList.isEmpty()) { - throw new IOExceptionList(causeList); + throw new IOExceptionList("append", causeList); } return this; } @@ -129,7 +129,7 @@ public class FilterCollectionWriter extends Writer { i++; } if (!causeList.isEmpty()) { - throw new IOExceptionList(causeList); + throw new IOExceptionList("append", causeList); } return this; } @@ -149,7 +149,7 @@ public class FilterCollectionWriter extends Writer { i++; } if (!causeList.isEmpty()) { - throw new IOExceptionList(causeList); + throw new IOExceptionList("close", causeList); } } @@ -174,7 +174,7 @@ public class FilterCollectionWriter extends Writer { i++; } if (!causeList.isEmpty()) { - throw new IOExceptionList(causeList); + throw new IOExceptionList("flush", causeList); } } @@ -203,7 +203,7 @@ public class FilterCollectionWriter extends Writer { i++; } if (!causeList.isEmpty()) { - throw new IOExceptionList(causeList); + throw new IOExceptionList("write", causeList); } } @@ -222,7 +222,7 @@ public class FilterCollectionWriter extends Writer { i++; } if (!causeList.isEmpty()) { - throw new IOExceptionList(causeList); + throw new IOExceptionList("write", causeList); } } @@ -246,7 +246,7 @@ public class FilterCollectionWriter extends Writer { i++; } if (!causeList.isEmpty()) { - throw new IOExceptionList(causeList); + throw new IOExceptionList("write", causeList); } } @@ -265,7 +265,7 @@ public class FilterCollectionWriter extends Writer { i++; } if (!causeList.isEmpty()) { - throw new IOExceptionList(causeList); + throw new IOExceptionList("write", causeList); } } @@ -294,7 +294,7 @@ public class FilterCollectionWriter extends Writer { i++; } if (!causeList.isEmpty()) { - throw new IOExceptionList(causeList); + throw new IOExceptionList("write", causeList); } } diff --git a/src/test/java/org/apache/commons/io/IOExceptionListTestCase.java b/src/test/java/org/apache/commons/io/IOExceptionListTestCase.java index ebe0b2d..d001d14 100644 --- a/src/test/java/org/apache/commons/io/IOExceptionListTestCase.java +++ b/src/test/java/org/apache/commons/io/IOExceptionListTestCase.java @@ -29,6 +29,9 @@ import java.util.List; import org.junit.jupiter.api.Test; +/** + * Tests {@link IOExceptionList}. + */ public class IOExceptionListTestCase { @Test @@ -47,6 +50,23 @@ public class IOExceptionListTestCase { } @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); assertNull(sqlExceptionList.getCause());