From: PJ Fanning Date: Sun, 10 Oct 2021 09:58:02 +0000 (+0000) Subject: [bug-65452] fix issue where WorkbookFactory.create(File, ...) returns null if file... X-Git-Tag: REL_5_2_0~410 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2514e4d0ab925a4de8baa4ed4bcdbb90ca2713ab;p=poi.git [bug-65452] fix issue where WorkbookFactory.create(File, ...) returns null if file type not recognised git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894097 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index ae0770f555..e65c92e48e 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -3623,4 +3623,25 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { } } } -} + + @Test + void testBug65452() throws IOException { + File file = XSSFTestDataSamples.getSampleFile("workbook.xml"); + try (FileInputStream fis = new FileInputStream(file)) { + try { + Workbook wb = WorkbookFactory.create(fis); + if (wb != null) wb.close(); + fail("WorkbookFactory.create should have failed"); + } catch (IOException ie) { + assertEquals("Can't open workbook - unsupported file type: XML", ie.getMessage()); + } + } + try { + Workbook wb = WorkbookFactory.create(file); + if (wb != null) wb.close(); + fail("WorkbookFactory.create should have failed"); + } catch (IOException ie) { + assertEquals("Can't open workbook - unsupported file type: XML", ie.getMessage()); + } + } +} \ No newline at end of file diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/poi/src/main/java/org/apache/poi/ss/usermodel/WorkbookFactory.java index 0b910f379f..823bbf5c4e 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/WorkbookFactory.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/WorkbookFactory.java @@ -19,11 +19,7 @@ package org.apache.poi.ss.usermodel; import static org.apache.poi.extractor.ExtractorFactory.OOXML_PACKAGE; import static org.apache.poi.poifs.crypt.Decryptor.DEFAULT_POIFS_ENTRY; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.ServiceLoader; @@ -291,9 +287,9 @@ public final class WorkbookFactory { ooxmlEnc = root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE); } return wp(ooxmlEnc ? FileMagic.OOXML : fm, w -> w.create(file, password, readOnly)); + } else { + throw new IOException("Can't open workbook - unsupported file type: "+fm); } - - return null; } diff --git a/test-data/spreadsheet/workbook.xml b/test-data/spreadsheet/workbook.xml new file mode 100644 index 0000000000..61531b0ee8 --- /dev/null +++ b/test-data/spreadsheet/workbook.xml @@ -0,0 +1 @@ +EXAMPLEKontoZuordnungNummerKONTO0001123456789PROJEKT 1KONTO0001123456789
\ No newline at end of file