]> source.dussan.org Git - poi.git/commitdiff
Do not return null for POITextExtractor.getMetadataTextExtractor() for old Excel...
authorDominik Stadler <centic@apache.org>
Mon, 26 Apr 2021 15:12:32 +0000 (15:12 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 26 Apr 2021 15:12:32 +0000 (15:12 +0000)
To adhere to the JavaDoc of the POITextExtractor interface which does not document a
possible null return.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1889205 13f79535-47bb-0310-9956-ffa450edef68

poi-integration/src/test/java/org/apache/poi/stress/HSSFFileHandler.java
poi/src/main/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
poi/src/test/java/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java
test-data/spreadsheet/stress.xls

index 9ee54b4790e566e982b8d06cf1d3dea2a47bfca1..1e0348cac59106351b9d53498005700b27552159 100644 (file)
@@ -107,7 +107,7 @@ class HSSFFileHandler extends SpreadsheetHandler {
        // a test-case to test this locally without executing the full TestAllFiles
        @Test
        void test() throws Exception {
-        File file = new File("test-data/spreadsheet/49219.xls");
+        File file = new File("../test-data/spreadsheet/59074.xls");
 
                try (InputStream stream = new FileInputStream(file)) {
                        handleFile(stream, file.getPath());
@@ -122,6 +122,6 @@ class HSSFFileHandler extends SpreadsheetHandler {
     @Test
        @SuppressWarnings("java:S2699")
     void testExtractor() throws Exception {
-        handleExtracting(new File("test-data/spreadsheet/BOOK_in_capitals.xls"));
+        handleExtracting(new File("../test-data/spreadsheet/59074.xls"));
     }
 }
\ No newline at end of file
index e6a55f88ff8f3dae9337a275d3860c93433d5729..d5ef5142e772b227b7ca1983d05ac945541f1d61 100644 (file)
@@ -316,7 +316,38 @@ public class OldExcelExtractor implements POITextExtractor {
 
     @Override
     public POITextExtractor getMetadataTextExtractor() {
-        return null;
+               return new POITextExtractor() {
+
+                       @Override
+                       public String getText() {
+                               return "";
+                       }
+
+                       @Override
+                       public POITextExtractor getMetadataTextExtractor() {
+                               throw new IllegalStateException("You already have the Metadata Text Extractor, not recursing!");
+                       }
+
+                       @Override
+                       public void setCloseFilesystem(boolean doCloseFilesystem) {
+
+                       }
+
+                       @Override
+                       public boolean isCloseFilesystem() {
+                               return toClose != null;
+                       }
+
+                       @Override
+                       public Closeable getFilesystem() {
+                               return toClose;
+                       }
+
+                       @Override
+                       public Object getDocument() {
+                               return ris;
+                       }
+               };
     }
 
     @Override
index 3fb037c93e724587e8854de09707096881fce489..7dbabe14b12f989c7d62889147c94f074ec1a70b 100644 (file)
@@ -35,6 +35,7 @@ import java.security.Permission;
 import org.apache.poi.EmptyFileException;
 import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.POIDataSamples;
+import org.apache.poi.extractor.POITextExtractor;
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
@@ -378,4 +379,24 @@ final class TestOldExcelExtractor {
             this.status = status;
         }
     }
+
+       @Test
+       void testMetaData() throws IOException {
+               try (OldExcelExtractor extractor = createExtractor("testEXCEL_3.xls")) {
+                       POITextExtractor metaData = extractor.getMetadataTextExtractor();
+                       assertNotNull(metaData);
+
+                       assertThrows(IllegalStateException.class, metaData::getMetadataTextExtractor);
+                       assertEquals("", metaData.getText());
+                       assertNotNull(metaData.getDocument());
+                       assertTrue(metaData.isCloseFilesystem());
+                       assertNotNull(metaData.getFilesystem());
+
+                       // the setter is a NOP
+                       metaData.setCloseFilesystem(false);
+                       assertTrue(metaData.isCloseFilesystem());
+
+                       metaData.close();
+               }
+       }
 }
index b613a8975179e0365968feb077d4655c041c670b..c773b819d5716d9d2d3bce7538501ffe956e3b2c 100644 (file)
Binary files a/test-data/spreadsheet/stress.xls and b/test-data/spreadsheet/stress.xls differ