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 16:18:49 UTC
[commons-io] 05/05: Use try-with-resources
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 fdfdf5c7249bb6dbbec0091e29574f3b067cb6e0
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jun 21 12:18:36 2022 -0400
Use try-with-resources
---
.../commons/io/input/NullInputStreamTest.java | 68 +++++------
.../apache/commons/io/input/NullReaderTest.java | 95 +++++++--------
.../commons/io/input/TaggedInputStreamTest.java | 47 ++++---
.../io/input/UnixLineEndingInputStreamTest.java | 11 +-
.../UnsynchronizedByteArrayInputStreamTest.java | 20 ++-
.../commons/io/input/XmlStreamReaderTest.java | 135 ++++++++++-----------
.../io/input/XmlStreamReaderUtilitiesTest.java | 18 ++-
7 files changed, 188 insertions(+), 206 deletions(-)
diff --git a/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java b/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java
index 2fcaa030..49f31a1c 100644
--- a/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java
@@ -71,51 +71,47 @@ public class NullInputStreamTest {
public void testMarkAndReset() throws Exception {
int position = 0;
final int readlimit = 10;
- @SuppressWarnings("resource") // this is actually closed
- final InputStream input = new TestNullInputStream(100, true, false);
+ try (InputStream input = new TestNullInputStream(100, true, false)) {
- assertTrue(input.markSupported(), "Mark Should be Supported");
+ assertTrue(input.markSupported(), "Mark Should be Supported");
- // No Mark
- try {
- input.reset();
- fail("Read limit exceeded, expected IOException ");
- } catch (final IOException e) {
- assertEquals("No position has been marked", e.getMessage(), "No Mark IOException message");
- }
+ // No Mark
+ try {
+ input.reset();
+ fail("Read limit exceeded, expected IOException ");
+ } catch (final IOException e) {
+ assertEquals("No position has been marked", e.getMessage(), "No Mark IOException message");
+ }
- for (; position < 3; position++) {
- assertEquals(position, input.read(), "Read Before Mark [" + position +"]");
- }
+ for (; position < 3; position++) {
+ assertEquals(position, input.read(), "Read Before Mark [" + position + "]");
+ }
- // Mark
- input.mark(readlimit);
+ // Mark
+ input.mark(readlimit);
- // Read further
- for (int i = 0; i < 3; i++) {
- assertEquals(position + i, input.read(), "Read After Mark [" + i +"]");
- }
+ // Read further
+ for (int i = 0; i < 3; i++) {
+ assertEquals(position + i, input.read(), "Read After Mark [" + i + "]");
+ }
- // Reset
- input.reset();
+ // Reset
+ input.reset();
- // Read From marked position
- for (int i = 0; i < readlimit + 1; i++) {
- assertEquals(position + i, input.read(), "Read After Reset [" + i +"]");
- }
+ // Read From marked position
+ for (int i = 0; i < readlimit + 1; i++) {
+ assertEquals(position + i, input.read(), "Read After Reset [" + i + "]");
+ }
- // Reset after read limit passed
- try {
- input.reset();
- fail("Read limit exceeded, expected IOException ");
- } catch (final IOException e) {
- assertEquals("Marked position [" + position
- + "] is no longer valid - passed the read limit ["
- + readlimit + "]",
- e.getMessage(),
- "Read limit IOException message");
+ // Reset after read limit passed
+ try {
+ input.reset();
+ fail("Read limit exceeded, expected IOException ");
+ } catch (final IOException e) {
+ assertEquals("Marked position [" + position + "] is no longer valid - passed the read limit [" + readlimit + "]", e.getMessage(),
+ "Read limit IOException message");
+ }
}
- input.close();
}
@Test
diff --git a/src/test/java/org/apache/commons/io/input/NullReaderTest.java b/src/test/java/org/apache/commons/io/input/NullReaderTest.java
index 87cd2542..39d8673e 100644
--- a/src/test/java/org/apache/commons/io/input/NullReaderTest.java
+++ b/src/test/java/org/apache/commons/io/input/NullReaderTest.java
@@ -71,51 +71,47 @@ public class NullReaderTest {
public void testMarkAndReset() throws Exception {
int position = 0;
final int readlimit = 10;
- @SuppressWarnings("resource") // this is actually closed
- final Reader reader = new TestNullReader(100, true, false);
+ try (Reader reader = new TestNullReader(100, true, false)) {
- assertTrue(reader.markSupported(), "Mark Should be Supported");
+ assertTrue(reader.markSupported(), "Mark Should be Supported");
- // No Mark
- try {
- reader.reset();
- fail("Read limit exceeded, expected IOException ");
- } catch (final IOException e) {
- assertEquals("No position has been marked", e.getMessage(), "No Mark IOException message");
- }
+ // No Mark
+ try {
+ reader.reset();
+ fail("Read limit exceeded, expected IOException ");
+ } catch (final IOException e) {
+ assertEquals("No position has been marked", e.getMessage(), "No Mark IOException message");
+ }
- for (; position < 3; position++) {
- assertEquals(position, reader.read(), "Read Before Mark [" + position +"]");
- }
+ for (; position < 3; position++) {
+ assertEquals(position, reader.read(), "Read Before Mark [" + position + "]");
+ }
- // Mark
- reader.mark(readlimit);
+ // Mark
+ reader.mark(readlimit);
- // Read further
- for (int i = 0; i < 3; i++) {
- assertEquals(position + i, reader.read(), "Read After Mark [" + i +"]");
- }
+ // Read further
+ for (int i = 0; i < 3; i++) {
+ assertEquals(position + i, reader.read(), "Read After Mark [" + i + "]");
+ }
- // Reset
- reader.reset();
+ // Reset
+ reader.reset();
- // Read From marked position
- for (int i = 0; i < readlimit + 1; i++) {
- assertEquals(position + i, reader.read(), "Read After Reset [" + i +"]");
- }
+ // Read From marked position
+ for (int i = 0; i < readlimit + 1; i++) {
+ assertEquals(position + i, reader.read(), "Read After Reset [" + i + "]");
+ }
- // Reset after read limit passed
- try {
- reader.reset();
- fail("Read limit exceeded, expected IOException ");
- } catch (final IOException e) {
- assertEquals("Marked position [" + position
- + "] is no longer valid - passed the read limit ["
- + readlimit + "]",
- e.getMessage(),
- "Read limit IOException message");
+ // Reset after read limit passed
+ try {
+ reader.reset();
+ fail("Read limit exceeded, expected IOException ");
+ } catch (final IOException e) {
+ assertEquals("Marked position [" + position + "] is no longer valid - passed the read limit [" + readlimit + "]", e.getMessage(),
+ "Read limit IOException message");
+ }
}
- reader.close();
}
@Test
@@ -209,20 +205,19 @@ public class NullReaderTest {
@Test
public void testSkip() throws Exception {
- final Reader reader = new TestNullReader(10, true, false);
- assertEquals(0, reader.read(), "Read 1");
- assertEquals(1, reader.read(), "Read 2");
- assertEquals(5, reader.skip(5), "Skip 1");
- assertEquals(7, reader.read(), "Read 3");
- assertEquals(2, reader.skip(5), "Skip 2"); // only 2 left to skip
- assertEquals(-1, reader.skip(5), "Skip 3 (EOF)"); // End of file
- try {
- reader.skip(5); //
- fail("Expected IOException for skipping after end of file");
- } catch (final IOException e) {
- assertEquals("Skip after end of file", e.getMessage(),
- "Skip after EOF IOException message");
+ try (Reader reader = new TestNullReader(10, true, false)) {
+ assertEquals(0, reader.read(), "Read 1");
+ assertEquals(1, reader.read(), "Read 2");
+ assertEquals(5, reader.skip(5), "Skip 1");
+ assertEquals(7, reader.read(), "Read 3");
+ assertEquals(2, reader.skip(5), "Skip 2"); // only 2 left to skip
+ assertEquals(-1, reader.skip(5), "Skip 3 (EOF)"); // End of file
+ try {
+ reader.skip(5); //
+ fail("Expected IOException for skipping after end of file");
+ } catch (final IOException e) {
+ assertEquals("Skip after end of file", e.getMessage(), "Skip after EOF IOException message");
+ }
}
- reader.close();
}
}
diff --git a/src/test/java/org/apache/commons/io/input/TaggedInputStreamTest.java b/src/test/java/org/apache/commons/io/input/TaggedInputStreamTest.java
index b09ba662..ffa2ff45 100644
--- a/src/test/java/org/apache/commons/io/input/TaggedInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/TaggedInputStreamTest.java
@@ -85,43 +85,40 @@ public class TaggedInputStreamTest {
@Test
public void testEmptyStream() throws IOException {
- final InputStream stream = new TaggedInputStream(ClosedInputStream.INSTANCE);
- assertEquals(0, stream.available());
- assertEquals(-1, stream.read());
- assertEquals(-1, stream.read(new byte[1]));
- assertEquals(-1, stream.read(new byte[1], 0, 1));
- stream.close();
+ try (InputStream stream = new TaggedInputStream(ClosedInputStream.INSTANCE)) {
+ assertEquals(0, stream.available());
+ assertEquals(-1, stream.read());
+ assertEquals(-1, stream.read(new byte[1]));
+ assertEquals(-1, stream.read(new byte[1], 0, 1));
+ }
}
@Test
public void testNormalStream() throws IOException {
- final InputStream stream = new TaggedInputStream(
- new ByteArrayInputStream(new byte[] { 'a', 'b', 'c' }));
- assertEquals(3, stream.available());
- assertEquals('a', stream.read());
- final byte[] buffer = new byte[1];
- assertEquals(1, stream.read(buffer));
- assertEquals('b', buffer[0]);
- assertEquals(1, stream.read(buffer, 0, 1));
- assertEquals('c', buffer[0]);
- assertEquals(-1, stream.read());
- stream.close();
+ try (InputStream stream = new TaggedInputStream(new ByteArrayInputStream(new byte[] {'a', 'b', 'c'}))) {
+ assertEquals(3, stream.available());
+ assertEquals('a', stream.read());
+ final byte[] buffer = new byte[1];
+ assertEquals(1, stream.read(buffer));
+ assertEquals('b', buffer[0]);
+ assertEquals(1, stream.read(buffer, 0, 1));
+ assertEquals('c', buffer[0]);
+ assertEquals(-1, stream.read());
+ }
}
@Test
public void testOtherException() throws Exception {
final IOException exception = new IOException("test exception");
- final TaggedInputStream stream = new TaggedInputStream(ClosedInputStream.INSTANCE);
+ try (TaggedInputStream stream = new TaggedInputStream(ClosedInputStream.INSTANCE)) {
- assertFalse(stream.isCauseOf(exception));
- assertFalse(stream.isCauseOf(
- new TaggedIOException(exception, UUID.randomUUID())));
+ assertFalse(stream.isCauseOf(exception));
+ assertFalse(stream.isCauseOf(new TaggedIOException(exception, UUID.randomUUID())));
- stream.throwIfCauseOf(exception);
+ stream.throwIfCauseOf(exception);
- stream.throwIfCauseOf(
- new TaggedIOException(exception, UUID.randomUUID()));
- stream.close();
+ stream.throwIfCauseOf(new TaggedIOException(exception, UUID.randomUUID()));
+ }
}
}
diff --git a/src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java b/src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java
index 32a1aa4f..66f06964 100644
--- a/src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java
@@ -62,12 +62,11 @@ public class UnixLineEndingInputStreamTest {
}
private String roundtrip(final String msg, final boolean ensure) throws IOException {
- final ByteArrayInputStream baos = new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8));
- final UnixLineEndingInputStream lf = new UnixLineEndingInputStream(baos, ensure);
- final byte[] buf = new byte[100];
- final int read = lf.read(buf);
- lf.close();
- return new String(buf, 0, read, StandardCharsets.UTF_8);
+ try (final ByteArrayInputStream baos = new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8));
+ final UnixLineEndingInputStream lf = new UnixLineEndingInputStream(baos, ensure)) {
+ final byte[] buf = new byte[100];
+ return new String(buf, 0, lf.read(buf), StandardCharsets.UTF_8);
+ }
}
@Test
diff --git a/src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java b/src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java
index 27b19d1c..6f13b4eb 100644
--- a/src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java
@@ -38,17 +38,15 @@ public class UnsynchronizedByteArrayInputStreamTest {
final byte[] one = new byte[1];
final byte[] some = new byte[25];
- UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(empty);
- assertEquals(empty.length, is.available());
-
- is.close();
- is = new UnsynchronizedByteArrayInputStream(one);
- assertEquals(one.length, is.available());
-
- is.close();
- is = new UnsynchronizedByteArrayInputStream(some);
- assertEquals(some.length, is.available());
- is.close();
+ try (UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(empty)) {
+ assertEquals(empty.length, is.available());
+ }
+ try (UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(one)) {
+ assertEquals(one.length, is.available());
+ }
+ try (UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(some)) {
+ assertEquals(some.length, is.available());
+ }
}
@Test
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 542fea07..6556cb75 100644
--- a/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
+++ b/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
@@ -149,34 +149,29 @@ public class XmlStreamReaderTest {
return new ByteArrayInputStream(baos.toByteArray());
}
- public void testAlternateDefaultEncoding(final String cT, final String bomEnc,
- final String streamEnc, final String prologEnc, final String alternateEnc)
- throws Exception {
- final InputStream is = getXmlInputStream(bomEnc, prologEnc == null ? XML1
- : XML3, streamEnc, prologEnc);
- final XmlStreamReader xmlReader = new XmlStreamReader(is, cT, false, alternateEnc);
- 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)
- final String enc = alternateEnc != null ? alternateEnc : streamEnc;
- assertEquals(xmlReader.getEncoding(), enc);
- } else {
- //String enc = (alternateEnc != null) ? alternateEnc : streamEnc;
- assertEquals(xmlReader.getEncoding().substring(0,
- streamEnc.length()), streamEnc);
+ public void testAlternateDefaultEncoding(final String cT, final String bomEnc, final String streamEnc, final String prologEnc, final String alternateEnc)
+ 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")) {
+ // 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)
+ final String enc = alternateEnc != null ? alternateEnc : streamEnc;
+ assertEquals(xmlReader.getEncoding(), enc);
+ } else {
+ // String enc = (alternateEnc != null) ? alternateEnc : streamEnc;
+ assertEquals(xmlReader.getEncoding().substring(0, streamEnc.length()), streamEnc);
+ }
}
- xmlReader.close();
}
-
@Test
public void testEncodingAttributeXML() throws Exception {
- final InputStream is = new ByteArrayInputStream(ENCODING_ATTRIBUTE_XML
- .getBytes(StandardCharsets.UTF_8));
- final XmlStreamReader xmlReader = new XmlStreamReader(is, "", true);
- assertEquals(xmlReader.getEncoding(), "UTF-8");
- xmlReader.close();
+ try (InputStream is = new ByteArrayInputStream(ENCODING_ATTRIBUTE_XML.getBytes(StandardCharsets.UTF_8));
+ XmlStreamReader xmlReader = new XmlStreamReader(is, "", true)) {
+ assertEquals(xmlReader.getEncoding(), "UTF-8");
+ }
}
@Test
@@ -280,54 +275,48 @@ public class XmlStreamReaderTest {
}
protected void testHttpInvalid(final String cT, final String bomEnc, final String streamEnc,
- final String prologEnc) throws Exception {
- final InputStream is = getXmlInputStream(bomEnc,
- prologEnc == null ? XML2 : XML3, streamEnc, prologEnc);
- try {
- new XmlStreamReader(is, cT, false).close();
- fail("It should have failed for HTTP Content-type " + cT + ", BOM "
- + bomEnc + ", streamEnc " + streamEnc + " and prologEnc "
- + prologEnc);
- } catch (final IOException ex) {
- assertTrue(ex.getMessage().contains("Invalid encoding,"));
+ final String prologEnc) throws Exception {
+ try (InputStream is = getXmlInputStream(bomEnc, prologEnc == null ? XML2 : XML3, streamEnc, prologEnc)) {
+ try {
+ new XmlStreamReader(is, cT, false).close();
+ fail("It should have failed for HTTP Content-type " + cT + ", BOM " + bomEnc + ", streamEnc " + streamEnc + " and prologEnc " + prologEnc);
+ } catch (final IOException ex) {
+ assertTrue(ex.getMessage().contains("Invalid encoding,"));
+ }
}
}
protected void testHttpLenient(final String cT, final String bomEnc, final String streamEnc,
- final String prologEnc, final String shouldbe) throws Exception {
- final InputStream is = getXmlInputStream(bomEnc,
- prologEnc == null ? XML2 : XML3, streamEnc, prologEnc);
- final XmlStreamReader xmlReader = new XmlStreamReader(is, cT, true);
- assertEquals(xmlReader.getEncoding(), shouldbe);
- xmlReader.close();
+ final String prologEnc, final String shouldbe) throws Exception {
+ try (InputStream is = getXmlInputStream(bomEnc, prologEnc == null ? XML2 : XML3, streamEnc, prologEnc);
+ XmlStreamReader xmlReader = new XmlStreamReader(is, cT, true)) {
+ assertEquals(xmlReader.getEncoding(), shouldbe);
+ }
}
public void testHttpValid(final String cT, final String bomEnc, final String streamEnc,
- final String prologEnc) throws Exception {
- final InputStream is = getXmlInputStream(bomEnc,
- prologEnc == null ? XML1 : XML3, streamEnc, prologEnc);
- final XmlStreamReader xmlReader = new XmlStreamReader(is, cT, false);
- 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)
- assertEquals(xmlReader.getEncoding(), streamEnc);
- } else {
- assertEquals(xmlReader.getEncoding().substring(0,
- streamEnc.length()), streamEnc);
+ 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")) {
+ // 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)
+ assertEquals(xmlReader.getEncoding(), streamEnc);
+ } else {
+ assertEquals(xmlReader.getEncoding().substring(0, streamEnc.length()), streamEnc);
+ }
}
- xmlReader.close();
}
// Turkish language has specific rules to convert dotted and dottless i character.
@Test
@DefaultLocale(language = "tr")
public void testLowerCaseEncodingWithTurkishLocale_IO_557() throws Exception {
- final String[] encodings = { "iso8859-1", "us-ascii", "utf-8" };
+ final String[] encodings = {"iso8859-1", "us-ascii", "utf-8"};
for (final String encoding : encodings) {
final String xml = getXML("no-bom", XML3, encoding, encoding);
- try (ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes(encoding));
- final XmlStreamReader xmlReader = new XmlStreamReader(is)) {
+ try (ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes(encoding)); XmlStreamReader xmlReader = new XmlStreamReader(is)) {
assertTrue(encoding.equalsIgnoreCase(xmlReader.getEncoding()), "Check encoding : " + encoding);
assertEquals(xml, IOUtils.toString(xmlReader), "Check content");
}
@@ -355,10 +344,12 @@ public class XmlStreamReaderTest {
protected void testNullURLConnectionInput() {
assertThrows(NullPointerException.class, () -> new XmlStreamReader((URLConnection) null, "US-ASCII"));
}
+
@Test
protected void testNullURLInput() {
assertThrows(NullPointerException.class, () -> new XmlStreamReader((URL) null));
}
+
protected void testRawBomInvalid(final String bomEnc, final String streamEnc,
final String prologEnc) throws Exception {
final InputStream is = getXmlInputStream(bomEnc, XML3, streamEnc, prologEnc);
@@ -375,6 +366,7 @@ public class XmlStreamReaderTest {
xmlReader.close();
}
}
+
@Test
public void testRawBomUtf16() throws Exception {
testRawBomValid("UTF-16BE");
@@ -385,6 +377,7 @@ public class XmlStreamReaderTest {
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");
@@ -395,6 +388,7 @@ public class XmlStreamReaderTest {
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");
@@ -412,14 +406,14 @@ public class XmlStreamReaderTest {
}
protected void testRawBomValid(final String encoding) throws Exception {
- final InputStream is = getXmlInputStream(encoding + "-bom", XML3, encoding, encoding);
- final XmlStreamReader xmlReader = new XmlStreamReader(is, false);
- if (!encoding.equals("UTF-16") && !encoding.equals("UTF-32")) {
- assertEquals(xmlReader.getEncoding(), encoding);
- } else {
- assertEquals(xmlReader.getEncoding().substring(0, encoding.length()), encoding);
+ try (InputStream is = getXmlInputStream(encoding + "-bom", XML3, encoding, encoding);
+ XmlStreamReader xmlReader = new XmlStreamReader(is, false)) {
+ if (!encoding.equals("UTF-16") && !encoding.equals("UTF-32")) {
+ assertEquals(xmlReader.getEncoding(), encoding);
+ } else {
+ assertEquals(xmlReader.getEncoding().substring(0, encoding.length()), encoding);
+ }
}
- xmlReader.close();
}
@Test
@@ -436,23 +430,28 @@ public class XmlStreamReaderTest {
public void testRawNoBomCp1047() throws Exception {
testRawNoBomValid("CP1047");
}
+
protected void testRawNoBomInvalid(final String encoding) throws Exception {
- final InputStream is = getXmlInputStream("no-bom", XML3, encoding, encoding);
- try {
- new XmlStreamReader(is, false).close();
- fail("It should have failed");
- } catch (final IOException ex) {
- assertTrue(ex.getMessage().contains("Invalid encoding,"));
+ try (final InputStream is = getXmlInputStream("no-bom", XML3, encoding, encoding)) {
+ try {
+ new XmlStreamReader(is, false).close();
+ fail("It should have failed");
+ } catch (final IOException ex) {
+ assertTrue(ex.getMessage().contains("Invalid encoding,"));
+ }
}
}
+
@Test
public void testRawNoBomIso8859_1() throws Exception {
testRawNoBomValid("ISO-8859-1");
}
+
@Test
public void testRawNoBomUsAscii() throws Exception {
testRawNoBomValid("US-ASCII");
}
+
@Test
public void testRawNoBomUtf16BE() throws Exception {
testRawNoBomValid("UTF-16BE");
diff --git a/src/test/java/org/apache/commons/io/input/XmlStreamReaderUtilitiesTest.java b/src/test/java/org/apache/commons/io/input/XmlStreamReaderUtilitiesTest.java
index 65e07fb9..2f90a263 100644
--- a/src/test/java/org/apache/commons/io/input/XmlStreamReaderUtilitiesTest.java
+++ b/src/test/java/org/apache/commons/io/input/XmlStreamReaderUtilitiesTest.java
@@ -54,19 +54,17 @@ public class XmlStreamReaderUtilitiesTest {
private static final String TXTXML = "text/xml";
protected String calculateHttpEncoding(final String httpContentType, final String bomEnc, final String xmlGuessEnc,
- final String xmlEnc, final boolean lenient, final String defaultEncoding) throws IOException {
- final MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding);
- final String enc = mock.calculateHttpEncoding(httpContentType, bomEnc, xmlGuessEnc, xmlEnc, lenient);
- mock.close();
- return enc;
+ final String xmlEnc, final boolean lenient, final String defaultEncoding) throws IOException {
+ try (MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding)) {
+ return mock.calculateHttpEncoding(httpContentType, bomEnc, xmlGuessEnc, xmlEnc, lenient);
+ }
}
protected String calculateRawEncoding(final String bomEnc, final String xmlGuessEnc, final String xmlEnc,
- final String defaultEncoding) throws IOException {
- final MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding);
- final String enc = mock.calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc);
- mock.close();
- return enc;
+ final String defaultEncoding) throws IOException {
+ try (MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding)) {
+ return mock.calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc);
+ }
}
@SuppressWarnings("boxing")