]> source.dussan.org Git - poi.git/commitdiff
Close file-handles in tests as well to avoid noise when running file-leak-detection
authorDominik Stadler <centic@apache.org>
Thu, 5 May 2016 12:22:37 +0000 (12:22 +0000)
committerDominik Stadler <centic@apache.org>
Thu, 5 May 2016 12:22:37 +0000 (12:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1742424 13f79535-47bb-0310-9956-ffa450edef68

src/testcases/org/apache/poi/poifs/macros/TestVBAMacroReader.java

index 181598a21bef36569b1d74eff447a1a88e092456..2f2feee07298e839f94aac36672ffc2c8d1b1a33 100644 (file)
 
 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<POIDataSamples, String> 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<POIDataSamples, String> _expectedMacroContents = new HashMap<POIDataSamples, String>();
         final POIDataSamples[] dataSamples = {