aboutsummaryrefslogtreecommitdiffstats
path: root/src/integrationtest/org/apache/poi/stress/XSSFFileHandler.java
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2015-09-13 19:05:40 +0000
committerDominik Stadler <centic@apache.org>2015-09-13 19:05:40 +0000
commitd390a9bf3f83f15bc5c23a678a0be0558c4a38af (patch)
tree14cc55c4dd1e414e55d78c3551dee4af2711f0f0 /src/integrationtest/org/apache/poi/stress/XSSFFileHandler.java
parentcda2a2b63d12ff3b399f2122ac4d94483d6a8964 (diff)
downloadpoi-d390a9bf3f83f15bc5c23a678a0be0558c4a38af.tar.gz
poi-d390a9bf3f83f15bc5c23a678a0be0558c4a38af.zip
Enhance integration testing to apply OPCPackage tests to more file-types and also run XSSFReader in XSSFFileHandler
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1702801 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/integrationtest/org/apache/poi/stress/XSSFFileHandler.java')
-rw-r--r--src/integrationtest/org/apache/poi/stress/XSSFFileHandler.java98
1 files changed, 73 insertions, 25 deletions
diff --git a/src/integrationtest/org/apache/poi/stress/XSSFFileHandler.java b/src/integrationtest/org/apache/poi/stress/XSSFFileHandler.java
index a268ed4658..764ca4ac75 100644
--- a/src/integrationtest/org/apache/poi/stress/XSSFFileHandler.java
+++ b/src/integrationtest/org/apache/poi/stress/XSSFFileHandler.java
@@ -16,14 +16,25 @@
==================================================================== */
package org.apache.poi.stress;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
+import java.io.IOException;
import java.io.InputStream;
+import java.util.Iterator;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.extractor.XSSFExportToXml;
import org.apache.poi.xssf.usermodel.XSSFMap;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@@ -31,27 +42,64 @@ import org.junit.Test;
import org.xml.sax.SAXException;
public class XSSFFileHandler extends SpreadsheetHandler {
- @Override
+ @Override
public void handleFile(InputStream stream) throws Exception {
- // ignore password protected files
- if (POIXMLDocumentHandler.isEncrypted(stream)) return;
-
- XSSFWorkbook wb = new XSSFWorkbook(stream);
-
- // use the combined handler for HSSF/XSSF
- handleWorkbook(wb, ".xlsx");
-
+ // ignore password protected files
+ if (POIXMLDocumentHandler.isEncrypted(stream)) return;
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ IOUtils.copy(stream, out);
+
+ XSSFWorkbook wb = new XSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
+
+ // use the combined handler for HSSF/XSSF
+ handleWorkbook(wb, ".xlsx");
+
// TODO: some documents fail currently...
//XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(wb);
//evaluator.evaluateAll();
- // also verify general POIFS-stuff
- new POIXMLDocumentHandler().handlePOIXMLDocument(wb);
-
- // and finally ensure that exporting to XML works
- exportToXML(wb);
- }
+ // also verify general POIFS-stuff
+ new POIXMLDocumentHandler().handlePOIXMLDocument(wb);
+
+ // and finally ensure that exporting to XML works
+ exportToXML(wb);
+
+ checkXSSFReader(OPCPackage.open(new ByteArrayInputStream(out.toByteArray())));
+ }
+
+ private void checkXSSFReader(OPCPackage p)
+ throws IOException, OpenXML4JException, InvalidFormatException {
+ XSSFReader reader = new XSSFReader(p);
+
+ // these can be null...
+ InputStream sharedStringsData = reader.getSharedStringsData();
+ if(sharedStringsData != null) {
+ sharedStringsData.close();
+ }
+ reader.getSharedStringsTable();
+
+ InputStream stylesData = reader.getStylesData();
+ if(stylesData != null) {
+ stylesData.close();
+ }
+ reader.getStylesTable();
+
+ InputStream themesData = reader.getThemesData();
+ if(themesData != null) {
+ themesData.close();
+ }
+
+ assertNotNull(reader.getWorkbookData());
+
+ Iterator<InputStream> sheetsData = reader.getSheetsData();
+ while(sheetsData.hasNext()) {
+ InputStream str = sheetsData.next();
+ str.close();
+ }
+ }
+
private void exportToXML(XSSFWorkbook wb) throws SAXException,
ParserConfigurationException, TransformerException {
for (XSSFMap map : wb.getCustomXMLMappings()) {
@@ -62,16 +110,16 @@ public class XSSFFileHandler extends SpreadsheetHandler {
}
}
- // a test-case to test this locally without executing the full TestAllFiles
- @Test
- public void test() throws Exception {
- InputStream stream = new FileInputStream("test-data/spreadsheet/WithConditionalFormatting.xlsx");
- try {
- handleFile(stream);
- } finally {
- stream.close();
- }
- }
+ // a test-case to test this locally without executing the full TestAllFiles
+ @Test
+ public void test() throws Exception {
+ InputStream stream = new BufferedInputStream(new FileInputStream("test-data/openxml4j/50154.xlsx"));
+ try {
+ handleFile(stream);
+ } finally {
+ stream.close();
+ }
+ }
// a test-case to test this locally without executing the full TestAllFiles
@Test