From b9c792dad07cab34bc1ecf9f13e8085292ad5e86 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Sun, 29 Nov 2015 15:26:53 +0000 Subject: [PATCH] close resources fix a few eclipse warnings git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1717077 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hslf/examples/ApacheconEU08.java | 2 +- .../hssf/usermodel/examples/Alignment.java | 10 ++++- .../apache/poi/ss/examples/AligningCells.java | 1 + .../usermodel/examples/AligningCells.java | 11 +++++- .../poi/poifs/crypt/agile/AgileDecryptor.java | 5 +-- .../devtools/forbidden-signatures.txt | 3 ++ .../poi/hwpf/converter/AbstractWordUtils.java | 10 ++--- .../eval/BaseTestCircularReferences.java | 39 ++++++++++++------- .../poi/ss/formula/functions/TestText.java | 5 ++- .../poi/ss/usermodel/BaseTestHyperlink.java | 22 +++++++---- 10 files changed, 70 insertions(+), 38 deletions(-) diff --git a/src/examples/src/org/apache/poi/hslf/examples/ApacheconEU08.java b/src/examples/src/org/apache/poi/hslf/examples/ApacheconEU08.java index 32a6c7d1c8..46eade5cfc 100644 --- a/src/examples/src/org/apache/poi/hslf/examples/ApacheconEU08.java +++ b/src/examples/src/org/apache/poi/hslf/examples/ApacheconEU08.java @@ -72,7 +72,7 @@ public final class ApacheconEU08 { FileOutputStream out = new FileOutputStream("apachecon_eu_08."+ext); ppt.write(out); out.close(); - + ppt.close(); } public static void slide1(SlideShow ppt) throws IOException { diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/Alignment.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/Alignment.java index b54640ee73..46f56d66fa 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/Alignment.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/Alignment.java @@ -19,11 +19,15 @@ package org.apache.poi.hssf.usermodel.examples; -import org.apache.poi.hssf.usermodel.*; - import java.io.FileOutputStream; import java.io.IOException; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + /** * Shows how various alignment options work. * @@ -46,6 +50,8 @@ public class Alignment { FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); + + wb.close(); } /** diff --git a/src/examples/src/org/apache/poi/ss/examples/AligningCells.java b/src/examples/src/org/apache/poi/ss/examples/AligningCells.java index 6571ff9dd9..80591fd70a 100644 --- a/src/examples/src/org/apache/poi/ss/examples/AligningCells.java +++ b/src/examples/src/org/apache/poi/ss/examples/AligningCells.java @@ -50,6 +50,7 @@ public class AligningCells { FileOutputStream fileOut = new FileOutputStream("ss-example-align.xlsx"); wb.write(fileOut); fileOut.close(); + wb.close(); } /** diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/AligningCells.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/AligningCells.java index aa96056e47..8b385fed14 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/AligningCells.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/AligningCells.java @@ -21,8 +21,13 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.xssf.usermodel.*; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.xssf.usermodel.XSSFCell; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; +import org.apache.poi.xssf.usermodel.XSSFRow; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTRowImpl; /** @@ -63,6 +68,8 @@ public class AligningCells { FileOutputStream fileOut = new FileOutputStream("xssf-align.xlsx"); wb.write(fileOut); fileOut.close(); + + wb.close(); } /** diff --git a/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java b/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java index 3af78128c8..f8c9bb551c 100644 --- a/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java +++ b/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java @@ -278,12 +278,11 @@ public class AgileDecryptor extends Decryptor { } } + @SuppressWarnings("resource") public InputStream getDataStream(DirectoryNode dir) throws IOException, GeneralSecurityException { DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY); _length = dis.readLong(); - - ChunkedCipherInputStream cipherStream = new AgileCipherInputStream(dis, _length); - return cipherStream; + return new AgileCipherInputStream(dis, _length); } public long getLength(){ diff --git a/src/resources/devtools/forbidden-signatures.txt b/src/resources/devtools/forbidden-signatures.txt index 75953ccc7e..1a506f3ffd 100644 --- a/src/resources/devtools/forbidden-signatures.txt +++ b/src/resources/devtools/forbidden-signatures.txt @@ -29,3 +29,6 @@ java.util.Date#toString() @ Do not use methods that depend on the current Local, # Disallow reflection on private object fields/methods java.lang.reflect.AccessibleObject#setAccessible(java.lang.reflect.AccessibleObject[], boolean) @ Reflection usage fails with SecurityManagers and likely will not work any more in Java 9 java.lang.reflect.AccessibleObject#setAccessible(boolean) @ Reflection usage fails with SecurityManagers and likely will not work any more in Java 9 + +java.text.DecimalFormatSymbols#DecimalFormatSymbols() @ use DecimalFormatSymbols.getInstance() +java.text.DecimalFormatSymbols#DecimalFormatSymbols(Locale) @ use DecimalFormatSymbols.getInstance() diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordUtils.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordUtils.java index 6efb6ad228..aff9519f8e 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordUtils.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordUtils.java @@ -37,7 +37,6 @@ import org.apache.poi.hwpf.usermodel.TableRow; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.Beta; -import org.apache.poi.util.IOUtils; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; import org.w3c.dom.Attr; @@ -484,13 +483,10 @@ public class AbstractWordUtils public static HWPFDocumentCore loadDoc( File docFile ) throws IOException { final FileInputStream istream = new FileInputStream( docFile ); - try - { + try { return loadDoc( istream ); - } - finally - { - IOUtils.closeQuietly( istream ); + } finally { + istream.close(); } } diff --git a/src/testcases/org/apache/poi/ss/formula/eval/BaseTestCircularReferences.java b/src/testcases/org/apache/poi/ss/formula/eval/BaseTestCircularReferences.java index 99250d5d22..e82fcf7517 100644 --- a/src/testcases/org/apache/poi/ss/formula/eval/BaseTestCircularReferences.java +++ b/src/testcases/org/apache/poi/ss/formula/eval/BaseTestCircularReferences.java @@ -16,8 +16,11 @@ ==================================================================== */ package org.apache.poi.ss.formula.eval; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.usermodel.Cell; @@ -26,12 +29,15 @@ import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; +import org.junit.Test; + +import junit.framework.AssertionFailedError; /** * Common superclass for testing cases of circular references * both for HSSF and XSSF */ -public abstract class BaseTestCircularReferences extends TestCase { +public abstract class BaseTestCircularReferences { protected final ITestDataProvider _testDataProvider; @@ -68,8 +74,8 @@ public abstract class BaseTestCircularReferences extends TestCase { * ASF Bugzilla Bug 44413 * "INDEX() formula cannot contain its own location in the data array range" */ - public void testIndexFormula() { - + @Test + public void testIndexFormula() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet("Sheet1"); @@ -91,13 +97,14 @@ public abstract class BaseTestCircularReferences extends TestCase { assertTrue(cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC); assertEquals(2, cellValue.getNumberValue(), 0); + wb.close(); } /** * Cell A1 has formula "=A1" */ - public void testSimpleCircularReference() { - + @Test + public void testSimpleCircularReference() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet("Sheet1"); @@ -108,13 +115,15 @@ public abstract class BaseTestCircularReferences extends TestCase { CellValue cellValue = evaluateWithCycles(wb, testCell); confirmCycleErrorCode(cellValue); + + wb.close(); } /** * A1=B1, B1=C1, C1=D1, D1=A1 */ - public void testMultiLevelCircularReference() { - + @Test + public void testMultiLevelCircularReference() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet("Sheet1"); @@ -128,9 +137,12 @@ public abstract class BaseTestCircularReferences extends TestCase { CellValue cellValue = evaluateWithCycles(wb, testCell); confirmCycleErrorCode(cellValue); + + wb.close(); } - public void testIntermediateCircularReferenceResults_bug46898() { + @Test + public void testIntermediateCircularReferenceResults_bug46898() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet("Sheet1"); @@ -162,9 +174,8 @@ public abstract class BaseTestCircularReferences extends TestCase { // Show the bug - evaluate another cell from the loop first fe.clearAllCachedResultValues(); cv = fe.evaluate(cellB1); - if (cv.getCellType() == ErrorEval.CIRCULAR_REF_ERROR.getErrorCode()) { - throw new AssertionFailedError("Identified bug 46898"); - } + // Identified bug 46898 + assertNotEquals(cv.getCellType(), ErrorEval.CIRCULAR_REF_ERROR.getErrorCode()); assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType()); assertEquals(46.0, cv.getNumberValue(), 0.0); @@ -173,5 +184,7 @@ public abstract class BaseTestCircularReferences extends TestCase { cv = fe.evaluate(cellE1); assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType()); assertEquals(43.0, cv.getNumberValue(), 0.0); + + wb.close(); } } diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestText.java b/src/testcases/org/apache/poi/ss/formula/functions/TestText.java index 928c639fae..0e6521ef23 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestText.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestText.java @@ -50,8 +50,9 @@ public final class TestText { ValueEval formatArg = new StringEval("#,###.00000"); ValueEval[] args = { numArg, formatArg }; ValueEval result = TextFunction.TEXT.evaluate(args, -1, (short)-1); - char groupSeparator = new DecimalFormatSymbols(LocaleUtil.getUserLocale()).getGroupingSeparator(); - char decimalSeparator = new DecimalFormatSymbols(LocaleUtil.getUserLocale()).getDecimalSeparator(); + DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale()); + char groupSeparator = dfs.getGroupingSeparator(); + char decimalSeparator = dfs.getDecimalSeparator(); ValueEval testResult = new StringEval("321" + groupSeparator + "321" + decimalSeparator + "32100"); assertEquals(testResult.toString(), result.toString()); numArg = new NumberEval(321.321); diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestHyperlink.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestHyperlink.java index d96cc3bbae..91b83861db 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestHyperlink.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestHyperlink.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotSame; +import java.io.IOException; import java.util.List; import org.junit.Test; @@ -41,13 +42,13 @@ public abstract class BaseTestHyperlink { } @Test - public final void testBasicTypes(){ - Workbook wb = _testDataProvider.createWorkbook(); - CreationHelper createHelper = wb.getCreationHelper(); + public final void testBasicTypes() throws IOException { + Workbook wb1 = _testDataProvider.createWorkbook(); + CreationHelper createHelper = wb1.getCreationHelper(); Cell cell; Hyperlink link; - Sheet sheet = wb.createSheet("Hyperlinks"); + Sheet sheet = wb1.createSheet("Hyperlinks"); //URL cell = sheet.createRow(0).createCell((short) 0); @@ -74,7 +75,7 @@ public abstract class BaseTestHyperlink { //link to a place in this workbook //create a target sheet and cell - Sheet sheet2 = wb.createSheet("Target Sheet"); + Sheet sheet2 = wb1.createSheet("Target Sheet"); sheet2.createRow(0).createCell((short) 0).setCellValue("Target Cell"); cell = sheet.createRow(3).createCell((short) 0); @@ -83,9 +84,10 @@ public abstract class BaseTestHyperlink { link.setAddress("'Target Sheet'!A1"); cell.setHyperlink(link); - wb = _testDataProvider.writeOutAndReadBack(wb); + Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1); + wb1.close(); - sheet = wb.getSheetAt(0); + sheet = wb2.getSheetAt(0); link = sheet.getRow(0).getCell(0).getHyperlink(); assertEquals("http://poi.apache.org/", link.getAddress()); @@ -95,11 +97,13 @@ public abstract class BaseTestHyperlink { assertEquals("mailto:poi@apache.org?subject=Hyperlinks", link.getAddress()); link = sheet.getRow(3).getCell(0).getHyperlink(); assertEquals("'Target Sheet'!A1", link.getAddress()); + + wb2.close(); } // copy a hyperlink via the copy constructor @Test - public void testCopyHyperlink() { + public void testCopyHyperlink() throws IOException { final Workbook wb = _testDataProvider.createWorkbook(); final CreationHelper createHelper = wb.getCreationHelper(); @@ -136,6 +140,8 @@ public abstract class BaseTestHyperlink { assertEquals(2, actualHyperlinks.size()); assertEquals(link1, actualHyperlinks.get(0)); assertEquals(link2, actualHyperlinks.get(1)); + + wb.close(); } public abstract Hyperlink copyHyperlink(Hyperlink link); -- 2.39.5