Fix some IntelliJ warnings

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1808908 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2017-09-19 16:23:37 +00:00
parent a59402a37f
commit c31d6686db
6 changed files with 41 additions and 96 deletions

View File

@ -220,10 +220,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
} }
// pack.originalPackagePath = file.getAbsolutePath(); // pack.originalPackagePath = file.getAbsolutePath();
return pack; return pack;
} catch (InvalidFormatException e) { } catch (InvalidFormatException | RuntimeException e) {
IOUtils.closeQuietly(pack);
throw e;
} catch (RuntimeException e) {
IOUtils.closeQuietly(pack); IOUtils.closeQuietly(pack);
throw e; throw e;
} }
@ -299,10 +296,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
} }
pack.originalPackagePath = file.getAbsolutePath(); pack.originalPackagePath = file.getAbsolutePath();
return pack; return pack;
} catch (InvalidFormatException e) { } catch (InvalidFormatException | RuntimeException e) {
IOUtils.closeQuietly(pack);
throw e;
} catch (RuntimeException e) {
IOUtils.closeQuietly(pack); IOUtils.closeQuietly(pack);
throw e; throw e;
} }
@ -326,10 +320,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
if (pack.partList == null) { if (pack.partList == null) {
pack.getParts(); pack.getParts();
} }
} catch (InvalidFormatException e) { } catch (InvalidFormatException | RuntimeException e) {
IOUtils.closeQuietly(pack);
throw e;
} catch (RuntimeException e) {
IOUtils.closeQuietly(pack); IOUtils.closeQuietly(pack);
throw e; throw e;
} }
@ -512,12 +503,9 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
} }
String name = path.substring(path.lastIndexOf(File.separatorChar) + 1); String name = path.substring(path.lastIndexOf(File.separatorChar) + 1);
FileInputStream is = new FileInputStream(path); try (FileInputStream is = new FileInputStream(path)) {
try { addThumbnail(name, is);
addThumbnail(name, is); }
} finally {
is.close();
}
} }
/** /**
* Add a thumbnail to the package. This method is provided to make easier * Add a thumbnail to the package. This method is provided to make easier

View File

@ -393,14 +393,10 @@ public abstract class ContentTypeManager {
String contentType = element.getAttribute(CONTENT_TYPE_ATTRIBUTE_NAME); String contentType = element.getAttribute(CONTENT_TYPE_ATTRIBUTE_NAME);
addOverrideContentType(partName, contentType); addOverrideContentType(partName, contentType);
} }
} catch (URISyntaxException urie) { } catch (URISyntaxException | IOException | SAXException e) {
throw new InvalidFormatException(urie.getMessage()); throw new InvalidFormatException(e.getMessage());
} catch (SAXException e) {
throw new InvalidFormatException(e.getMessage());
} catch (IOException e) {
throw new InvalidFormatException(e.getMessage());
} }
} }
/** /**
* Save the contents type part. * Save the contents type part.

View File

@ -40,7 +40,6 @@ import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.openxml4j.OpenXML4JTestDataSamples; import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; 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.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.sl.usermodel.SlideShowFactory;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
@ -94,11 +93,8 @@ public class TestZipPackage {
private void assertEntityLimitReached(Exception e) throws UnsupportedEncodingException { private void assertEntityLimitReached(Exception e) throws UnsupportedEncodingException {
ByteArrayOutputStream str = new ByteArrayOutputStream(); ByteArrayOutputStream str = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(str, "UTF-8")); try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(str, "UTF-8"))) {
try {
e.printStackTrace(writer); e.printStackTrace(writer);
} finally {
writer.close();
} }
String string = new String(str.toByteArray(), "UTF-8"); String string = new String(str.toByteArray(), "UTF-8");
assertTrue("Had: " + string, string.contains("The parser has encountered more than")); assertTrue("Had: " + string, string.contains("The parser has encountered more than"));
@ -115,8 +111,7 @@ public class TestZipPackage {
} }
try { try {
POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-xmlbomb.xlsx")); try (POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-xmlbomb.xlsx"))) {
try {
assertNotNull(extractor); assertNotNull(extractor);
try { try {
@ -124,8 +119,6 @@ public class TestZipPackage {
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// expected due to shared strings expansion // expected due to shared strings expansion
} }
} finally {
extractor.close();
} }
} catch (POIXMLException e) { } catch (POIXMLException e) {
assertEntityLimitReached(e); assertEntityLimitReached(e);
@ -137,8 +130,7 @@ public class TestZipPackage {
Workbook wb = WorkbookFactory.create(XSSFTestDataSamples.openSamplePackage("poc-shared-strings.xlsx")); Workbook wb = WorkbookFactory.create(XSSFTestDataSamples.openSamplePackage("poc-shared-strings.xlsx"));
wb.close(); wb.close();
POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx")); try (POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx"))) {
try {
assertNotNull(extractor); assertNotNull(extractor);
try { try {
@ -146,8 +138,6 @@ public class TestZipPackage {
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// expected due to shared strings expansion // expected due to shared strings expansion
} }
} finally {
extractor.close();
} }
} }
@ -156,8 +146,7 @@ public class TestZipPackage {
boolean before = ExtractorFactory.getThreadPrefersEventExtractors(); boolean before = ExtractorFactory.getThreadPrefersEventExtractors();
ExtractorFactory.setThreadPrefersEventExtractors(true); ExtractorFactory.setThreadPrefersEventExtractors(true);
try { try {
POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx")); try (POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx"))) {
try {
assertNotNull(extractor); assertNotNull(extractor);
try { try {
@ -165,8 +154,6 @@ public class TestZipPackage {
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// expected due to shared strings expansion // expected due to shared strings expansion
} }
} finally {
extractor.close();
} }
} catch (XmlException e) { } catch (XmlException e) {
assertEntityLimitReached(e); assertEntityLimitReached(e);
@ -232,8 +219,9 @@ public class TestZipPackage {
try { try {
pkg.getParts(); pkg.getParts();
fail("Shouldn't work"); fail("Shouldn't work");
} catch (ODFNotOfficeXmlFileException e) { } catch (NotOfficeXmlFileException e) {
} catch (NotOfficeXmlFileException ne) {} // expected here
}
pkg.close(); pkg.close();
assertNotNull(pkg.getZipArchive()); assertNotNull(pkg.getZipArchive());
@ -246,8 +234,9 @@ public class TestZipPackage {
try { try {
pkg.getParts(); pkg.getParts();
fail("Shouldn't work"); fail("Shouldn't work");
} catch (ODFNotOfficeXmlFileException e) { } catch (NotOfficeXmlFileException e) {
} catch (NotOfficeXmlFileException ne) {} // expected here
}
pkg.close(); pkg.close();
assertNotNull(pkg.getZipArchive()); assertNotNull(pkg.getZipArchive());

View File

@ -220,16 +220,13 @@ public class TestSignatureInfo {
@Test @Test
public void office2007prettyPrintedRels() throws Exception { public void office2007prettyPrintedRels() throws Exception {
OPCPackage pkg = OPCPackage.open(testdata.getFile("office2007prettyPrintedRels.docx"), PackageAccess.READ); try (OPCPackage pkg = OPCPackage.open(testdata.getFile("office2007prettyPrintedRels.docx"), PackageAccess.READ)) {
try {
SignatureConfig sic = new SignatureConfig(); SignatureConfig sic = new SignatureConfig();
sic.setOpcPackage(pkg); sic.setOpcPackage(pkg);
SignatureInfo si = new SignatureInfo(); SignatureInfo si = new SignatureInfo();
si.setSignatureConfig(sic); si.setSignatureConfig(sic);
boolean isValid = si.verifySignature(); boolean isValid = si.verifySignature();
assertTrue(isValid); assertTrue(isValid);
} finally {
pkg.close();
} }
} }
@ -277,8 +274,7 @@ public class TestSignatureInfo {
}; };
for (String testFile : testFiles) { for (String testFile : testFiles) {
OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ); try (OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ)) {
try {
SignatureConfig sic = new SignatureConfig(); SignatureConfig sic = new SignatureConfig();
sic.setOpcPackage(pkg); sic.setOpcPackage(pkg);
SignatureInfo si = new SignatureInfo(); SignatureInfo si = new SignatureInfo();
@ -291,15 +287,13 @@ public class TestSignatureInfo {
} }
assertNotNull(result); assertNotNull(result);
assertEquals("test-file: "+testFile, 1, result.size()); assertEquals("test-file: " + testFile, 1, result.size());
X509Certificate signer = result.get(0); X509Certificate signer = result.get(0);
LOG.log(POILogger.DEBUG, "signer: " + signer.getSubjectX500Principal()); LOG.log(POILogger.DEBUG, "signer: " + signer.getSubjectX500Principal());
boolean b = si.verifySignature(); boolean b = si.verifySignature();
assertTrue("test-file: "+testFile, b); assertTrue("test-file: " + testFile, b);
pkg.revert(); pkg.revert();
} finally {
pkg.close();
} }
} }
} }
@ -307,8 +301,7 @@ public class TestSignatureInfo {
@Test @Test
public void getMultiSigners() throws Exception { public void getMultiSigners() throws Exception {
String testFile = "hello-world-signed-twice.docx"; String testFile = "hello-world-signed-twice.docx";
OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ); try (OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ)) {
try {
SignatureConfig sic = new SignatureConfig(); SignatureConfig sic = new SignatureConfig();
sic.setOpcPackage(pkg); sic.setOpcPackage(pkg);
SignatureInfo si = new SignatureInfo(); SignatureInfo si = new SignatureInfo();
@ -321,17 +314,15 @@ public class TestSignatureInfo {
} }
assertNotNull(result); assertNotNull(result);
assertEquals("test-file: "+testFile, 2, result.size()); assertEquals("test-file: " + testFile, 2, result.size());
X509Certificate signer1 = result.get(0); X509Certificate signer1 = result.get(0);
X509Certificate signer2 = result.get(1); X509Certificate signer2 = result.get(1);
LOG.log(POILogger.DEBUG, "signer 1: " + signer1.getSubjectX500Principal()); LOG.log(POILogger.DEBUG, "signer 1: " + signer1.getSubjectX500Principal());
LOG.log(POILogger.DEBUG, "signer 2: " + signer2.getSubjectX500Principal()); LOG.log(POILogger.DEBUG, "signer 2: " + signer2.getSubjectX500Principal());
boolean b = si.verifySignature(); boolean b = si.verifySignature();
assertTrue("test-file: "+testFile, b); assertTrue("test-file: " + testFile, b);
pkg.revert(); pkg.revert();
} finally {
pkg.close();
} }
} }
@ -703,12 +694,9 @@ public class TestSignatureInfo {
//X509Certificate x509B = x509; //X509Certificate x509B = x509;
File tpl = copy(testdata.getFile("bug58630.xlsx")); File tpl = copy(testdata.getFile("bug58630.xlsx"));
OPCPackage pkg = OPCPackage.open(tpl); try (OPCPackage pkg = OPCPackage.open(tpl)) {
try {
//SignatureConfig signatureConfig = new SignatureConfig(); //SignatureConfig signatureConfig = new SignatureConfig();
assertNotNull(pkg); assertNotNull(pkg);
} finally {
pkg.close();
} }
} }
@ -822,16 +810,10 @@ public class TestSignatureInfo {
} }
File tmpFile = new File(buildDir, "sigtest"+extension); File tmpFile = new File(buildDir, "sigtest"+extension);
OutputStream fos = new FileOutputStream(tmpFile); try (OutputStream fos = new FileOutputStream(tmpFile)) {
try { try (InputStream fis = new FileInputStream(input)) {
InputStream fis = new FileInputStream(input);
try {
IOUtils.copy(fis, fos); IOUtils.copy(fis, fos);
} finally {
fis.close();
} }
} finally {
fos.close();
} }
return tmpFile; return tmpFile;

View File

@ -121,14 +121,11 @@ public final class TestSXSSFWorkbookWithCustomZipEntrySource {
assertEquals(1, tempFiles.size()); assertEquals(1, tempFiles.size());
File tempFile = tempFiles.get(0); File tempFile = tempFiles.get(0);
assertTrue("tempFile exists?", tempFile.exists()); assertTrue("tempFile exists?", tempFile.exists());
InputStream stream = new FileInputStream(tempFile); try (InputStream stream = new FileInputStream(tempFile)) {
try {
byte[] data = IOUtils.toByteArray(stream); byte[] data = IOUtils.toByteArray(stream);
String text = new String(data, UTF_8); String text = new String(data, UTF_8);
assertFalse(text.contains(sheetName)); assertFalse(text.contains(sheetName));
assertFalse(text.contains(cellValue)); assertFalse(text.contains(cellValue));
} finally {
stream.close();
} }
workbook.dispose(); workbook.dispose();
assertFalse("tempFile deleted after dispose?", tempFile.exists()); assertFalse("tempFile deleted after dispose?", tempFile.exists());

View File

@ -246,8 +246,8 @@ public abstract class BaseTestCell {
Workbook wb1 = _testDataProvider.createWorkbook(); Workbook wb1 = _testDataProvider.createWorkbook();
Sheet s = wb1.createSheet("testSheet1"); Sheet s = wb1.createSheet("testSheet1");
Row r = null; Row r;
Cell c = null; Cell c;
CellStyle cs = wb1.createCellStyle(); CellStyle cs = wb1.createCellStyle();
Font f = wb1.createFont(); Font f = wb1.createFont();
f.setFontHeightInPoints((short) 20); f.setFontHeightInPoints((short) 20);
@ -380,8 +380,6 @@ public abstract class BaseTestCell {
/** /**
* bug 58452: Copy cell formulas containing unregistered function names * 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. * Make sure that formulas with unknown/unregistered UDFs can be written to and read back from a file.
*
* @throws IOException
*/ */
@Test @Test
public void testFormulaWithUnknownUDF() throws IOException { 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 * 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). * an problem that cell could not return error value form formula cell).
* @throws IOException
*/ */
@Test @Test
public void testGetErrorCellValueFromFormulaCell() throws IOException { public void testGetErrorCellValueFromFormulaCell() throws IOException {
Workbook wb = _testDataProvider.createWorkbook(); try (Workbook wb = _testDataProvider.createWorkbook()) {
try {
Sheet sheet = wb.createSheet(); Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0); Row row = sheet.createRow(0);
Cell cell = row.createCell(0); Cell cell = row.createCell(0);
cell.setCellFormula("SQRT(-1)"); cell.setCellFormula("SQRT(-1)");
wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(cell); wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(cell);
assertEquals(36, cell.getErrorCellValue()); 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. * The maximum length of cell contents (text) is 32,767 characters.
* @throws IOException
*/ */
@Test @Test
public void testMaxTextLength() throws IOException{ public void testMaxTextLength() throws IOException{
@ -945,7 +938,7 @@ public abstract class BaseTestCell {
: SpreadsheetVersion.EXCEL2007.getMaxTextLength(); : SpreadsheetVersion.EXCEL2007.getMaxTextLength();
assertEquals(32767, maxlen); assertEquals(32767, maxlen);
StringBuffer b = new StringBuffer() ; StringBuilder b = new StringBuilder() ;
// 32767 is okay // 32767 is okay
for( int i = 0 ; i < maxlen ; i++ ) for( int i = 0 ; i < maxlen ; i++ )