import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
private static final String[] xls_prot = new String[] {"password.xls", "password"};
private static final String[] xlsx_prot = new String[]{"protected_passtika.xlsx", "tika"};
private static final String txt = "SampleSS.txt";
+
+ private static final POILogger LOGGER = POILogFactory.getLogger(TestWorkbookFactory.class);
+
+ /**
+ * // TODO: close() re-writes the sample-file?! Resort to revert() for now to close file handle...
+ * Revert the changes that were made to the workbook rather than closing the workbook.
+ * This allows the file handle to be closed to avoid the file handle leak detector.
+ * This is a temporary fix until we figure out why wb.close() writes changes to disk.
+ *
+ * @param wb
+ */
+ private static void revert(Workbook wb) {
+ // TODO: close() re-writes the sample-file?! Resort to revert() for now to close file handle...
+ LOGGER.log(POILogger.WARN,
+ "reverting XSSFWorkbook rather than closing it to avoid close() modifying the file on disk." +
+ "This is a separate bug that isn't tested by this unit test.");
+ if (wb instanceof XSSFWorkbook) {
+ ((XSSFWorkbook) wb).getPackage().revert();
+ } else {
+ throw new RuntimeException("Unsupported workbook type");
+ }
+ }
public void testCreateNative() throws Exception {
Workbook wb;
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
// TODO: this re-writes the sample-file?! wb.close();
+ revert(wb);
// File -> either
wb = WorkbookFactory.create(
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
-
// TODO: close() re-writes the sample-file?! Resort to revert() for now to close file handle...
- ((XSSFWorkbook)wb).getPackage().revert();
+ revert(wb);
// Invalid type -> exception
try {
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
+ revert(wb);
// Unprotected, wrong password, opens normally
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
+ revert(wb);
// Protected, correct password, opens fine
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
+ revert(wb);
// Protected, wrong password, throws Exception
wb = WorkbookFactory.create(
HSSFTestDataSamples.openSampleFileStream(xls_prot[0]), "wrong"
);
+ wb.close();
fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {}
wb = WorkbookFactory.create(
HSSFTestDataSamples.openSampleFileStream(xlsx_prot[0]), "wrong"
);
+ revert(wb);
fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {}
}
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
- wb.close();
+ revert(wb);
// Unprotected, wrong password, opens normally
wb = WorkbookFactory.create(
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
- wb.close();
+ revert(wb);
// Protected, correct password, opens fine
wb = WorkbookFactory.create(
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
- wb.close();
+ revert(wb);
// Protected, wrong password, throws Exception
try {
wb = WorkbookFactory.create(
HSSFTestDataSamples.getSampleFile(xls_prot[0]), "wrong"
);
+ wb.close();
fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {}
wb = WorkbookFactory.create(
HSSFTestDataSamples.getSampleFile(xlsx_prot[0]), "wrong"
);
+ revert(wb);
fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {}
}