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
This commit is contained in:
Dominik Stadler 2015-12-22 22:35:59 +00:00
parent b333ed457d
commit 0a3370347e
13 changed files with 168 additions and 85 deletions

View File

@ -44,21 +44,27 @@ public class EFBiffViewer
public void run() throws IOException { public void run() throws IOException {
NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(file), true); NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(file), true);
InputStream din = BiffViewer.getPOIFSInputStream(fs); try {
HSSFRequest req = new HSSFRequest(); InputStream din = BiffViewer.getPOIFSInputStream(fs);
try {
HSSFRequest req = new HSSFRequest();
req.addListenerForAllRecords(new HSSFListener() req.addListenerForAllRecords(new HSSFListener()
{ {
public void processRecord(Record rec) public void processRecord(Record rec)
{ {
System.out.println(rec.toString()); System.out.println(rec.toString());
}
});
HSSFEventFactory factory = new HSSFEventFactory();
factory.processEvents(req, din);
} finally {
din.close();
} }
}); } finally {
HSSFEventFactory factory = new HSSFEventFactory(); fs.close();
}
factory.processEvents(req, din);
din.close();
fs.close();
} }
public void setFile(String file) public void setFile(String file)

View File

@ -51,37 +51,42 @@ public class RecordLister
throws IOException throws IOException
{ {
NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(file), true); NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(file), true);
InputStream din = BiffViewer.getPOIFSInputStream(fs); try {
RecordInputStream rinp = new RecordInputStream(din); InputStream din = BiffViewer.getPOIFSInputStream(fs);
try {
RecordInputStream rinp = new RecordInputStream(din);
while(rinp.hasNextRecord()) { while(rinp.hasNextRecord()) {
int sid = rinp.getNextSid(); int sid = rinp.getNextSid();
rinp.nextRecord(); rinp.nextRecord();
int size = rinp.available(); int size = rinp.available();
Class<? extends Record> clz = RecordFactory.getRecordClass(sid); Class<? extends Record> clz = RecordFactory.getRecordClass(sid);
System.out.print( System.out.print(
formatSID(sid) + formatSID(sid) +
" - " + " - " +
formatSize(size) + formatSize(size) +
" bytes" " bytes"
); );
if(clz != null) { if(clz != null) {
System.out.print(" \t"); System.out.print(" \t");
System.out.print(clz.getName().replace("org.apache.poi.hssf.record.", "")); System.out.print(clz.getName().replace("org.apache.poi.hssf.record.", ""));
} }
System.out.println(); System.out.println();
byte[] data = rinp.readRemainder(); byte[] data = rinp.readRemainder();
if(data.length > 0) { if(data.length > 0) {
System.out.print(" "); System.out.print(" ");
System.out.println( formatData(data) ); System.out.println( formatData(data) );
} }
}
} finally {
din.close();
}
} finally {
fs.close();
} }
din.close();
fs.close();
} }
private static String formatSID(int sid) { private static String formatSID(int sid) {

View File

@ -716,6 +716,7 @@ public final class TestPackage {
Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(buf)); Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(buf));
wb.getSheetAt(0); wb.getSheetAt(0);
wb.close(); wb.close();
zipFile.close();
} }
@Test @Test

View File

@ -26,9 +26,6 @@ import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.openxml4j.OpenXML4JTestDataSamples; import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 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.IOUtils;
import org.apache.poi.util.TempFile; import org.apache.poi.util.TempFile;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
/** /**
* Test core properties Open Packaging Convention compliance. * Test core properties Open Packaging Convention compliance.
* *
@ -249,6 +249,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
pkg.save(baos); pkg.save(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
pkg.revert();
pkg = OPCPackage.open(bais); pkg = OPCPackage.open(bais);
@ -257,7 +258,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
assertNotNull(pkg.getPackageProperties()); assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty()); assertNotNull(pkg.getPackageProperties().getLanguageProperty());
assertNull(pkg.getPackageProperties().getLanguageProperty().getValue()); assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
pkg.close();
// Open a new copy of it // Open a new copy of it
pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath()); 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 // Save and re-load, without having touched the properties yet
baos = new ByteArrayOutputStream(); baos = new ByteArrayOutputStream();
pkg.save(baos); pkg.save(baos);
pkg.revert();
bais = new ByteArrayInputStream(baos.toByteArray()); bais = new ByteArrayInputStream(baos.toByteArray());
pkg = OPCPackage.open(bais); 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 // Copy this into a temp file, so we can play with it
File tmp = TempFile.createTempFile("poi-test", ".opc"); File tmp = TempFile.createTempFile("poi-test", ".opc");
FileOutputStream out = new FileOutputStream(tmp); FileOutputStream out = new FileOutputStream(tmp);
InputStream in = POIDataSamples.getOpenXML4JInstance().openResourceAsStream(sampleFileName);
IOUtils.copy( IOUtils.copy(
POIDataSamples.getOpenXML4JInstance().openResourceAsStream(sampleFileName), in,
out); out);
out.close(); out.close();
in.close();
// Open it from that temp file // Open it from that temp file
OPCPackage pkg = OPCPackage.open(tmp); OPCPackage pkg = OPCPackage.open(tmp);
@ -302,7 +307,6 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
// Save and close // Save and close
pkg.close(); pkg.close();
// Re-open and check // Re-open and check
pkg = OPCPackage.open(tmp); pkg = OPCPackage.open(tmp);

View File

@ -228,7 +228,7 @@ public final class TestWorkbookFactory extends TestCase {
); );
assertNotNull(wb); assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook); assertTrue(wb instanceof XSSFWorkbook);
wb.close();
// Unprotected, wrong password, opens normally // Unprotected, wrong password, opens normally
wb = WorkbookFactory.create( wb = WorkbookFactory.create(
@ -243,7 +243,7 @@ public final class TestWorkbookFactory extends TestCase {
); );
assertNotNull(wb); assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook); assertTrue(wb instanceof XSSFWorkbook);
wb.close();
// Protected, correct password, opens fine // Protected, correct password, opens fine
wb = WorkbookFactory.create( wb = WorkbookFactory.create(
@ -258,7 +258,7 @@ public final class TestWorkbookFactory extends TestCase {
); );
assertNotNull(wb); assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook); assertTrue(wb instanceof XSSFWorkbook);
wb.close();
// Protected, wrong password, throws Exception // Protected, wrong password, throws Exception
try { try {

View File

@ -72,6 +72,7 @@ public final class TestSXSSFFormulaEvaluation {
xwb.createSheet("Open"); xwb.createSheet("Open");
xwb.createSheet("Closed"); xwb.createSheet("Closed");
wb.close();
wb = new SXSSFWorkbook(xwb, 5); wb = new SXSSFWorkbook(xwb, 5);
s = wb.getSheet("Closed"); s = wb.getSheet("Closed");
s.flushRows(); s.flushRows();
@ -83,10 +84,12 @@ public final class TestSXSSFFormulaEvaluation {
eval.evaluateAll(); eval.evaluateAll();
fail("Evaluate All shouldn't work, as sheets flushed"); fail("Evaluate All shouldn't work, as sheets flushed");
} catch (SXSSFFormulaEvaluator.SheetsFlushedException e) {} } catch (SXSSFFormulaEvaluator.SheetsFlushedException e) {}
wb.close();
} }
@Test @Test
public void testEvaluateRefOutsideWindowFails() { public void testEvaluateRefOutsideWindowFails() throws IOException {
SXSSFWorkbook wb = new SXSSFWorkbook(5); SXSSFWorkbook wb = new SXSSFWorkbook(5);
SXSSFSheet s = wb.createSheet(); SXSSFSheet s = wb.createSheet();
@ -108,14 +111,17 @@ public final class TestSXSSFFormulaEvaluation {
} catch(SXSSFFormulaEvaluator.RowFlushedException e) { } catch(SXSSFFormulaEvaluator.RowFlushedException e) {
// Expected // Expected
} }
wb.close();
} }
/** /**
* If all formula cells + their references are inside the window, * If all formula cells + their references are inside the window,
* then evaluation works * then evaluation works
* @throws IOException
*/ */
@Test @Test
public void testEvaluateAllInWindow() { public void testEvaluateAllInWindow() throws IOException {
SXSSFWorkbook wb = new SXSSFWorkbook(5); SXSSFWorkbook wb = new SXSSFWorkbook(5);
SXSSFSheet s = wb.createSheet(); SXSSFSheet s = wb.createSheet();
s.createRow(0).createCell(0).setCellFormula("1+2"); 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(3, (int)s.getRow(0).getCell(0).getNumericCellValue());
assertEquals(13, (int)s.getRow(1).getCell(1).getNumericCellValue()); assertEquals(13, (int)s.getRow(1).getCell(1).getNumericCellValue());
assertEquals(113, (int)s.getRow(2).getCell(2).getNumericCellValue()); assertEquals(113, (int)s.getRow(2).getCell(2).getNumericCellValue());
wb.close();
} }
@Test @Test
public void testEvaluateRefInsideWindow() { public void testEvaluateRefInsideWindow() throws IOException {
SXSSFWorkbook wb = new SXSSFWorkbook(5); SXSSFWorkbook wb = new SXSSFWorkbook(5);
SXSSFSheet s = wb.createSheet(); SXSSFSheet s = wb.createSheet();
@ -146,10 +154,12 @@ public final class TestSXSSFFormulaEvaluation {
assertEquals(0, (int)c.getNumericCellValue()); assertEquals(0, (int)c.getNumericCellValue());
eval.evaluateFormulaCell(c); eval.evaluateFormulaCell(c);
assertEquals(3, (int)c.getNumericCellValue()); assertEquals(3, (int)c.getNumericCellValue());
wb.close();
} }
@Test @Test
public void testEvaluateSimple() { public void testEvaluateSimple() throws IOException {
SXSSFWorkbook wb = new SXSSFWorkbook(5); SXSSFWorkbook wb = new SXSSFWorkbook(5);
SXSSFSheet s = wb.createSheet(); SXSSFSheet s = wb.createSheet();
@ -165,5 +175,7 @@ public final class TestSXSSFFormulaEvaluation {
c.setCellFormula("CONCATENATE(\"hello\",\" \",\"world\")"); c.setCellFormula("CONCATENATE(\"hello\",\" \",\"world\")");
eval.evaluateFormulaCell(c); eval.evaluateFormulaCell(c);
assertEquals("hello world", c.getStringCellValue()); assertEquals("hello world", c.getStringCellValue());
wb.close();
} }
} }

View File

@ -129,6 +129,7 @@ public final class TestExtractor {
*/ */
@Test @Test
public void testMissingCoreRecords() throws Exception { public void testMissingCoreRecords() throws Exception {
ppe.close();
ppe = new PowerPointExtractor(slTests.openResourceAsStream("missing_core_records.ppt")); ppe = new PowerPointExtractor(slTests.openResourceAsStream("missing_core_records.ppt"));
String text = ppe.getText(true, false); String text = ppe.getText(true, false);
@ -171,6 +172,7 @@ public final class TestExtractor {
// Check the first file // Check the first file
ss = new HSLFSlideShowImpl(dirA); ss = new HSLFSlideShowImpl(dirA);
ppe.close();
ppe = new PowerPointExtractor(ss); ppe = new PowerPointExtractor(ss);
assertEquals("Sample PowerPoint file\nThis is the 1st file\nNot much too it\n", assertEquals("Sample PowerPoint file\nThis is the 1st file\nNot much too it\n",
ppe.getText(true, false) ppe.getText(true, false)
@ -178,6 +180,7 @@ public final class TestExtractor {
// And the second // And the second
ss = new HSLFSlideShowImpl(dirB); ss = new HSLFSlideShowImpl(dirB);
ppe.close();
ppe = new PowerPointExtractor(ss); ppe = new PowerPointExtractor(ss);
assertEquals("Sample PowerPoint file\nThis is the 2nd file\nNot much too it either\n", assertEquals("Sample PowerPoint file\nThis is the 2nd file\nNot much too it either\n",
ppe.getText(true, false) ppe.getText(true, false)
@ -192,6 +195,7 @@ public final class TestExtractor {
@Test @Test
public void testExtractFromOwnEmbeded() throws Exception { public void testExtractFromOwnEmbeded() throws Exception {
String path = "ppt_with_embeded.ppt"; String path = "ppt_with_embeded.ppt";
ppe.close();
ppe = new PowerPointExtractor(POIDataSamples.getSlideShowInstance().openResourceAsStream(path)); ppe = new PowerPointExtractor(POIDataSamples.getSlideShowInstance().openResourceAsStream(path));
List<OLEShape> shapes = ppe.getOLEShapes(); List<OLEShape> shapes = ppe.getOLEShapes();
assertEquals("Expected 6 ole shapes in " + path, 6, shapes.size()); assertEquals("Expected 6 ole shapes in " + path, 6, shapes.size());
@ -209,6 +213,7 @@ public final class TestExtractor {
} else if ("Presentation".equals(name)) { } else if ("Presentation".equals(name)) {
num_ppt++; num_ppt++;
HSLFSlideShow ppt = new HSLFSlideShow(data); HSLFSlideShow ppt = new HSLFSlideShow(data);
ppt.close();
} }
data.close(); data.close();
} }
@ -223,6 +228,7 @@ public final class TestExtractor {
@Test @Test
public void test52991() throws Exception { public void test52991() throws Exception {
String path = "badzip.ppt"; String path = "badzip.ppt";
ppe.close();
ppe = new PowerPointExtractor(POIDataSamples.getSlideShowInstance().openResourceAsStream(path)); ppe = new PowerPointExtractor(POIDataSamples.getSlideShowInstance().openResourceAsStream(path));
List<OLEShape> shapes = ppe.getOLEShapes(); List<OLEShape> shapes = ppe.getOLEShapes();
@ -236,6 +242,7 @@ public final class TestExtractor {
*/ */
@Test @Test
public void testWithComments() throws Exception { public void testWithComments() throws Exception {
ppe.close();
ppe = new PowerPointExtractor(slTests.openResourceAsStream("WithComments.ppt")); ppe = new PowerPointExtractor(slTests.openResourceAsStream("WithComments.ppt"));
String text = ppe.getText(); String text = ppe.getText();
@ -248,6 +255,7 @@ public final class TestExtractor {
// And another file // And another file
ppe.close();
ppe = new PowerPointExtractor(slTests.openResourceAsStream("45543.ppt")); ppe = new PowerPointExtractor(slTests.openResourceAsStream("45543.ppt"));
text = ppe.getText(); text = ppe.getText();
@ -271,6 +279,7 @@ public final class TestExtractor {
HSLFSlideShow ss = new HSLFSlideShow(hslf); HSLFSlideShow ss = new HSLFSlideShow(hslf);
assertNotNull(ss.getNotesHeadersFooters()); assertNotNull(ss.getNotesHeadersFooters());
assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getHeaderText()); assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getHeaderText());
ppe.close();
ppe = new PowerPointExtractor(hslf); ppe = new PowerPointExtractor(hslf);
@ -282,13 +291,14 @@ public final class TestExtractor {
text = ppe.getText(); text = ppe.getText();
assertContains(text, "testdoc"); assertContains(text, "testdoc");
assertContains(text, "test phrase"); assertContains(text, "test phrase");
ss.close();
// And with a footer, also on notes // And with a footer, also on notes
hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("45537_Footer.ppt")); hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("45537_Footer.ppt"));
ss = new HSLFSlideShow(hslf); ss = new HSLFSlideShow(hslf);
assertNotNull(ss.getNotesHeadersFooters()); assertNotNull(ss.getNotesHeadersFooters());
assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getFooterText()); assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getFooterText());
ppe.close();
ppe = new PowerPointExtractor(slTests.openResourceAsStream("45537_Footer.ppt")); ppe = new PowerPointExtractor(slTests.openResourceAsStream("45537_Footer.ppt"));
@ -309,6 +319,7 @@ public final class TestExtractor {
String masterRandomText = "This text comes from the Master Slide"; String masterRandomText = "This text comes from the Master Slide";
String masterFooterText = "Footer from the master slide"; String masterFooterText = "Footer from the master slide";
HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("WithMaster.ppt")); HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("WithMaster.ppt"));
ppe.close();
ppe = new PowerPointExtractor(hslf); ppe = new PowerPointExtractor(hslf);
@ -320,6 +331,7 @@ public final class TestExtractor {
@Test @Test
public void testMasterText() throws Exception { public void testMasterText() throws Exception {
ppe.close();
ppe = new PowerPointExtractor(slTests.openResourceAsStream("master_text.ppt")); ppe = new PowerPointExtractor(slTests.openResourceAsStream("master_text.ppt"));
// Initially not there // Initially not there
@ -338,6 +350,7 @@ public final class TestExtractor {
// Will always show up // Will always show up
String masterText = "Footer from the master slide"; String masterText = "Footer from the master slide";
HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("WithMaster.ppt")); HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("WithMaster.ppt"));
ppe.close();
ppe = new PowerPointExtractor(hslf); ppe = new PowerPointExtractor(hslf);
@ -352,6 +365,7 @@ public final class TestExtractor {
@Test @Test
public void testChineseText() throws Exception { public void testChineseText() throws Exception {
HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("54880_chinese.ppt")); HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("54880_chinese.ppt"));
ppe.close();
ppe = new PowerPointExtractor(hslf); ppe = new PowerPointExtractor(hslf);
String text = ppe.getText(); 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 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"; // "Row 5, Cell 1\tRow 5, Cell 2\tRow 5, Cell 3\tRow 5, Cell 4\n";
// assertTrue(text.contains(target)); // assertTrue(text.contains(target));
ppe.close();
ppe = new PowerPointExtractor(slTests.openResourceAsStream("54722.ppt")); ppe = new PowerPointExtractor(slTests.openResourceAsStream("54722.ppt"));
String text = ppe.getText(); String text = ppe.getText();

View File

@ -69,12 +69,14 @@ public final class TestCurrentUserAtom {
public void readEnc() throws Exception { public void readEnc() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(_slTests.getFile(encFile)); POIFSFileSystem fs = new POIFSFileSystem(_slTests.getFile(encFile));
new CurrentUserAtom(fs.getRoot()); try {
assertTrue(true); // not yet failed new CurrentUserAtom(fs.getRoot());
assertTrue(true); // not yet failed
new HSLFSlideShowImpl(fs); new HSLFSlideShowImpl(fs).close();
} finally {
fs.close(); fs.close();
}
} }
@Test @Test

View File

@ -21,6 +21,7 @@ import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -31,7 +32,6 @@ import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.junit.Test; import org.junit.Test;
@SuppressWarnings("resource")
public class TestEscherDump { public class TestEscherDump {
@Test @Test
public void testSimple() throws Exception { public void testSimple() throws Exception {
@ -56,10 +56,15 @@ public class TestEscherDump {
//new EscherDump().dumpOld(data.length, new ByteArrayInputStream(data), System.out); //new EscherDump().dumpOld(data.length, new ByteArrayInputStream(data), System.out);
data = new byte[2586114]; data = new byte[2586114];
int bytes = IOUtils.readFully(HSSFTestDataSamples.openSampleFileStream("44593.xls"), data); InputStream stream = HSSFTestDataSamples.openSampleFileStream("44593.xls");
assertTrue(bytes != -1); try {
//new EscherDump().dump(bytes, data, System.out); int bytes = IOUtils.readFully(stream, data);
//new EscherDump().dumpOld(bytes, new ByteArrayInputStream(data), System.out); assertTrue(bytes != -1);
//new EscherDump().dump(bytes, data, System.out);
//new EscherDump().dumpOld(bytes, new ByteArrayInputStream(data), System.out);
} finally {
stream.close();
}
} }
/** /**

View File

@ -48,15 +48,18 @@ public class TestBiffViewer extends BaseXLSIteratingTest {
@Override @Override
void runOneFile(File fileIn) throws IOException { void runOneFile(File fileIn) throws IOException {
NPOIFSFileSystem fs = new NPOIFSFileSystem(fileIn, true); NPOIFSFileSystem fs = new NPOIFSFileSystem(fileIn, true);
InputStream is = BiffViewer.getPOIFSInputStream(fs); try {
try { InputStream is = BiffViewer.getPOIFSInputStream(fs);
// use a NullOutputStream to not write the bytes anywhere for best runtime try {
PrintWriter dummy = new PrintWriter(new OutputStreamWriter(NULL_OUTPUT_STREAM, LocaleUtil.CHARSET_1252)); // use a NullOutputStream to not write the bytes anywhere for best runtime
BiffViewer.runBiffViewer(dummy, is, true, true, true, false); PrintWriter dummy = new PrintWriter(new OutputStreamWriter(NULL_OUTPUT_STREAM, LocaleUtil.CHARSET_1252));
} finally { BiffViewer.runBiffViewer(dummy, is, true, true, true, false);
is.close(); } finally {
fs.close(); is.close();
} }
} finally {
fs.close();
}
} }
// @Test // @Test

View File

@ -45,13 +45,14 @@ public final class TestExcelExtractor {
Biff8EncryptionKey.setCurrentUserPassword(null); Biff8EncryptionKey.setCurrentUserPassword(null);
} }
@SuppressWarnings("resource")
private static ExcelExtractor createExtractor(String sampleFileName) throws IOException { private static ExcelExtractor createExtractor(String sampleFileName) throws IOException {
File file = HSSFTestDataSamples.getSampleFile(sampleFileName); 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 @Test
public void testSimple() throws IOException { public void testSimple() throws IOException {
ExcelExtractor extractor = createExtractor("Simple.xls"); ExcelExtractor extractor = createExtractor("Simple.xls");

View File

@ -36,27 +36,38 @@ public class BaseTestSlideShowFactory {
// from file // from file
ss = SlideShowFactory.create(fromFile(file)); ss = SlideShowFactory.create(fromFile(file));
assertNotNull(ss); assertNotNull(ss);
ss.close();
// from stream // from stream
ss = SlideShowFactory.create(fromStream(file)); ss = SlideShowFactory.create(fromStream(file));
assertNotNull(ss); assertNotNull(ss);
ss.close();
// from NPOIFS // from NPOIFS
if (!file.contains("pptx")) { if (!file.contains("pptx")) {
NPOIFSFileSystem npoifs = new NPOIFSFileSystem(fromFile(file)); NPOIFSFileSystem npoifs = new NPOIFSFileSystem(fromFile(file));
ss = SlideShowFactory.create(npoifs); ss = SlideShowFactory.create(npoifs);
assertNotNull(ss); assertNotNull(ss);
npoifs.close(); npoifs.close();
ss.close();
} }
// from protected file // from protected file
ss = SlideShowFactory.create(fromFile(protectedFile), password); ss = SlideShowFactory.create(fromFile(protectedFile), password);
assertNotNull(ss); assertNotNull(ss);
ss.close();
// from protected stream // from protected stream
ss = SlideShowFactory.create(fromStream(protectedFile), password); ss = SlideShowFactory.create(fromStream(protectedFile), password);
assertNotNull(ss); assertNotNull(ss);
ss.close();
// from protected NPOIFS // from protected NPOIFS
NPOIFSFileSystem npoifs = new NPOIFSFileSystem(fromFile(protectedFile)); NPOIFSFileSystem npoifs = new NPOIFSFileSystem(fromFile(protectedFile));
ss = SlideShowFactory.create(npoifs, password); ss = SlideShowFactory.create(npoifs, password);
assertNotNull(ss); assertNotNull(ss);
npoifs.close(); npoifs.close();
ss.close();
} }
private static File fromFile(String file) { private static File fromFile(String file) {

View File

@ -25,12 +25,15 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.ITestDataProvider;
import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
@ -44,6 +47,16 @@ public abstract class BaseTestCell {
protected final ITestDataProvider _testDataProvider; 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 * @param testDataProvider an object that provides test data in HSSF / XSSF specific way
*/ */
@ -350,8 +363,11 @@ public abstract class BaseTestCell {
wb.close(); wb.close();
} }
private Cell createACell() { 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(); B1.setAsActiveCell();
assertEquals(B1.getAddress(), sheet.getActiveCell()); assertEquals(B1.getAddress(), sheet.getActiveCell());
wb.close();
} }
} }