import org.apache.poi.stress.XSSFFileHandler;
import org.apache.poi.stress.XWPFFileHandler;
import org.apache.tools.ant.DirectoryScanner;
+import org.junit.AssumptionViolatedException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
*/
@RunWith(Parameterized.class)
public class TestAllFiles {
-
private static final File ROOT_DIR = new File("test-data");
static final String[] SCAN_EXCLUDES = new String[] { "**/.svn/**", "lost+found" };
HANDLERS.put("spreadsheet/test_properties1", new NullFileHandler());
}
- private static Set<String> unmodifiableHashSet(String... a) {
+ private static final Set<String> unmodifiableHashSet(String... a) {
return Collections.unmodifiableSet(hashSet(a));
}
- private static Set<String> hashSet(String... a) {
+ private static final Set<String> hashSet(String... a) {
return new HashSet<String>(Arrays.asList(a));
}
// Old Word Documents where we can at least extract some text
- private static final Set<String> OLD_FILES = unmodifiableHashSet(
+ private static final Set<String> OLD_FILES_HWPF = unmodifiableHashSet(
"document/Bug49933.doc",
"document/Bug51944.doc",
"document/Word6.doc",
"document/Bug60942.doc",
"document/Bug60942b.doc",
"hpsf/TestMickey.doc",
- "document/52117.doc"
+ "document/52117.doc",
+ "hpsf/TestInvertedClassID.doc",
+ "hpsf/TestBug52117.doc"
);
private static final Set<String> EXPECTED_FAILURES = unmodifiableHashSet(
// OPC handler works / XSSF handler fails
"spreadsheet/57181.xlsm"
);
-
+
@Parameters(name="{index}: {0} using {1}")
public static Iterable<Object[]> files() {
DirectoryScanner scanner = new DirectoryScanner();
handler instanceof XWPFFileHandler ||
handler instanceof XSLFFileHandler ||
handler instanceof XDGFFileHandler) {
- files.add(new Object[] { file, HANDLERS.get(".ooxml") });
+ files.add(new Object[] { file, new OPCFileHandler() });
+ }
+
+ if (handler instanceof HSSFFileHandler ||
+ handler instanceof HSLFFileHandler ||
+ handler instanceof HWPFFileHandler ||
+ handler instanceof HDGFFileHandler) {
+ files.add(new Object[] { file, new HPSFFileHandler() });
}
}
@Parameter(value=1)
public FileHandler handler;
-
+
@Test
public void testAllFiles() throws Exception {
System.out.println("Reading " + file + " with " + handler.getClass());
assertNotNull("Unknown file extension for file: " + file + ": " + getExtension(file), handler);
File inputFile = new File(ROOT_DIR, file);
+ // special cases where docx-handling breaks, but OPCPackage handling works
+ boolean ignoredOPC = (file.endsWith(".docx") || file.endsWith(".xlsx") ||
+ file.endsWith(".xlsb") || file.endsWith(".pptx")) &&
+ handler instanceof OPCFileHandler;
+ boolean ignoreHPSF = (handler instanceof HPSFFileHandler);
+
+
try {
InputStream stream = new BufferedInputStream(new FileInputStream(inputFile), 64*1024);
try {
- handler.handleFile(stream);
-
+ handler.handleFile(stream, file);
assertFalse("Expected to fail for file " + file + " and handler " + handler + ", but did not fail!",
- OLD_FILES.contains(file));
+ OLD_FILES_HWPF.contains(file) && !ignoreHPSF);
} finally {
stream.close();
}
handler.handleExtracting(inputFile);
- // special cases where docx-handling breaks, but OPCPackage handling works
- boolean ignoredOPC = (file.endsWith(".docx") || file.endsWith(".xlsx") ||
- file.endsWith(".xlsb") || file.endsWith(".pptx")) &&
- handler instanceof OPCFileHandler;
-
assertFalse("Expected to fail for file " + file + " and handler " + handler + ", but did not fail!",
- EXPECTED_FAILURES.contains(file) && !ignoredOPC);
+ EXPECTED_FAILURES.contains(file) && !ignoredOPC && !ignoreHPSF);
} catch (OldFileFormatException e) {
// for old word files we should still support extracting text
- if(OLD_FILES.contains(file)) {
+ if(OLD_FILES_HWPF.contains(file)) {
handler.handleExtracting(inputFile);
} else {
// check if we expect failure for this file
throw new Exception("While handling " + file, e);
}
}
+ } catch (AssumptionViolatedException e) {
+ // file handler ignored this file
} catch (Exception e) {
// check if we expect failure for this file
if(!EXPECTED_FAILURES.contains(file) && !AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.contains(file)) {
private static class NullFileHandler implements FileHandler {
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
}
@Override
* Closing is handled by the framework outside this call.
*
* @param stream The input stream to read the file from.
+ * @param path the relative path to the file
* @throws Exception If an error happens in the file-specific handler
*/
- void handleFile(InputStream stream) throws Exception;
+ void handleFile(InputStream stream, String path) throws Exception;
/**
* Ensures that extracting text from the given file
public class HDGFFileHandler extends POIFSFileHandler {
@Override
- public void handleFile(InputStream stream) throws IOException {
+ public void handleFile(InputStream stream, String path) throws IOException {
POIFSFileSystem poifs = new POIFSFileSystem(stream);
HDGFDiagram diagram = new HDGFDiagram(poifs);
Stream[] topLevelStreams = diagram.getTopLevelStreams();
@Override
@Test
public void test() throws Exception {
- File file = new File("test-data/diagram/44501.vsd");
+ File file = new File("test-data/diagram/44501.vsd");
- InputStream stream = new FileInputStream(file);
+ InputStream stream = new FileInputStream(file);
try {
- handleFile(stream);
+ handleFile(stream, file.getPath());
} finally {
stream.close();
}
public class HMEFFileHandler extends AbstractFileHandler {
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
HMEFMessage msg = new HMEFMessage(stream);
// list all properties
// a test-case to test this locally without executing the full TestAllFiles
@Test
public void test() throws Exception {
- InputStream stream = new FileInputStream("test-data/hmef/quick-winmail.dat");
+ String path = "test-data/hmef/quick-winmail.dat";
+ InputStream stream = new FileInputStream(path);
try {
- handleFile(stream);
+ handleFile(stream, path);
} finally {
stream.close();
}
public class HPBFFileHandler extends POIFSFileHandler {
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
HPBFDocument pub = new HPBFDocument(new POIFSFileSystem(stream));
assertNotNull(pub.getEscherDelayStm());
assertNotNull(pub.getMainContents());
assertNotNull(pub.getQuillContents());
// writing is not yet implemented... handlePOIDocument(pub);
+ pub.close();
}
// a test-case to test this locally without executing the full TestAllFiles
@Override
@Test
public void test() throws Exception {
- File file = new File("test-data/publisher/SampleBrochure.pub");
+ File file = new File("test-data/publisher/SampleBrochure.pub");
- InputStream stream = new FileInputStream(file);
+ InputStream stream = new FileInputStream(file);
try {
- handleFile(stream);
+ handleFile(stream, file.getPath());
} finally {
stream.close();
}
public class HSLFFileHandler extends SlideShowHandler {
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
HSLFSlideShowImpl slide = new HSLFSlideShowImpl(stream);
assertNotNull(slide.getCurrentUserAtom());
assertNotNull(slide.getEmbeddedObjects());
assertNotNull("Found a record which was null", record);
assertTrue(record.getRecordType() >= 0);
}
-
+
handlePOIDocument(slide);
-
+
HSLFSlideShow ss = new HSLFSlideShow(slide);
handleSlideShow(ss);
}
-
+
@Test
public void testOne() throws Exception {
testOneFile(new File("test-data/slideshow/54880_chinese.ppt"));
//System.setProperty("org.apache.poi.util.POILogger", "org.apache.poi.util.SystemOutLogger");
InputStream stream = new FileInputStream(file);
try {
- handleFile(stream);
- } finally {
- stream.close();
- }
+ handleFile(stream, file.getPath());
+ } finally {
+ stream.close();
+ }
handleExtracting(file);
}
System.setProperty("org.apache.poi.util.POILogger", "org.apache.poi.util.SystemOutLogger");
InputStream stream = new FileInputStream(args[0]);
try {
- new HSLFFileHandler().handleFile(stream);
+ new HSLFFileHandler().handleFile(stream, args[0]);
} finally {
stream.close();
}
public class HSMFFileHandler extends POIFSFileHandler {
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
MAPIMessage mapi = new MAPIMessage(stream);
assertNotNull(mapi.getAttachmentFiles());
assertNotNull(mapi.getDisplayBCC());
*/
// writing is not yet supported... handlePOIDocument(mapi);
+
+ mapi.close();
}
// private void writeToFile(MAPIMessage mapi, File file)
@Override
@Test
public void test() throws Exception {
- File file = new File("test-data/hsmf/logsat.com_signatures_valid.msg");
+ File file = new File("test-data/hsmf/logsat.com_signatures_valid.msg");
InputStream stream = new FileInputStream(file);
try {
- handleFile(stream);
+ handleFile(stream, file.getPath());
} finally {
stream.close();
}
public class HSSFFileHandler extends SpreadsheetHandler {
private final POIFSFileHandler delegate = new POIFSFileHandler();
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
HSSFWorkbook wb = new HSSFWorkbook(stream);
handleWorkbook(wb);
// a test-case to test this locally without executing the full TestAllFiles
@Test
public void test() throws Exception {
- File file = new File("test-data/spreadsheet/49219.xls");
-
+ File file = new File("test-data/spreadsheet/49219.xls");
+
InputStream stream = new FileInputStream(file);
try {
- handleFile(stream);
+ handleFile(stream, file.getPath());
} finally {
stream.close();
}
- handleExtracting(file);
}
+
+ // 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
public class HWPFFileHandler extends POIFSFileHandler {
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
HWPFDocument doc = new HWPFDocument(stream);
assertNotNull(doc.getBookmarks());
assertNotNull(doc.getCharacterTable());
@Override
@Test
public void test() throws Exception {
- File file = new File("test-data/document/52117.doc");
+ File file = new File("test-data/document/52117.doc");
- InputStream stream = new FileInputStream(file);
+ InputStream stream = new FileInputStream(file);
try {
- handleFile(stream);
+ handleFile(stream, file.getPath());
} finally {
stream.close();
}
public class OPCFileHandler extends AbstractFileHandler {
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
// ignore password protected files
if (POIXMLDocumentHandler.isEncrypted(stream)) return;
// a test-case to test this locally without executing the full TestAllFiles
@Test
public void test() throws Exception {
- File file = new File("test-data/diagram/test.vsdx");
+ File file = new File("test-data/diagram/test.vsdx");
- InputStream stream = new PushbackInputStream(new FileInputStream(file), 100000);
+ InputStream stream = new PushbackInputStream(new FileInputStream(file), 100000);
try {
- handleFile(stream);
+ handleFile(stream, file.getPath());
} finally {
stream.close();
}
public class POIFSFileHandler extends AbstractFileHandler {
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(stream);
try {
handlePOIFSFileSystem(fs);
InputStream stream = new FileInputStream(file);
try {
- handleFile(stream);
+ handleFile(stream, file.getPath());
} finally {
stream.close();
}
public class XDGFFileHandler extends AbstractFileHandler {
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
// ignore password protected files
if (POIXMLDocumentHandler.isEncrypted(stream)) return;
public class XSLFFileHandler extends SlideShowHandler {
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
XMLSlideShow slide = new XMLSlideShow(stream);
XSLFSlideShow slideInner = new XSLFSlideShow(slide.getPackage());
assertNotNull(slideInner.getPresentation());
@Override
@Test
public void test() throws Exception {
- File file = new File("test-data/slideshow/ae.ac.uaeu.faculty_nafaachbili_GeomLec1.pptx");
- InputStream stream = new FileInputStream(file);
+ File file = new File("test-data/slideshow/ae.ac.uaeu.faculty_nafaachbili_GeomLec1.pptx");
+ InputStream stream = new FileInputStream(file);
try {
- handleFile(stream);
+ handleFile(stream, file.getPath());
} finally {
stream.close();
}
- handleExtracting(file);
- }
-}
+ handleExtracting(file);
+ }
+}
\ No newline at end of file
}
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(stream, out);
private void testNotHandledByWorkbookException(OPCPackage pkg) throws IOException {
try {
- new XSSFWorkbook(pkg);
+ new XSSFWorkbook(pkg).close();
} catch (XLSBUnsupportedException e) {
//this is what we'd expect
//swallow
==================================================================== */
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.OutputStream;
+import java.io.PrintStream;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Locale;
+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.junit.Test;
import org.xml.sax.SAXException;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.TransformerException;
-import java.io.*;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Locale;
-import java.util.Set;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-
public class XSSFFileHandler extends SpreadsheetHandler {
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
// ignore password protected files
if (POIXMLDocumentHandler.isEncrypted(stream)) return;
// make sure the potentially large byte-array is freed up quickly again
{
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- IOUtils.copy(stream, out);
- final byte[] bytes = out.toByteArray();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ IOUtils.copy(stream, out);
+ final byte[] bytes = out.toByteArray();
- checkXSSFReader(OPCPackage.open(new ByteArrayInputStream(bytes)));
+ checkXSSFReader(OPCPackage.open(new ByteArrayInputStream(bytes)));
- wb = new XSSFWorkbook(new ByteArrayInputStream(bytes));
+ wb = new XSSFWorkbook(new ByteArrayInputStream(bytes));
}
// use the combined handler for HSSF/XSSF
// this allows to trigger a heap-dump at this point to see which memory is still allocated
//HeapDump.dumpHeap("/tmp/poi.hprof", false);
+
+ wb.close();
}
InputStream stream = new BufferedInputStream(new FileInputStream(file));
try {
- handleFile(stream);
+ handleFile(stream, file.getPath());
} finally {
stream.close();
}
public class XWPFFileHandler extends AbstractFileHandler {
@Override
- public void handleFile(InputStream stream) throws Exception {
+ public void handleFile(InputStream stream, String path) throws Exception {
// ignore password protected files
if (POIXMLDocumentHandler.isEncrypted(stream)) return;
// a test-case to test this locally without executing the full TestAllFiles
@Test
public void test() throws Exception {
- File file = new File("test-data/document/51921-Word-Crash067.docx");
+ File file = new File("test-data/document/51921-Word-Crash067.docx");
- InputStream stream = new PushbackInputStream(new FileInputStream(file), 100000);
+ InputStream stream = new PushbackInputStream(new FileInputStream(file), 100000);
try {
- handleFile(stream);
+ handleFile(stream, file.getPath());
} finally {
stream.close();
}