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;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.xssf.usermodel.XSSFWorkbookFactory;
*
* @param filename the sample workbook to read in
* @param wb the workbook to close
- * @throws IOException
*/
private static void assertCloseDoesNotModifyFile(String filename, Workbook wb) throws IOException {
final byte[] before = HSSFTestDataSamples.getTestDataFileContent(filename);
HSSFTestDataSamples.openSampleFileStream(xlsx))
);
assertNotNull(wb);
+ //noinspection ConstantConditions
assertTrue(wb instanceof XSSFWorkbook);
assertCloseDoesNotModifyFile(xlsx, wb);
}
// Invalid type -> exception
final byte[] before = HSSFTestDataSamples.getTestDataFileContent(txt);
try {
- InputStream stream = HSSFTestDataSamples.openSampleFileStream(txt);
- try {
+ try (InputStream stream = HSSFTestDataSamples.openSampleFileStream(txt)) {
wb = WorkbookFactory.create(stream);
- } finally {
- stream.close();
+ assertNotNull(wb);
}
fail();
} catch(IOException e) {
);
assertCloseDoesNotModifyFile(xls_prot[0], wb);
fail("Shouldn't be able to open with the wrong password");
- } catch (EncryptedDocumentException e) {}
+ } catch (EncryptedDocumentException e) {
+ // expected here
+ }
try {
wb = WorkbookFactory.create(
);
assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
fail("Shouldn't be able to open with the wrong password");
- } catch (EncryptedDocumentException e) {}
+ } catch (EncryptedDocumentException e) {
+ // expected here
+ }
}
/**
try {
WorkbookFactory.create(emptyFile);
fail("Shouldn't be able to create for an empty file");
- } catch (final EmptyFileException expected) {}
- emptyFile.delete();
+ } catch (final EmptyFileException expected) {
+ // expected here
+ }
+
+ assertTrue(emptyFile.delete());
}
/**
* Check that a helpful exception is raised on a non-existing file
*/
@Test
- public void testNonExistantFile() throws Exception {
- File nonExistantFile = new File("notExistantFile");
- assertFalse(nonExistantFile.exists());
+ public void testNonExistingFile() throws Exception {
+ File nonExistingFile = new File("notExistingFile");
+ assertFalse(nonExistingFile.exists());
try {
- WorkbookFactory.create(nonExistantFile, "password", true);
- fail("Should not be able to create for a non-existant file");
+ WorkbookFactory.create(nonExistingFile, "password", true);
+ fail("Should not be able to create for a non-existing file");
} catch (final FileNotFoundException e) {
// expected
}
@SuppressWarnings("resource")
protected static void testFactoryFromProtectedFile(String protectedFile, String password) throws Exception {
- SlideShow<?,?> ss;
- // from protected file
- ss = SlideShowFactory.create(fromFile(protectedFile), password);
+ // from protected file
+ SlideShow<?,?> ss = SlideShowFactory.create(fromFile(protectedFile), password);
assertNotNull(ss);
assertCloseDoesNotModifyFile(protectedFile, ss);
}
@SuppressWarnings("resource")
protected static void testFactoryFromProtectedStream(String protectedFile, String password) throws Exception {
- SlideShow<?,?> ss;
// from protected stream
- ss = SlideShowFactory.create(fromStream(protectedFile), password);
+ SlideShow<?,?> ss = SlideShowFactory.create(fromStream(protectedFile), password);
assertNotNull(ss);
assertCloseDoesNotModifyFile(protectedFile, ss);
}
*
* @param filename the sample filename or full path of the slideshow to check before and after closing
* @param ss the slideshow to close or revert
- * @throws IOException
*/
private static void assertCloseDoesNotModifyFile(String filename, SlideShow<?,?> ss) throws IOException {
final byte[] before = readFile(filename);
// if the file after closing is different, then re-set
// the file to the state before in order to not have a dirty SCM
// working tree when running this test
- FileOutputStream str = new FileOutputStream(_slTests.getFile(filename));
- try {
+ try (FileOutputStream str = new FileOutputStream(_slTests.getFile(filename))) {
str.write(before);
- } finally {
- str.close();
}
throw e;