#59724 Take advantage of all POIDocument classes being Closeable to tidy the OLE2 text extractor closing

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1749214 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2016-06-19 22:13:41 +00:00
parent b7b990f56f
commit 7da9b2cd67
6 changed files with 23 additions and 21 deletions

View File

@ -44,6 +44,10 @@ public abstract class POIOLE2TextExtractor extends POITextExtractor {
*/
public POIOLE2TextExtractor(POIDocument document) {
this.document = document;
// Ensure any underlying resources, such as open files,
// will get cleaned up if the user calls #close()
setFilesystem(document);
}
/**

View File

@ -17,7 +17,6 @@
package org.apache.poi.hpsf.extractor;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
@ -41,8 +40,6 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
* textual form.
*/
public class HPSFPropertiesExtractor extends POIOLE2TextExtractor {
private Closeable toClose;
public HPSFPropertiesExtractor(POIOLE2TextExtractor mainExtractor) {
super(mainExtractor);
}
@ -54,7 +51,6 @@ public class HPSFPropertiesExtractor extends POIOLE2TextExtractor {
}
public HPSFPropertiesExtractor(NPOIFSFileSystem fs) {
super(new HPSFPropertiesOnlyDocument(fs));
this.toClose = fs;
}
public String getDocumentSummaryInformationText() {
@ -132,19 +128,6 @@ public class HPSFPropertiesExtractor extends POIOLE2TextExtractor {
throw new IllegalStateException("You already have the Metadata Text Extractor, not recursing!");
}
public void close() throws IOException {
super.close();
if(toClose != null) {
toClose.close();
toClose = null;
}
}
private static abstract class HelperPropertySet extends SpecialPropertySet {
public HelperPropertySet() {
super(null);

View File

@ -73,6 +73,7 @@ public class EventBasedExcelExtractor extends POIOLE2TextExtractor implements or
public EventBasedExcelExtractor(POIFSFileSystem fs) {
this(fs.getRoot());
super.setFilesystem(fs);
}
/**

View File

@ -144,6 +144,8 @@ public final class TestFixedSizedProperties {
// @Ignore("TODO Work out why the Fri 22nd vs Monday 25th problem is occurring and fix")
public void testReadMessageDateSucceedsWithOutlookTextExtractor() throws Exception {
OutlookTextExtactor ext = new OutlookTextExtactor(mapiMessageSucceeds);
ext.setFilesystem(null); // Don't close re-used test resources here
String text = ext.getText();
assertContains(text, "Date: Fri, 22 Jun 2012 18:32:54 +0000\n");
ext.close();
@ -156,6 +158,8 @@ public final class TestFixedSizedProperties {
// @Ignore("TODO Work out why the Thu 21st vs Monday 25th problem is occurring and fix")
public void testReadMessageDateFailsWithOutlookTextExtractor() throws Exception {
OutlookTextExtactor ext = new OutlookTextExtactor(mapiMessageFails);
ext.setFilesystem(null); // Don't close re-used test resources here
String text = ext.getText();
assertContains(text, "Date: Thu, 21 Jun 2012 14:14:04 +0000\n");
ext.close();

View File

@ -49,10 +49,8 @@ import org.apache.poi.util.POILogger;
import junit.framework.TestCase;
/**
* Test different problems reported in Apache Bugzilla
*
* @author Nick Burch (nick at torchbox dot com)
* @author Sergey Vladimirov (vlsergey {at} gmail {dot} com)
* Test different problems reported in the Apache Bugzilla
* against HWPF
*/
public class TestBugs extends TestCase
{
@ -245,6 +243,7 @@ public class TestBugs extends TestCase
*/
public void test45473() throws IOException
{
// Fetch the current text
HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("Bug45473.doc");
WordExtractor wordExtractor = new WordExtractor(doc1);
final String text1;
@ -254,6 +253,8 @@ public class TestBugs extends TestCase
wordExtractor.close();
}
// Re-load, then re-save and re-check
doc1 = HWPFTestDataSamples.openSampleFile("Bug45473.doc");
HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);
WordExtractor wordExtractor2 = new WordExtractor(doc2);
final String text2;
@ -313,6 +314,7 @@ public class TestBugs extends TestCase
@SuppressWarnings("deprecation")
public void test47286() throws IOException
{
// Fetch the current text
HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("Bug47286.doc");
WordExtractor wordExtractor = new WordExtractor(doc1);
final String text1;
@ -322,6 +324,8 @@ public class TestBugs extends TestCase
wordExtractor.close();
}
// Re-load, then re-save and re-check
doc1 = HWPFTestDataSamples.openSampleFile("Bug47286.doc");
HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);
WordExtractor wordExtractor2 = new WordExtractor(doc2);
final String text2;

View File

@ -115,6 +115,7 @@ public final class TestHPSFPropertiesExtractor extends TestCase {
final String fsText;
HPSFPropertiesExtractor fsExt = new HPSFPropertiesExtractor(fs);
fsExt.setFilesystem(null); // Don't close re-used test resources!
try {
fsText = fsExt.getText();
} finally {
@ -123,6 +124,7 @@ public final class TestHPSFPropertiesExtractor extends TestCase {
final String hwText;
HPSFPropertiesExtractor hwExt = new HPSFPropertiesExtractor(wb);
hwExt.setFilesystem(null); // Don't close re-used test resources!
try {
hwText = hwExt.getText();
} finally {
@ -131,6 +133,7 @@ public final class TestHPSFPropertiesExtractor extends TestCase {
final String eeText;
HPSFPropertiesExtractor eeExt = new HPSFPropertiesExtractor(excelExt);
eeExt.setFilesystem(null); // Don't close re-used test resources!
try {
eeText = eeExt.getText();
} finally {
@ -142,6 +145,9 @@ public final class TestHPSFPropertiesExtractor extends TestCase {
assertTrue(fsText.indexOf("AUTHOR = marshall") > -1);
assertTrue(fsText.indexOf("TITLE = Titel: \u00c4h") > -1);
// Finally tidy
wb.close();
}
public void test42726() throws IOException {