60826 -- update stress tests to handle xlsb

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1787264 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tim Allison 2017-03-16 22:32:24 +00:00
parent 730f394261
commit 60b4624cf7
3 changed files with 144 additions and 20 deletions

View File

@ -17,7 +17,40 @@
package org.apache.poi;
import org.apache.poi.stress.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.apache.poi.stress.AbstractFileHandler;
import org.apache.poi.stress.FileHandler;
import org.apache.poi.stress.HDGFFileHandler;
import org.apache.poi.stress.HMEFFileHandler;
import org.apache.poi.stress.HPBFFileHandler;
import org.apache.poi.stress.HPSFFileHandler;
import org.apache.poi.stress.HSLFFileHandler;
import org.apache.poi.stress.HSMFFileHandler;
import org.apache.poi.stress.HSSFFileHandler;
import org.apache.poi.stress.HWPFFileHandler;
import org.apache.poi.stress.OPCFileHandler;
import org.apache.poi.stress.POIFSFileHandler;
import org.apache.poi.stress.XDGFFileHandler;
import org.apache.poi.stress.XSLFFileHandler;
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.Test;
import org.junit.runner.RunWith;
@ -25,15 +58,6 @@ import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
/**
* This is an integration test which performs various actions on all stored test-files and tries
* to reveal problems which are introduced, but not covered (yet) by unit tests.
@ -60,7 +84,8 @@ import static org.junit.Assert.assertNotNull;
*/
@RunWith(Parameterized.class)
public class TestAllFiles {
private static final File ROOT_DIR = new File("test-data");
private static final File ROOT_DIR = new File("C:/users/tallison/idea projects/poi-trunk/test-data");
static final String[] SCAN_EXCLUDES = new String[] { "**/.svn/**", "lost+found" };
@ -72,7 +97,7 @@ public class TestAllFiles {
HANDLERS.put(".xlsx", new XSSFFileHandler());
HANDLERS.put(".xlsm", new XSSFFileHandler());
HANDLERS.put(".xltx", new XSSFFileHandler());
HANDLERS.put(".xlsb", new XSSFFileHandler());
HANDLERS.put(".xlsb", new XSSFBFileHandler());
// Word
HANDLERS.put(".doc", new HWPFFileHandler());
@ -250,7 +275,6 @@ public class TestAllFiles {
"openxml4j/invalid.xlsx",
"spreadsheet/54764-2.xlsx", // see TestXSSFBugs.bug54764()
"spreadsheet/54764.xlsx", // see TestXSSFBugs.bug54764()
"spreadsheet/Simple.xlsb",
"poifs/unknown_properties.msg", // POIFS properties corrupted
"poifs/only-zero-byte-streams.ole2", // No actual contents
"spreadsheet/poc-xmlbomb.xlsx", // contains xml-entity-expansion

View File

@ -0,0 +1,96 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.stress;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.XLSBUnsupportedException;
import org.apache.poi.xssf.extractor.XSSFBEventBasedExcelExtractor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XSSFBFileHandler extends AbstractFileHandler {
static {
//this is a "Beta" xlsb version and is not openable with Excel 2016
//TODO: see if we can support this easily enough
AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.add(
"spreadsheet/Simple.xlsb"
);
}
@Override
public void handleFile(InputStream stream) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(stream, out);
final byte[] bytes = out.toByteArray();
OPCPackage opcPackage = OPCPackage.open(new ByteArrayInputStream(bytes));
try {
testOne(opcPackage);
} finally {
opcPackage.close();
}
testNotHandledByWorkbookException(OPCPackage.open(new ByteArrayInputStream(bytes)));
}
private void testNotHandledByWorkbookException(OPCPackage pkg) throws IOException {
try {
XSSFWorkbook workbook = new XSSFWorkbook(pkg);
} catch (XLSBUnsupportedException e) {
//this is what we'd expect
//swallow
}
}
@Override
public void handleExtracting(File file) throws Exception {
OPCPackage pkg = OPCPackage.open(file, PackageAccess.READ);
try {
testOne(pkg);
} finally {
pkg.close();
}
pkg = OPCPackage.open(file, PackageAccess.READ);
try {
testNotHandledByWorkbookException(pkg);
} finally {
pkg.close();
}
}
private void testOne(OPCPackage pkg) throws Exception {
XSSFBEventBasedExcelExtractor ex = new XSSFBEventBasedExcelExtractor(pkg);
String txt = ex.getText();
if (txt.length() < 1) {
throw new RuntimeException("Should have gotten some text.");
}
}
}

View File

@ -19,18 +19,24 @@ package org.apache.poi.stress;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import java.io.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
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.NotOfficeXmlFileException;
import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
@ -131,8 +137,6 @@ public class XSSFFileHandler extends SpreadsheetHandler {
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");