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 {
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)

View 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) {

View File

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

View 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);

View 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 {

View 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();
}
}

View 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();

View 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

View 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();
}
}
/**

View 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

View 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");

View 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) {

View 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();
}
}