mirror of https://github.com/apache/poi.git
IntegrationTest - move excludes to file handler
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1793595 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
06eb866034
commit
3388787d2c
|
@ -52,6 +52,7 @@ import org.apache.poi.stress.XSSFBFileHandler;
|
|||
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;
|
||||
|
@ -84,7 +85,6 @@ import org.junit.runners.Parameterized.Parameters;
|
|||
*/
|
||||
@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" };
|
||||
|
@ -202,15 +202,15 @@ public class TestAllFiles {
|
|||
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",
|
||||
|
@ -222,7 +222,9 @@ public class TestAllFiles {
|
|||
"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(
|
||||
|
@ -346,7 +348,14 @@ public class TestAllFiles {
|
|||
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() });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -366,29 +375,30 @@ public class TestAllFiles {
|
|||
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
|
||||
|
@ -397,6 +407,8 @@ public class TestAllFiles {
|
|||
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)) {
|
||||
|
@ -420,7 +432,7 @@ public class TestAllFiles {
|
|||
|
||||
private static class NullFileHandler implements FileHandler {
|
||||
@Override
|
||||
public void handleFile(InputStream stream) throws Exception {
|
||||
public void handleFile(InputStream stream, String path) throws Exception {
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -32,9 +32,10 @@ public interface FileHandler {
|
|||
* 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
|
||||
|
|
|
@ -33,7 +33,7 @@ import org.junit.Test;
|
|||
|
||||
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();
|
||||
|
@ -59,7 +59,7 @@ public class HDGFFileHandler extends POIFSFileHandler {
|
|||
|
||||
InputStream stream = new FileInputStream(file);
|
||||
try {
|
||||
handleFile(stream);
|
||||
handleFile(stream, file.getPath());
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ import org.junit.Test;
|
|||
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
|
||||
|
@ -50,9 +50,10 @@ public class HMEFFileHandler extends AbstractFileHandler {
|
|||
// 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();
|
||||
}
|
||||
|
|
|
@ -29,13 +29,14 @@ import org.junit.Test;
|
|||
|
||||
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
|
||||
|
@ -46,7 +47,7 @@ public class HPBFFileHandler extends POIFSFileHandler {
|
|||
|
||||
InputStream stream = new FileInputStream(file);
|
||||
try {
|
||||
handleFile(stream);
|
||||
handleFile(stream, file.getPath());
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ import org.junit.Test;
|
|||
|
||||
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());
|
||||
|
@ -81,7 +81,7 @@ public class HSLFFileHandler extends SlideShowHandler {
|
|||
//System.setProperty("org.apache.poi.util.POILogger", "org.apache.poi.util.SystemOutLogger");
|
||||
InputStream stream = new FileInputStream(file);
|
||||
try {
|
||||
handleFile(stream);
|
||||
handleFile(stream, file.getPath());
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ public class HSLFFileHandler extends SlideShowHandler {
|
|||
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();
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ import org.junit.Test;
|
|||
|
||||
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());
|
||||
|
@ -60,6 +60,8 @@ public class HSMFFileHandler extends POIFSFileHandler {
|
|||
*/
|
||||
|
||||
// writing is not yet supported... handlePOIDocument(mapi);
|
||||
|
||||
mapi.close();
|
||||
}
|
||||
|
||||
// private void writeToFile(MAPIMessage mapi, File file)
|
||||
|
@ -79,7 +81,7 @@ public class HSMFFileHandler extends POIFSFileHandler {
|
|||
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();
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ import static org.junit.Assert.assertFalse;
|
|||
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);
|
||||
|
||||
|
@ -104,10 +104,15 @@ public class HSSFFileHandler extends SpreadsheetHandler {
|
|||
|
||||
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"));
|
||||
}
|
||||
}
|
|
@ -28,7 +28,7 @@ import org.junit.Test;
|
|||
|
||||
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());
|
||||
|
@ -45,7 +45,7 @@ public class HWPFFileHandler extends POIFSFileHandler {
|
|||
|
||||
InputStream stream = new FileInputStream(file);
|
||||
try {
|
||||
handleFile(stream);
|
||||
handleFile(stream, file.getPath());
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ import org.junit.Test;
|
|||
|
||||
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;
|
||||
|
||||
|
@ -67,7 +67,7 @@ public class OPCFileHandler extends AbstractFileHandler {
|
|||
|
||||
InputStream stream = new PushbackInputStream(new FileInputStream(file), 100000);
|
||||
try {
|
||||
handleFile(stream);
|
||||
handleFile(stream, file.getPath());
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ import org.junit.Test;
|
|||
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);
|
||||
|
@ -80,7 +80,7 @@ public class POIFSFileHandler extends AbstractFileHandler {
|
|||
|
||||
InputStream stream = new FileInputStream(file);
|
||||
try {
|
||||
handleFile(stream);
|
||||
handleFile(stream, file.getPath());
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ import org.junit.Test;
|
|||
|
||||
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;
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ import org.junit.Test;
|
|||
|
||||
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());
|
||||
|
@ -72,7 +72,7 @@ public class XSLFFileHandler extends SlideShowHandler {
|
|||
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();
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ public class XSSFBFileHandler extends AbstractFileHandler {
|
|||
}
|
||||
|
||||
@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);
|
||||
|
||||
|
@ -54,7 +54,7 @@ public class XSSFBFileHandler extends AbstractFileHandler {
|
|||
|
||||
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
|
||||
|
|
|
@ -16,6 +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.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;
|
||||
|
@ -31,20 +51,9 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
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;
|
||||
|
||||
|
@ -76,6 +85,8 @@ public class XSSFFileHandler extends SpreadsheetHandler {
|
|||
|
||||
// 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();
|
||||
}
|
||||
|
||||
|
||||
|
@ -185,7 +196,7 @@ public class XSSFFileHandler extends SpreadsheetHandler {
|
|||
|
||||
InputStream stream = new BufferedInputStream(new FileInputStream(file));
|
||||
try {
|
||||
handleFile(stream);
|
||||
handleFile(stream, file.getPath());
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ import org.junit.Test;
|
|||
|
||||
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;
|
||||
|
||||
|
@ -42,7 +42,7 @@ public class XWPFFileHandler extends AbstractFileHandler {
|
|||
|
||||
InputStream stream = new PushbackInputStream(new FileInputStream(file), 100000);
|
||||
try {
|
||||
handleFile(stream);
|
||||
handleFile(stream, file.getPath());
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue