From a5b4a3504af71ba9c5068842bed1a1fc46fcde85 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Tue, 3 Oct 2023 06:06:03 +0000 Subject: [PATCH] Adjust order of testing and use try-with-resources git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912708 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/stress/AbstractFileHandler.java | 68 +++++++++---------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/poi-integration/src/test/java/org/apache/poi/stress/AbstractFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/AbstractFileHandler.java index 4bc11d0c40..c413fb0733 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/AbstractFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/AbstractFileHandler.java @@ -37,7 +37,6 @@ import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor; import org.apache.poi.hssf.extractor.EventBasedExcelExtractor; import org.apache.poi.ooxml.POIXMLException; import org.apache.poi.ss.extractor.ExcelExtractor; -import org.apache.poi.util.IOUtils; /** * Base class with things that can be run for any supported file handler @@ -88,53 +87,52 @@ public abstract class AbstractFileHandler implements FileHandler { long length = file.length(); long modified = file.lastModified(); - POITextExtractor extractor = null; String fileAndParentName = file.getParentFile().getName() + "/" + file.getName(); try { + handleExtractingAsStream(file); + // fix windows absolute paths for exception message tracking String relPath = file.getPath().replaceAll(".*test-data", "test-data").replace('\\', '/'); - extractor = ExtractorFactory.createExtractor(file); - assertNotNull(extractor, "Should get a POITextExtractor but had none for file " + relPath); + try (POITextExtractor extractor = ExtractorFactory.createExtractor(file)) { + assertNotNull(extractor, "Should get a POITextExtractor but had none for file " + relPath); - assertNotNull(extractor.getText(), "Should get some text but had none for file " + relPath); + assertNotNull(extractor.getText(), "Should get some text but had none for file " + relPath); - // also try metadata - @SuppressWarnings("resource") - POITextExtractor metadataExtractor = extractor.getMetadataTextExtractor(); - assertNotNull(metadataExtractor.getText()); + // also try metadata + POITextExtractor metadataExtractor = extractor.getMetadataTextExtractor(); + assertNotNull(metadataExtractor.getText()); - assertFalse(EXPECTED_EXTRACTOR_FAILURES.contains(fileAndParentName), - "Expected Extraction to fail for file " + relPath + " and handler " + this + ", but did not fail!"); + assertFalse(EXPECTED_EXTRACTOR_FAILURES.contains(fileAndParentName), + "Expected Extraction to fail for file " + relPath + " and handler " + this + ", but did not fail!"); - assertEquals(length, file.length(), "File should not be modified by extractor"); - assertEquals(modified, file.lastModified(), "File should not be modified by extractor"); + assertEquals(length, file.length(), "File should not be modified by extractor"); + assertEquals(modified, file.lastModified(), "File should not be modified by extractor"); - handleExtractingAsStream(file); - - if (extractor instanceof POIOLE2TextExtractor) { - try (HPSFPropertiesExtractor hpsfExtractor = new HPSFPropertiesExtractor((POIOLE2TextExtractor) extractor)) { - assertNotNull(hpsfExtractor.getDocumentSummaryInformationText()); - assertNotNull(hpsfExtractor.getSummaryInformationText()); - String text = hpsfExtractor.getText(); - //System.out.println(text); - assertNotNull(text); + if (extractor instanceof POIOLE2TextExtractor) { + try (HPSFPropertiesExtractor hpsfExtractor = new HPSFPropertiesExtractor((POIOLE2TextExtractor) extractor)) { + assertNotNull(hpsfExtractor.getDocumentSummaryInformationText()); + assertNotNull(hpsfExtractor.getSummaryInformationText()); + String text = hpsfExtractor.getText(); + //System.out.println(text); + assertNotNull(text); + } } - } - // test again with including formulas and cell-comments as this caused some bugs - if (extractor instanceof ExcelExtractor && - // comment-extraction and formula extraction are not well supported in event based extraction - !(extractor instanceof EventBasedExcelExtractor)) { - ((ExcelExtractor) extractor).setFormulasNotResults(true); + // test again with including formulas and cell-comments as this caused some bugs + if (extractor instanceof ExcelExtractor && + // comment-extraction and formula extraction are not well supported in event based extraction + !(extractor instanceof EventBasedExcelExtractor)) { + ((ExcelExtractor) extractor).setFormulasNotResults(true); - String text = extractor.getText(); - assertNotNull(text); - // */ + String text = extractor.getText(); + assertNotNull(text); + // */ - ((ExcelExtractor) extractor).setIncludeCellComments(true); + ((ExcelExtractor) extractor).setIncludeCellComments(true); - text = extractor.getText(); - assertNotNull(text); + text = extractor.getText(); + assertNotNull(text); + } } } catch (IOException | POIXMLException e) { Exception prevE = e; @@ -159,8 +157,6 @@ public abstract class AbstractFileHandler implements FileHandler { if (!e.getMessage().contains("POI Scratchpad jar missing") || !Boolean.getBoolean("scratchpad.ignore")) { throw e; } - } finally { - IOUtils.closeQuietly(extractor); } } -- 2.39.5