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 085226e  Use try-with-resources in tests and more.
085226e is described below

commit 085226e8473cae1c8ddf216fad002151465bfaa3
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Tue Apr 9 11:28:16 2019 -0400

    Use try-with-resources in tests and more.
    
    - Don't nest if you do not have to.
    - Document empty blocks.
    - Only run FileFilterTestCase#testCanExecute() on Windows to fix Jenkins
    build.
---
 pom.xml                                            |   6 +
 .../commons/io/filefilter/FileFilterTestCase.java  |  10 +-
 .../commons/io/input/BOMInputStreamTest.java       |  48 +++--
 .../io/input/compatibility/XmlStreamReader.java    |  13 +-
 .../XmlStreamReaderUtilitiesCompatibilityTest.java |  20 +-
 .../commons/io/monitor/CollectionFileListener.java |   1 +
 .../commons/io/output/ChunkedWriterTest.java       |  30 +--
 .../io/output/CountingOutputStreamTest.java        |  63 +++---
 .../commons/io/output/NullOutputStreamTest.java    |  19 +-
 .../apache/commons/io/output/NullWriterTest.java   |  18 +-
 .../apache/commons/io/output/ProxyWriterTest.java  | 211 +++++++++++----------
 .../commons/io/output/StringBuilderWriterTest.java | 125 ++++++------
 .../commons/io/output/TaggedOutputStreamTest.java  |  45 ++---
 .../commons/io/output/TeeOutputStreamTest.java     |  60 +++---
 .../io/output/ThresholdingOutputStreamTest.java    |  18 +-
 .../commons/io/output/XmlStreamWriterTest.java     |  14 +-
 .../commons/io/serialization/ClosingBase.java      |   1 +
 17 files changed, 369 insertions(+), 333 deletions(-)

diff --git a/pom.xml b/pom.xml
index 8a7ab54..e040934 100644
--- a/pom.xml
+++ b/pom.xml
@@ -232,6 +232,12 @@ file comparators, endian transformation classes, and much 
more.
       <version>4.12</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <properties>
diff --git 
a/src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java 
b/src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java
index e9f5889..b6091d8 100644
--- a/src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java
+++ b/src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java
@@ -43,6 +43,9 @@ import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOCase;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.testtools.TestUtils;
+import org.apache.commons.lang3.SystemUtils;
+import org.junit.Assert;
+import org.junit.Assume;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
@@ -941,12 +944,13 @@ public class FileFilterTestCase {
 
     @Test
     public void testCanExecute() throws Exception {
+        Assume.assumeTrue(SystemUtils.IS_OS_WINDOWS);
         final File executableFile = 
File.createTempFile(getClass().getSimpleName(), ".temp");
         try {
             try (final BufferedOutputStream output = new 
BufferedOutputStream(new FileOutputStream(executableFile))) {
                 TestUtils.generateTestData(output, 32);
             }
-            executableFile.setExecutable(true);
+            Assert.assertTrue(executableFile.setExecutable(true));
             assertFiltering(CanExecuteFileFilter.CAN_EXECUTE, executableFile, 
true);
             executableFile.setExecutable(false);
             assertFiltering(CanExecuteFileFilter.CANNOT_EXECUTE, 
executableFile, false);
@@ -966,7 +970,7 @@ public class FileFilterTestCase {
                 new BufferedOutputStream(new FileOutputStream(readOnlyFile))){
             TestUtils.generateTestData(output, 32);
         }
-        readOnlyFile.setReadOnly();
+        Assert.assertTrue(readOnlyFile.setReadOnly());
         assertFiltering(CanReadFileFilter.CAN_READ,  readOnlyFile, true);
         assertFiltering(CanReadFileFilter.CANNOT_READ,  readOnlyFile, false);
         assertFiltering(CanReadFileFilter.READ_ONLY, readOnlyFile, true);
@@ -984,7 +988,7 @@ public class FileFilterTestCase {
                 new BufferedOutputStream(new FileOutputStream(readOnlyFile))){
             TestUtils.generateTestData(output, 32);
         }
-        readOnlyFile.setReadOnly();
+        Assert.assertTrue(readOnlyFile.setReadOnly());
         assertFiltering(CanWriteFileFilter.CAN_WRITE,    getTestDirectory(), 
true);
         assertFiltering(CanWriteFileFilter.CANNOT_WRITE, getTestDirectory(), 
false);
         assertFiltering(CanWriteFileFilter.CAN_WRITE,    readOnlyFile, false);
diff --git a/src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java 
b/src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java
index 8a9f4cd..d5a19b1 100644
--- a/src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java
@@ -609,7 +609,9 @@ public class BOMInputStreamTest {
         // UCS-2 is BE.
         Assume.assumeTrue(Charset.isSupported("ISO-10646-UCS-2"));
         final byte[] data = "<?xml version=\"1.0\" 
encoding=\"ISO-10646-UCS-2\"?><X/>".getBytes("ISO-10646-UCS-2");
-        parseXml(new BOMInputStream(createUtf16BeDataStream(data, true), 
ByteOrderMark.UTF_16BE));
+        try (BOMInputStream in = new 
BOMInputStream(createUtf16BeDataStream(data, true), ByteOrderMark.UTF_16BE)) {
+            parseXml(in);
+        }
         parseXml(createUtf16BeDataStream(data, true));
     }
 
@@ -620,23 +622,29 @@ public class BOMInputStreamTest {
         Assume.assumeTrue(Charset.isSupported("ISO-10646-UCS-4"));
         final byte[] data = "<?xml version=\"1.0\" 
encoding=\"ISO-10646-UCS-4\"?><X/>".getBytes("ISO-10646-UCS-4");
         // XML parser does not know what to do with UTF-32
-        parseXml(new BOMInputStream(createUtf32BeDataStream(data, true), 
ByteOrderMark.UTF_32BE));
-        // XML parser does not know what to do with UTF-32
-        Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", 
jvmAndSaxBothSupportCharset("UTF_32LE"));
+        try (BOMInputStream in = new 
BOMInputStream(createUtf32BeDataStream(data, true), ByteOrderMark.UTF_32BE)) {
+            parseXml(in);
+            // XML parser does not know what to do with UTF-32
+            Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", 
jvmAndSaxBothSupportCharset("UTF_32LE"));
+        }
         parseXml(createUtf32BeDataStream(data, true));
     }
 
     @Test
     public void testReadXmlWithBOMUtf16Be() throws Exception {
         final byte[] data = "<?xml version=\"1.0\" 
encoding=\"UTF-16BE\"?><X/>".getBytes(StandardCharsets.UTF_16BE);
-        parseXml(new BOMInputStream(createUtf16BeDataStream(data, true), 
ByteOrderMark.UTF_16BE));
+        try (BOMInputStream in = new 
BOMInputStream(createUtf16BeDataStream(data, true), ByteOrderMark.UTF_16BE)) {
+            parseXml(in);
+        }
         parseXml(createUtf16BeDataStream(data, true));
     }
 
     @Test
     public void testReadXmlWithBOMUtf16Le() throws Exception {
         final byte[] data = "<?xml version=\"1.0\" 
encoding=\"UTF-16LE\"?><X/>".getBytes(StandardCharsets.UTF_16LE);
-        parseXml(new BOMInputStream(createUtf16LeDataStream(data, true), 
ByteOrderMark.UTF_16LE));
+        try (BOMInputStream in = new 
BOMInputStream(createUtf16LeDataStream(data, true), ByteOrderMark.UTF_16LE)) {
+            parseXml(in);
+        }
         parseXml(createUtf16LeDataStream(data, true));
     }
 
@@ -644,24 +652,34 @@ public class BOMInputStreamTest {
     public void testReadXmlWithBOMUtf32Be() throws Exception {
         Assume.assumeTrue("JVM and SAX need to support UTF_32BE for this", 
jvmAndSaxBothSupportCharset("UTF_32BE"));
         final byte[] data = "<?xml version=\"1.0\" 
encoding=\"UTF-32BE\"?><X/>".getBytes("UTF_32BE");
-        parseXml(new BOMInputStream(createUtf32BeDataStream(data, true), 
ByteOrderMark.UTF_32BE));
+        try (BOMInputStream in = new 
BOMInputStream(createUtf32BeDataStream(data, true), ByteOrderMark.UTF_32BE)) {
+            parseXml(in);
+        }
         // XML parser does not know what to do with UTF-32, so we warp the 
input stream with a XmlStreamReader
-        parseXml(new XmlStreamReader(createUtf32BeDataStream(data, true)));
+        try (XmlStreamReader in = new 
XmlStreamReader(createUtf32BeDataStream(data, true))) {
+            parseXml(in);
+        }
     }
 
     @Test
     public void testReadXmlWithBOMUtf32Le() throws Exception {
         Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", 
jvmAndSaxBothSupportCharset("UTF_32LE"));
         final byte[] data = "<?xml version=\"1.0\" 
encoding=\"UTF-32LE\"?><X/>".getBytes("UTF_32LE");
-        parseXml(new BOMInputStream(createUtf32LeDataStream(data, true), 
ByteOrderMark.UTF_32LE));
+        try (BOMInputStream in = new 
BOMInputStream(createUtf32LeDataStream(data, true), ByteOrderMark.UTF_32LE)) {
+            parseXml(in);
+        }
         // XML parser does not know what to do with UTF-32, so we warp the 
input stream with a XmlStreamReader
-        parseXml(new XmlStreamReader(createUtf32LeDataStream(data, true)));
+        try (XmlStreamReader in = new 
XmlStreamReader(createUtf32LeDataStream(data, true))) {
+            parseXml(in);
+        }
     }
 
     @Test
     public void testReadXmlWithBOMUtf8() throws Exception {
         final byte[] data = "<?xml version=\"1.0\" 
encoding=\"UTF-8\"?><X/>".getBytes(StandardCharsets.UTF_8);
-        parseXml(new BOMInputStream(createUtf8DataStream(data, true)));
+        try (BOMInputStream in = new BOMInputStream(createUtf8DataStream(data, 
true))) {
+            parseXml(in);
+        }
         parseXml(createUtf8DataStream(data, true));
     }
 
@@ -669,7 +687,9 @@ public class BOMInputStreamTest {
     public void testReadXmlWithoutBOMUtf32Be() throws Exception {
         Assume.assumeTrue("JVM and SAX need to support UTF_32BE for this", 
jvmAndSaxBothSupportCharset("UTF_32BE"));
         final byte[] data = "<?xml version=\"1.0\" 
encoding=\"UTF_32BE\"?><X/>".getBytes("UTF_32BE");
-        parseXml(new BOMInputStream(createUtf32BeDataStream(data, false)));
+        try (BOMInputStream in = new 
BOMInputStream(createUtf32BeDataStream(data, false))) {
+            parseXml(in);
+        }
         parseXml(createUtf32BeDataStream(data, false));
     }
 
@@ -677,7 +697,9 @@ public class BOMInputStreamTest {
     public void testReadXmlWithoutBOMUtf32Le() throws Exception {
         Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", 
jvmAndSaxBothSupportCharset("UTF_32LE"));
         final byte[] data = "<?xml version=\"1.0\" 
encoding=\"UTF-32LE\"?><X/>".getBytes("UTF_32LE");
-        parseXml(new BOMInputStream(createUtf32LeDataStream(data, false)));
+        try (BOMInputStream in = new 
BOMInputStream(createUtf32LeDataStream(data, false))) {
+            parseXml(in);
+        }
         parseXml(createUtf32BeDataStream(data, false));
     }
 
diff --git 
a/src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReader.java 
b/src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReader.java
index fe417ba..1c7c0ff 100644
--- 
a/src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReader.java
+++ 
b/src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReader.java
@@ -182,9 +182,8 @@ public class XmlStreamReader extends Reader {
         } catch (final XmlStreamReaderException ex) {
             if (!lenient) {
                 throw ex;
-            } else {
-                doLenientDetection(null, ex);
             }
+            doLenientDetection(null, ex);
         }
     }
 
@@ -319,9 +318,8 @@ public class XmlStreamReader extends Reader {
         } catch (final XmlStreamReaderException ex) {
             if (!lenient) {
                 throw ex;
-            } else {
-                doLenientDetection(httpContentType, ex);
             }
+            doLenientDetection(httpContentType, ex);
         }
     }
 
@@ -681,11 +679,10 @@ public class XmlStreamReader extends Reader {
             if (firstGT == -1) {
                 if (c == -1) {
                     throw new IOException("Unexpected end of XML stream");
-                } else {
-                    throw new IOException(
-                            "XML prolog or ROOT element not found on first "
-                                    + offset + " bytes");
                 }
+                throw new IOException(
+                        "XML prolog or ROOT element not found on first "
+                                + offset + " bytes");
             }
             final int bytesRead = offset;
             if (bytesRead > 0) {
diff --git 
a/src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReaderUtilitiesCompatibilityTest.java
 
b/src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReaderUtilitiesCompatibilityTest.java
index af2a8f7..b107107 100644
--- 
a/src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReaderUtilitiesCompatibilityTest.java
+++ 
b/src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReaderUtilitiesCompatibilityTest.java
@@ -29,21 +29,19 @@ public class XmlStreamReaderUtilitiesCompatibilityTest 
extends XmlStreamReaderUt
     @Override
     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, null);
-        mock.close();
-        return enc;
+        try (final MockXmlStreamReader mock = new 
MockXmlStreamReader(defaultEncoding)) {
+            return mock.calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc, 
null);
+        }
     }
     @Override
     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(
-                XmlStreamReader.getContentTypeMime(httpContentType),
-                XmlStreamReader.getContentTypeEncoding(httpContentType),
-                bomEnc, xmlGuessEnc, xmlEnc, null, lenient);
-        mock.close();
-        return enc;
+        try (final MockXmlStreamReader mock = new 
MockXmlStreamReader(defaultEncoding)) {
+                return mock.calculateHttpEncoding(
+                        XmlStreamReader.getContentTypeMime(httpContentType),
+                        
XmlStreamReader.getContentTypeEncoding(httpContentType),
+                        bomEnc, xmlGuessEnc, xmlEnc, null, lenient);
+        }
     }
 
     /** Mock {@link XmlStreamReader} implementation */
diff --git 
a/src/test/java/org/apache/commons/io/monitor/CollectionFileListener.java 
b/src/test/java/org/apache/commons/io/monitor/CollectionFileListener.java
index 773409c..21fdcba 100644
--- a/src/test/java/org/apache/commons/io/monitor/CollectionFileListener.java
+++ b/src/test/java/org/apache/commons/io/monitor/CollectionFileListener.java
@@ -190,6 +190,7 @@ public class CollectionFileListener implements 
FileAlterationListener, Serializa
      */
     @Override
     public void onStop(final FileAlterationObserver observer) {
+        // noop
     }
 
 }
diff --git a/src/test/java/org/apache/commons/io/output/ChunkedWriterTest.java 
b/src/test/java/org/apache/commons/io/output/ChunkedWriterTest.java
index 426b142..efbb343 100644
--- a/src/test/java/org/apache/commons/io/output/ChunkedWriterTest.java
+++ b/src/test/java/org/apache/commons/io/output/ChunkedWriterTest.java
@@ -28,25 +28,25 @@ public class ChunkedWriterTest {
     @Test
     public void write_four_chunks() throws Exception {
         final AtomicInteger numWrites = new AtomicInteger();
-        final OutputStreamWriter osw = getOutputStreamWriter(numWrites);
-
-        final ChunkedWriter chunked = new ChunkedWriter(osw, 10);
-        chunked.write("0123456789012345678901234567891".toCharArray());
-        chunked.flush();
-        assertEquals(4, numWrites.get());
-        chunked.close();
+        try (final OutputStreamWriter osw = getOutputStreamWriter(numWrites)) {
+            try (final ChunkedWriter chunked = new ChunkedWriter(osw, 10)) {
+                chunked.write("0123456789012345678901234567891".toCharArray());
+                chunked.flush();
+                assertEquals(4, numWrites.get());
+            }
+        }
     }
 
     @Test
     public void write_two_chunks_default_constructor() throws Exception {
         final AtomicInteger numWrites = new AtomicInteger();
-        final OutputStreamWriter osw = getOutputStreamWriter(numWrites);
-
-        final ChunkedWriter chunked = new ChunkedWriter(osw);
-        chunked.write(new char[1024 * 4 + 1]);
-        chunked.flush();
-        assertEquals(2, numWrites.get());
-        chunked.close();
+        try (final OutputStreamWriter osw = getOutputStreamWriter(numWrites)) {
+            try (final ChunkedWriter chunked = new ChunkedWriter(osw)) {
+                chunked.write(new char[1024 * 4 + 1]);
+                chunked.flush();
+                assertEquals(2, numWrites.get());
+            }
+        }
     }
 
     private OutputStreamWriter getOutputStreamWriter(final AtomicInteger 
numWrites) {
@@ -62,6 +62,6 @@ public class ChunkedWriterTest {
 
     @Test(expected = IllegalArgumentException.class)
     public void negative_chunksize_not_permitted() throws Exception {
-        (new ChunkedWriter(new OutputStreamWriter(new 
ByteArrayOutputStream()), 0)).close();;
+        (new ChunkedWriter(new OutputStreamWriter(new 
ByteArrayOutputStream()), 0)).close();
     }
 }
diff --git 
a/src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java 
b/src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java
index 89ab9d8..303b06f 100644
--- a/src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java
@@ -39,39 +39,38 @@ public class CountingOutputStreamTest {
     @Test
     public void testCounting() throws IOException {
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        final CountingOutputStream cos = new CountingOutputStream(baos);
-
-        for(int i = 0; i < 20; i++) {
-            cos.write(i);
-        }
-        assertByteArrayEquals("CountingOutputStream.write(int)", 
baos.toByteArray(), 0, 20);
-        assertEquals("CountingOutputStream.getCount()", cos.getCount(), 20);
-
-        final byte[] array = new byte[10];
-        for(int i = 20; i < 30; i++) {
-            array[i-20] = (byte)i;
-        }
-        cos.write(array);
-        assertByteArrayEquals("CountingOutputStream.write(byte[])", 
baos.toByteArray(), 0, 30);
-        assertEquals("CountingOutputStream.getCount()", cos.getCount(), 30);
-
-        for(int i = 25; i < 35; i++) {
-            array[i-25] = (byte)i;
+        try (final CountingOutputStream cos = new CountingOutputStream(baos)) {
+
+            for (int i = 0; i < 20; i++) {
+                cos.write(i);
+            }
+            assertByteArrayEquals("CountingOutputStream.write(int)", 
baos.toByteArray(), 0, 20);
+            assertEquals("CountingOutputStream.getCount()", cos.getCount(), 
20);
+
+            final byte[] array = new byte[10];
+            for (int i = 20; i < 30; i++) {
+                array[i - 20] = (byte) i;
+            }
+            cos.write(array);
+            assertByteArrayEquals("CountingOutputStream.write(byte[])", 
baos.toByteArray(), 0, 30);
+            assertEquals("CountingOutputStream.getCount()", cos.getCount(), 
30);
+
+            for (int i = 25; i < 35; i++) {
+                array[i - 25] = (byte) i;
+            }
+            cos.write(array, 5, 5);
+            assertByteArrayEquals("CountingOutputStream.write(byte[], int, 
int)", baos.toByteArray(), 0, 35);
+            assertEquals("CountingOutputStream.getCount()", cos.getCount(), 
35);
+
+            final int count = cos.resetCount();
+            assertEquals("CountingOutputStream.resetCount()", count, 35);
+
+            for (int i = 0; i < 10; i++) {
+                cos.write(i);
+            }
+            assertByteArrayEquals("CountingOutputStream.write(int)", 
baos.toByteArray(), 35, 45);
+            assertEquals("CountingOutputStream.getCount()", cos.getCount(), 
10);
         }
-        cos.write(array, 5, 5);
-        assertByteArrayEquals("CountingOutputStream.write(byte[], int, int)", 
baos.toByteArray(), 0, 35);
-        assertEquals("CountingOutputStream.getCount()", cos.getCount(), 35);
-
-        final int count = cos.resetCount();
-        assertEquals("CountingOutputStream.resetCount()", count, 35);
-
-        for(int i = 0; i < 10; i++) {
-            cos.write(i);
-        }
-        assertByteArrayEquals("CountingOutputStream.write(int)", 
baos.toByteArray(), 35, 45);
-        assertEquals("CountingOutputStream.getCount()", cos.getCount(), 10);
-
-        cos.close();
     }
 
     /*
diff --git 
a/src/test/java/org/apache/commons/io/output/NullOutputStreamTest.java 
b/src/test/java/org/apache/commons/io/output/NullOutputStreamTest.java
index c12b272..c5a6bc2 100644
--- a/src/test/java/org/apache/commons/io/output/NullOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/NullOutputStreamTest.java
@@ -32,15 +32,16 @@ public class NullOutputStreamTest {
 
     @Test
     public void testNull() throws IOException {
-        final NullOutputStream nos = new NullOutputStream();
-        nos.write("string".getBytes());
-        nos.write("some string".getBytes(), 3, 5);
-        nos.write(1);
-        nos.write(0x0f);
-        nos.flush();
-        nos.close();
-        nos.write("allowed".getBytes());
-        nos.write(255);
+        try (final NullOutputStream nos = new NullOutputStream()) {
+            nos.write("string".getBytes());
+            nos.write("some string".getBytes(), 3, 5);
+            nos.write(1);
+            nos.write(0x0f);
+            nos.flush();
+            nos.close();
+            nos.write("allowed".getBytes());
+            nos.write(255);
+        }
     }
 
 }
diff --git a/src/test/java/org/apache/commons/io/output/NullWriterTest.java 
b/src/test/java/org/apache/commons/io/output/NullWriterTest.java
index 8f4a2b4..4d7ce28 100644
--- a/src/test/java/org/apache/commons/io/output/NullWriterTest.java
+++ b/src/test/java/org/apache/commons/io/output/NullWriterTest.java
@@ -27,15 +27,15 @@ public class NullWriterTest {
 
     @Test
     public void testNull() {
-        final char[] chars = new char[] {'A', 'B', 'C'};
-        final NullWriter writer = new NullWriter();
-        writer.write(1);
-        writer.write(chars);
-        writer.write(chars, 1, 1);
-        writer.write("some string");
-        writer.write("some string", 2, 2);
-        writer.flush();
-        writer.close();
+        final char[] chars = new char[] { 'A', 'B', 'C' };
+        try (final NullWriter writer = new NullWriter()) {
+            writer.write(1);
+            writer.write(chars);
+            writer.write(chars, 1, 1);
+            writer.write("some string");
+            writer.write("some string", 2, 2);
+            writer.flush();
+        }
     }
 
 }
diff --git a/src/test/java/org/apache/commons/io/output/ProxyWriterTest.java 
b/src/test/java/org/apache/commons/io/output/ProxyWriterTest.java
index 88d2eef..fbd49e7 100644
--- a/src/test/java/org/apache/commons/io/output/ProxyWriterTest.java
+++ b/src/test/java/org/apache/commons/io/output/ProxyWriterTest.java
@@ -33,233 +33,240 @@ public class ProxyWriterTest {
 
     @Test
     public void appendCharSequence() throws Exception {
-        final StringBuilderWriter writer = new StringBuilderWriter();
-        final ProxyWriter proxy = new ProxyWriter(writer);
-        proxy.append("ABC");
-        assertEquals("ABC", writer.toString());
-        proxy.close();
+        try (final StringBuilderWriter writer = new StringBuilderWriter();
+                final ProxyWriter proxy = new ProxyWriter(writer)) {
+            proxy.append("ABC");
+            assertEquals("ABC", writer.toString());
+        }
     }
 
     @Test
     public void appendCharSequence_with_offset() throws Exception {
-        final StringBuilderWriter writer = new StringBuilderWriter();
-        final ProxyWriter proxy = new ProxyWriter(writer);
-        proxy.append("ABC", 1, 3);
-        proxy.flush();
-        assertEquals("BC", writer.toString());
-        proxy.close();
+        try (final StringBuilderWriter writer = new StringBuilderWriter();
+                final ProxyWriter proxy = new ProxyWriter(writer)) {
+            proxy.append("ABC", 1, 3);
+            proxy.flush();
+            assertEquals("BC", writer.toString());
+        }
     }
 
     @Test
     public void appendChar() throws Exception {
-        final StringBuilderWriter writer = new StringBuilderWriter();
-        final ProxyWriter proxy = new ProxyWriter(writer);
-        proxy.append('c');
-        assertEquals("c", writer.toString());
-        proxy.close();
+        try (final StringBuilderWriter writer = new StringBuilderWriter();
+                final ProxyWriter proxy = new ProxyWriter(writer)) {
+            proxy.append('c');
+            assertEquals("c", writer.toString());
+        }
     }
 
     @Test
     public void writeString() throws Exception {
-        final StringBuilderWriter writer = new StringBuilderWriter();
-        final ProxyWriter proxy = new ProxyWriter(writer);
-        proxy.write("ABC");
-        assertEquals("ABC", writer.toString());
-        proxy.close();
+        try (final StringBuilderWriter writer = new StringBuilderWriter();
+                final ProxyWriter proxy = new ProxyWriter(writer)) {
+            proxy.write("ABC");
+            assertEquals("ABC", writer.toString());
+        }
     }
 
     @Test
     public void writeStringPartial() throws Exception {
-        final StringBuilderWriter writer = new StringBuilderWriter();
-        final ProxyWriter proxy = new ProxyWriter(writer);
-        proxy.write("ABC", 1, 2);
-        assertEquals("BC", writer.toString());
-        proxy.close();
+        try (final StringBuilderWriter writer = new StringBuilderWriter();
+                final ProxyWriter proxy = new ProxyWriter(writer)) {
+            proxy.write("ABC", 1, 2);
+            assertEquals("BC", writer.toString());
+        }
     }
 
     @Test
     public void writeCharArray() throws Exception {
-        final StringBuilderWriter writer = new StringBuilderWriter();
-        final ProxyWriter proxy = new ProxyWriter(writer);
-        proxy.write(new char[]{'A', 'B', 'C'});
-        assertEquals("ABC", writer.toString());
-        proxy.close();
+        try (final StringBuilderWriter writer = new StringBuilderWriter();
+                final ProxyWriter proxy = new ProxyWriter(writer)) {
+            proxy.write(new char[] { 'A', 'B', 'C' });
+            assertEquals("ABC", writer.toString());
+        }
     }
 
     @Test
     public void writeInt() throws Exception {
-        final StringBuilderWriter writer = new StringBuilderWriter();
-        final ProxyWriter proxy = new ProxyWriter(writer);
-        proxy.write(65);
-        assertEquals("A", writer.toString());
-        proxy.close();
+        try (final StringBuilderWriter writer = new StringBuilderWriter();
+                final ProxyWriter proxy = new ProxyWriter(writer)) {
+            proxy.write(65);
+            assertEquals("A", writer.toString());
+        }
     }
 
     @Test
     public void writeCharArrayPartial() throws Exception {
-        final StringBuilderWriter writer = new StringBuilderWriter();
-        final ProxyWriter proxy = new ProxyWriter(writer);
-        proxy.write(new char[]{'A', 'B', 'C'}, 1, 2);
-        assertEquals("BC", writer.toString());
-        proxy.close();
+        try (final StringBuilderWriter writer = new StringBuilderWriter();
+                final ProxyWriter proxy = new ProxyWriter(writer)) {
+            proxy.write(new char[] { 'A', 'B', 'C' }, 1, 2);
+            assertEquals("BC", writer.toString());
+        }
     }
 
     @Test
     public void nullString() throws Exception {
-
-        final ProxyWriter proxy = new ProxyWriter(new NullWriter());
-
-        proxy.write((String) null);
-        proxy.write((String) null, 0, 0);
-        proxy.close();
+        try (final ProxyWriter proxy = new ProxyWriter(new NullWriter())) {
+            proxy.write((String) null);
+            proxy.write((String) null, 0, 0);
+            proxy.close();
+        }
     }
 
     @Test
     public void nullCharArray() throws Exception {
-
-        final ProxyWriter proxy = new ProxyWriter(new NullWriter());
-
-        proxy.write((char[]) null);
-
-        proxy.write((char[]) null, 0, 0);
-        proxy.close();
+        try (final ProxyWriter proxy = new ProxyWriter(new NullWriter())) {
+            proxy.write((char[]) null);
+            proxy.write((char[]) null, 0, 0);
+        }
     }
 
     @Test
     public void nullCharSequencec() throws Exception {
-
-        final ProxyWriter proxy = new ProxyWriter(new NullWriter());
-
-        proxy.append(null);
-        proxy.close();
+        try (final ProxyWriter proxy = new ProxyWriter(new NullWriter())) {
+            proxy.append(null);
+            proxy.close();
+        }
     }
 
     @Test(expected = UnsupportedEncodingException.class)
     public void exceptions_in_append_char() throws IOException {
-        final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
-            @Override
-            public void write(final int c) throws IOException {
-                throw new UnsupportedEncodingException("Bah");
+        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                final OutputStreamWriter osw = new OutputStreamWriter(baos) {
+                    @Override
+                    public void write(final int c) throws IOException {
+                        throw new UnsupportedEncodingException("Bah");
+                    }
+                }) {
+            try (ProxyWriter proxy = new ProxyWriter(osw)) {
+                proxy.append('c');
             }
-        };
-        try (ProxyWriter proxy = new ProxyWriter(osw)) {
-            proxy.append('c');
         }
     }
 
     @Test(expected = UnsupportedEncodingException.class)
     public void exceptions_in_append_charSequence() throws IOException {
-        final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
+        try (final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
             @Override
             public Writer append(final CharSequence csq) throws IOException {
                 throw new UnsupportedEncodingException("Bah");
             }
-        };
-        try (ProxyWriter proxy = new ProxyWriter(osw)) {
-            proxy.append("ABCE");
+        }) {
+            try (ProxyWriter proxy = new ProxyWriter(osw)) {
+                proxy.append("ABCE");
+            }
         }
     }
 
     @Test(expected = UnsupportedEncodingException.class)
     public void exceptions_in_append_charSequence_offset() throws IOException {
-        final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
+        try (final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
             @Override
             public Writer append(final CharSequence csq, final int start, 
final int end) throws IOException {
                 throw new UnsupportedEncodingException("Bah");
             }
-        };
-        try (ProxyWriter proxy = new ProxyWriter(osw)) {
-            proxy.append("ABCE", 1, 2);
+        }) {
+            try (ProxyWriter proxy = new ProxyWriter(osw)) {
+                proxy.append("ABCE", 1, 2);
+            }
         }
     }
 
     @Test(expected = UnsupportedEncodingException.class)
     public void exceptions_in_write_int() throws IOException {
-        final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
+        try (final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
             @Override
             public void write(final int c) throws IOException {
                 throw new UnsupportedEncodingException("Bah");
             }
-        };
-        try (ProxyWriter proxy = new ProxyWriter(osw)) {
-            proxy.write('a');
+        }) {
+            try (ProxyWriter proxy = new ProxyWriter(osw)) {
+                proxy.write('a');
+            }
         }
     }
 
     @Test(expected = UnsupportedEncodingException.class)
     public void exceptions_in_write_char_array() throws IOException {
-        final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
+        try (final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
             @Override
             public void write(final char[] cbuf) throws IOException {
                 throw new UnsupportedEncodingException("Bah");
             }
-        };
-        try (ProxyWriter proxy = new ProxyWriter(osw)) {
-            proxy.write("ABCE".toCharArray());
+        }) {
+            try (ProxyWriter proxy = new ProxyWriter(osw)) {
+                proxy.write("ABCE".toCharArray());
+            }
         }
     }
 
     @Test(expected = UnsupportedEncodingException.class)
     public void exceptions_in_write_offset_char_array() throws IOException {
-        final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
+        try (final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
             @Override
             public void write(final char[] cbuf, final int off, final int len) 
throws IOException {
                 throw new UnsupportedEncodingException("Bah");
             }
-        };
-        try (ProxyWriter proxy = new ProxyWriter(osw)) {
-            proxy.write("ABCE".toCharArray(), 2, 3);
+        }) {
+            try (ProxyWriter proxy = new ProxyWriter(osw)) {
+                proxy.write("ABCE".toCharArray(), 2, 3);
+            }
         }
     }
 
     @Test(expected = UnsupportedEncodingException.class)
     public void exceptions_in_write_string() throws IOException {
-        final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
+        try (final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
             @Override
             public void write(final String str) throws IOException {
                 throw new UnsupportedEncodingException("Bah");
             }
-        };
-        try (ProxyWriter proxy = new ProxyWriter(osw)) {
-            proxy.write("ABCE");
+        }) {
+            try (ProxyWriter proxy = new ProxyWriter(osw)) {
+                proxy.write("ABCE");
+            }
         }
     }
 
     @Test(expected = UnsupportedEncodingException.class)
     public void exceptions_in_write_string_offset() throws IOException {
-        final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
+        try (final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
             @Override
             public void write(final String str, final int off, final int len) 
throws IOException {
                 throw new UnsupportedEncodingException("Bah");
             }
-        };
-        try (ProxyWriter proxy = new ProxyWriter(osw)) {
-            proxy.write("ABCE", 1, 3);
+        }) {
+            try (ProxyWriter proxy = new ProxyWriter(osw)) {
+                proxy.write("ABCE", 1, 3);
+            }
         }
     }
 
     @Test(expected = UnsupportedEncodingException.class)
     public void exceptions_in_flush() throws IOException {
-        final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
+        try (final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
             @Override
             public void flush() throws IOException {
                 throw new UnsupportedEncodingException("Bah");
             }
-        };
-        try (ProxyWriter proxy = new ProxyWriter(osw)) {
-            proxy.flush();
+        }) {
+            try (ProxyWriter proxy = new ProxyWriter(osw)) {
+                proxy.flush();
+            }
         }
     }
 
     @Test(expected = UnsupportedEncodingException.class)
     public void exceptions_in_close() throws IOException {
-        final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
+        try (final OutputStreamWriter osw = new OutputStreamWriter(new 
ByteArrayOutputStream()) {
             @Override
             public void close() throws IOException {
                 throw new UnsupportedEncodingException("Bah");
             }
-        };
-        final ProxyWriter proxy = new ProxyWriter(osw);
-        proxy.close();
+        }) {
+            try (final ProxyWriter proxy = new ProxyWriter(osw)) {
+                // noop
+            }
+        }
     }
 }
diff --git 
a/src/test/java/org/apache/commons/io/output/StringBuilderWriterTest.java 
b/src/test/java/org/apache/commons/io/output/StringBuilderWriterTest.java
index 03d61f6..0d7445f 100644
--- a/src/test/java/org/apache/commons/io/output/StringBuilderWriterTest.java
+++ b/src/test/java/org/apache/commons/io/output/StringBuilderWriterTest.java
@@ -35,117 +35,118 @@ public class StringBuilderWriterTest {
 
     @Test
     public void testAppendConstructCapacity() throws IOException {
-        final Writer writer = new StringBuilderWriter(100);
-        writer.append("Foo");
-        assertEquals("Foo", writer.toString());
-        writer.close();
+        try (final Writer writer = new StringBuilderWriter(100)) {
+            writer.append("Foo");
+            assertEquals("Foo", writer.toString());
+        }
     }
 
     @Test
     public void testAppendConstructStringBuilder() {
         final StringBuilder builder = new StringBuilder("Foo");
-        final StringBuilderWriter writer = new StringBuilderWriter(builder);
-        writer.append("Bar");
-        assertEquals("FooBar", writer.toString());
-        assertSame(builder, writer.getBuilder());
-        writer.close();
+        try (final StringBuilderWriter writer = new 
StringBuilderWriter(builder)) {
+            writer.append("Bar");
+            assertEquals("FooBar", writer.toString());
+            assertSame(builder, writer.getBuilder());
+        }
     }
 
     @Test
     public void testAppendConstructNull() throws IOException {
-        final Writer writer = new StringBuilderWriter(null);
-        writer.append("Foo");
-        assertEquals("Foo", writer.toString());
-        writer.close();
+        try (final Writer writer = new StringBuilderWriter(null)) {
+            writer.append("Foo");
+            assertEquals("Foo", writer.toString());
+        }
     }
 
     @Test
     public void testAppendChar() throws IOException {
-        final Writer writer = new StringBuilderWriter();
-        writer.append('F').append('o').append('o');
-        assertEquals("Foo", writer.toString());
-        writer.close();
+        try (final Writer writer = new StringBuilderWriter()) {
+            writer.append('F').append('o').append('o');
+            assertEquals("Foo", writer.toString());
+        }
     }
 
     @Test
     public void testAppendCharSequence() throws IOException {
-        final Writer writer = new StringBuilderWriter();
-        writer.append("Foo").append("Bar");
-        assertEquals("FooBar", writer.toString());
-        writer.close();
+        try (final Writer writer = new StringBuilderWriter()) {
+            writer.append("Foo").append("Bar");
+            assertEquals("FooBar", writer.toString());
+        }
     }
 
     @Test
     public void testAppendCharSequencePortion() throws IOException {
-        final Writer writer = new StringBuilderWriter();
-        writer.append("FooBar", 3, 6).append(new StringBuffer("FooBar"), 0, 3);
-        assertEquals("BarFoo", writer.toString());
-        writer.close();
+        try (final Writer writer = new StringBuilderWriter()) {
+            writer.append("FooBar", 3, 6).append(new StringBuffer("FooBar"), 
0, 3);
+            assertEquals("BarFoo", writer.toString());
+        }
     }
 
     @Test
-    public void testClose() {
-        final Writer writer = new StringBuilderWriter();
-        try {
-            writer.append("Foo");
-            writer.close();
-            writer.append("Bar");
-        } catch (final Throwable t) {
-            fail("Threw: " + t);
+    public void testClose() throws IOException {
+        try (final Writer writer = new StringBuilderWriter()) {
+            try {
+                writer.append("Foo");
+                writer.close();
+                writer.append("Bar");
+            } catch (final Throwable t) {
+                fail("Threw: " + t);
+            }
+            assertEquals("FooBar", writer.toString());
         }
-        assertEquals("FooBar", writer.toString());
     }
 
     @Test
     public void testWriteChar() throws IOException {
-        final Writer writer = new StringBuilderWriter();
-        writer.write('F');
-        assertEquals("F", writer.toString());
-        writer.write('o');
-        assertEquals("Fo", writer.toString());
-        writer.write('o');
-        assertEquals("Foo", writer.toString());
-        writer.close();
+        try (final Writer writer = new StringBuilderWriter()) {
+            writer.write('F');
+            assertEquals("F", writer.toString());
+            writer.write('o');
+            assertEquals("Fo", writer.toString());
+            writer.write('o');
+            assertEquals("Foo", writer.toString());
+        }
     }
 
     @Test
     public void testWriteCharArray() throws IOException {
-        final Writer writer = new StringBuilderWriter();
-        writer.write(new char[] {'F', 'o', 'o'});
-        assertEquals("Foo", writer.toString());
-        writer.write(new char[] {'B', 'a', 'r'});
-        assertEquals("FooBar", writer.toString());
-        writer.close();
+        try (final Writer writer = new StringBuilderWriter()) {
+            writer.write(new char[] { 'F', 'o', 'o' });
+            assertEquals("Foo", writer.toString());
+            writer.write(new char[] { 'B', 'a', 'r' });
+            assertEquals("FooBar", writer.toString());
+        }
     }
 
     @Test
     public void testWriteCharArrayPortion() throws IOException {
-        final Writer writer = new StringBuilderWriter();
+        try (final Writer writer = new StringBuilderWriter()) {
         writer.write(FOOBAR_CHARS, 3, 3);
         assertEquals("Bar", writer.toString());
         writer.write(FOOBAR_CHARS, 0, 3);
         assertEquals("BarFoo", writer.toString());
-        writer.close();
+        }
     }
 
     @Test
     public void testWriteString() throws IOException {
-        final Writer writer = new StringBuilderWriter();
-        writer.write("Foo");
-        assertEquals("Foo", writer.toString());
-        writer.write("Bar");
-        assertEquals("FooBar", writer.toString());
-        writer.close();
+        try (final Writer writer = new StringBuilderWriter()) {
+            writer.write("Foo");
+            assertEquals("Foo", writer.toString());
+            writer.write("Bar");
+            assertEquals("FooBar", writer.toString());
+        }
     }
 
     @Test
     public void testWriteStringPortion() throws IOException {
-        final Writer writer = new StringBuilderWriter();
-        writer.write("FooBar", 3, 3);
-        assertEquals("Bar", writer.toString());
-        writer.write("FooBar", 0, 3);
-        assertEquals("BarFoo", writer.toString());
-        writer.close();
+        try (final Writer writer = new StringBuilderWriter()) {
+            writer.write("FooBar", 3, 3);
+            assertEquals("Bar", writer.toString());
+            writer.write("FooBar", 0, 3);
+            assertEquals("BarFoo", writer.toString());
+        }
     }
 
 }
diff --git 
a/src/test/java/org/apache/commons/io/output/TaggedOutputStreamTest.java 
b/src/test/java/org/apache/commons/io/output/TaggedOutputStreamTest.java
index 9357bb6..02a9c4d 100644
--- a/src/test/java/org/apache/commons/io/output/TaggedOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/TaggedOutputStreamTest.java
@@ -35,14 +35,13 @@ public class TaggedOutputStreamTest  {
 
     @Test
     public void testNormalStream() {
-        try {
-            final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-            final OutputStream stream = new TaggedOutputStream(buffer);
-            stream.write('a');
-            stream.write(new byte[] { 'b' });
-            stream.write(new byte[] { 'c' }, 0, 1);
-            stream.flush();
-            stream.close();
+        try (final ByteArrayOutputStream buffer = new ByteArrayOutputStream()) 
{
+            try (final OutputStream stream = new TaggedOutputStream(buffer)) {
+                stream.write('a');
+                stream.write(new byte[] { 'b' });
+                stream.write(new byte[] { 'c' }, 0, 1);
+                stream.flush();
+            }
             assertEquals(3, buffer.size());
             assertEquals('a', buffer.toByteArray()[0]);
             assertEquals('b', buffer.toByteArray()[1]);
@@ -104,26 +103,24 @@ public class TaggedOutputStreamTest  {
     @Test
     public void testOtherException() throws Exception {
         final IOException exception = new IOException("test exception");
-        final OutputStream closed = new ClosedOutputStream();
-        final TaggedOutputStream stream = new TaggedOutputStream(closed);
+        try (final OutputStream closed = new ClosedOutputStream();
+                final TaggedOutputStream stream = new 
TaggedOutputStream(closed)) {
 
-        assertFalse(stream.isCauseOf(exception));
-        assertFalse(stream.isCauseOf(
-                new TaggedIOException(exception, UUID.randomUUID())));
+            assertFalse(stream.isCauseOf(exception));
+            assertFalse(stream.isCauseOf(new TaggedIOException(exception, 
UUID.randomUUID())));
 
-        try {
-            stream.throwIfCauseOf(exception);
-        } catch (final IOException e) {
-            fail("Unexpected exception thrown");
-        }
+            try {
+                stream.throwIfCauseOf(exception);
+            } catch (final IOException e) {
+                fail("Unexpected exception thrown");
+            }
 
-        try {
-            stream.throwIfCauseOf(
-                    new TaggedIOException(exception, UUID.randomUUID()));
-        } catch (final IOException e) {
-            fail("Unexpected exception thrown");
+            try {
+                stream.throwIfCauseOf(new TaggedIOException(exception, 
UUID.randomUUID()));
+            } catch (final IOException e) {
+                fail("Unexpected exception thrown");
+            }
         }
-        stream.close();
     }
 
 }
diff --git 
a/src/test/java/org/apache/commons/io/output/TeeOutputStreamTest.java 
b/src/test/java/org/apache/commons/io/output/TeeOutputStreamTest.java
index 8b548e9..92c0011 100644
--- a/src/test/java/org/apache/commons/io/output/TeeOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/TeeOutputStreamTest.java
@@ -88,36 +88,38 @@ public class TeeOutputStreamTest {
         final ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
         final ByteArrayOutputStream expected = new ByteArrayOutputStream();
 
-        final TeeOutputStream tos = new TeeOutputStream(baos1, baos2);
-        for (int i = 0; i < 20; i++) {
-            tos.write(i);
-            expected.write(i);
+        try (final TeeOutputStream tos = new TeeOutputStream(baos1, baos2)) {
+            for (int i = 0; i < 20; i++) {
+                tos.write(i);
+                expected.write(i);
+            }
+            assertByteArrayEquals("TeeOutputStream.write(int)", 
expected.toByteArray(), baos1.toByteArray());
+            assertByteArrayEquals("TeeOutputStream.write(int)", 
expected.toByteArray(), baos2.toByteArray());
+
+            final byte[] array = new byte[10];
+            for (int i = 20; i < 30; i++) {
+                array[i - 20] = (byte) i;
+            }
+            tos.write(array);
+            expected.write(array);
+            assertByteArrayEquals("TeeOutputStream.write(byte[])", 
expected.toByteArray(), baos1.toByteArray());
+            assertByteArrayEquals("TeeOutputStream.write(byte[])", 
expected.toByteArray(), baos2.toByteArray());
+
+            for (int i = 25; i < 35; i++) {
+                array[i - 25] = (byte) i;
+            }
+            tos.write(array, 5, 5);
+            expected.write(array, 5, 5);
+            assertByteArrayEquals("TeeOutputStream.write(byte[], int, int)", 
expected.toByteArray(),
+                    baos1.toByteArray());
+            assertByteArrayEquals("TeeOutputStream.write(byte[], int, int)", 
expected.toByteArray(),
+                    baos2.toByteArray());
+
+            expected.flush();
+            expected.close();
+
+            tos.flush();
         }
-        assertByteArrayEquals("TeeOutputStream.write(int)", 
expected.toByteArray(), baos1.toByteArray());
-        assertByteArrayEquals("TeeOutputStream.write(int)", 
expected.toByteArray(), baos2.toByteArray());
-
-        final byte[] array = new byte[10];
-        for (int i = 20; i < 30; i++) {
-            array[i - 20] = (byte) i;
-        }
-        tos.write(array);
-        expected.write(array);
-        assertByteArrayEquals("TeeOutputStream.write(byte[])", 
expected.toByteArray(), baos1.toByteArray());
-        assertByteArrayEquals("TeeOutputStream.write(byte[])", 
expected.toByteArray(), baos2.toByteArray());
-
-        for (int i = 25; i < 35; i++) {
-            array[i - 25] = (byte) i;
-        }
-        tos.write(array, 5, 5);
-        expected.write(array, 5, 5);
-        assertByteArrayEquals("TeeOutputStream.write(byte[], int, int)", 
expected.toByteArray(), baos1.toByteArray());
-        assertByteArrayEquals("TeeOutputStream.write(byte[], int, int)", 
expected.toByteArray(), baos2.toByteArray());
-
-        expected.flush();
-        expected.close();
-
-        tos.flush();
-        tos.close();
     }
 
     private void assertByteArrayEquals(final String msg, final byte[] array1, 
final byte[] array2) {
diff --git 
a/src/test/java/org/apache/commons/io/output/ThresholdingOutputStreamTest.java 
b/src/test/java/org/apache/commons/io/output/ThresholdingOutputStreamTest.java
index a71996c..8129584 100644
--- 
a/src/test/java/org/apache/commons/io/output/ThresholdingOutputStreamTest.java
+++ 
b/src/test/java/org/apache/commons/io/output/ThresholdingOutputStreamTest.java
@@ -29,10 +29,11 @@ public class ThresholdingOutputStreamTest {
     @Test
     public void testSetByteCount() throws Exception {
         final AtomicBoolean reached = new AtomicBoolean(false);
-        final ThresholdingOutputStream tos = new ThresholdingOutputStream(3) {
+        try (final ThresholdingOutputStream tos = new 
ThresholdingOutputStream(3) {
             {
                 setByteCount(2);
             }
+
             @Override
             protected OutputStream getStream() throws IOException {
                 return new ByteArrayOutputStream(4);
@@ -40,14 +41,13 @@ public class ThresholdingOutputStreamTest {
 
             @Override
             protected void thresholdReached() throws IOException {
-                reached.set( true);
+                reached.set(true);
             }
-        };
-
-        tos.write(12);
-        assertFalse( reached.get());
-        tos.write(12);
-        assertTrue(reached.get());
-        tos.close();
+        }) {
+            tos.write(12);
+            assertFalse(reached.get());
+            tos.write(12);
+            assertTrue(reached.get());
+        }
     }
 }
\ No newline at end of file
diff --git 
a/src/test/java/org/apache/commons/io/output/XmlStreamWriterTest.java 
b/src/test/java/org/apache/commons/io/output/XmlStreamWriterTest.java
index 51f5867..9836100 100644
--- a/src/test/java/org/apache/commons/io/output/XmlStreamWriterTest.java
+++ b/src/test/java/org/apache/commons/io/output/XmlStreamWriterTest.java
@@ -89,13 +89,13 @@ public class XmlStreamWriterTest {
     @Test
     public void testEmpty() throws IOException {
         final ByteArrayOutputStream out = new ByteArrayOutputStream();
-        final XmlStreamWriter writer = new XmlStreamWriter(out);
-        writer.flush();
-        writer.write("");
-        writer.flush();
-        writer.write(".");
-        writer.flush();
-        writer.close();
+        try (final XmlStreamWriter writer = new XmlStreamWriter(out)) {
+            writer.flush();
+            writer.write("");
+            writer.flush();
+            writer.write(".");
+            writer.flush();
+        }
     }
 
     @Test
diff --git a/src/test/java/org/apache/commons/io/serialization/ClosingBase.java 
b/src/test/java/org/apache/commons/io/serialization/ClosingBase.java
index 960b8fc..c8c47bf 100644
--- a/src/test/java/org/apache/commons/io/serialization/ClosingBase.java
+++ b/src/test/java/org/apache/commons/io/serialization/ClosingBase.java
@@ -48,6 +48,7 @@ public class ClosingBase {
             try {
                 c.close();
             } catch (final IOException ignored) {
+                // ignore
             }
         }
     }

Reply via email to