diff options
author | Dominik Stadler <centic@apache.org> | 2015-12-22 22:35:59 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2015-12-22 22:35:59 +0000 |
commit | 83eb1aaefcdc8cca48677ab95f64c1eb4ff04850 (patch) | |
tree | 1ab9cde44c16048fea9d44a20ad583b0166f0126 /src/testcases | |
parent | 70d75027d3013e0e812b8fa746026fae3dcdb04e (diff) | |
download | poi-83eb1aaefcdc8cca48677ab95f64c1eb4ff04850.tar.gz poi-83eb1aaefcdc8cca48677ab95f64c1eb4ff04850.zip |
Fix freeing resources in some tests and dev-tools so we can run unit-tests with enabled file-leak-detector
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1721468 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases')
5 files changed, 56 insertions, 18 deletions
diff --git a/src/testcases/org/apache/poi/ddf/TestEscherDump.java b/src/testcases/org/apache/poi/ddf/TestEscherDump.java index efc6427144..992eb5e1bd 100644 --- a/src/testcases/org/apache/poi/ddf/TestEscherDump.java +++ b/src/testcases/org/apache/poi/ddf/TestEscherDump.java @@ -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(); + } } /** diff --git a/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java b/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java index 0463162daf..15a8954888 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java @@ -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 diff --git a/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java b/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java index 2a5698881d..7f980e29cf 100644 --- a/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java +++ b/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java @@ -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"); diff --git a/src/testcases/org/apache/poi/sl/usermodel/BaseTestSlideShowFactory.java b/src/testcases/org/apache/poi/sl/usermodel/BaseTestSlideShowFactory.java index ab8c168253..631cf37724 100644 --- a/src/testcases/org/apache/poi/sl/usermodel/BaseTestSlideShowFactory.java +++ b/src/testcases/org/apache/poi/sl/usermodel/BaseTestSlideShowFactory.java @@ -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) { diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index 1d20d6c225..384edddb5c 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -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(); } } |