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/testcases/org/apache | |
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/testcases/org/apache')
3 files changed, 52 insertions, 28 deletions
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index 6eb4700161..07fd7910f8 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -1222,7 +1222,10 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { try { wb.write(); fail("Shouldn't work for new files"); - } catch (IllegalStateException e) {} + } catch (IllegalStateException e) { + // expected here + } + wb.close(); // Can't work for InputStream opened files wb = new HSSFWorkbook( @@ -1230,7 +1233,10 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { try { wb.write(); fail("Shouldn't work for InputStream"); - } catch (IllegalStateException e) {} + } catch (IllegalStateException e) { + // expected here + } + wb.close(); // Can't work for OPOIFS OPOIFSFileSystem ofs = new OPOIFSFileSystem( @@ -1239,7 +1245,10 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { try { wb.write(); fail("Shouldn't work for OPOIFSFileSystem"); - } catch (IllegalStateException e) {} + } catch (IllegalStateException e) { + // expected here + } + wb.close(); // Can't work for Read-Only files NPOIFSFileSystem fs = new NPOIFSFileSystem( @@ -1248,17 +1257,27 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { try { wb.write(); fail("Shouldn't work for Read Only"); - } catch (IllegalStateException e) {} + } catch (IllegalStateException e) { + // expected here + } + wb.close(); } @Test public void inPlaceWrite() throws Exception { // Setup as a copy of a known-good file final File file = TempFile.createTempFile("TestHSSFWorkbook", ".xls"); - IOUtils.copy( - POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls"), - new FileOutputStream(file) - ); + InputStream inputStream = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls"); + 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 HSSFWorkbook wb = new HSSFWorkbook(new NPOIFSFileSystem(file, false)); @@ -1276,6 +1295,8 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { wb = new HSSFWorkbook(new NPOIFSFileSystem(file)); assertEquals(1, wb.getNumberOfSheets()); assertEquals("Changed!", wb.getSheetAt(0).getRow(0).getCell(0).toString()); + + wb.close(); } @Test diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java index 1833b184f3..f4a18a03d2 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java @@ -24,11 +24,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; +import java.io.*; import java.nio.ByteBuffer; import java.util.Iterator; @@ -96,20 +92,25 @@ public final class TestNPOIFSFileSystem { HeaderBlock header = new HeaderBlock(new ByteArrayInputStream(baos.toByteArray())); return header; } - - protected static NPOIFSFileSystem writeOutAndReadBack(NPOIFSFileSystem original) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - original.writeFilesystem(baos); - original.close(); - return new NPOIFSFileSystem(new ByteArrayInputStream(baos.toByteArray())); - } - protected static NPOIFSFileSystem writeOutFileAndReadBack(NPOIFSFileSystem original) throws IOException { - final File file = TempFile.createTempFile("TestPOIFS", ".ole2"); - final FileOutputStream fout = new FileOutputStream(file); - original.writeFilesystem(fout); - original.close(); - return new NPOIFSFileSystem(file, false); - } + + protected static NPOIFSFileSystem writeOutAndReadBack(NPOIFSFileSystem original) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + original.writeFilesystem(baos); + original.close(); + return new NPOIFSFileSystem(new ByteArrayInputStream(baos.toByteArray())); + } + + protected static NPOIFSFileSystem writeOutFileAndReadBack(NPOIFSFileSystem original) throws IOException { + final File file = TempFile.createTempFile("TestPOIFS", ".ole2"); + final OutputStream fout = new FileOutputStream(file); + try { + original.writeFilesystem(fout); + } finally { + fout.close(); + } + original.close(); + return new NPOIFSFileSystem(file, false); + } @Test public void basicOpen() throws Exception { diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index a19cf6684b..be097d3482 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -1017,7 +1017,7 @@ public abstract class BaseTestCell { } @Test - public void primitiveToEnumReplacementDoesNotBreakBackwardsCompatibility() { + public void primitiveToEnumReplacementDoesNotBreakBackwardsCompatibility() throws IOException { // bug 59836 // until we have changes POI from working on primitives (int) to enums, // we should make sure we minimize backwards compatibility breakages. @@ -1046,5 +1046,7 @@ public abstract class BaseTestCell { default: fail("unexpected cell type: " + cell.getCellType()); } + + wb.close(); } } |