Search in sources :

Example 1 with HPSFFileHandler

use of org.apache.poi.stress.HPSFFileHandler in project poi by apache.

the class TestAllFiles method files.

@Parameters(name = "{index}: {0} using {1}")
public static Iterable<Object[]> files() {
    DirectoryScanner scanner = new DirectoryScanner();
    scanner.setBasedir(ROOT_DIR);
    scanner.setExcludes(SCAN_EXCLUDES);
    scanner.scan();
    System.out.println("Handling " + scanner.getIncludedFiles().length + " files");
    List<Object[]> files = new ArrayList<Object[]>();
    for (String file : scanner.getIncludedFiles()) {
        // ... failures/handlers lookup doesn't work on windows otherwise
        file = file.replace('\\', '/');
        if (IGNORED.contains(file)) {
            System.out.println("Ignoring " + file);
            continue;
        }
        FileHandler handler = HANDLERS.get(getExtension(file));
        files.add(new Object[] { file, handler });
        // for some file-types also run OPCFileHandler
        if (handler instanceof XSSFFileHandler || handler instanceof XWPFFileHandler || handler instanceof XSLFFileHandler || handler instanceof XDGFFileHandler) {
            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() });
        }
    }
    return files;
}
Also used : XDGFFileHandler(org.apache.poi.stress.XDGFFileHandler) XSSFFileHandler(org.apache.poi.stress.XSSFFileHandler) HPSFFileHandler(org.apache.poi.stress.HPSFFileHandler) ArrayList(java.util.ArrayList) XWPFFileHandler(org.apache.poi.stress.XWPFFileHandler) OPCFileHandler(org.apache.poi.stress.OPCFileHandler) POIFSFileHandler(org.apache.poi.stress.POIFSFileHandler) HSLFFileHandler(org.apache.poi.stress.HSLFFileHandler) HDGFFileHandler(org.apache.poi.stress.HDGFFileHandler) HMEFFileHandler(org.apache.poi.stress.HMEFFileHandler) AbstractFileHandler(org.apache.poi.stress.AbstractFileHandler) XSSFBFileHandler(org.apache.poi.stress.XSSFBFileHandler) HWPFFileHandler(org.apache.poi.stress.HWPFFileHandler) HSMFFileHandler(org.apache.poi.stress.HSMFFileHandler) XSLFFileHandler(org.apache.poi.stress.XSLFFileHandler) XWPFFileHandler(org.apache.poi.stress.XWPFFileHandler) FileHandler(org.apache.poi.stress.FileHandler) HSSFFileHandler(org.apache.poi.stress.HSSFFileHandler) HPBFFileHandler(org.apache.poi.stress.HPBFFileHandler) XDGFFileHandler(org.apache.poi.stress.XDGFFileHandler) XSSFFileHandler(org.apache.poi.stress.XSSFFileHandler) HPSFFileHandler(org.apache.poi.stress.HPSFFileHandler) OPCFileHandler(org.apache.poi.stress.OPCFileHandler) HSLFFileHandler(org.apache.poi.stress.HSLFFileHandler) HWPFFileHandler(org.apache.poi.stress.HWPFFileHandler) XSLFFileHandler(org.apache.poi.stress.XSLFFileHandler) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) HDGFFileHandler(org.apache.poi.stress.HDGFFileHandler) HSSFFileHandler(org.apache.poi.stress.HSSFFileHandler) Parameters(org.junit.runners.Parameterized.Parameters)

Example 2 with HPSFFileHandler

use of org.apache.poi.stress.HPSFFileHandler in project poi by apache.

the class TestAllFiles method testAllFiles.

@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, file);
            assertFalse("Expected to fail for file " + file + " and handler " + handler + ", but did not fail!", OLD_FILES_HWPF.contains(file) && !ignoreHPSF);
        } finally {
            stream.close();
        }
        handler.handleExtracting(inputFile);
        assertFalse("Expected to fail for file " + file + " and handler " + handler + ", but did not fail!", EXPECTED_FAILURES.contains(file) && !ignoredOPC && !ignoreHPSF);
    } catch (OldFileFormatException e) {
        // for old word files we should still support extracting text
        if (OLD_FILES_HWPF.contains(file)) {
            handler.handleExtracting(inputFile);
        } else {
            // check if we expect failure for this file
            if (!EXPECTED_FAILURES.contains(file) && !AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.contains(file)) {
                System.out.println("Failed: " + 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)) {
            System.out.println("Failed: " + file);
            throw new Exception("While handling " + file, e);
        }
    }
    // let some file handlers do additional stuff
    handler.handleAdditional(inputFile);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) AssumptionViolatedException(org.junit.AssumptionViolatedException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HPSFFileHandler(org.apache.poi.stress.HPSFFileHandler) File(java.io.File) FileInputStream(java.io.FileInputStream) AssumptionViolatedException(org.junit.AssumptionViolatedException) OPCFileHandler(org.apache.poi.stress.OPCFileHandler) Test(org.junit.Test)

Aggregations

HPSFFileHandler (org.apache.poi.stress.HPSFFileHandler)2 OPCFileHandler (org.apache.poi.stress.OPCFileHandler)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 AbstractFileHandler (org.apache.poi.stress.AbstractFileHandler)1 FileHandler (org.apache.poi.stress.FileHandler)1 HDGFFileHandler (org.apache.poi.stress.HDGFFileHandler)1 HMEFFileHandler (org.apache.poi.stress.HMEFFileHandler)1 HPBFFileHandler (org.apache.poi.stress.HPBFFileHandler)1 HSLFFileHandler (org.apache.poi.stress.HSLFFileHandler)1 HSMFFileHandler (org.apache.poi.stress.HSMFFileHandler)1 HSSFFileHandler (org.apache.poi.stress.HSSFFileHandler)1 HWPFFileHandler (org.apache.poi.stress.HWPFFileHandler)1 POIFSFileHandler (org.apache.poi.stress.POIFSFileHandler)1 XDGFFileHandler (org.apache.poi.stress.XDGFFileHandler)1 XSLFFileHandler (org.apache.poi.stress.XSLFFileHandler)1 XSSFBFileHandler (org.apache.poi.stress.XSSFBFileHandler)1