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();
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

View File

@ -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.

View File

@ -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());

View File

@ -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;

View File

@ -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());

View File

@ -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++ )