From 63780176d952875bb5618567f36b3392b8c6684d Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Fri, 1 Apr 2016 06:25:49 +0000 Subject: [PATCH] Enhance integration tests to call some of the common examples/devtools to also trigger these with all sample files, currently XLSX2CSV, BiffViewer, FromHowTo git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1737308 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 2 + .../poi/xssf/eventusermodel/XLSX2CSV.java | 8 +- .../org/apache/poi/TestAllFiles.java | 8 ++ .../poi/stress/AbstractFileHandler.java | 5 ++ .../org/apache/poi/stress/FileHandler.java | 6 ++ .../apache/poi/stress/HSSFFileHandler.java | 62 +++++++++++++- .../apache/poi/stress/XSSFFileHandler.java | 85 +++++++++++++++++-- 7 files changed, 162 insertions(+), 14 deletions(-) diff --git a/build.xml b/build.xml index 19f9c8b416..3008840fe7 100644 --- a/build.xml +++ b/build.xml @@ -334,6 +334,7 @@ under the License. + @@ -998,6 +999,7 @@ under the License. + diff --git a/src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java b/src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java index 8e0007edf2..11d14ae376 100644 --- a/src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java +++ b/src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java @@ -28,6 +28,7 @@ import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.ss.usermodel.DataFormatter; +import org.apache.poi.ss.util.CellAddress; import org.apache.poi.ss.util.CellReference; import org.apache.poi.util.SAXHelper; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler; @@ -106,7 +107,12 @@ public class XLSX2CSV { } else { output.append(','); } - + + // gracefully handle missing CellRef here in a similar way as XSSFCell does + if(cellReference == null) { + cellReference = new CellAddress(currentRow, currentCol).formatAsString(); + } + // Did we miss any cells? int thisCol = (new CellReference(cellReference)).getCol(); int missedCols = thisCol - currentCol - 1; diff --git a/src/integrationtest/org/apache/poi/TestAllFiles.java b/src/integrationtest/org/apache/poi/TestAllFiles.java index b7a098699f..42ef117a51 100644 --- a/src/integrationtest/org/apache/poi/TestAllFiles.java +++ b/src/integrationtest/org/apache/poi/TestAllFiles.java @@ -308,6 +308,7 @@ public class TestAllFiles { return files; } + @SuppressWarnings("DefaultAnnotationParam") @Parameter(value=0) public String file; @@ -356,6 +357,9 @@ public class TestAllFiles { throw new Exception("While handling " + file, e); } } + + // let some file handlers do additional stuff + handler.handleAdditional(inputFile); } static String getExtension(String file) { @@ -375,5 +379,9 @@ public class TestAllFiles { @Override public void handleExtracting(File file) throws Exception { } + + @Override + public void handleAdditional(File file) throws Exception { + } } } diff --git a/src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java b/src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java index eeccc0510b..65edbaae50 100644 --- a/src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java +++ b/src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java @@ -133,4 +133,9 @@ public abstract class AbstractFileHandler implements FileHandler { stream.close(); } } + + @Override + public void handleAdditional(File file) throws Exception { + // by default we do nothing here + } } diff --git a/src/integrationtest/org/apache/poi/stress/FileHandler.java b/src/integrationtest/org/apache/poi/stress/FileHandler.java index ce2991b0bc..8b65cfa478 100644 --- a/src/integrationtest/org/apache/poi/stress/FileHandler.java +++ b/src/integrationtest/org/apache/poi/stress/FileHandler.java @@ -41,4 +41,10 @@ public interface FileHandler { * is returning some text. */ void handleExtracting(File file) throws Exception; + + /** + * Allows to perform some additional work, e.g. run + * some of the example applications + */ + void handleAdditional(File file) throws Exception; } diff --git a/src/integrationtest/org/apache/poi/stress/HSSFFileHandler.java b/src/integrationtest/org/apache/poi/stress/HSSFFileHandler.java index 005dd1ade6..19a06ab635 100644 --- a/src/integrationtest/org/apache/poi/stress/HSSFFileHandler.java +++ b/src/integrationtest/org/apache/poi/stress/HSSFFileHandler.java @@ -16,13 +16,19 @@ ==================================================================== */ package org.apache.poi.stress; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; - +import org.apache.poi.EncryptedDocumentException; +import org.apache.poi.hssf.OldExcelFormatException; +import org.apache.poi.hssf.dev.BiffViewer; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.util.RecordFormatException; import org.junit.Test; +import java.io.*; +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertFalse; + public class HSSFFileHandler extends SpreadsheetHandler { private POIFSFileHandler delegate = new POIFSFileHandler(); @Override @@ -40,6 +46,54 @@ public class HSSFFileHandler extends SpreadsheetHandler { // TODO: still fails on some records... RecordsStresser.handleWorkbook(wb); } + private static final Set EXPECTED_ADDITIONAL_FAILURES = new HashSet(); + static { + // encrypted + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/35897-type4.xls"); + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/xor-encryption-abc.xls"); + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/password.xls"); + // broken files + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/43493.xls"); + // TODO: ok to ignore? + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/50833.xls"); + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/51832.xls"); + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/XRefCalc.xls"); + } + + @Override + public void handleAdditional(File file) throws Exception { + // redirect stdout as the examples often write lots of text + PrintStream oldOut = System.out; + try { + System.setOut(new PrintStream(new OutputStream() { + @Override + public void write(int b) throws IOException { + } + })); + + BiffViewer.main(new String[]{file.getAbsolutePath()}); + + assertFalse("Expected Extraction to fail for file " + file + " and handler " + this + ", but did not fail!", + EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())); + } catch (OldExcelFormatException e) { + // old excel formats are not supported here + } catch (EncryptedDocumentException e) { + if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) { + throw e; + } + } catch (RecordFormatException e) { + if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) { + throw e; + } + } catch (RuntimeException e) { + if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) { + throw e; + } + } finally { + System.setOut(oldOut); + } + } + // a test-case to test this locally without executing the full TestAllFiles @Test public void test() throws Exception { diff --git a/src/integrationtest/org/apache/poi/stress/XSSFFileHandler.java b/src/integrationtest/org/apache/poi/stress/XSSFFileHandler.java index 8dfa65fb2c..65e539635c 100644 --- a/src/integrationtest/org/apache/poi/stress/XSSFFileHandler.java +++ b/src/integrationtest/org/apache/poi/stress/XSSFFileHandler.java @@ -16,24 +16,26 @@ ==================================================================== */ package org.apache.poi.stress; +import static org.junit.Assert.assertFalse; 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.io.*; +import java.util.HashSet; import java.util.Iterator; +import java.util.Set; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; +import org.apache.poi.POIXMLException; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException; 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.XLSX2CSV; import org.apache.poi.xssf.eventusermodel.XSSFReader; +import org.apache.poi.xssf.eventusermodel.examples.FromHowTo; import org.apache.poi.xssf.extractor.XSSFExportToXml; import org.apache.poi.xssf.usermodel.XSSFMap; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @@ -111,7 +113,67 @@ public class XSSFFileHandler extends SpreadsheetHandler { exporter.exportToXML(os, true); } } - + + private static final Set EXPECTED_ADDITIONAL_FAILURES = new HashSet(); + static { + // expected sheet-id not found + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/52348.xlsx"); + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/59021.xlsx"); + // zip-bomb + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/54764.xlsx"); + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/54764-2.xlsx"); + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/54764.xlsx"); + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/poc-xmlbomb.xlsx"); + // strict OOXML + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/57914.xlsx"); + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/SampleSS.strict.xlsx"); + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/SimpleStrict.xlsx"); + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/sample.strict.xlsx"); + // binary format + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/Simple.xlsb"); + // TODO: good to ignore? + EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/sample-beta.xlsx"); + } + + @Override + public void handleAdditional(File file) throws Exception { + // redirect stdout as the examples often write lots of text + PrintStream oldOut = System.out; + try { + System.setOut(new PrintStream(new OutputStream() { + @Override + public void write(int b) throws IOException { + } + })); + FromHowTo.main(new String[]{file.getAbsolutePath()}); + XLSX2CSV.main(new String[]{file.getAbsolutePath()}); + + assertFalse("Expected Extraction to fail for file " + file + " and handler " + this + ", but did not fail!", + EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())); + + } catch (OLE2NotOfficeXmlFileException e) { + // we have some files that are not actually OOXML and thus cannot be tested here + } catch (IllegalArgumentException e) { + if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) { + throw e; + } + } catch (InvalidFormatException e) { + if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) { + throw e; + } + } catch (IOException e) { + if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) { + throw e; + } + } catch (POIXMLException e) { + if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) { + throw e; + } + } finally { + System.setOut(oldOut); + } + } + // a test-case to test this locally without executing the full TestAllFiles @Test public void test() throws Exception { @@ -128,4 +190,9 @@ public class XSSFFileHandler extends SpreadsheetHandler { public void testExtractor() throws Exception { handleExtracting(new File("test-data/spreadsheet/ref-56737.xlsx")); } -} \ No newline at end of file + + @Test + public void testAdditional() throws Exception { + handleAdditional(new File("test-data/spreadsheet/poc-xmlbomb.xlsx")); + } +} -- 2.39.5