Bug 59427: Add a check to better report cases when a document is already closed.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1744306 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2016-05-17 19:08:34 +00:00
parent df72dbfbed
commit 0c78005743
2 changed files with 22 additions and 1 deletions

View File

@ -196,6 +196,10 @@ public abstract class POIXMLDocument extends POIXMLDocumentPart implements Close
//save extended and custom properties
getProperties().commit();
getPackage().save(stream);
OPCPackage pkg = getPackage();
if(pkg == null) {
throw new IOException("Cannot write data, document seems to have been closed already");
}
pkg.save(stream);
}
}

View File

@ -1090,4 +1090,21 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
//wb = new XSSFWorkbook(OPCPackage.open(file));
//assertCloseDoesNotModifyFile(filename, wb);
}
@Test
public void testCloseBeforeWrite() throws IOException {
Workbook wb = new XSSFWorkbook();
wb.createSheet("somesheet");
// test what happens if we close the Workbook before we write it out
wb.close();
try {
XSSFTestDataSamples.writeOutAndReadBack(wb);
fail("Expecting IOException here");
} catch (RuntimeException e) {
// expected here
assertTrue("Had: " + e.getCause(), e.getCause() instanceof IOException);
}
}
}