diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java index 636a3f732..b6215a6c1 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java @@ -220,10 +220,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { } // pack.originalPackagePath = file.getAbsolutePath(); return pack; - } catch (InvalidFormatException e) { - IOUtils.closeQuietly(pack); - throw e; - } catch (RuntimeException e) { + } catch (InvalidFormatException | RuntimeException e) { IOUtils.closeQuietly(pack); throw e; } @@ -299,10 +296,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { } pack.originalPackagePath = file.getAbsolutePath(); return pack; - } catch (InvalidFormatException e) { - IOUtils.closeQuietly(pack); - throw e; - } catch (RuntimeException e) { + } catch (InvalidFormatException | RuntimeException e) { IOUtils.closeQuietly(pack); throw e; } @@ -326,10 +320,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { if (pack.partList == null) { pack.getParts(); } - } catch (InvalidFormatException e) { - IOUtils.closeQuietly(pack); - throw e; - } catch (RuntimeException e) { + } catch (InvalidFormatException | RuntimeException e) { IOUtils.closeQuietly(pack); throw e; } @@ -512,12 +503,9 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { } String name = path.substring(path.lastIndexOf(File.separatorChar) + 1); - FileInputStream is = new FileInputStream(path); - try { - addThumbnail(name, is); - } finally { - is.close(); - } + try (FileInputStream is = new FileInputStream(path)) { + addThumbnail(name, is); + } } /** * Add a thumbnail to the package. This method is provided to make easier diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java index 1740d5794..c8b4a540d 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java @@ -393,14 +393,10 @@ public abstract class ContentTypeManager { String contentType = element.getAttribute(CONTENT_TYPE_ATTRIBUTE_NAME); addOverrideContentType(partName, contentType); } - } catch (URISyntaxException urie) { - throw new InvalidFormatException(urie.getMessage()); - } catch (SAXException e) { - throw new InvalidFormatException(e.getMessage()); - } catch (IOException e) { - throw new InvalidFormatException(e.getMessage()); + } catch (URISyntaxException | IOException | SAXException e) { + throw new InvalidFormatException(e.getMessage()); } - } + } /** * Save the contents type part. diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestZipPackage.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestZipPackage.java index 91a3db04a..88c5bbb1a 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestZipPackage.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestZipPackage.java @@ -40,7 +40,6 @@ import org.apache.poi.extractor.ExtractorFactory; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.openxml4j.OpenXML4JTestDataSamples; import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; -import org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException; import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.ss.usermodel.Workbook; @@ -94,11 +93,8 @@ public class TestZipPackage { private void assertEntityLimitReached(Exception e) throws UnsupportedEncodingException { ByteArrayOutputStream str = new ByteArrayOutputStream(); - PrintWriter writer = new PrintWriter(new OutputStreamWriter(str, "UTF-8")); - try { + try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(str, "UTF-8"))) { e.printStackTrace(writer); - } finally { - writer.close(); } String string = new String(str.toByteArray(), "UTF-8"); assertTrue("Had: " + string, string.contains("The parser has encountered more than")); @@ -115,17 +111,14 @@ public class TestZipPackage { } try { - POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-xmlbomb.xlsx")); - try { + try (POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-xmlbomb.xlsx"))) { assertNotNull(extractor); - + try { extractor.getText(); } catch (IllegalStateException e) { // expected due to shared strings expansion } - } finally { - extractor.close(); } } catch (POIXMLException e) { assertEntityLimitReached(e); @@ -136,9 +129,8 @@ public class TestZipPackage { public void testZipEntityExpansionSharedStringTable() throws Exception { Workbook wb = WorkbookFactory.create(XSSFTestDataSamples.openSamplePackage("poc-shared-strings.xlsx")); wb.close(); - - POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx")); - try { + + try (POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx"))) { assertNotNull(extractor); try { @@ -146,8 +138,6 @@ public class TestZipPackage { } catch (IllegalStateException e) { // expected due to shared strings expansion } - } finally { - extractor.close(); } } @@ -156,17 +146,14 @@ public class TestZipPackage { boolean before = ExtractorFactory.getThreadPrefersEventExtractors(); ExtractorFactory.setThreadPrefersEventExtractors(true); try { - POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx")); - try { + try (POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx"))) { assertNotNull(extractor); - + try { extractor.getText(); } catch (IllegalStateException e) { // expected due to shared strings expansion } - } finally { - extractor.close(); } } catch (XmlException e) { assertEntityLimitReached(e); @@ -232,8 +219,9 @@ public class TestZipPackage { try { pkg.getParts(); fail("Shouldn't work"); - } catch (ODFNotOfficeXmlFileException e) { - } catch (NotOfficeXmlFileException ne) {} + } catch (NotOfficeXmlFileException e) { + // expected here + } pkg.close(); assertNotNull(pkg.getZipArchive()); @@ -246,8 +234,9 @@ public class TestZipPackage { try { pkg.getParts(); fail("Shouldn't work"); - } catch (ODFNotOfficeXmlFileException e) { - } catch (NotOfficeXmlFileException ne) {} + } catch (NotOfficeXmlFileException e) { + // expected here + } pkg.close(); assertNotNull(pkg.getZipArchive()); diff --git a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java index 6c0d903c8..e2f482527 100644 --- a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java +++ b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java @@ -220,16 +220,13 @@ public class TestSignatureInfo { @Test public void office2007prettyPrintedRels() throws Exception { - OPCPackage pkg = OPCPackage.open(testdata.getFile("office2007prettyPrintedRels.docx"), PackageAccess.READ); - try { + try (OPCPackage pkg = OPCPackage.open(testdata.getFile("office2007prettyPrintedRels.docx"), PackageAccess.READ)) { SignatureConfig sic = new SignatureConfig(); sic.setOpcPackage(pkg); SignatureInfo si = new SignatureInfo(); si.setSignatureConfig(sic); boolean isValid = si.verifySignature(); assertTrue(isValid); - } finally { - pkg.close(); } } @@ -277,8 +274,7 @@ public class TestSignatureInfo { }; for (String testFile : testFiles) { - OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ); - try { + try (OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ)) { SignatureConfig sic = new SignatureConfig(); sic.setOpcPackage(pkg); SignatureInfo si = new SignatureInfo(); @@ -289,17 +285,15 @@ public class TestSignatureInfo { result.add(sp.getSigner()); } } - + assertNotNull(result); - assertEquals("test-file: "+testFile, 1, result.size()); + assertEquals("test-file: " + testFile, 1, result.size()); X509Certificate signer = result.get(0); LOG.log(POILogger.DEBUG, "signer: " + signer.getSubjectX500Principal()); - + boolean b = si.verifySignature(); - assertTrue("test-file: "+testFile, b); + assertTrue("test-file: " + testFile, b); pkg.revert(); - } finally { - pkg.close(); } } } @@ -307,8 +301,7 @@ public class TestSignatureInfo { @Test public void getMultiSigners() throws Exception { String testFile = "hello-world-signed-twice.docx"; - OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ); - try { + try (OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ)) { SignatureConfig sic = new SignatureConfig(); sic.setOpcPackage(pkg); SignatureInfo si = new SignatureInfo(); @@ -319,19 +312,17 @@ public class TestSignatureInfo { result.add(sp.getSigner()); } } - + assertNotNull(result); - assertEquals("test-file: "+testFile, 2, result.size()); + assertEquals("test-file: " + testFile, 2, result.size()); X509Certificate signer1 = result.get(0); X509Certificate signer2 = result.get(1); LOG.log(POILogger.DEBUG, "signer 1: " + signer1.getSubjectX500Principal()); LOG.log(POILogger.DEBUG, "signer 2: " + signer2.getSubjectX500Principal()); - + boolean b = si.verifySignature(); - assertTrue("test-file: "+testFile, b); + assertTrue("test-file: " + testFile, b); pkg.revert(); - } finally { - pkg.close(); } } @@ -703,12 +694,9 @@ public class TestSignatureInfo { //X509Certificate x509B = x509; File tpl = copy(testdata.getFile("bug58630.xlsx")); - OPCPackage pkg = OPCPackage.open(tpl); - try { + try (OPCPackage pkg = OPCPackage.open(tpl)) { //SignatureConfig signatureConfig = new SignatureConfig(); assertNotNull(pkg); - } finally { - pkg.close(); } } @@ -822,16 +810,10 @@ public class TestSignatureInfo { } File tmpFile = new File(buildDir, "sigtest"+extension); - OutputStream fos = new FileOutputStream(tmpFile); - try { - InputStream fis = new FileInputStream(input); - try { + try (OutputStream fos = new FileOutputStream(tmpFile)) { + try (InputStream fis = new FileInputStream(input)) { IOUtils.copy(fis, fos); - } finally { - fis.close(); } - } finally { - fos.close(); } return tmpFile; diff --git a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFWorkbookWithCustomZipEntrySource.java b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFWorkbookWithCustomZipEntrySource.java index b477c8b10..43c3f22ff 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFWorkbookWithCustomZipEntrySource.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFWorkbookWithCustomZipEntrySource.java @@ -121,14 +121,11 @@ public final class TestSXSSFWorkbookWithCustomZipEntrySource { assertEquals(1, tempFiles.size()); File tempFile = tempFiles.get(0); assertTrue("tempFile exists?", tempFile.exists()); - InputStream stream = new FileInputStream(tempFile); - try { + try (InputStream stream = new FileInputStream(tempFile)) { byte[] data = IOUtils.toByteArray(stream); String text = new String(data, UTF_8); assertFalse(text.contains(sheetName)); assertFalse(text.contains(cellValue)); - } finally { - stream.close(); } workbook.dispose(); assertFalse("tempFile deleted after dispose?", tempFile.exists()); diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index 18dbf79f3..b18661afa 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -246,8 +246,8 @@ public abstract class BaseTestCell { Workbook wb1 = _testDataProvider.createWorkbook(); Sheet s = wb1.createSheet("testSheet1"); - Row r = null; - Cell c = null; + Row r; + Cell c; CellStyle cs = wb1.createCellStyle(); Font f = wb1.createFont(); f.setFontHeightInPoints((short) 20); @@ -380,8 +380,6 @@ public abstract class BaseTestCell { /** * bug 58452: Copy cell formulas containing unregistered function names * Make sure that formulas with unknown/unregistered UDFs can be written to and read back from a file. - * - * @throws IOException */ @Test public void testFormulaWithUnknownUDF() throws IOException { @@ -809,20 +807,16 @@ public abstract class BaseTestCell { /** * Cell with the formula that returns error must return error code(There was * an problem that cell could not return error value form formula cell). - * @throws IOException */ @Test public void testGetErrorCellValueFromFormulaCell() throws IOException { - Workbook wb = _testDataProvider.createWorkbook(); - try { + try (Workbook wb = _testDataProvider.createWorkbook()) { Sheet sheet = wb.createSheet(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellFormula("SQRT(-1)"); wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(cell); assertEquals(36, cell.getErrorCellValue()); - } finally { - wb.close(); } } @@ -932,7 +926,6 @@ public abstract class BaseTestCell { /** * The maximum length of cell contents (text) is 32,767 characters. - * @throws IOException */ @Test public void testMaxTextLength() throws IOException{ @@ -945,7 +938,7 @@ public abstract class BaseTestCell { : SpreadsheetVersion.EXCEL2007.getMaxTextLength(); assertEquals(32767, maxlen); - StringBuffer b = new StringBuffer() ; + StringBuilder b = new StringBuilder() ; // 32767 is okay for( int i = 0 ; i < maxlen ; i++ )