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

@ -42,111 +42,105 @@ import org.apache.poi.util.HexDump;
*/ */
public class NumberComparingSpreadsheetGenerator { public class NumberComparingSpreadsheetGenerator {
private static final class SheetWriter { private static final class SheetWriter {
private final HSSFSheet _sheet; private final HSSFSheet _sheet;
private int _rowIndex; private int _rowIndex;
public SheetWriter(HSSFWorkbook wb) { public SheetWriter(HSSFWorkbook wb) {
HSSFSheet sheet = wb.createSheet("Sheet1"); HSSFSheet sheet = wb.createSheet("Sheet1");
writeHeaderRow(wb, sheet); writeHeaderRow(wb, sheet);
_sheet = sheet; _sheet = sheet;
_rowIndex = 1; _rowIndex = 1;
} }
public void addTestRow(double a, double b, int expResult) { public void addTestRow(double a, double b, int expResult) {
writeDataRow(_sheet, _rowIndex++, a, b, expResult); writeDataRow(_sheet, _rowIndex++, a, b, expResult);
} }
} }
private static void writeHeaderCell(HSSFRow row, int i, String text, HSSFCellStyle style) { private static void writeHeaderCell(HSSFRow row, int i, String text, HSSFCellStyle style) {
HSSFCell cell = row.createCell(i); HSSFCell cell = row.createCell(i);
cell.setCellValue(new HSSFRichTextString(text)); cell.setCellValue(new HSSFRichTextString(text));
cell.setCellStyle(style); cell.setCellStyle(style);
} }
static void writeHeaderRow(HSSFWorkbook wb, HSSFSheet sheet) { static void writeHeaderRow(HSSFWorkbook wb, HSSFSheet sheet) {
sheet.setColumnWidth(0, 6000); sheet.setColumnWidth(0, 6000);
sheet.setColumnWidth(1, 6000); sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(2, 3600); sheet.setColumnWidth(2, 3600);
sheet.setColumnWidth(3, 3600); sheet.setColumnWidth(3, 3600);
sheet.setColumnWidth(4, 2400); sheet.setColumnWidth(4, 2400);
sheet.setColumnWidth(5, 2400); sheet.setColumnWidth(5, 2400);
sheet.setColumnWidth(6, 2400); sheet.setColumnWidth(6, 2400);
sheet.setColumnWidth(7, 2400); sheet.setColumnWidth(7, 2400);
sheet.setColumnWidth(8, 2400); sheet.setColumnWidth(8, 2400);
HSSFRow row = sheet.createRow(0); HSSFRow row = sheet.createRow(0);
HSSFCellStyle style = wb.createCellStyle(); HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont(); HSSFFont font = wb.createFont();
font.setBold(true); font.setBold(true);
style.setFont(font); style.setFont(font);
writeHeaderCell(row, 0, "Raw Long Bits A", style); writeHeaderCell(row, 0, "Raw Long Bits A", style);
writeHeaderCell(row, 1, "Raw Long Bits B", style); writeHeaderCell(row, 1, "Raw Long Bits B", style);
writeHeaderCell(row, 2, "Value A", style); writeHeaderCell(row, 2, "Value A", style);
writeHeaderCell(row, 3, "Value B", style); writeHeaderCell(row, 3, "Value B", style);
writeHeaderCell(row, 4, "Exp Cmp", style); writeHeaderCell(row, 4, "Exp Cmp", style);
writeHeaderCell(row, 5, "LT", style); writeHeaderCell(row, 5, "LT", style);
writeHeaderCell(row, 6, "EQ", style); writeHeaderCell(row, 6, "EQ", style);
writeHeaderCell(row, 7, "GT", style); writeHeaderCell(row, 7, "GT", style);
writeHeaderCell(row, 8, "Check", style); writeHeaderCell(row, 8, "Check", style);
} }
/** /**
* Fills a spreadsheet row with one comparison example. The two numeric values are written to * Fills a spreadsheet row with one comparison example. The two numeric values are written to
* columns C and D. Columns (F, G and H) respectively get formulas ("v0<v1", "v0=v1", "v0>v1"), * columns C and D. Columns (F, G and H) respectively get formulas ("v0<v1", "v0=v1", "v0>v1"),
* which will be evaluated by Excel. Column D gets the expected comparison result. Column I * which will be evaluated by Excel. Column D gets the expected comparison result. Column I
* gets a formula to check that Excel's comparison results match that predicted in column D. * gets a formula to check that Excel's comparison results match that predicted in column D.
* *
* @param v0 the first value to be compared * @param v0 the first value to be compared
* @param v1 the second value to be compared * @param v1 the second value to be compared
* @param expRes expected comparison result (-1, 0, or +1) * @param expRes expected comparison result (-1, 0, or +1)
*/ */
static void writeDataRow(HSSFSheet sheet, int rowIx, double v0, double v1, int expRes) { static void writeDataRow(HSSFSheet sheet, int rowIx, double v0, double v1, int expRes) {
HSSFRow row = sheet.createRow(rowIx); HSSFRow row = sheet.createRow(rowIx);
int rowNum = rowIx + 1; int rowNum = rowIx + 1;
row.createCell(0).setCellValue(formatDoubleAsHex(v0)); row.createCell(0).setCellValue(formatDoubleAsHex(v0));
row.createCell(1).setCellValue(formatDoubleAsHex(v1)); row.createCell(1).setCellValue(formatDoubleAsHex(v1));
row.createCell(2).setCellValue(v0); row.createCell(2).setCellValue(v0);
row.createCell(3).setCellValue(v1); row.createCell(3).setCellValue(v1);
row.createCell(4).setCellValue(expRes < 0 ? "LT" : expRes > 0 ? "GT" : "EQ"); row.createCell(4).setCellValue(expRes < 0 ? "LT" : expRes > 0 ? "GT" : "EQ");
row.createCell(5).setCellFormula("C" + rowNum + "<" + "D" + rowNum); row.createCell(5).setCellFormula("C" + rowNum + "<" + "D" + rowNum);
row.createCell(6).setCellFormula("C" + rowNum + "=" + "D" + rowNum); row.createCell(6).setCellFormula("C" + rowNum + "=" + "D" + rowNum);
row.createCell(7).setCellFormula("C" + rowNum + ">" + "D" + rowNum); row.createCell(7).setCellFormula("C" + rowNum + ">" + "D" + rowNum);
// TODO - bug elsewhere in POI - something wrong with encoding of NOT() function // TODO - bug elsewhere in POI - something wrong with encoding of NOT() function
String frm = "if(or(" + String frm = "if(or(" +
"and(E#='LT', F# , G#=FALSE, H#=FALSE)," + "and(E#='LT', F# , G#=FALSE, H#=FALSE)," +
"and(E#='EQ', F#=FALSE, G# , H#=FALSE)," + "and(E#='EQ', F#=FALSE, G# , H#=FALSE)," +
"and(E#='GT', F#=FALSE, G#=FALSE, H# )" + "and(E#='GT', F#=FALSE, G#=FALSE, H# )" +
"), 'OK', 'error')" ; "), 'OK', 'error')" ;
row.createCell(8).setCellFormula(frm.replaceAll("#", String.valueOf(rowNum)).replace('\'', '"')); row.createCell(8).setCellFormula(frm.replaceAll("#", String.valueOf(rowNum)).replace('\'', '"'));
} }
private static String formatDoubleAsHex(double d) { private static String formatDoubleAsHex(double d) {
long l = Double.doubleToLongBits(d); long l = Double.doubleToLongBits(d);
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()) {
SheetWriter sw = new SheetWriter(wb);
ComparisonExample[] ces = NumberComparisonExamples.getComparisonExamples();
for (ComparisonExample ce : ces) {
sw.addTestRow(ce.getA(), ce.getB(), ce.getExpectedResult());
}
HSSFWorkbook wb = new HSSFWorkbook(); File outputFile = new File("ExcelNumberCompare.xls");
SheetWriter sw = new SheetWriter(wb); try (FileOutputStream os = new FileOutputStream(outputFile)) {
ComparisonExample[] ces = NumberComparisonExamples.getComparisonExamples(); wb.write(os);
for (ComparisonExample ce : ces) { }
sw.addTestRow(ce.getA(), ce.getB(), ce.getExpectedResult()); System.out.println("Finished writing '" + outputFile.getAbsolutePath() + "'");
} }
}
File outputFile = new File("ExcelNumberCompare.xls");
try {
FileOutputStream os = new FileOutputStream(outputFile);
wb.write(os);
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
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,11 +41,10 @@ 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) {
sheetName = "DateTimes"; sheetName = "DateTimes";
} else { } else {
sheetName = "Dates"; sheetName = "Dates";
@ -64,7 +63,7 @@ public final class TestDateFormatConverter extends TestCase {
header.createCell(6).setCellValue("Excel pattern"); header.createCell(6).setCellValue("Excel pattern");
int rowNum = 1; int rowNum = 1;
for( Locale locale : DateFormat.getAvailableLocales() ) { for (Locale locale : DateFormat.getAvailableLocales()) {
try { try {
Row row = sheet.createRow(rowNum++); Row row = sheet.createRow(rowNum++);
@ -72,8 +71,8 @@ public final class TestDateFormatConverter extends TestCase {
row.createCell(1).setCellValue(locale.getDisplayName(Locale.ROOT)); row.createCell(1).setCellValue(locale.getDisplayName(Locale.ROOT));
DateFormat dateFormat; DateFormat dateFormat;
if( dates ) { if (dates) {
if( times ) { if (times) {
dateFormat = DateFormat.getDateTimeInstance(style, style, locale); dateFormat = DateFormat.getDateTimeInstance(style, style, locale);
} else { } else {
dateFormat = DateFormat.getDateInstance(style, locale); dateFormat = DateFormat.getDateInstance(style, locale);
@ -87,7 +86,7 @@ public final class TestDateFormatConverter extends TestCase {
cell.setCellValue(date); cell.setCellValue(date);
CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle(); CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
String javaDateFormatPattern = ((SimpleDateFormat)dateFormat).toPattern(); String javaDateFormatPattern = ((SimpleDateFormat) dateFormat).toPattern();
String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern); String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern);
DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat(); DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat();
@ -97,7 +96,7 @@ public final class TestDateFormatConverter extends TestCase {
cell.setCellStyle(cellStyle); cell.setCellStyle(cellStyle);
// the formula returns TRUE is the formatted date in column C equals to the string in column D // the formula returns TRUE is the formatted date in column C equals to the string in column D
row.createCell(4).setCellFormula("TEXT(C"+rowNum+",G"+rowNum+")=D" + rowNum); row.createCell(4).setCellFormula("TEXT(C" + rowNum + ",G" + rowNum + ")=D" + rowNum);
row.createCell(5).setCellValue(javaDateFormatPattern); row.createCell(5).setCellValue(javaDateFormatPattern);
row.createCell(6).setCellValue(excelFormatPattern); row.createCell(6).setCellValue(excelFormatPattern);
} catch (Exception e) { } catch (Exception e) {
@ -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();
} }
} }