Skip to content

Commit

Permalink
Adjust order of testing and use try-with-resources
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912708 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
centic9 committed Oct 3, 2023
1 parent 360c05d commit a5b4a35
Showing 1 changed file with 32 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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
Expand Down Expand Up @@ -88,53 +87,52 @@ private void handleExtractingInternal(File file) throws Exception {
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;
Expand All @@ -159,8 +157,6 @@ private void handleExtractingInternal(File file) throws Exception {
if (!e.getMessage().contains("POI Scratchpad jar missing") || !Boolean.getBoolean("scratchpad.ignore")) {
throw e;
}
} finally {
IOUtils.closeQuietly(extractor);
}
}

Expand Down

0 comments on commit a5b4a35

Please sign in to comment.