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/21 17:40:44 UTC
[commons-io] branch master updated (fdfdf5c7 -> 57313b3e)
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-io.git
from fdfdf5c7 Use try-with-resources
new 3620e8e4 Refactor magic strings
new 57313b3e Add IOUtils.closeQuietly(Collection<Closeable>).
The 2 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 | 6 +-
src/main/java/org/apache/commons/io/IOUtils.java | 61 ++++-
.../java/org/apache/commons/io/IOUtilsTest.java | 16 +-
.../commons/io/input/XmlStreamReaderTest.java | 254 +++++++++++----------
.../serialization/AbstractCloseableListTest.java | 10 +-
5 files changed, 199 insertions(+), 148 deletions(-)
[commons-io] 01/02: Refactor magic strings
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-io.git
commit 3620e8e42d205efd1440a3f54928a1963d151673
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jun 21 12:25:56 2022 -0400
Refactor magic strings
---
.../commons/io/input/XmlStreamReaderTest.java | 254 +++++++++++----------
1 file changed, 132 insertions(+), 122 deletions(-)
diff --git a/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java b/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
index 6556cb75..6e6e6839 100644
--- a/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
+++ b/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
@@ -41,6 +41,16 @@ import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.DefaultLocale;
public class XmlStreamReaderTest {
+
+ private static final String ISO_8859_1 = "ISO-8859-1";
+ private static final String US_ASCII = "US-ASCII";
+ private static final String UTF_16 = "UTF-16";
+ private static final String UTF_16LE = "UTF-16LE";
+ private static final String UTF_16BE = "UTF-16BE";
+ private static final String UTF_32 = "UTF-32";
+ private static final String UTF_32LE = "UTF-32LE";
+ private static final String UTF_32BE = "UTF-32BE";
+ private static final String UTF_8 = "UTF-8";
private static final String XML5 = "xml-prolog-encoding-spaced-single-quotes";
private static final String XML4 = "xml-prolog-encoding-single-quotes";
private static final String XML3 = "xml-prolog-encoding-double-quotes";
@@ -153,7 +163,7 @@ public class XmlStreamReaderTest {
throws Exception {
try (InputStream is = getXmlInputStream(bomEnc, prologEnc == null ? XML1 : XML3, streamEnc, prologEnc);
XmlStreamReader xmlReader = new XmlStreamReader(is, cT, false, alternateEnc)) {
- if (!streamEnc.equals("UTF-16")) {
+ if (!streamEnc.equals(UTF_16)) {
// we can not assert things here because UTF-8, US-ASCII and
// ISO-8859-1 look alike for the chars used for detection
// (niallp 2010-10-06 - I re-instated the check below - the tests(6) passed)
@@ -170,7 +180,7 @@ public class XmlStreamReaderTest {
public void testEncodingAttributeXML() throws Exception {
try (InputStream is = new ByteArrayInputStream(ENCODING_ATTRIBUTE_XML.getBytes(StandardCharsets.UTF_8));
XmlStreamReader xmlReader = new XmlStreamReader(is, "", true)) {
- assertEquals(xmlReader.getEncoding(), "UTF-8");
+ assertEquals(xmlReader.getEncoding(), UTF_8);
}
}
@@ -180,93 +190,93 @@ public class XmlStreamReaderTest {
// checks for non-UTF-16 encodings (18 tests) and these failed
// _testHttpValid("application/xml", "no-bom", "US-ASCII", null);
// _testHttpValid("application/xml", "UTF-8-bom", "US-ASCII", null);
- testHttpValid("application/xml", "UTF-8-bom", "UTF-8", null);
- testHttpValid("application/xml", "UTF-8-bom", "UTF-8", "UTF-8");
- testHttpValid("application/xml;charset=UTF-8", "UTF-8-bom", "UTF-8", null);
- testHttpValid("application/xml;charset=\"UTF-8\"", "UTF-8-bom", "UTF-8", null);
- testHttpValid("application/xml;charset='UTF-8'", "UTF-8-bom", "UTF-8", null);
- testHttpValid("application/xml;charset=UTF-8", "UTF-8-bom", "UTF-8", "UTF-8");
- testHttpValid("application/xml;charset=UTF-16", "UTF-16BE-bom", "UTF-16BE", null);
- testHttpValid("application/xml;charset=UTF-16", "UTF-16BE-bom", "UTF-16BE", "UTF-16");
- testHttpValid("application/xml;charset=UTF-16", "UTF-16BE-bom", "UTF-16BE", "UTF-16BE");
-
- testHttpInvalid("application/xml;charset=UTF-16BE", "UTF-16BE-bom", "UTF-16BE", null);
- testHttpInvalid("application/xml;charset=UTF-16BE", "UTF-16BE-bom", "UTF-16BE", "UTF-16");
- testHttpInvalid("application/xml;charset=UTF-16BE", "UTF-16BE-bom", "UTF-16BE", "UTF-16BE");
-
- testHttpInvalid("application/xml;charset=UTF-32BE", "UTF-32BE-bom", "UTF-32BE", null);
- testHttpInvalid("application/xml;charset=UTF-32BE", "UTF-32BE-bom", "UTF-32BE", "UTF-32");
- testHttpInvalid("application/xml;charset=UTF-32BE", "UTF-32BE-bom", "UTF-32BE", "UTF-32BE");
-
- testHttpInvalid("application/xml", "UTF-8-bom", "US-ASCII", "US-ASCII");
- testHttpInvalid("application/xml;charset=UTF-16", "UTF-16LE", "UTF-8", "UTF-8");
- testHttpInvalid("application/xml;charset=UTF-16", "no-bom", "UTF-16BE", "UTF-16BE");
- testHttpInvalid("application/xml;charset=UTF-32", "UTF-32LE", "UTF-8", "UTF-8");
- testHttpInvalid("application/xml;charset=UTF-32", "no-bom", "UTF-32BE", "UTF-32BE");
-
- testHttpValid("text/xml", "no-bom", "US-ASCII", null);
- testHttpValid("text/xml;charset=UTF-8", "UTF-8-bom", "UTF-8", "UTF-8");
- testHttpValid("text/xml;charset=UTF-8", "UTF-8-bom", "UTF-8", null);
- testHttpValid("text/xml;charset=UTF-16", "UTF-16BE-bom", "UTF-16BE", null);
- testHttpValid("text/xml;charset=UTF-16", "UTF-16BE-bom", "UTF-16BE", "UTF-16");
- testHttpValid("text/xml;charset=UTF-16", "UTF-16BE-bom", "UTF-16BE", "UTF-16BE");
- testHttpValid("text/xml;charset=UTF-32", "UTF-32BE-bom", "UTF-32BE", null);
- testHttpValid("text/xml;charset=UTF-32", "UTF-32BE-bom", "UTF-32BE", "UTF-32");
- testHttpValid("text/xml;charset=UTF-32", "UTF-32BE-bom", "UTF-32BE", "UTF-32BE");
- testHttpValid("text/xml", "UTF-8-bom", "US-ASCII", null);
-
- testAlternateDefaultEncoding("application/xml", "UTF-8-bom", "UTF-8", null, null);
- testAlternateDefaultEncoding("application/xml", "no-bom", "US-ASCII", null, "US-ASCII");
- testAlternateDefaultEncoding("application/xml", "UTF-8-bom", "UTF-8", null, "UTF-8");
- testAlternateDefaultEncoding("text/xml", "no-bom", "US-ASCII", null, null);
- testAlternateDefaultEncoding("text/xml", "no-bom", "US-ASCII", null, "US-ASCII");
- testAlternateDefaultEncoding("text/xml", "no-bom", "US-ASCII", null, "UTF-8");
-
- testHttpInvalid("text/xml;charset=UTF-16BE", "UTF-16BE-bom", "UTF-16BE", null);
- testHttpInvalid("text/xml;charset=UTF-16BE", "UTF-16BE-bom", "UTF-16BE", "UTF-16");
- testHttpInvalid("text/xml;charset=UTF-16BE", "UTF-16BE-bom", "UTF-16BE", "UTF-16BE");
- testHttpInvalid("text/xml;charset=UTF-16", "no-bom", "UTF-16BE", "UTF-16BE");
- testHttpInvalid("text/xml;charset=UTF-16", "no-bom", "UTF-16BE", null);
-
- testHttpInvalid("text/xml;charset=UTF-32BE", "UTF-32BE-bom", "UTF-32BE", null);
- testHttpInvalid("text/xml;charset=UTF-32BE", "UTF-32BE-bom", "UTF-32BE", "UTF-32");
- testHttpInvalid("text/xml;charset=UTF-32BE", "UTF-32BE-bom", "UTF-32BE", "UTF-32BE");
- testHttpInvalid("text/xml;charset=UTF-32", "no-bom", "UTF-32BE", "UTF-32BE");
- testHttpInvalid("text/xml;charset=UTF-32", "no-bom", "UTF-32BE", null);
-
- testHttpLenient("text/xml", "no-bom", "US-ASCII", null, "US-ASCII");
- testHttpLenient("text/xml;charset=UTF-8", "UTF-8-bom", "UTF-8", "UTF-8", "UTF-8");
- testHttpLenient("text/xml;charset=UTF-8", "UTF-8-bom", "UTF-8", null, "UTF-8");
- testHttpLenient("text/xml;charset=UTF-16", "UTF-16BE-bom", "UTF-16BE", null, "UTF-16BE");
- testHttpLenient("text/xml;charset=UTF-16", "UTF-16BE-bom", "UTF-16BE", "UTF-16", "UTF-16");
- testHttpLenient("text/xml;charset=UTF-16", "UTF-16BE-bom", "UTF-16BE", "UTF-16BE", "UTF-16BE");
- testHttpLenient("text/xml;charset=UTF-32", "UTF-32BE-bom", "UTF-32BE", null, "UTF-32BE");
- testHttpLenient("text/xml;charset=UTF-32", "UTF-32BE-bom", "UTF-32BE", "UTF-32", "UTF-32");
- testHttpLenient("text/xml;charset=UTF-32", "UTF-32BE-bom", "UTF-32BE", "UTF-32BE", "UTF-32BE");
- testHttpLenient("text/xml", "UTF-8-bom", "US-ASCII", null, "US-ASCII");
-
- testHttpLenient("text/xml;charset=UTF-16BE", "UTF-16BE-bom", "UTF-16BE", null, "UTF-16BE");
- testHttpLenient("text/xml;charset=UTF-16BE", "UTF-16BE-bom", "UTF-16BE", "UTF-16", "UTF-16");
- testHttpLenient("text/xml;charset=UTF-16BE", "UTF-16BE-bom", "UTF-16BE", "UTF-16BE", "UTF-16BE");
- testHttpLenient("text/xml;charset=UTF-16", "no-bom", "UTF-16BE", "UTF-16BE", "UTF-16BE");
- testHttpLenient("text/xml;charset=UTF-16", "no-bom", "UTF-16BE", null, "UTF-16");
-
- testHttpLenient("text/xml;charset=UTF-32BE", "UTF-32BE-bom", "UTF-32BE", null, "UTF-32BE");
- testHttpLenient("text/xml;charset=UTF-32BE", "UTF-32BE-bom", "UTF-32BE", "UTF-32", "UTF-32");
- testHttpLenient("text/xml;charset=UTF-32BE", "UTF-32BE-bom", "UTF-32BE", "UTF-32BE", "UTF-32BE");
- testHttpLenient("text/xml;charset=UTF-32", "no-bom", "UTF-32BE", "UTF-32BE", "UTF-32BE");
- testHttpLenient("text/xml;charset=UTF-32", "no-bom", "UTF-32BE", null, "UTF-32");
-
- testHttpLenient("text/html", "no-bom", "US-ASCII", "US-ASCII", "US-ASCII");
- testHttpLenient("text/html", "no-bom", "US-ASCII", null, "US-ASCII");
- testHttpLenient("text/html;charset=UTF-8", "no-bom", "US-ASCII", "UTF-8", "UTF-8");
- testHttpLenient("text/html;charset=UTF-16BE", "no-bom", "US-ASCII", "UTF-8", "UTF-8");
- testHttpLenient("text/html;charset=UTF-32BE", "no-bom", "US-ASCII", "UTF-8", "UTF-8");
+ testHttpValid("application/xml", "UTF-8-bom", UTF_8, null);
+ testHttpValid("application/xml", "UTF-8-bom", UTF_8, UTF_8);
+ testHttpValid("application/xml;charset=UTF-8", "UTF-8-bom", UTF_8, null);
+ testHttpValid("application/xml;charset=\"UTF-8\"", "UTF-8-bom", UTF_8, null);
+ testHttpValid("application/xml;charset='UTF-8'", "UTF-8-bom", UTF_8, null);
+ testHttpValid("application/xml;charset=UTF-8", "UTF-8-bom", UTF_8, UTF_8);
+ testHttpValid("application/xml;charset=UTF-16", "UTF-16BE-bom", UTF_16BE, null);
+ testHttpValid("application/xml;charset=UTF-16", "UTF-16BE-bom", UTF_16BE, UTF_16);
+ testHttpValid("application/xml;charset=UTF-16", "UTF-16BE-bom", UTF_16BE, UTF_16BE);
+
+ testHttpInvalid("application/xml;charset=UTF-16BE", "UTF-16BE-bom", UTF_16BE, null);
+ testHttpInvalid("application/xml;charset=UTF-16BE", "UTF-16BE-bom", UTF_16BE, UTF_16);
+ testHttpInvalid("application/xml;charset=UTF-16BE", "UTF-16BE-bom", UTF_16BE, UTF_16BE);
+
+ testHttpInvalid("application/xml;charset=UTF-32BE", "UTF-32BE-bom", UTF_32BE, null);
+ testHttpInvalid("application/xml;charset=UTF-32BE", "UTF-32BE-bom", UTF_32BE, UTF_32);
+ testHttpInvalid("application/xml;charset=UTF-32BE", "UTF-32BE-bom", UTF_32BE, UTF_32BE);
+
+ testHttpInvalid("application/xml", "UTF-8-bom", US_ASCII, US_ASCII);
+ testHttpInvalid("application/xml;charset=UTF-16", UTF_16LE, UTF_8, UTF_8);
+ testHttpInvalid("application/xml;charset=UTF-16", "no-bom", UTF_16BE, UTF_16BE);
+ testHttpInvalid("application/xml;charset=UTF-32", UTF_32LE, UTF_8, UTF_8);
+ testHttpInvalid("application/xml;charset=UTF-32", "no-bom", UTF_32BE, UTF_32BE);
+
+ testHttpValid("text/xml", "no-bom", US_ASCII, null);
+ testHttpValid("text/xml;charset=UTF-8", "UTF-8-bom", UTF_8, UTF_8);
+ testHttpValid("text/xml;charset=UTF-8", "UTF-8-bom", UTF_8, null);
+ testHttpValid("text/xml;charset=UTF-16", "UTF-16BE-bom", UTF_16BE, null);
+ testHttpValid("text/xml;charset=UTF-16", "UTF-16BE-bom", UTF_16BE, UTF_16);
+ testHttpValid("text/xml;charset=UTF-16", "UTF-16BE-bom", UTF_16BE, UTF_16BE);
+ testHttpValid("text/xml;charset=UTF-32", "UTF-32BE-bom", UTF_32BE, null);
+ testHttpValid("text/xml;charset=UTF-32", "UTF-32BE-bom", UTF_32BE, UTF_32);
+ testHttpValid("text/xml;charset=UTF-32", "UTF-32BE-bom", UTF_32BE, UTF_32BE);
+ testHttpValid("text/xml", "UTF-8-bom", US_ASCII, null);
+
+ testAlternateDefaultEncoding("application/xml", "UTF-8-bom", UTF_8, null, null);
+ testAlternateDefaultEncoding("application/xml", "no-bom", US_ASCII, null, US_ASCII);
+ testAlternateDefaultEncoding("application/xml", "UTF-8-bom", UTF_8, null, UTF_8);
+ testAlternateDefaultEncoding("text/xml", "no-bom", US_ASCII, null, null);
+ testAlternateDefaultEncoding("text/xml", "no-bom", US_ASCII, null, US_ASCII);
+ testAlternateDefaultEncoding("text/xml", "no-bom", US_ASCII, null, UTF_8);
+
+ testHttpInvalid("text/xml;charset=UTF-16BE", "UTF-16BE-bom", UTF_16BE, null);
+ testHttpInvalid("text/xml;charset=UTF-16BE", "UTF-16BE-bom", UTF_16BE, UTF_16);
+ testHttpInvalid("text/xml;charset=UTF-16BE", "UTF-16BE-bom", UTF_16BE, UTF_16BE);
+ testHttpInvalid("text/xml;charset=UTF-16", "no-bom", UTF_16BE, UTF_16BE);
+ testHttpInvalid("text/xml;charset=UTF-16", "no-bom", UTF_16BE, null);
+
+ testHttpInvalid("text/xml;charset=UTF-32BE", "UTF-32BE-bom", UTF_32BE, null);
+ testHttpInvalid("text/xml;charset=UTF-32BE", "UTF-32BE-bom", UTF_32BE, UTF_32);
+ testHttpInvalid("text/xml;charset=UTF-32BE", "UTF-32BE-bom", UTF_32BE, UTF_32BE);
+ testHttpInvalid("text/xml;charset=UTF-32", "no-bom", UTF_32BE, UTF_32BE);
+ testHttpInvalid("text/xml;charset=UTF-32", "no-bom", UTF_32BE, null);
+
+ testHttpLenient("text/xml", "no-bom", US_ASCII, null, US_ASCII);
+ testHttpLenient("text/xml;charset=UTF-8", "UTF-8-bom", UTF_8, UTF_8, UTF_8);
+ testHttpLenient("text/xml;charset=UTF-8", "UTF-8-bom", UTF_8, null, UTF_8);
+ testHttpLenient("text/xml;charset=UTF-16", "UTF-16BE-bom", UTF_16BE, null, UTF_16BE);
+ testHttpLenient("text/xml;charset=UTF-16", "UTF-16BE-bom", UTF_16BE, UTF_16, UTF_16);
+ testHttpLenient("text/xml;charset=UTF-16", "UTF-16BE-bom", UTF_16BE, UTF_16BE, UTF_16BE);
+ testHttpLenient("text/xml;charset=UTF-32", "UTF-32BE-bom", UTF_32BE, null, UTF_32BE);
+ testHttpLenient("text/xml;charset=UTF-32", "UTF-32BE-bom", UTF_32BE, UTF_32, UTF_32);
+ testHttpLenient("text/xml;charset=UTF-32", "UTF-32BE-bom", UTF_32BE, UTF_32BE, UTF_32BE);
+ testHttpLenient("text/xml", "UTF-8-bom", US_ASCII, null, US_ASCII);
+
+ testHttpLenient("text/xml;charset=UTF-16BE", "UTF-16BE-bom", UTF_16BE, null, UTF_16BE);
+ testHttpLenient("text/xml;charset=UTF-16BE", "UTF-16BE-bom", UTF_16BE, UTF_16, UTF_16);
+ testHttpLenient("text/xml;charset=UTF-16BE", "UTF-16BE-bom", UTF_16BE, UTF_16BE, UTF_16BE);
+ testHttpLenient("text/xml;charset=UTF-16", "no-bom", UTF_16BE, UTF_16BE, UTF_16BE);
+ testHttpLenient("text/xml;charset=UTF-16", "no-bom", UTF_16BE, null, UTF_16);
+
+ testHttpLenient("text/xml;charset=UTF-32BE", "UTF-32BE-bom", UTF_32BE, null, UTF_32BE);
+ testHttpLenient("text/xml;charset=UTF-32BE", "UTF-32BE-bom", UTF_32BE, UTF_32, UTF_32);
+ testHttpLenient("text/xml;charset=UTF-32BE", "UTF-32BE-bom", UTF_32BE, UTF_32BE, UTF_32BE);
+ testHttpLenient("text/xml;charset=UTF-32", "no-bom", UTF_32BE, UTF_32BE, UTF_32BE);
+ testHttpLenient("text/xml;charset=UTF-32", "no-bom", UTF_32BE, null, UTF_32);
+
+ testHttpLenient("text/html", "no-bom", US_ASCII, US_ASCII, US_ASCII);
+ testHttpLenient("text/html", "no-bom", US_ASCII, null, US_ASCII);
+ testHttpLenient("text/html;charset=UTF-8", "no-bom", US_ASCII, UTF_8, UTF_8);
+ testHttpLenient("text/html;charset=UTF-16BE", "no-bom", US_ASCII, UTF_8, UTF_8);
+ testHttpLenient("text/html;charset=UTF-32BE", "no-bom", US_ASCII, UTF_8, UTF_8);
}
@Test
public void testHttpContent() throws Exception {
- final String encoding = "UTF-8";
+ final String encoding = UTF_8;
final String xml = getXML("no-bom", XML3, encoding, encoding);
try (XmlStreamReader xmlReader = new XmlStreamReader(new StringInputStream(xml, encoding))) {
assertEquals(xmlReader.getEncoding(), encoding, "Check encoding");
@@ -298,7 +308,7 @@ public class XmlStreamReaderTest {
final String prologEnc) throws Exception {
try (InputStream is = getXmlInputStream(bomEnc, prologEnc == null ? XML1 : XML3, streamEnc, prologEnc);
XmlStreamReader xmlReader = new XmlStreamReader(is, cT, false)) {
- if (!streamEnc.equals("UTF-16")) {
+ if (!streamEnc.equals(UTF_16)) {
// we can not assert things here because UTF-8, US-ASCII and
// ISO-8859-1 look alike for the chars used for detection
// (niallp 2010-10-06 - I re-instated the check below and removed the 2 tests that failed)
@@ -342,7 +352,7 @@ public class XmlStreamReaderTest {
@Test
protected void testNullURLConnectionInput() {
- assertThrows(NullPointerException.class, () -> new XmlStreamReader((URLConnection) null, "US-ASCII"));
+ assertThrows(NullPointerException.class, () -> new XmlStreamReader((URLConnection) null, US_ASCII));
}
@Test
@@ -369,46 +379,46 @@ public class XmlStreamReaderTest {
@Test
public void testRawBomUtf16() throws Exception {
- testRawBomValid("UTF-16BE");
- testRawBomValid("UTF-16LE");
- testRawBomValid("UTF-16");
+ testRawBomValid(UTF_16BE);
+ testRawBomValid(UTF_16LE);
+ testRawBomValid(UTF_16);
- testRawBomInvalid("UTF-16BE-bom", "UTF-16BE", "UTF-16LE");
- testRawBomInvalid("UTF-16LE-bom", "UTF-16LE", "UTF-16BE");
- testRawBomInvalid("UTF-16LE-bom", "UTF-16LE", "UTF-8");
+ testRawBomInvalid("UTF-16BE-bom", UTF_16BE, UTF_16LE);
+ testRawBomInvalid("UTF-16LE-bom", UTF_16LE, UTF_16BE);
+ testRawBomInvalid("UTF-16LE-bom", UTF_16LE, UTF_8);
}
@Test
public void testRawBomUtf32() throws Exception {
- testRawBomValid("UTF-32BE");
- testRawBomValid("UTF-32LE");
- testRawBomValid("UTF-32");
+ testRawBomValid(UTF_32BE);
+ testRawBomValid(UTF_32LE);
+ testRawBomValid(UTF_32);
- testRawBomInvalid("UTF-32BE-bom", "UTF-32BE", "UTF-32LE");
- testRawBomInvalid("UTF-32LE-bom", "UTF-32LE", "UTF-32BE");
- testRawBomInvalid("UTF-32LE-bom", "UTF-32LE", "UTF-8");
+ testRawBomInvalid("UTF-32BE-bom", UTF_32BE, UTF_32LE);
+ testRawBomInvalid("UTF-32LE-bom", UTF_32LE, UTF_32BE);
+ testRawBomInvalid("UTF-32LE-bom", UTF_32LE, UTF_8);
}
@Test
public void testRawBomUtf8() throws Exception {
- testRawBomValid("UTF-8");
- testRawBomInvalid("UTF-8-bom", "US-ASCII", "US-ASCII");
- testRawBomInvalid("UTF-8-bom", "ISO-8859-1", "ISO-8859-1");
- testRawBomInvalid("UTF-8-bom", "UTF-8", "UTF-16");
- testRawBomInvalid("UTF-8-bom", "UTF-8", "UTF-16BE");
- testRawBomInvalid("UTF-8-bom", "UTF-8", "UTF-16LE");
- testRawBomInvalid("UTF-16BE-bom", "UTF-16BE", "UTF-16LE");
- testRawBomInvalid("UTF-16LE-bom", "UTF-16LE", "UTF-16BE");
- testRawBomInvalid("UTF-16LE-bom", "UTF-16LE", "UTF-8");
- testRawBomInvalid("UTF-32BE-bom", "UTF-32BE", "UTF-32LE");
- testRawBomInvalid("UTF-32LE-bom", "UTF-32LE", "UTF-32BE");
- testRawBomInvalid("UTF-32LE-bom", "UTF-32LE", "UTF-8");
+ testRawBomValid(UTF_8);
+ testRawBomInvalid("UTF-8-bom", US_ASCII, US_ASCII);
+ testRawBomInvalid("UTF-8-bom", ISO_8859_1, ISO_8859_1);
+ testRawBomInvalid("UTF-8-bom", UTF_8, UTF_16);
+ testRawBomInvalid("UTF-8-bom", UTF_8, UTF_16BE);
+ testRawBomInvalid("UTF-8-bom", UTF_8, UTF_16LE);
+ testRawBomInvalid("UTF-16BE-bom", UTF_16BE, UTF_16LE);
+ testRawBomInvalid("UTF-16LE-bom", UTF_16LE, UTF_16BE);
+ testRawBomInvalid("UTF-16LE-bom", UTF_16LE, UTF_8);
+ testRawBomInvalid("UTF-32BE-bom", UTF_32BE, UTF_32LE);
+ testRawBomInvalid("UTF-32LE-bom", UTF_32LE, UTF_32BE);
+ testRawBomInvalid("UTF-32LE-bom", UTF_32LE, UTF_8);
}
protected void testRawBomValid(final String encoding) throws Exception {
try (InputStream is = getXmlInputStream(encoding + "-bom", XML3, encoding, encoding);
XmlStreamReader xmlReader = new XmlStreamReader(is, false)) {
- if (!encoding.equals("UTF-16") && !encoding.equals("UTF-32")) {
+ if (!encoding.equals(UTF_16) && !encoding.equals(UTF_32)) {
assertEquals(xmlReader.getEncoding(), encoding);
} else {
assertEquals(xmlReader.getEncoding().substring(0, encoding.length()), encoding);
@@ -418,7 +428,7 @@ public class XmlStreamReaderTest {
@Test
public void testRawContent() throws Exception {
- final String encoding = "UTF-8";
+ final String encoding = UTF_8;
final String xml = getXML("no-bom", XML3, encoding, encoding);
try (XmlStreamReader xmlReader = new XmlStreamReader(new StringInputStream(xml, encoding))) {
assertEquals(xmlReader.getEncoding(), encoding, "Check encoding");
@@ -444,48 +454,48 @@ public class XmlStreamReaderTest {
@Test
public void testRawNoBomIso8859_1() throws Exception {
- testRawNoBomValid("ISO-8859-1");
+ testRawNoBomValid(ISO_8859_1);
}
@Test
public void testRawNoBomUsAscii() throws Exception {
- testRawNoBomValid("US-ASCII");
+ testRawNoBomValid(US_ASCII);
}
@Test
public void testRawNoBomUtf16BE() throws Exception {
- testRawNoBomValid("UTF-16BE");
+ testRawNoBomValid(UTF_16BE);
}
@Test
public void testRawNoBomUtf16LE() throws Exception {
- testRawNoBomValid("UTF-16LE");
+ testRawNoBomValid(UTF_16LE);
}
@Test
public void testRawNoBomUtf32BE() throws Exception {
- testRawNoBomValid("UTF-32BE");
+ testRawNoBomValid(UTF_32BE);
}
@Test
public void testRawNoBomUtf32LE() throws Exception {
- testRawNoBomValid("UTF-32LE");
+ testRawNoBomValid(UTF_32LE);
}
@Test
public void testRawNoBomUtf8() throws Exception {
- testRawNoBomValid("UTF-8");
+ testRawNoBomValid(UTF_8);
}
protected void testRawNoBomValid(final String encoding) throws Exception {
InputStream is = getXmlInputStream("no-bom", XML1, encoding, encoding);
XmlStreamReader xmlReader = new XmlStreamReader(is, false);
- assertEquals(xmlReader.getEncoding(), "UTF-8");
+ assertEquals(xmlReader.getEncoding(), UTF_8);
xmlReader.close();
is = getXmlInputStream("no-bom", XML2, encoding, encoding);
xmlReader = new XmlStreamReader(is);
- assertEquals(xmlReader.getEncoding(), "UTF-8");
+ assertEquals(xmlReader.getEncoding(), UTF_8);
xmlReader.close();
is = getXmlInputStream("no-bom", XML3, encoding, encoding);
[commons-io] 02/02: Add IOUtils.closeQuietly(Collection).
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-io.git
commit 57313b3ed3b356d9a81103f4dbcc1eb38b9f2c0b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jun 21 13:40:37 2022 -0400
Add IOUtils.closeQuietly(Collection<Closeable>).
Add IOUtils.closeQuietly(Stream<Closeable>).
Internal refactoring to avoid type casts.
---
src/changes/changes.xml | 6 ++-
src/main/java/org/apache/commons/io/IOUtils.java | 61 ++++++++++++++++++----
.../java/org/apache/commons/io/IOUtilsTest.java | 16 +++---
.../serialization/AbstractCloseableListTest.java | 10 +---
4 files changed, 67 insertions(+), 26 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index fd142081..2b1a69bf 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -334,7 +334,7 @@ The <action> type attribute can be add,update,fix,remove.
- PathUtils.getAclFileAttributeView(Path, LinkOption...)
- PathUtils.getDosFileAttributeView(Path, LinkOption...)
- PathUtils.getPosixFileAttributeView(Path, LinkOption...)
- </action>
+ </action>
<action issue="IO-747" dev="mgrigorov" type="add">
Make commons-io a JPMS module by adding module-info.class.
</action>
@@ -389,6 +389,10 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add IOBiConsumer.noop().
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add IOUtils.closeQuietly(Collection<Closeable>).
+ Add IOUtils.closeQuietly(Stream<Closeable>).
+ </action>
<!-- UPDATE -->
<action dev="kinow" type="update" due-to="Dependabot, Gary Gregory">
Bump actions/cache from 2.1.6 to 3.0.4 #307, #337.
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java
index 1e1b2a95..7ac55595 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -51,6 +51,7 @@ import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
+import java.util.stream.Stream;
import org.apache.commons.io.function.IOConsumer;
import org.apache.commons.io.input.QueueInputStream;
@@ -472,6 +473,13 @@ public class IOUtils {
closeQuietly(closeable, null);
}
+ /**
+ * Avoids the need to type cast.
+ */
+ private static void closeQ(final Closeable closeable) {
+ closeQuietly(closeable, null);
+ }
+
/**
* Closes a {@link Closeable} unconditionally.
* <p>
@@ -520,7 +528,7 @@ public class IOUtils {
*/
public static void closeQuietly(final Closeable... closeables) {
if (closeables != null) {
- Arrays.stream(closeables).forEach(IOUtils::closeQuietly);
+ closeQuietly(Arrays.stream(closeables));
}
}
@@ -573,7 +581,39 @@ public class IOUtils {
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
public static void closeQuietly(final InputStream input) {
- closeQuietly((Closeable) input);
+ closeQ(input);
+ }
+
+ /**
+ * Closes an iterable of {@link Closeable} unconditionally.
+ * <p>
+ * Equivalent calling {@link Closeable#close()} on each element, except any exceptions will be ignored.
+ * </p>
+ *
+ * @param closeables the objects to close, may be null or already closed
+ * @see #closeQuietly(Closeable)
+ * @since 2.12.0
+ */
+ public static void closeQuietly(final Iterable<Closeable> closeables) {
+ if (closeables != null) {
+ closeables.forEach(IOUtils::closeQuietly);
+ }
+ }
+
+ /**
+ * Closes a stream of {@link Closeable} unconditionally.
+ * <p>
+ * Equivalent calling {@link Closeable#close()} on each element, except any exceptions will be ignored.
+ * </p>
+ *
+ * @param closeables the objects to close, may be null or already closed
+ * @see #closeQuietly(Closeable)
+ * @since 2.12.0
+ */
+ public static void closeQuietly(final Stream<Closeable> closeables) {
+ if (closeables != null) {
+ closeables.forEach(IOUtils::closeQuietly);
+ }
}
/**
@@ -607,7 +647,7 @@ public class IOUtils {
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
public static void closeQuietly(final OutputStream output) {
- closeQuietly((Closeable) output);
+ closeQ(output);
}
/**
@@ -640,7 +680,7 @@ public class IOUtils {
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
public static void closeQuietly(final Reader reader) {
- closeQuietly((Closeable) reader);
+ closeQ(reader);
}
/**
@@ -673,7 +713,7 @@ public class IOUtils {
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
public static void closeQuietly(final Selector selector) {
- closeQuietly((Closeable) selector);
+ closeQ(selector);
}
/**
@@ -706,7 +746,7 @@ public class IOUtils {
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
public static void closeQuietly(final ServerSocket serverSocket) {
- closeQuietly((Closeable) serverSocket);
+ closeQ(serverSocket);
}
/**
@@ -739,7 +779,7 @@ public class IOUtils {
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
public static void closeQuietly(final Socket socket) {
- closeQuietly((Closeable) socket);
+ closeQ(socket);
}
/**
@@ -771,7 +811,7 @@ public class IOUtils {
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
public static void closeQuietly(final Writer writer) {
- closeQuietly((Closeable) writer);
+ closeQ(writer);
}
/**
@@ -787,8 +827,7 @@ public class IOUtils {
* @throws IOException if an I/O error occurs.
* @since 2.8.0
*/
- public static long consume(final InputStream input)
- throws IOException {
+ public static long consume(final InputStream input) throws IOException {
return copyLarge(input, NullOutputStream.INSTANCE, getByteArray());
}
@@ -3297,6 +3336,7 @@ public class IOUtils {
write(data, output, Charsets.toCharset(charsetName));
}
+
/**
* Writes chars from a {@link CharSequence} to a {@link Writer}.
*
@@ -3312,7 +3352,6 @@ public class IOUtils {
}
}
-
/**
* Writes chars from a {@link String} to bytes on an
* {@link OutputStream} using the default character encoding of the
diff --git a/src/test/java/org/apache/commons/io/IOUtilsTest.java b/src/test/java/org/apache/commons/io/IOUtilsTest.java
index ab140520..c2cba682 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTest.java
@@ -57,12 +57,15 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
+import java.util.stream.Stream;
import org.apache.commons.io.function.IOConsumer;
+import org.apache.commons.io.input.BrokenInputStream;
import org.apache.commons.io.input.CircularInputStream;
import org.apache.commons.io.input.NullInputStream;
import org.apache.commons.io.input.StringInputStream;
import org.apache.commons.io.output.AppendableWriter;
+import org.apache.commons.io.output.BrokenOutputStream;
import org.apache.commons.io.output.CountingOutputStream;
import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.io.output.NullWriter;
@@ -339,18 +342,19 @@ public class IOUtilsTest {
@Test
public void testCloseQuietly_AllCloseableIOException() {
- final Closeable closeable = () -> {
- throw new IOException();
- };
+ final Closeable closeable = BrokenInputStream.INSTANCE;
assertDoesNotThrow(() -> IOUtils.closeQuietly(closeable, null, closeable));
+ assertDoesNotThrow(() -> IOUtils.closeQuietly(Arrays.asList(closeable, null, closeable)));
+ assertDoesNotThrow(() -> IOUtils.closeQuietly(Stream.of(closeable, null, closeable)));
}
@Test
public void testCloseQuietly_CloseableIOException() {
assertDoesNotThrow(() -> {
- IOUtils.closeQuietly(() -> {
- throw new IOException();
- });
+ IOUtils.closeQuietly(BrokenInputStream.INSTANCE);
+ });
+ assertDoesNotThrow(() -> {
+ IOUtils.closeQuietly(BrokenOutputStream.INSTANCE);
});
}
diff --git a/src/test/java/org/apache/commons/io/serialization/AbstractCloseableListTest.java b/src/test/java/org/apache/commons/io/serialization/AbstractCloseableListTest.java
index 8efa7a3f..71bfbd27 100644
--- a/src/test/java/org/apache/commons/io/serialization/AbstractCloseableListTest.java
+++ b/src/test/java/org/apache/commons/io/serialization/AbstractCloseableListTest.java
@@ -19,10 +19,10 @@
package org.apache.commons.io.serialization;
import java.io.Closeable;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@@ -34,13 +34,7 @@ public abstract class AbstractCloseableListTest {
@AfterEach
public void cleanup() {
- for (final Closeable c : closeableList) {
- try {
- c.close();
- } catch (final IOException ignored) {
- // ignore
- }
- }
+ IOUtils.closeQuietly(closeableList);
}
/**