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)
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) {
Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(buf));
wb.getSheetAt(0);
wb.close();
+ zipFile.close();
}
@Test
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;
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.
*
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pkg.save(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+ pkg.revert();
pkg = OPCPackage.open(bais);
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());
// 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);
// 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);
// Save and close
pkg.close();
-
// Re-open and check
pkg = OPCPackage.open(tmp);
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
-
+ wb.close();
// Unprotected, wrong password, opens normally
wb = WorkbookFactory.create(
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
-
+ wb.close();
// Protected, correct password, opens fine
wb = WorkbookFactory.create(
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
-
+ wb.close();
// Protected, wrong password, throws Exception
try {
XSSFWorkbook xwb = new XSSFWorkbook();
xwb.createSheet("Open");
xwb.createSheet("Closed");
-
+
+ wb.close();
wb = new SXSSFWorkbook(xwb, 5);
s = wb.getSheet("Closed");
s.flushRows();
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();
} 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");
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();
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();
c.setCellFormula("CONCATENATE(\"hello\",\" \",\"world\")");
eval.evaluateFormulaCell(c);
assertEquals("hello world", c.getStringCellValue());
+
+ wb.close();
}
}
*/
@Test
public void testMissingCoreRecords() throws Exception {
+ ppe.close();
ppe = new PowerPointExtractor(slTests.openResourceAsStream("missing_core_records.ppt"));
String text = ppe.getText(true, false);
// 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)
// 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)
@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());
} else if ("Presentation".equals(name)) {
num_ppt++;
HSLFSlideShow ppt = new HSLFSlideShow(data);
+ ppt.close();
}
data.close();
}
@Test
public void test52991() throws Exception {
String path = "badzip.ppt";
+ ppe.close();
ppe = new PowerPointExtractor(POIDataSamples.getSlideShowInstance().openResourceAsStream(path));
List<OLEShape> shapes = ppe.getOLEShapes();
*/
@Test
public void testWithComments() throws Exception {
+ ppe.close();
ppe = new PowerPointExtractor(slTests.openResourceAsStream("WithComments.ppt"));
String text = ppe.getText();
// And another file
+ ppe.close();
ppe = new PowerPointExtractor(slTests.openResourceAsStream("45543.ppt"));
text = ppe.getText();
HSLFSlideShow ss = new HSLFSlideShow(hslf);
assertNotNull(ss.getNotesHeadersFooters());
assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getHeaderText());
+ ppe.close();
ppe = new PowerPointExtractor(hslf);
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"));
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();
@Test
public void testMasterText() throws Exception {
+ ppe.close();
ppe = new PowerPointExtractor(slTests.openResourceAsStream("master_text.ppt"));
// Initially not there
// 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();
@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();
// "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();
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
import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import org.apache.poi.util.LocaleUtil;
import org.junit.Test;
-@SuppressWarnings("resource")
public class TestEscherDump {
@Test
public void testSimple() throws Exception {
//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();
+ }
}
/**
@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
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");
// 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) {
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;
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
*/
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);
}
/**
B1.setAsActiveCell();
assertEquals(B1.getAddress(), sheet.getActiveCell());
+
+ wb.close();
}
}