]> source.dussan.org Git - poi.git/commitdiff
Fix freeing resources in some tests and dev-tools so we can run unit-tests with enabl...
authorDominik Stadler <centic@apache.org>
Tue, 22 Dec 2015 22:35:59 +0000 (22:35 +0000)
committerDominik Stadler <centic@apache.org>
Tue, 22 Dec 2015 22:35:59 +0000 (22:35 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1721468 13f79535-47bb-0310-9956-ffa450edef68

13 files changed:
src/java/org/apache/poi/hssf/dev/EFBiffViewer.java
src/java/org/apache/poi/hssf/dev/RecordLister.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java
src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java
src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFFormulaEvaluation.java
src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java
src/scratchpad/testcases/org/apache/poi/hslf/record/TestCurrentUserAtom.java
src/testcases/org/apache/poi/ddf/TestEscherDump.java
src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java
src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java
src/testcases/org/apache/poi/sl/usermodel/BaseTestSlideShowFactory.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java

index c87f5f73b910c592c028a08c09ff6055280fd77e..be8fe537a2d222bd24f27ced1390ffea1de90ac5 100644 (file)
@@ -44,21 +44,27 @@ public class EFBiffViewer
 
     public void run() throws IOException {
         NPOIFSFileSystem fs   = new NPOIFSFileSystem(new File(file), true);
-        InputStream     din   = BiffViewer.getPOIFSInputStream(fs);
-        HSSFRequest     req   = new HSSFRequest();
-
-        req.addListenerForAllRecords(new HSSFListener()
-        {
-            public void processRecord(Record rec)
-            {
-                System.out.println(rec.toString());
+        try {
+            InputStream     din   = BiffViewer.getPOIFSInputStream(fs);
+            try {
+                HSSFRequest     req   = new HSSFRequest();
+        
+                req.addListenerForAllRecords(new HSSFListener()
+                {
+                    public void processRecord(Record rec)
+                    {
+                        System.out.println(rec.toString());
+                    }
+                });
+                HSSFEventFactory factory = new HSSFEventFactory();
+        
+                factory.processEvents(req, din);
+            } finally {
+                din.close();
             }
-        });
-        HSSFEventFactory factory = new HSSFEventFactory();
-
-        factory.processEvents(req, din);
-        din.close();
-        fs.close();
+        } finally {
+            fs.close();
+        }
     }
 
     public void setFile(String file)
index 7c626b8a12d86b59a0c4e1dc9a323fc8708fe6f5..2f61ea3765394d2c66b3b5eb997d8e6109bac785 100644 (file)
@@ -51,37 +51,42 @@ public class RecordLister
         throws IOException
     {
         NPOIFSFileSystem  fs    = new NPOIFSFileSystem(new File(file), true);
-        InputStream       din   = BiffViewer.getPOIFSInputStream(fs);
-        RecordInputStream rinp  = new RecordInputStream(din);
-
-        while(rinp.hasNextRecord()) {
-           int sid  = rinp.getNextSid();
-           rinp.nextRecord();
-           
-           int size = rinp.available();
-           Class<? extends Record> clz = RecordFactory.getRecordClass(sid);
-           
-           System.out.print(
-                 formatSID(sid) +
-                 " - " +
-                 formatSize(size) +
-                 " bytes"
-           );
-           if(clz != null) {
-              System.out.print("  \t");
-              System.out.print(clz.getName().replace("org.apache.poi.hssf.record.", ""));
-           }
-           System.out.println();
-           
-           byte[] data = rinp.readRemainder();
-           if(data.length > 0) {
-              System.out.print("   ");
-              System.out.println( formatData(data) );
-           }
-        }
+        try {
+            InputStream       din   = BiffViewer.getPOIFSInputStream(fs);
+            try {
+                RecordInputStream rinp  = new RecordInputStream(din);
         
-        din.close();
-        fs.close();
+                while(rinp.hasNextRecord()) {
+                   int sid  = rinp.getNextSid();
+                   rinp.nextRecord();
+                   
+                   int size = rinp.available();
+                   Class<? extends Record> clz = RecordFactory.getRecordClass(sid);
+                   
+                   System.out.print(
+                         formatSID(sid) +
+                         " - " +
+                         formatSize(size) +
+                         " bytes"
+                   );
+                   if(clz != null) {
+                      System.out.print("  \t");
+                      System.out.print(clz.getName().replace("org.apache.poi.hssf.record.", ""));
+                   }
+                   System.out.println();
+                   
+                   byte[] data = rinp.readRemainder();
+                   if(data.length > 0) {
+                      System.out.print("   ");
+                      System.out.println( formatData(data) );
+                   }
+                }
+            } finally {
+                din.close();
+            }
+        } finally {
+            fs.close();
+        }
     }
     
     private static String formatSID(int sid) {
index d412d97d340cee1d98b9b8998c9fe27252c071d3..25842cc556c8704f944448f4c27ee8e695da6706 100644 (file)
@@ -716,6 +716,7 @@ public final class TestPackage {
         Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(buf));
         wb.getSheetAt(0);
         wb.close();
+        zipFile.close();
     }
     
     @Test
index b403e90d7875ae7c10aaa6bb2f49ddbb841992c9..5ee7b2397248daaa9d44377d4667310dce5a412e 100644 (file)
@@ -26,9 +26,6 @@ import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 
-import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
-
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@@ -41,6 +38,9 @@ import org.apache.poi.openxml4j.opc.TargetMode;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.TempFile;
 
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+
 /**
  * Test core properties Open Packaging Convention compliance.
  * 
@@ -249,6 +249,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         pkg.save(baos);
         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+        pkg.revert();
 
         pkg = OPCPackage.open(bais);
 
@@ -257,7 +258,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
         assertNotNull(pkg.getPackageProperties());
         assertNotNull(pkg.getPackageProperties().getLanguageProperty());
         assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
-        
+        pkg.close();
         
         // Open a new copy of it
         pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath());
@@ -265,6 +266,8 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
         // Save and re-load, without having touched the properties yet
         baos = new ByteArrayOutputStream();
         pkg.save(baos);
+        pkg.revert();
+
         bais = new ByteArrayInputStream(baos.toByteArray());
         pkg = OPCPackage.open(bais);
         
@@ -285,10 +288,12 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
         // Copy this into a temp file, so we can play with it
         File tmp = TempFile.createTempFile("poi-test", ".opc");
         FileOutputStream out = new FileOutputStream(tmp);
+        InputStream in = POIDataSamples.getOpenXML4JInstance().openResourceAsStream(sampleFileName);
         IOUtils.copy(
-                POIDataSamples.getOpenXML4JInstance().openResourceAsStream(sampleFileName),
+                in,
                 out);
         out.close();
+        in.close();
 
         // Open it from that temp file
         OPCPackage pkg = OPCPackage.open(tmp);
@@ -302,7 +307,6 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
         // Save and close
         pkg.close();
 
-
         // Re-open and check
         pkg = OPCPackage.open(tmp);
 
index 965aa2c3a4f087667982f5293f98c413ec6626c3..cafdee46cbd275e0959a116868e9253dc2f69932 100644 (file)
@@ -228,7 +228,7 @@ public final class TestWorkbookFactory extends TestCase {
         );
         assertNotNull(wb);
         assertTrue(wb instanceof XSSFWorkbook);
-
+        wb.close();
 
         // Unprotected, wrong password, opens normally
         wb = WorkbookFactory.create(
@@ -243,7 +243,7 @@ public final class TestWorkbookFactory extends TestCase {
         );
         assertNotNull(wb);
         assertTrue(wb instanceof XSSFWorkbook);
-
+        wb.close();
 
         // Protected, correct password, opens fine
         wb = WorkbookFactory.create(
@@ -258,7 +258,7 @@ public final class TestWorkbookFactory extends TestCase {
         );
         assertNotNull(wb);
         assertTrue(wb instanceof XSSFWorkbook);
-
+        wb.close();
 
         // Protected, wrong password, throws Exception
         try {
index 1c750fedbf78d0d8334b1ba4548dfef1846c6703..ff3ee87b3845bc9e23a97d55969a99b2923eb470 100644 (file)
@@ -71,7 +71,8 @@ public final class TestSXSSFFormulaEvaluation {
         XSSFWorkbook xwb = new XSSFWorkbook();
         xwb.createSheet("Open");
         xwb.createSheet("Closed");
-        
+
+        wb.close();
         wb = new SXSSFWorkbook(xwb, 5);
         s = wb.getSheet("Closed");
         s.flushRows();
@@ -83,10 +84,12 @@ public final class TestSXSSFFormulaEvaluation {
             eval.evaluateAll();
             fail("Evaluate All shouldn't work, as sheets flushed");
         } catch (SXSSFFormulaEvaluator.SheetsFlushedException e) {}
+        
+        wb.close();
     }
     
     @Test
-    public void testEvaluateRefOutsideWindowFails() {
+    public void testEvaluateRefOutsideWindowFails() throws IOException {
         SXSSFWorkbook wb = new SXSSFWorkbook(5);
         SXSSFSheet s = wb.createSheet();
         
@@ -108,14 +111,17 @@ public final class TestSXSSFFormulaEvaluation {
         } catch(SXSSFFormulaEvaluator.RowFlushedException e) {
             // Expected
         }
+        
+        wb.close();
     }
     
     /**
      * If all formula cells + their references are inside the window,
      *  then evaluation works
+     * @throws IOException 
      */
     @Test
-    public void testEvaluateAllInWindow() {
+    public void testEvaluateAllInWindow() throws IOException {
         SXSSFWorkbook wb = new SXSSFWorkbook(5);
         SXSSFSheet s = wb.createSheet();
         s.createRow(0).createCell(0).setCellFormula("1+2");
@@ -128,10 +134,12 @@ public final class TestSXSSFFormulaEvaluation {
         assertEquals(3, (int)s.getRow(0).getCell(0).getNumericCellValue());
         assertEquals(13, (int)s.getRow(1).getCell(1).getNumericCellValue());
         assertEquals(113, (int)s.getRow(2).getCell(2).getNumericCellValue());
+        
+        wb.close();
     }
     
     @Test
-    public void testEvaluateRefInsideWindow() {
+    public void testEvaluateRefInsideWindow() throws IOException {
         SXSSFWorkbook wb = new SXSSFWorkbook(5);
         SXSSFSheet s = wb.createSheet();
         
@@ -146,10 +154,12 @@ public final class TestSXSSFFormulaEvaluation {
         assertEquals(0, (int)c.getNumericCellValue());
         eval.evaluateFormulaCell(c);
         assertEquals(3, (int)c.getNumericCellValue());
+        
+        wb.close();
     }
     
     @Test
-    public void testEvaluateSimple() {
+    public void testEvaluateSimple() throws IOException {
         SXSSFWorkbook wb = new SXSSFWorkbook(5);
         SXSSFSheet s = wb.createSheet();
         
@@ -165,5 +175,7 @@ public final class TestSXSSFFormulaEvaluation {
         c.setCellFormula("CONCATENATE(\"hello\",\" \",\"world\")");
         eval.evaluateFormulaCell(c);
         assertEquals("hello world", c.getStringCellValue());
+        
+        wb.close();
     }
 }
index b665492357ef6d55fc755613ce08bffdd0c10c7a..5b78d1484c6edc30c9f92b014b26f05c643108b8 100644 (file)
@@ -129,6 +129,7 @@ public final class TestExtractor {
         */
     @Test
        public void testMissingCoreRecords() throws Exception {
+        ppe.close();
                ppe = new PowerPointExtractor(slTests.openResourceAsStream("missing_core_records.ppt"));
 
                String text = ppe.getText(true, false);
@@ -171,6 +172,7 @@ public final class TestExtractor {
 
          // Check the first file
          ss = new HSLFSlideShowImpl(dirA);
+         ppe.close();
          ppe = new PowerPointExtractor(ss);
          assertEquals("Sample PowerPoint file\nThis is the 1st file\nNot much too it\n",
                  ppe.getText(true, false)
@@ -178,6 +180,7 @@ public final class TestExtractor {
 
          // And the second
          ss = new HSLFSlideShowImpl(dirB);
+         ppe.close();
          ppe = new PowerPointExtractor(ss);
          assertEquals("Sample PowerPoint file\nThis is the 2nd file\nNot much too it either\n",
                  ppe.getText(true, false)
@@ -192,6 +195,7 @@ public final class TestExtractor {
      @Test
      public void testExtractFromOwnEmbeded() throws Exception {
          String path = "ppt_with_embeded.ppt";
+         ppe.close();
          ppe = new PowerPointExtractor(POIDataSamples.getSlideShowInstance().openResourceAsStream(path));
          List<OLEShape> shapes = ppe.getOLEShapes();
          assertEquals("Expected 6 ole shapes in " + path, 6, shapes.size());
@@ -209,6 +213,7 @@ public final class TestExtractor {
              } else if ("Presentation".equals(name)) {
                  num_ppt++;
                  HSLFSlideShow ppt = new HSLFSlideShow(data);
+                 ppt.close();
              }
              data.close();
          }
@@ -223,6 +228,7 @@ public final class TestExtractor {
     @Test
     public void test52991() throws Exception {
         String path = "badzip.ppt";
+        ppe.close();
         ppe = new PowerPointExtractor(POIDataSamples.getSlideShowInstance().openResourceAsStream(path));
         List<OLEShape> shapes = ppe.getOLEShapes();
         
@@ -236,6 +242,7 @@ public final class TestExtractor {
      */
     @Test
     public void testWithComments() throws Exception {
+        ppe.close();
                ppe = new PowerPointExtractor(slTests.openResourceAsStream("WithComments.ppt"));
 
                String text = ppe.getText();
@@ -248,6 +255,7 @@ public final class TestExtractor {
 
                
                // And another file
+        ppe.close();
                ppe = new PowerPointExtractor(slTests.openResourceAsStream("45543.ppt"));
 
                text = ppe.getText();
@@ -271,6 +279,7 @@ public final class TestExtractor {
        HSLFSlideShow ss = new HSLFSlideShow(hslf);
        assertNotNull(ss.getNotesHeadersFooters());
        assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getHeaderText());
+       ppe.close();
 
        ppe = new PowerPointExtractor(hslf);
 
@@ -282,13 +291,14 @@ public final class TestExtractor {
        text = ppe.getText();
        assertContains(text, "testdoc");
        assertContains(text, "test phrase");
-
+       ss.close();
 
        // And with a footer, also on notes
        hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("45537_Footer.ppt"));
        ss = new HSLFSlideShow(hslf);
        assertNotNull(ss.getNotesHeadersFooters());
        assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getFooterText());
+       ppe.close();
 
        ppe = new PowerPointExtractor(slTests.openResourceAsStream("45537_Footer.ppt"));
 
@@ -309,7 +319,8 @@ public final class TestExtractor {
       String masterRandomText = "This text comes from the Master Slide";
       String masterFooterText = "Footer from the master slide";
       HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("WithMaster.ppt"));
-      
+      ppe.close();
+
       ppe = new PowerPointExtractor(hslf);
       
       String text = ppe.getText();
@@ -320,6 +331,7 @@ public final class TestExtractor {
 
    @Test
    public void testMasterText() throws Exception {
+       ppe.close();
        ppe = new PowerPointExtractor(slTests.openResourceAsStream("master_text.ppt"));
        
        // Initially not there
@@ -338,7 +350,8 @@ public final class TestExtractor {
        // Will always show up
        String masterText = "Footer from the master slide";
        HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("WithMaster.ppt"));
-       
+       ppe.close();
+
        ppe = new PowerPointExtractor(hslf);
        
        text = ppe.getText();
@@ -352,6 +365,7 @@ public final class TestExtractor {
     @Test
     public void testChineseText() throws Exception {
        HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("54880_chinese.ppt"));
+       ppe.close();
        ppe = new PowerPointExtractor(hslf);
        
        String text = ppe.getText();
@@ -409,6 +423,7 @@ public final class TestExtractor {
 //                         "Row 4, Cell 1\tRow 4, Cell 2\tRow 4, Cell 3\tRow 4, Cell 4\n"+ 
 //                         "Row 5, Cell 1\tRow 5, Cell 2\tRow 5, Cell 3\tRow 5, Cell 4\n";
 //        assertTrue(text.contains(target));
+        ppe.close();
 
         ppe = new PowerPointExtractor(slTests.openResourceAsStream("54722.ppt"));
         String text = ppe.getText();
index b406d6eed8ab15ce00cbdd9cde959e9f4c8f799e..7ce0f6cbd18f93036bcb29024d53b9179bed0f2d 100644 (file)
@@ -69,12 +69,14 @@ public final class TestCurrentUserAtom {
        public void readEnc() throws Exception {
                POIFSFileSystem fs = new POIFSFileSystem(_slTests.getFile(encFile));
 
-               new CurrentUserAtom(fs.getRoot());
-               assertTrue(true); // not yet failed
-               
-               new HSLFSlideShowImpl(fs);
-               
-               fs.close();
+               try {
+               new CurrentUserAtom(fs.getRoot());
+               assertTrue(true); // not yet failed
+               
+               new HSLFSlideShowImpl(fs).close();
+               } finally {
+                   fs.close();
+               }
        }
 
        @Test
index efc642714462b6495e775d227ecf4ac8f78599c9..992eb5e1bd81619f892c15ee2c1c678cac817077 100644 (file)
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintStream;
 import java.io.UnsupportedEncodingException;
@@ -31,7 +32,6 @@ import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.LocaleUtil;
 import org.junit.Test;
 
-@SuppressWarnings("resource")
 public class TestEscherDump {
     @Test
     public void testSimple() throws Exception {
@@ -56,10 +56,15 @@ public class TestEscherDump {
         //new EscherDump().dumpOld(data.length, new ByteArrayInputStream(data), System.out);
         
         data = new byte[2586114];
-        int bytes = IOUtils.readFully(HSSFTestDataSamples.openSampleFileStream("44593.xls"), data);
-        assertTrue(bytes != -1);
-        //new EscherDump().dump(bytes, data, System.out);
-        //new EscherDump().dumpOld(bytes, new ByteArrayInputStream(data), System.out);
+        InputStream stream = HSSFTestDataSamples.openSampleFileStream("44593.xls");
+        try {
+            int bytes = IOUtils.readFully(stream, data);
+            assertTrue(bytes != -1);
+            //new EscherDump().dump(bytes, data, System.out);
+            //new EscherDump().dumpOld(bytes, new ByteArrayInputStream(data), System.out);
+        } finally {
+            stream.close();
+        }
     }
     
     /**
index 0463162daf1980f7268077355e12a05b2dcc0def..15a89548888a9a8f48dc7a238cf00142106d7850 100644 (file)
@@ -48,15 +48,18 @@ public class TestBiffViewer extends BaseXLSIteratingTest {
        @Override
        void runOneFile(File fileIn) throws IOException {
            NPOIFSFileSystem fs  = new NPOIFSFileSystem(fileIn, true);
-               InputStream is = BiffViewer.getPOIFSInputStream(fs);
-               try {
-                       // use a NullOutputStream to not write the bytes anywhere for best runtime
-                   PrintWriter dummy = new PrintWriter(new OutputStreamWriter(NULL_OUTPUT_STREAM, LocaleUtil.CHARSET_1252));
-                       BiffViewer.runBiffViewer(dummy, is, true, true, true, false);
-               } finally {
-                       is.close();
-                       fs.close();
-               }
+           try {
+               InputStream is = BiffViewer.getPOIFSInputStream(fs);
+               try {
+                       // use a NullOutputStream to not write the bytes anywhere for best runtime
+                   PrintWriter dummy = new PrintWriter(new OutputStreamWriter(NULL_OUTPUT_STREAM, LocaleUtil.CHARSET_1252));
+                       BiffViewer.runBiffViewer(dummy, is, true, true, true, false);
+               } finally {
+                       is.close();
+               }
+           } finally {
+               fs.close();
+           }
        }
        
 //     @Test
index 2a5698881db9e8d83614a7d01b78d39508419ed5..7f980e29cf6c9e1f07e2712e9c52337b02ab78d1 100644 (file)
@@ -45,13 +45,14 @@ public final class TestExcelExtractor {
         Biff8EncryptionKey.setCurrentUserPassword(null);
     }
 
-       @SuppressWarnings("resource")
     private static ExcelExtractor createExtractor(String sampleFileName) throws IOException {
                File file = HSSFTestDataSamples.getSampleFile(sampleFileName);
-        return new ExcelExtractor(new POIFSFileSystem(file));
+        POIFSFileSystem fs = new POIFSFileSystem(file);
+        ExcelExtractor extractor = new ExcelExtractor(fs);
+        extractor.setFilesystem(fs);
+        return extractor;
        }
 
-
        @Test
        public void testSimple() throws IOException {
                ExcelExtractor extractor = createExtractor("Simple.xls");
index ab8c1682532880c3751e0f760cea2edfd6d9736a..631cf37724730cbf23b8dcb6560d60157e57fba0 100644 (file)
@@ -36,27 +36,38 @@ public class BaseTestSlideShowFactory {
         // from file
         ss = SlideShowFactory.create(fromFile(file));
         assertNotNull(ss);
+        ss.close();
+
         // from stream
         ss = SlideShowFactory.create(fromStream(file));
         assertNotNull(ss);
+        ss.close();
+
         // from NPOIFS
         if (!file.contains("pptx")) {
             NPOIFSFileSystem npoifs = new NPOIFSFileSystem(fromFile(file));
             ss = SlideShowFactory.create(npoifs);
             assertNotNull(ss);
             npoifs.close();
+            ss.close();
         }
+
         // from protected file
         ss = SlideShowFactory.create(fromFile(protectedFile), password);
         assertNotNull(ss);
+        ss.close();
+
         // from protected stream
         ss = SlideShowFactory.create(fromStream(protectedFile), password);
         assertNotNull(ss);
+        ss.close();
+
         // from protected NPOIFS
         NPOIFSFileSystem npoifs = new NPOIFSFileSystem(fromFile(protectedFile));
         ss = SlideShowFactory.create(npoifs, password);
         assertNotNull(ss);
         npoifs.close();
+        ss.close();
     }
     
     private static File fromFile(String file) {
index 1d20d6c2250b8dd6c32ee63edf122e90de490da1..384edddb5c687319521f8c81b6aedb5c2759764d 100644 (file)
@@ -25,12 +25,15 @@ import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.List;
 
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.ITestDataProvider;
 import org.apache.poi.ss.SpreadsheetVersion;
 import org.apache.poi.util.LocaleUtil;
+import org.junit.After;
 import org.junit.Test;
 
 import junit.framework.AssertionFailedError;
@@ -44,6 +47,16 @@ public abstract class BaseTestCell {
 
     protected final ITestDataProvider _testDataProvider;
 
+    private List<Workbook> workbooksToClose = new ArrayList<Workbook>(); 
+
+    @After
+    public void tearDown() throws IOException {
+        // free resources correctly
+        for(Workbook wb : workbooksToClose) {
+            wb.close();
+        }
+    }
+
     /**
      * @param testDataProvider an object that provides test data in HSSF / XSSF specific way
      */
@@ -350,8 +363,11 @@ public abstract class BaseTestCell {
         wb.close();
 
     }
+
     private Cell createACell() {
-        return _testDataProvider.createWorkbook().createSheet("Sheet1").createRow(0).createCell(0);
+        Workbook wb = _testDataProvider.createWorkbook();
+        workbooksToClose.add(wb);
+        return wb.createSheet("Sheet1").createRow(0).createCell(0);
     }
     
     /**
@@ -953,5 +969,7 @@ public abstract class BaseTestCell {
 
         B1.setAsActiveCell();
         assertEquals(B1.getAddress(), sheet.getActiveCell());
+        
+        wb.close();
     }
 }