try {
InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx");
pkg = OPCPackage.open(is);
- } catch (InvalidFormatException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
+ } catch (InvalidFormatException | IOException e) {
throw new RuntimeException(e);
}
pkg.revert();
OPCPackage pkg;
try {
pkg = OPCPackage.open(is);
- } catch (InvalidFormatException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
+ } catch (InvalidFormatException | IOException e) {
throw new RuntimeException(e);
}
URI partUri = createURI("/docProps/core2.xml");
import org.apache.poi.EmptyFileException;
import org.apache.poi.EncryptedDocumentException;
+import org.apache.poi.POIDataSamples;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
assertTrue(wb instanceof HSSFWorkbook);
assertCloseDoesNotModifyFile(xls, wb);
+ wb = WorkbookFactory.create(
+ new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream(xls)).getRoot()
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof HSSFWorkbook);
+ assertCloseDoesNotModifyFile(xls, wb);
+
// Package -> xssf
wb = XSSFWorkbookFactory.create(
OPCPackage.open(
assertTrue(altXLS.exists());
assertTrue(altXLSX.exists());
- try (Workbook wb = WorkbookFactory.create(altXLS)) {
- assertNotNull(wb);
- assertTrue(wb instanceof HSSFWorkbook);
- }
+ Workbook wb = WorkbookFactory.create(altXLS);
+ assertNotNull(wb);
+ assertTrue(wb instanceof HSSFWorkbook);
+ closeOrRevert(wb);
- try (Workbook wb = WorkbookFactory.create(altXLSX)) {
- assertNotNull(wb);
- assertTrue(wb instanceof XSSFWorkbook);
- }
+ wb = WorkbookFactory.create(altXLSX);
+ assertNotNull(wb);
+ assertTrue(wb instanceof XSSFWorkbook);
+ closeOrRevert(wb);
}
private static class TestFile extends File {
*/
@Test
public void testCreateEmpty() throws Exception {
- try (Workbook wb = WorkbookFactory.create(false)) {
- assertTrue(wb instanceof HSSFWorkbook);
- }
+ Workbook wb = WorkbookFactory.create(false);
+ assertTrue(wb instanceof HSSFWorkbook);
+ closeOrRevert(wb);
+
+ wb = WorkbookFactory.create(true);
+ assertTrue(wb instanceof XSSFWorkbook);
+ closeOrRevert(wb);
+ }
- try (Workbook wb = WorkbookFactory.create(true)) {
- assertTrue(wb instanceof XSSFWorkbook);
+ @Test
+ public void testInvalidFormatException() {
+ String filename = "OPCCompliance_DerivedPartNameFAIL.docx";
+ try {
+ WorkbookFactory.create(POIDataSamples.getOpenXML4JInstance().openResourceAsStream(filename));
+ fail("Expecting an Exception for this document");
+ } catch (IOException e) {
+ // expected here
}
}
+
}