diff options
author | Dominik Stadler <centic@apache.org> | 2016-10-05 20:00:07 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2016-10-05 20:00:07 +0000 |
commit | e65020bb72ffb7f8ef0003758137b9513ad91ef8 (patch) | |
tree | f4d3e5cd2d961a08a30e7ebce5d5735f45215228 /src/scratchpad | |
parent | b66689d2b026280202c04d3a52cb33199abef2ab (diff) | |
download | poi-e65020bb72ffb7f8ef0003758137b9513ad91ef8.tar.gz poi-e65020bb72ffb7f8ef0003758137b9513ad91ef8.zip |
Use IOUtils.closeQuietly() in more places
Avoid two possible file-handle leaks when opening files fails with an exception
Make tests close resources properly to not spam the output when running with file-leak-detector
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1763485 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/scratchpad')
-rw-r--r-- | src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java index a45b031da2..f829c10cc6 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java @@ -17,11 +17,7 @@ package org.apache.poi.hwpf.usermodel; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.*; import org.apache.poi.POIDataSamples; import org.apache.poi.hwpf.HWPFDocument; @@ -90,10 +86,17 @@ public final class TestHWPFWrite extends HWPFTestCase { public void testInPlaceWrite() throws Exception { // Setup as a copy of a known-good file final File file = TempFile.createTempFile("TestDocument", ".doc"); - IOUtils.copy( - POIDataSamples.getDocumentInstance().openResourceAsStream("SampleDoc.doc"), - new FileOutputStream(file) - ); + InputStream inputStream = POIDataSamples.getDocumentInstance().openResourceAsStream("SampleDoc.doc"); + try { + FileOutputStream outputStream = new FileOutputStream(file); + try { + IOUtils.copy(inputStream, outputStream); + } finally { + outputStream.close(); + } + } finally { + inputStream.close(); + } // Open from the temp file in read-write mode HWPFDocument doc = new HWPFDocument(new NPOIFSFileSystem(file, false).getRoot()); @@ -108,7 +111,9 @@ public final class TestHWPFWrite extends HWPFTestCase { doc.close(); doc = new HWPFDocument(new NPOIFSFileSystem(file).getRoot()); + r = doc.getRange(); assertEquals("X XX a test document\r", r.getParagraph(0).text()); + doc.close(); } @SuppressWarnings("resource") @@ -121,7 +126,10 @@ public final class TestHWPFWrite extends HWPFTestCase { try { doc.write(); fail("Shouldn't work for InputStream"); - } catch (IllegalStateException e) {} + } catch (IllegalStateException e) { + // expected here + } + doc.close(); // Can't work for OPOIFS OPOIFSFileSystem ofs = new OPOIFSFileSystem( @@ -130,7 +138,10 @@ public final class TestHWPFWrite extends HWPFTestCase { try { doc.write(); fail("Shouldn't work for OPOIFSFileSystem"); - } catch (IllegalStateException e) {} + } catch (IllegalStateException e) { + // expected here + } + doc.close(); // Can't work for Read-Only files NPOIFSFileSystem fs = new NPOIFSFileSystem( @@ -139,6 +150,9 @@ public final class TestHWPFWrite extends HWPFTestCase { try { doc.write(); fail("Shouldn't work for Read Only"); - } catch (IllegalStateException e) {} + } catch (IllegalStateException e) { + // expected here + } + doc.close(); } } |