aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2019-01-27 09:57:49 +0000
committerDominik Stadler <centic@apache.org>2019-01-27 09:57:49 +0000
commite8ed5e3a8c168ec1264add208cddf23f57998de5 (patch)
tree239ddd6acff95273713594e36c4abfbac853a1cd
parentc376d662f92577aa6226e454bb3eb5f113ec817d (diff)
downloadpoi-e8ed5e3a8c168ec1264add208cddf23f57998de5.tar.gz
poi-e8ed5e3a8c168ec1264add208cddf23f57998de5.zip
Adjust handling of some exceptions in regression tests
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1852278 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/integrationtest/org/apache/poi/BaseIntegrationTest.java62
-rw-r--r--src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java2
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java15
-rw-r--r--src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java9
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java3
5 files changed, 61 insertions, 30 deletions
diff --git a/src/integrationtest/org/apache/poi/BaseIntegrationTest.java b/src/integrationtest/org/apache/poi/BaseIntegrationTest.java
index 304794b231..4b833d99bd 100644
--- a/src/integrationtest/org/apache/poi/BaseIntegrationTest.java
+++ b/src/integrationtest/org/apache/poi/BaseIntegrationTest.java
@@ -51,30 +51,33 @@ public class BaseIntegrationTest {
File inputFile = new File(rootDir, file);
try {
- handleFile(inputFile);
+ try {
+ handleFile(inputFile);
+ } catch (IllegalArgumentException e) {
+ handleWrongFileExtension(inputFile, e);
+ return;
+ }
} catch (OfficeXmlFileException e) {
// check if the file-extension is wrong
if(!e.getMessage().contains("data appears to be in the Office 2007")) {
- throw e;
+ // use XWPF instead of HWPF and XSSF instead of HSSF as the file seems to have the wrong extension
+ handleWrongOLE2XMLExtension(inputFile, e);
+ return;
}
- // use XWPF instead of HWPF and XSSF instead of HSSF as the file seems to have the wrong extension
- handleWrongExtension(inputFile, e);
+ throw e;
} catch (OldFileFormatException e) {
if (e.getClass().equals(OldFileFormatException.class)) {
// Not even text extraction is supported for these: handler.handleExtracting(inputFile);
- //noinspection ConstantConditions
Assume.assumeFalse("File " + file + " excluded because it is unsupported old Excel format", true);
}
// otherwise try at least to perform extracting tests on these old files
} catch (EncryptedDocumentException e) {
// Do not try to read encrypted files
- //noinspection ConstantConditions
Assume.assumeFalse("File " + file + " excluded because it is password-encrypted", true);
} catch (ZipException e) {
// some files are corrupted
- if (e.getMessage().equals("unexpected EOF")) {
- //noinspection ConstantConditions
+ if (e.getMessage().equals("unexpected EOF") || e.getMessage().equals("Truncated ZIP file")) {
Assume.assumeFalse("File " + file + " excluded because the Zip file is incomplete", true);
}
@@ -82,37 +85,58 @@ public class BaseIntegrationTest {
} catch (IOException e) {
// sometimes binary format has XML-format-extension...
if(e.getMessage().contains("rong file format or file extension for OO XML file")) {
- handleWrongExtension(inputFile, e);
- } else {
- throw e;
+ handleWrongOLE2XMLExtension(inputFile, e);
+ return;
}
+
+ throw e;
} catch (IllegalArgumentException e) {
// ignore errors for documents with incorrect extension
String message = e.getMessage();
if(message != null && (message.equals("The document is really a RTF file") ||
message.equals("The document is really a PDF file") ||
message.equals("The document is really a HTML file"))) {
- //noinspection ConstantConditions
- Assume.assumeFalse("File " + file + " excluded because it is actually a PDF/RTF file", true);
+ Assume.assumeFalse("File " + file + " excluded because it is actually a PDF/RTF/HTML file", true);
}
- if(e.getMessage().equals("The document is really a OOXML file")) {
- handleWrongExtension(inputFile, e);
- } else {
- throw e;
+ if(message != null && message.equals("The document is really a OOXML file")) {
+ handleWrongOLE2XMLExtension(inputFile, e);
+ return;
}
+
+ throw e;
}
try {
handler.handleExtracting(inputFile);
} catch (EncryptedDocumentException e) {
// Do not try to read encrypted files
- //noinspection ConstantConditions
Assume.assumeFalse("File " + file + " excluded because it is password-encrypted", true);
}
}
- void handleWrongExtension(File inputFile, Exception e) throws Exception {
+ private void handleWrongFileExtension(File inputFile, IllegalArgumentException e) throws Exception {
+ // we sometimes have wrong extensions, so for some exceptions we try to handle it
+ // with the correct FileHandler instead
+ String message = e.getMessage();
+ if(message != null && (message.equals("The document is really a XLS file"))) {
+ handler = TestAllFiles.HANDLERS.get(".xls");
+ handleFile(inputFile);
+ } else if(message != null && (message.equals("The document is really a PPT file"))) {
+ handler = TestAllFiles.HANDLERS.get(".ppt");
+ handleFile(inputFile);
+ } else if(message != null && (message.equals("The document is really a DOC file"))) {
+ handler = TestAllFiles.HANDLERS.get(".doc");
+ handleFile(inputFile);
+ } else if(message != null && (message.equals("The document is really a VSD file"))) {
+ handler = TestAllFiles.HANDLERS.get(".vsd");
+ handleFile(inputFile);
+ }
+
+ throw e;
+ }
+
+ void handleWrongOLE2XMLExtension(File inputFile, Exception e) throws Exception {
// use XWPF instead of HWPF and XSSF instead of HSSF as the file seems to have the wrong extension
if (handler instanceof HWPFFileHandler) {
handler = TestAllFiles.HANDLERS.get(".docx");
diff --git a/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java b/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
index 4561221b8d..e056e49513 100644
--- a/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
+++ b/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
@@ -138,7 +138,7 @@ public class OldExcelExtractor implements Closeable {
DocumentNode book;
try {
book = (DocumentNode)directory.getEntry(OLD_WORKBOOK_DIR_ENTRY_NAME);
- } catch (FileNotFoundException e) {
+ } catch (FileNotFoundException | IllegalArgumentException e) {
// some files have "Workbook" instead
book = (DocumentNode)directory.getEntry(WORKBOOK_DIR_ENTRY_NAMES[0]);
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
index c7019cfe38..1f3485d8e4 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
@@ -266,21 +266,20 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
}
// check for an encrypted .xlsx file - they get OLE2 wrapped
- try {
- directory.getEntry(Decryptor.DEFAULT_POIFS_ENTRY);
+ if(directory.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
throw new EncryptedDocumentException("The supplied spreadsheet seems to be an Encrypted .xlsx file. " +
"It must be decrypted before use by XSSF, it cannot be used by HSSF");
- } catch (FileNotFoundException e) {
- // fall through
}
// check for previous version of file format
- try {
- directory.getEntry(OLD_WORKBOOK_DIR_ENTRY_NAME);
+ if(directory.hasEntry(OLD_WORKBOOK_DIR_ENTRY_NAME)) {
throw new OldExcelFormatException("The supplied spreadsheet seems to be Excel 5.0/7.0 (BIFF5) format. "
+ "POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003)");
- } catch (FileNotFoundException e) {
- // fall through
+ }
+
+ // throw more useful exceptions for known wrong file-extensions
+ if(directory.hasEntry("WordDocument")) {
+ throw new IllegalArgumentException("The document is really a DOC file");
}
throw new IllegalArgumentException("The supplied POIFSFileSystem does not contain a BIFF8 'Workbook' entry. "
diff --git a/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java b/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java
index 69acd476ee..84b2de913d 100644
--- a/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java
+++ b/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java
@@ -328,6 +328,15 @@ public class DirectoryNode
rval = _byname.get(name);
}
if (rval == null) {
+ // throw more useful exceptions for known wrong file-extensions
+ if(_byname.containsKey("Workbook")) {
+ throw new IllegalArgumentException("The document is really a XLS file");
+ } else if(_byname.containsKey("PowerPoint Document")) {
+ throw new IllegalArgumentException("The document is really a PPT file");
+ } else if(_byname.containsKey("VisioDocument")) {
+ throw new IllegalArgumentException("The document is really a VSD file");
+ }
+
// either a null name was given, or there is no such name
throw new FileNotFoundException("no such entry: \"" + name
+ "\", had: " + _byname.keySet());
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
index 1beb8ff11d..e2fc485bf0 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
@@ -33,7 +33,6 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
@@ -923,7 +922,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
* Problems with extracting check boxes from
* HSSFObjectData
*/
- @Test(expected = FileNotFoundException.class)
+ @Test(expected = IllegalArgumentException.class)
public void bug44840() throws Exception {
HSSFWorkbook wb = openSample("WithCheckBoxes.xls");