Fix some compiler warnings

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1808523 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2017-09-16 08:29:28 +00:00
parent 2600c60fb5
commit 1f4344a048
8 changed files with 142 additions and 161 deletions

View File

@ -77,16 +77,13 @@ public class BaseTestCellUtil {
@Test(expected=RuntimeException.class) @Test(expected=RuntimeException.class)
public void setCellStylePropertyWithInvalidValue() throws IOException { public void setCellStylePropertyWithInvalidValue() throws IOException {
Workbook wb = _testDataProvider.createWorkbook(); try (Workbook wb = _testDataProvider.createWorkbook()) {
try {
Sheet s = wb.createSheet(); Sheet s = wb.createSheet();
Row r = s.createRow(0); Row r = s.createRow(0);
Cell c = r.createCell(0); Cell c = r.createCell(0);
// An invalid BorderStyle constant // An invalid BorderStyle constant
CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, 42); CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, 42);
} finally {
wb.close();
} }
} }
@ -213,9 +210,6 @@ public class BaseTestCellUtil {
/** /**
* @deprecated by {@link #setAlignmentEnum()} * @deprecated by {@link #setAlignmentEnum()}
*
* @throws IOException
*
*/ */
@Deprecated @Deprecated
@SuppressWarnings("deprecated") @SuppressWarnings("deprecated")

View File

@ -128,25 +128,19 @@ public class NumberComparingSpreadsheetGenerator {
return HexDump.longToHex(l)+'L'; return HexDump.longToHex(l)+'L';
} }
public static void main(String[] args) { public static void main(String[] args) throws IOException {
try (HSSFWorkbook wb = new HSSFWorkbook()) {
HSSFWorkbook wb = new HSSFWorkbook();
SheetWriter sw = new SheetWriter(wb); SheetWriter sw = new SheetWriter(wb);
ComparisonExample[] ces = NumberComparisonExamples.getComparisonExamples(); ComparisonExample[] ces = NumberComparisonExamples.getComparisonExamples();
for (ComparisonExample ce : ces) { for (ComparisonExample ce : ces) {
sw.addTestRow(ce.getA(), ce.getB(), ce.getExpectedResult()); sw.addTestRow(ce.getA(), ce.getB(), ce.getExpectedResult());
} }
File outputFile = new File("ExcelNumberCompare.xls"); File outputFile = new File("ExcelNumberCompare.xls");
try (FileOutputStream os = new FileOutputStream(outputFile)) {
try {
FileOutputStream os = new FileOutputStream(outputFile);
wb.write(os); wb.write(os);
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
} }
System.out.println("Finished writing '" + outputFile.getAbsolutePath() + "'"); System.out.println("Finished writing '" + outputFile.getAbsolutePath() + "'");
} }
} }
}

View File

@ -18,6 +18,7 @@
package org.apache.poi.ss.util; package org.apache.poi.ss.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@ -115,7 +116,7 @@ final class NumberComparisonExamples {
addStepTransition(temp, 0x001000000000001BL); addStepTransition(temp, 0x001000000000001BL);
addStepTransition(temp, 0x001000000000002FL); addStepTransition(temp, 0x001000000000002FL);
for(ComparisonExample ce : new ComparisonExample[] { Collections.addAll(temp, new ComparisonExample[]{
// negative, and exponents differ by more than 1 // negative, and exponents differ by more than 1
ce(0xBF30000000000000L, 0xBE60000000000000L, -1), ce(0xBF30000000000000L, 0xBE60000000000000L, -1),
@ -137,9 +138,7 @@ final class NumberComparisonExamples {
ce(0x000FFFFFFFFFFFF9L, 0x0010000000000007L, -1), ce(0x000FFFFFFFFFFFF9L, 0x0010000000000007L, -1),
ce(0x000FFFFFFFFFFFFAL, 0x0010000000000008L, -1), ce(0x000FFFFFFFFFFFFAL, 0x0010000000000008L, -1),
ce(0x000FFFFFFFFFFFFBL, 0x0010000000000008L, -1), ce(0x000FFFFFFFFFFFFBL, 0x0010000000000008L, -1),
}) { });
temp.add(ce);
}
ComparisonExample[] result = new ComparisonExample[temp.size()]; ComparisonExample[] result = new ComparisonExample[temp.size()];
temp.toArray(result); temp.toArray(result);
@ -151,13 +150,11 @@ final class NumberComparisonExamples {
} }
private static void addStepTransition(List<ComparisonExample> temp, long rawBits) { private static void addStepTransition(List<ComparisonExample> temp, long rawBits) {
for(ComparisonExample ce : new ComparisonExample[] { Collections.addAll(temp, new ComparisonExample[]{
ce(rawBits - 1, rawBits + 0, 0), ce(rawBits - 1, rawBits + 0, 0),
ce(rawBits + 0, rawBits + 1, -1), ce(rawBits + 0, rawBits + 1, -1),
ce(rawBits + 1, rawBits + 2, 0), ce(rawBits + 1, rawBits + 2, 0),
}) { });
temp.add(ce);
}
} }

View File

@ -136,9 +136,7 @@ public class NumberRenderingSpreadsheetGenerator {
} }
private static String formatLongAsHex(long l) { private static String formatLongAsHex(long l) {
StringBuilder sb = new StringBuilder(20); return HexDump.longToHex(l) + 'L';
sb.append(HexDump.longToHex(l)).append('L');
return sb.toString();
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -27,8 +27,6 @@ import java.util.Arrays;
/** /**
* Tests that the common CellAddress works as we need it to. * Tests that the common CellAddress works as we need it to.
* Note - some additional testing is also done in the HSSF class,
* {@link org.apache.poi.hssf.util.TestCellAddress}
*/ */
public final class TestCellAddress { public final class TestCellAddress {
@Test @Test
@ -60,6 +58,7 @@ public final class TestCellAddress {
assertNotEquals(new CellReference(4, 6), new CellReference(6, 4)); assertNotEquals(new CellReference(4, 6), new CellReference(6, 4));
} }
@SuppressWarnings("EqualsWithItself")
@Test @Test
public void testCompareTo() { public void testCompareTo() {
final CellAddress A1 = new CellAddress(0, 0); final CellAddress A1 = new CellAddress(0, 0);
@ -89,7 +88,7 @@ public final class TestCellAddress {
CellAddress[] sorted = {A1, B1, A2, B2}; CellAddress[] sorted = {A1, B1, A2, B2};
CellAddress[] unsorted = {B1, B2, A1, A2}; CellAddress[] unsorted = {B1, B2, A1, A2};
assumeTrue(!sorted.equals(unsorted)); assumeTrue(!Arrays.equals(sorted, unsorted));
Arrays.sort(unsorted); Arrays.sort(unsorted);
assertArrayEquals(sorted, unsorted); assertArrayEquals(sorted, unsorted);
} }

View File

@ -20,7 +20,6 @@ package org.apache.poi.ss.util;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -53,8 +52,9 @@ public final class TestCellRangeAddress {
@Test @Test
public void testLoadInvalid() { public void testLoadInvalid() {
try { try {
assertNotNull(new CellRangeAddress( new CellRangeAddress(
TestcaseRecordInputStream.create(0x000, new byte[] { (byte)0x02 }))); TestcaseRecordInputStream.create(0x000, new byte[] { (byte)0x02 }));
fail();
} catch (RuntimeException e) { } catch (RuntimeException e) {
assertTrue("Had: " + e, e.getMessage().contains("Ran out of data")); assertTrue("Had: " + e, e.getMessage().contains("Ran out of data"));
} }
@ -66,8 +66,7 @@ public final class TestCellRangeAddress {
byte[] recordBytes; byte[] recordBytes;
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
LittleEndianOutputStream out = new LittleEndianOutputStream(baos); try (LittleEndianOutputStream out = new LittleEndianOutputStream(baos)) {
try {
// With nothing set // With nothing set
ref.serialize(out); ref.serialize(out);
recordBytes = baos.toByteArray(); recordBytes = baos.toByteArray();
@ -91,8 +90,6 @@ public final class TestCellRangeAddress {
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
assertEquals("At offset " + i, data[i], recordBytes[i]); assertEquals("At offset " + i, data[i], recordBytes[i]);
} }
} finally {
out.close();
} }
} }
@ -100,13 +97,13 @@ public final class TestCellRangeAddress {
public void testCreateIllegal() throws IOException { public void testCreateIllegal() throws IOException {
// for some combinations we expected exceptions // for some combinations we expected exceptions
try { try {
assertNotNull(new CellRangeAddress(1, 0, 0, 0)); new CellRangeAddress(1, 0, 0, 0);
fail("Expect to catch an exception"); fail("Expect to catch an exception");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected here // expected here
} }
try { try {
assertNotNull(new CellRangeAddress(0, 0, 1, 0)); new CellRangeAddress(0, 0, 1, 0);
fail("Expect to catch an exception"); fail("Expect to catch an exception");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected here // expected here

View File

@ -250,17 +250,23 @@ public final class TestCellReference {
try { try {
new CellReference("Sheet1!#REF!"); new CellReference("Sheet1!#REF!");
fail("Shouldn't be able to create a #REF! refence"); fail("Shouldn't be able to create a #REF! refence");
} catch(IllegalArgumentException expected) {} } catch(IllegalArgumentException expected) {
// expected here
}
try { try {
new CellReference("'MySheetName'!#REF!"); new CellReference("'MySheetName'!#REF!");
fail("Shouldn't be able to create a #REF! refence"); fail("Shouldn't be able to create a #REF! refence");
} catch(IllegalArgumentException expected) {} } catch(IllegalArgumentException expected) {
// expected here
}
try { try {
new CellReference("#REF!"); new CellReference("#REF!");
fail("Shouldn't be able to create a #REF! refence"); fail("Shouldn't be able to create a #REF! refence");
} catch(IllegalArgumentException expected) {} } catch(IllegalArgumentException expected) {
// expected here
}
} }
private static void confirmCrInRange(boolean expResult, String colStr, String rowStr, private static void confirmCrInRange(boolean expResult, String colStr, String rowStr,
@ -340,8 +346,10 @@ public final class TestCellReference {
assertEquals("equals", ref1, ref2); assertEquals("equals", ref1, ref2);
assertEquals("hash code", ref1.hashCode(), ref2.hashCode()); assertEquals("hash code", ref1.hashCode(), ref2.hashCode());
//noinspection ObjectEqualsNull
assertFalse("null", ref1.equals(null)); assertFalse("null", ref1.equals(null));
assertFalse("3D vs 2D", ref1.equals(new CellReference("A5"))); assertFalse("3D vs 2D", ref1.equals(new CellReference("A5")));
//noinspection EqualsBetweenInconvertibleTypes
assertFalse("type", ref1.equals(new Integer(0))); assertFalse("type", ref1.equals(new Integer(0)));
} }

View File

@ -41,8 +41,7 @@ import org.apache.poi.util.TempFile;
public final class TestDateFormatConverter extends TestCase { public final class TestDateFormatConverter extends TestCase {
private void outputLocaleDataFormats( Date date, boolean dates, boolean times, int style, String styleName ) throws Exception { private void outputLocaleDataFormats( Date date, boolean dates, boolean times, int style, String styleName ) throws Exception {
Workbook workbook = new HSSFWorkbook(); try (Workbook workbook = new HSSFWorkbook()) {
try {
String sheetName; String sheetName;
if (dates) { if (dates) {
if (times) { if (times) {
@ -107,16 +106,11 @@ public final class TestDateFormatConverter extends TestCase {
} }
File outputFile = TempFile.createTempFile("Locale" + sheetName + styleName, ".xlsx"); File outputFile = TempFile.createTempFile("Locale" + sheetName + styleName, ".xlsx");
FileOutputStream outputStream = new FileOutputStream(outputFile); try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
try {
workbook.write(outputStream); workbook.write(outputStream);
} finally {
outputStream.close();
} }
System.out.println("Open " + outputFile.getAbsolutePath() + " in Excel"); System.out.println("Open " + outputFile.getAbsolutePath() + " in Excel");
} finally {
workbook.close();
} }
} }