]> source.dussan.org Git - poi.git/commitdiff
Enhance integration tests to call some of the common examples/devtools to also trigg...
authorDominik Stadler <centic@apache.org>
Fri, 1 Apr 2016 06:25:49 +0000 (06:25 +0000)
committerDominik Stadler <centic@apache.org>
Fri, 1 Apr 2016 06:25:49 +0000 (06:25 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1737308 13f79535-47bb-0310-9956-ffa450edef68

build.xml
src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java
src/integrationtest/org/apache/poi/TestAllFiles.java
src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java
src/integrationtest/org/apache/poi/stress/FileHandler.java
src/integrationtest/org/apache/poi/stress/HSSFFileHandler.java
src/integrationtest/org/apache/poi/stress/XSSFFileHandler.java

index 19f9c8b4166a05d0ae79194fbc9acc09c11f4a25..3008840fe7ff70918dca87e9a87f74bdd954f11a 100644 (file)
--- a/build.xml
+++ b/build.xml
@@ -334,6 +334,7 @@ under the License.
         <pathelement location="${main.output.test.dir}"/>
         <pathelement location="${ooxml.output.dir}"/>
         <pathelement location="${integration.output.test.dir}"/>
+        <pathelement location="${examples.output.dir}"/>
     </path>
 
     <path id="ooxml-lite.classpath">
@@ -998,6 +999,7 @@ under the License.
                 <path refid="ooxml.classpath"/>
                 <pathelement location="${ooxml.output.dir}"/>
                 <pathelement location="${main.output.test.dir}"/>
+                <pathelement location="${examples.output.dir}"/>
                 <pathelement location="${main.ant.jar}"/>
             </classpath>
         </javac>
index 8e0007edf2269d9e7e553ff57b01afed6ba6b363..11d14ae37675a063878dbb5fc98a9ddf007994de 100644 (file)
@@ -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;
index b7a098699f74c8ae8047016cddded1c5484c7be2..42ef117a510c210f01d60450c4578dffaa212365 100644 (file)
@@ -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 {
+        }
     }
 }
index eeccc0510bf282689f8878077d1a5f97068dbc1f..65edbaae507f3a8ccbd87783e7599eefa1d7bfe6 100644 (file)
@@ -133,4 +133,9 @@ public abstract class AbstractFileHandler implements FileHandler {
             stream.close();\r
         }\r
     }\r
+\r
+    @Override\r
+    public void handleAdditional(File file) throws Exception {\r
+        // by default we do nothing here\r
+    }\r
 }\r
index ce2991b0bc3b8e7f9a9cd93901e79fde87c08cd6..8b65cfa478c626fff3cc0f077b5236f025e2a4ba 100644 (file)
@@ -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;
 }
index 005dd1ade6e245ce77fc517ba4175ce6ca65db22..19a06ab6357e4eca3a607c2bb37dca50f81be317 100644 (file)
 ==================================================================== */
 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<String> EXPECTED_ADDITIONAL_FAILURES = new HashSet<String>();
+       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 {
index 8dfa65fb2c576f27c47f9a34ff13e1094ed3ed9d..65e539635c6da10b06d11f16f992d3c87b458d65 100644 (file)
 ==================================================================== */
 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<String> EXPECTED_ADDITIONAL_FAILURES = new HashSet<String>();
+    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"));
+    }
+}