@Test
public void testAllFiles() throws Exception {
assertNotNull("Unknown file extension for file: " + file + ": " + getExtension(file), handler);
- InputStream stream = new BufferedInputStream(new FileInputStream(new File(ROOT_DIR, file)),100);
+ File inputFile = new File(ROOT_DIR, file);
+
try {
- handler.handleFile(stream);
-
- assertFalse("Expected to fail for file " + file + " and handler " + handler + ", but did not fail!",
- EXPECTED_FAILURES.contains(file));
- } catch (Exception e) {
- // check if we expect failure for this file
- if(!EXPECTED_FAILURES.contains(file)) {
- throw new Exception("While handling " + file, e);
- }
- } finally {
- stream.close();
- }
+ InputStream stream = new BufferedInputStream(new FileInputStream(inputFile),100);
+ try {
+ handler.handleFile(stream);
+
+ assertFalse("Expected to fail for file " + file + " and handler " + handler + ", but did not fail!",
+ EXPECTED_FAILURES.contains(file));
+ } finally {
+ stream.close();
+ }
+
+ handler.handleExtracting(inputFile);
+ } catch (Exception e) {
+ // check if we expect failure for this file
+ if(!EXPECTED_FAILURES.contains(file) && !AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.contains(file)) {
+ throw new Exception("While handling " + file, e);
+ }
+ }
}
private static String getExtension(String file) {
@Override
public void handleFile(InputStream stream) throws Exception {
}
+
+ @Override
+ public void handleExtracting(File file) throws Exception {
+ }
}
}
--- /dev/null
+package org.apache.poi.stress;\r
+\r
+import static org.junit.Assert.assertFalse;\r
+import static org.junit.Assert.assertNotNull;\r
+\r
+import java.io.File;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import org.apache.poi.POITextExtractor;\r
+import org.apache.poi.extractor.ExtractorFactory;\r
+\r
+public abstract class AbstractFileHandler implements FileHandler {\r
+ public static final Set<String> EXPECTED_EXTRACTOR_FAILURES = new HashSet<String>();\r
+ static {\r
+ // password protected files\r
+ EXPECTED_EXTRACTOR_FAILURES.add("document/bug53475-password-is-pass.docx");\r
+ EXPECTED_EXTRACTOR_FAILURES.add("poifs/extenxls_pwd123.xlsx");\r
+ EXPECTED_EXTRACTOR_FAILURES.add("poifs/protect.xlsx");\r
+ EXPECTED_EXTRACTOR_FAILURES.add("poifs/protected_agile.docx");\r
+ EXPECTED_EXTRACTOR_FAILURES.add("poifs/protected_sha512.xlsx");\r
+ \r
+ // unsupported file-types, no supported OLE2 parts\r
+ EXPECTED_EXTRACTOR_FAILURES.add("hmef/quick-winmail.dat");\r
+ EXPECTED_EXTRACTOR_FAILURES.add("hmef/winmail-sample1.dat");\r
+ EXPECTED_EXTRACTOR_FAILURES.add("hmef/bug52400-winmail-simple.dat");\r
+ EXPECTED_EXTRACTOR_FAILURES.add("hmef/bug52400-winmail-with-attachments.dat");\r
+ EXPECTED_EXTRACTOR_FAILURES.add("hpsf/Test0313rur.adm");\r
+ EXPECTED_EXTRACTOR_FAILURES.add("hsmf/attachment_msg_pdf.msg");\r
+ EXPECTED_EXTRACTOR_FAILURES.add("poifs/Notes.ole2");\r
+ EXPECTED_EXTRACTOR_FAILURES.add("slideshow/testPPT.thmx");\r
+ }\r
+\r
+ public void handleExtracting(File file) throws Exception {\r
+ POITextExtractor extractor = ExtractorFactory.createExtractor(file);\r
+ try {\r
+ assertNotNull(extractor);\r
+\r
+ assertNotNull(extractor.getText());\r
+ \r
+ // also try metadata\r
+ POITextExtractor metadataExtractor = extractor.getMetadataTextExtractor();\r
+ assertNotNull(metadataExtractor.getText());\r
+\r
+ assertFalse("Expected Extraction to fail for file " + file + " and handler " + this + ", but did not fail!", \r
+ EXPECTED_EXTRACTOR_FAILURES.contains(file));\r
+ } catch (IllegalArgumentException e) {\r
+ if(!EXPECTED_EXTRACTOR_FAILURES.contains(file)) {\r
+ throw new Exception("While handling " + file, e);\r
+ }\r
+ } finally {\r
+ extractor.close();\r
+ }\r
+ }\r
+}\r
==================================================================== */
package org.apache.poi.stress;
+import java.io.File;
import java.io.InputStream;
/**
* @throws Exception
*/
void handleFile(InputStream stream) throws Exception;
+
+ /**
+ * Ensures that extracting text from the given file
+ * is returning some text.
+ */
+ void handleExtracting(File file) throws Exception;
}
import org.apache.poi.hmef.attribute.MAPIStringAttribute;
import org.junit.Test;
-public class HMEFFileHandler implements FileHandler {
+public class HMEFFileHandler extends AbstractFileHandler {
@Override
public void handleFile(InputStream stream) throws Exception {
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.junit.Test;
-public class HPSFFileHandler implements FileHandler {
+public class HPSFFileHandler extends AbstractFileHandler {
@Override
public void handleFile(InputStream stream) throws Exception {
HPSFPropertiesOnlyDocument hpsf = new HPSFPropertiesOnlyDocument(new POIFSFileSystem(stream));
==================================================================== */
package org.apache.poi.stress;
+import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
stream.close();
}
}
+
+ // a test-case to test this locally without executing the full TestAllFiles
+ @Test
+ public void testExtractor() throws Exception {
+ handleExtracting(new File("test-data/spreadsheet/BOOK_in_capitals.xls"));
+ }
}
\ No newline at end of file
import org.apache.poi.POIDocument;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
-public class POIFSFileHandler implements FileHandler {
+public class POIFSFileHandler extends AbstractFileHandler {
@Override
public void handleFile(InputStream stream) throws Exception {
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
-public abstract class SpreadsheetHandler implements FileHandler {
+public abstract class SpreadsheetHandler extends AbstractFileHandler {
public void handleWorkbook(Workbook wb, String extension) throws IOException {
// try to access some of the content
readContent(wb);
import org.apache.poi.xslf.XSLFSlideShow;
import org.junit.Test;
-public class XSLFFileHandler implements FileHandler {
+public class XSLFFileHandler extends AbstractFileHandler {
@Override
public void handleFile(InputStream stream) throws Exception {
// ignore password protected files
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.Test;
-public class XWPFFileHandler implements FileHandler {
+public class XWPFFileHandler extends AbstractFileHandler {
@Override
public void handleFile(InputStream stream) throws Exception {
// ignore password protected files
{
// Look for certain entries in the stream, to figure it
// out from
- if (poifsDir.hasEntry("Workbook")) {
+ if (poifsDir.hasEntry("Workbook") ||
+ // some XLS files have different entry-names
+ poifsDir.hasEntry("WORKBOOK") || poifsDir.hasEntry("BOOK")) {
if (getPreferEventExtractor()) {
return new EventBasedExcelExtractor(poifsDir);
}
}
POIXMLTextExtractor extractor =
new XSSFExcelExtractor(args[0]);
- System.out.println(extractor.getText());
+ try {
+ System.out.println(extractor.getText());
+ } finally {
+ extractor.close();
+ }
}
/**
if (type == Cell.CELL_TYPE_NUMERIC) {
CellStyle cs = cell.getCellStyle();
- if (cs.getDataFormatString() != null) {
+ if (cs != null && cs.getDataFormatString() != null) {
text.append(formatter.formatRawCellContents(
cell.getNumericCellValue(), cs.getDataFormat(), cs.getDataFormatString()
));