diff --git a/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java index 645a28bf3..ade80688d 100644 --- a/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java +++ b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java @@ -36,7 +36,6 @@ 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; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.xssf.usermodel.XSSFWorkbookFactory; @@ -57,7 +56,6 @@ public final class TestWorkbookFactory { * * @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); @@ -113,6 +111,7 @@ public final class TestWorkbookFactory { HSSFTestDataSamples.openSampleFileStream(xlsx)) ); assertNotNull(wb); + //noinspection ConstantConditions assertTrue(wb instanceof XSSFWorkbook); assertCloseDoesNotModifyFile(xlsx, wb); } @@ -176,11 +175,9 @@ public final class TestWorkbookFactory { // 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) { @@ -254,7 +251,9 @@ public final class TestWorkbookFactory { ); 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( @@ -262,7 +261,9 @@ public final class TestWorkbookFactory { ); assertCloseDoesNotModifyFile(xlsx_prot[0], wb); fail("Shouldn't be able to open with the wrong password"); - } catch (EncryptedDocumentException e) {} + } catch (EncryptedDocumentException e) { + // expected here + } } /** @@ -363,21 +364,24 @@ public final class TestWorkbookFactory { 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 } diff --git a/src/testcases/org/apache/poi/sl/usermodel/BaseTestSlideShowFactory.java b/src/testcases/org/apache/poi/sl/usermodel/BaseTestSlideShowFactory.java index d9c869e56..2365c5943 100644 --- a/src/testcases/org/apache/poi/sl/usermodel/BaseTestSlideShowFactory.java +++ b/src/testcases/org/apache/poi/sl/usermodel/BaseTestSlideShowFactory.java @@ -75,18 +75,16 @@ public class BaseTestSlideShowFactory { @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); } @@ -163,7 +161,6 @@ public class BaseTestSlideShowFactory { * * @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); @@ -177,11 +174,8 @@ public class BaseTestSlideShowFactory { // 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;