From afe06db4e6d0c0e865aa13ba503aba45d0b425ff Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Thu, 5 May 2016 12:22:37 +0000 Subject: [PATCH] Close file-handles in tests as well to avoid noise when running file-leak-detection git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1742424 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/poifs/macros/TestVBAMacroReader.java | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/testcases/org/apache/poi/poifs/macros/TestVBAMacroReader.java b/src/testcases/org/apache/poi/poifs/macros/TestVBAMacroReader.java index 181598a21b..2f2feee072 100644 --- a/src/testcases/org/apache/poi/poifs/macros/TestVBAMacroReader.java +++ b/src/testcases/org/apache/poi/poifs/macros/TestVBAMacroReader.java @@ -17,46 +17,51 @@ package org.apache.poi.poifs.macros; -import static org.apache.poi.POITestCase.assertContains; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import org.apache.poi.POIDataSamples; +import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; +import org.apache.poi.util.IOUtils; +import org.apache.poi.util.StringUtil; +import org.junit.Ignore; +import org.junit.Test; import java.io.File; import java.io.FileInputStream; -import java.io.InputStream; import java.io.IOException; +import java.io.InputStream; import java.util.Collections; import java.util.HashMap; import java.util.Map; -import org.apache.poi.POIDataSamples; -import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; -import org.apache.poi.util.IOUtils; -import org.apache.poi.util.StringUtil; -import org.junit.Ignore; -import org.junit.Test; +import static org.apache.poi.POITestCase.assertContains; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; public class TestVBAMacroReader { - private static final Map expectedMacroContents; + protected static String readVBA(POIDataSamples poiDataSamples) { File macro = poiDataSamples.getFile("SimpleMacro.vba"); - byte[] bytes; + final byte[] bytes; try { - bytes = IOUtils.toByteArray(new FileInputStream(macro)); + FileInputStream stream = new FileInputStream(macro); + try { + bytes = IOUtils.toByteArray(stream); + } finally { + stream.close(); + } } catch (IOException e) { throw new RuntimeException(e); } + String testMacroContents = new String(bytes, StringUtil.UTF8); if (! testMacroContents.startsWith("Sub ")) { throw new IllegalArgumentException("Not a macro"); } - String testMacroNoSub = testMacroContents.substring(testMacroContents.indexOf("()")+3); - return testMacroNoSub; + + return testMacroContents.substring(testMacroContents.indexOf("()")+3); } + static { final Map _expectedMacroContents = new HashMap(); final POIDataSamples[] dataSamples = { -- 2.39.5