Bug 62165: Do not close stream when opening succeeds

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1828377 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2018-04-04 19:41:10 +00:00
parent 971c52347e
commit e9121036cc
2 changed files with 31 additions and 30 deletions

View File

@ -114,8 +114,14 @@ public class OldExcelExtractor implements Closeable {
: new BufferedInputStream(biffStream, 8); : new BufferedInputStream(biffStream, 8);
if (FileMagic.valueOf(bis) == FileMagic.OLE2) { if (FileMagic.valueOf(bis) == FileMagic.OLE2) {
try (NPOIFSFileSystem poifs = new NPOIFSFileSystem(bis)) { NPOIFSFileSystem poifs = new NPOIFSFileSystem(bis);
try {
open(poifs); open(poifs);
toClose = poifs; // Fixed by GR, we should not close it here
} finally {
if (toClose == null) {
poifs.close();
}
} }
} else { } else {
ris = new RecordInputStream(bis); ris = new RecordInputStream(bis);

View File

@ -218,6 +218,22 @@ public final class TestOldExcelExtractor {
} }
} }
@Test
public void testFromInputStream() throws IOException {
for (String ver : new String[] {"4", "5", "95"}) {
String filename = "testEXCEL_"+ver+".xls";
File f = HSSFTestDataSamples.getSampleFile(filename);
try (InputStream stream = new FileInputStream(f)) {
OldExcelExtractor extractor = new OldExcelExtractor(stream);
String text = extractor.getText();
assertNotNull(text);
assertTrue(text.length() > 100);
extractor.close();
}
}
}
@Test(expected=OfficeXmlFileException.class) @Test(expected=OfficeXmlFileException.class)
public void testOpenInvalidFile1() throws IOException { public void testOpenInvalidFile1() throws IOException {
// a file that exists, but is a different format // a file that exists, but is a different format
@ -234,11 +250,8 @@ public final class TestOldExcelExtractor {
@Test(expected=FileNotFoundException.class) @Test(expected=FileNotFoundException.class)
public void testOpenInvalidFile3() throws IOException { public void testOpenInvalidFile3() throws IOException {
// a POIFS file which is not a Workbook // a POIFS file which is not a Workbook
InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream("47304.doc"); try (InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream("47304.doc")) {
try {
new OldExcelExtractor(is).close(); new OldExcelExtractor(is).close();
} finally {
is.close();
} }
} }
@ -252,65 +265,50 @@ public final class TestOldExcelExtractor {
@Test @Test
public void testInputStream() throws IOException { public void testInputStream() throws IOException {
File file = HSSFTestDataSamples.getSampleFile("testEXCEL_3.xls"); File file = HSSFTestDataSamples.getSampleFile("testEXCEL_3.xls");
InputStream stream = new FileInputStream(file); try (InputStream stream = new FileInputStream(file)) {
try {
OldExcelExtractor extractor = new OldExcelExtractor(stream); OldExcelExtractor extractor = new OldExcelExtractor(stream);
String text = extractor.getText(); String text = extractor.getText();
assertNotNull(text); assertNotNull(text);
extractor.close(); extractor.close();
} finally {
stream.close();
} }
} }
@Test @Test
public void testInputStreamNPOIHeader() throws IOException { public void testInputStreamNPOIHeader() throws IOException {
File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls"); File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls");
InputStream stream = new FileInputStream(file); try (InputStream stream = new FileInputStream(file)) {
try {
OldExcelExtractor extractor = new OldExcelExtractor(stream); OldExcelExtractor extractor = new OldExcelExtractor(stream);
extractor.close(); extractor.close();
} finally {
stream.close();
} }
} }
@Test @Test
public void testNPOIFSFileSystem() throws IOException { public void testNPOIFSFileSystem() throws IOException {
File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls"); File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls");
NPOIFSFileSystem fs = new NPOIFSFileSystem(file); try (NPOIFSFileSystem fs = new NPOIFSFileSystem(file)) {
try {
OldExcelExtractor extractor = new OldExcelExtractor(fs); OldExcelExtractor extractor = new OldExcelExtractor(fs);
extractor.close(); extractor.close();
} finally {
fs.close();
} }
} }
@Test @Test
public void testDirectoryNode() throws IOException { public void testDirectoryNode() throws IOException {
File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls"); File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls");
NPOIFSFileSystem fs = new NPOIFSFileSystem(file); try (NPOIFSFileSystem fs = new NPOIFSFileSystem(file)) {
try {
OldExcelExtractor extractor = new OldExcelExtractor(fs.getRoot()); OldExcelExtractor extractor = new OldExcelExtractor(fs.getRoot());
extractor.close(); extractor.close();
} finally {
fs.close();
} }
} }
@Test @Test
public void testDirectoryNodeInvalidFile() throws IOException { public void testDirectoryNodeInvalidFile() throws IOException {
File file = POIDataSamples.getDocumentInstance().getFile("test.doc"); File file = POIDataSamples.getDocumentInstance().getFile("test.doc");
NPOIFSFileSystem fs = new NPOIFSFileSystem(file); try (NPOIFSFileSystem fs = new NPOIFSFileSystem(file)) {
try {
OldExcelExtractor extractor = new OldExcelExtractor(fs.getRoot()); OldExcelExtractor extractor = new OldExcelExtractor(fs.getRoot());
extractor.close(); extractor.close();
fail("Should catch exception here"); fail("Should catch exception here");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// expected here // expected here
} finally {
fs.close();
} }
} }
@ -319,13 +317,10 @@ public final class TestOldExcelExtractor {
public void testMainUsage() throws IOException { public void testMainUsage() throws IOException {
PrintStream save = System.err; PrintStream save = System.err;
try { try {
ByteArrayOutputStream out = new ByteArrayOutputStream(); try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
try {
PrintStream str = new PrintStream(out, false, "UTF-8"); PrintStream str = new PrintStream(out, false, "UTF-8");
System.setErr(str); System.setErr(str);
OldExcelExtractor.main(new String[]{}); OldExcelExtractor.main(new String[]{});
} finally {
out.close();
} }
} finally { } finally {
System.setErr(save); System.setErr(save);