mirror of https://github.com/apache/poi.git
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
This commit is contained in:
parent
360c05d9e3
commit
a5b4a3504a
|
@ -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,54 +87,53 @@ 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 {
|
||||
// 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);
|
||||
|
||||
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());
|
||||
|
||||
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");
|
||||
|
||||
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);
|
||||
// fix windows absolute paths for exception message tracking
|
||||
String relPath = file.getPath().replaceAll(".*test-data", "test-data").replace('\\', '/');
|
||||
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);
|
||||
|
||||
// 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!");
|
||||
|
||||
assertEquals(length, file.length(), "File should not be modified by extractor");
|
||||
assertEquals(modified, file.lastModified(), "File should not be modified by extractor");
|
||||
|
||||
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);
|
||||
|
||||
String text = extractor.getText();
|
||||
assertNotNull(text);
|
||||
// */
|
||||
|
||||
((ExcelExtractor) extractor).setIncludeCellComments(true);
|
||||
|
||||
text = extractor.getText();
|
||||
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);
|
||||
|
||||
String text = extractor.getText();
|
||||
assertNotNull(text);
|
||||
// */
|
||||
|
||||
((ExcelExtractor) extractor).setIncludeCellComments(true);
|
||||
|
||||
text = extractor.getText();
|
||||
assertNotNull(text);
|
||||
}
|
||||
} catch (IOException | POIXMLException e) {
|
||||
Exception prevE = e;
|
||||
Throwable cause;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue