From e80e47c659ee68f3b2a7b09f9f3ac0e03bb163b6 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Fri, 11 Jan 2019 19:30:44 +0000 Subject: [PATCH] Add one more test for bug 62828 and fix some IDE warnings git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1851088 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xssf/usermodel/TestXSSFCell.java | 33 +++++++------ .../poi/xssf/usermodel/TestXSSFSheet.java | 25 ++++++---- .../poi/hssf/util/TestCellReference.java | 4 +- .../apache/poi/ss/util/TestCellReference.java | 48 ++++++++++++++----- 4 files changed, 70 insertions(+), 40 deletions(-) diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java index b044e97c06..6e83a5e6e2 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java @@ -222,7 +222,7 @@ public final class TestXSSFCell extends BaseTestXCell { assertEquals("a", cell.toString()); //Gnumeric produces spreadsheets without styles //make sure we return null for that instead of throwing OutOfBounds - assertEquals(null, cell.getCellStyle()); + assertNull(cell.getCellStyle()); //try a numeric cell cell = sh.getRow(1).getCell(0); @@ -231,7 +231,7 @@ public final class TestXSSFCell extends BaseTestXCell { assertEquals("1.0", cell.toString()); //Gnumeric produces spreadsheets without styles //make sure we return null for that instead of throwing OutOfBounds - assertEquals(null, cell.getCellStyle()); + assertNull(cell.getCellStyle()); wb.close(); } @@ -565,7 +565,7 @@ public final class TestXSSFCell extends BaseTestXCell { // Old cell value should not have been overwritten assertNotEquals(CellType.BLANK, destCell.getCellType()); assertEquals(CellType.BOOLEAN, destCell.getCellType()); - assertEquals(true, destCell.getBooleanCellValue()); + assertTrue(destCell.getBooleanCellValue()); } @Test @@ -580,12 +580,7 @@ public final class TestXSSFCell extends BaseTestXCell { srcCell.setHyperlink(link); // Set link cell style (optional) - CellStyle hlinkStyle = wb.createCellStyle(); - Font hlinkFont = wb.createFont(); - hlinkFont.setUnderline(Font.U_SINGLE); - hlinkFont.setColor(IndexedColors.BLUE.getIndex()); - hlinkStyle.setFont(hlinkFont); - srcCell.setCellStyle(hlinkStyle); + setLinkCellStyle(wb, srcCell); // Copy hyperlink final CellCopyPolicy policy = new CellCopyPolicy.Builder().copyHyperlink(true).mergeHyperlink(false).build(); @@ -604,7 +599,16 @@ public final class TestXSSFCell extends BaseTestXCell { wb.close(); } - + + private void setLinkCellStyle(Workbook wb, XSSFCell srcCell) { + CellStyle hlinkStyle = wb.createCellStyle(); + Font hlinkFont = wb.createFont(); + hlinkFont.setUnderline(Font.U_SINGLE); + hlinkFont.setColor(IndexedColors.BLUE.getIndex()); + hlinkStyle.setFont(hlinkFont); + srcCell.setCellStyle(hlinkStyle); + } + @Test public final void testCopyCellFrom_CellCopyPolicy_mergeHyperlink() throws IOException { setUp_testCopyCellFrom_CellCopyPolicy(); @@ -617,13 +621,8 @@ public final class TestXSSFCell extends BaseTestXCell { destCell.setHyperlink(link); // Set link cell style (optional) - CellStyle hlinkStyle = wb.createCellStyle(); - Font hlinkFont = wb.createFont(); - hlinkFont.setUnderline(Font.U_SINGLE); - hlinkFont.setColor(IndexedColors.BLUE.getIndex()); - hlinkStyle.setFont(hlinkFont); - destCell.setCellStyle(hlinkStyle); - + setLinkCellStyle(wb, destCell); + // Pre-condition assumptions. This test is broken if either of these fail. assertSame("unit test assumes srcCell and destCell are on the same sheet", srcCell.getSheet(), destCell.getSheet()); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 85118916c8..6e54db73ca 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -113,7 +113,7 @@ public final class TestXSSFSheet extends BaseTestXSheet { ftr = (XSSFOddFooter) s1.getFooter(); assertEquals("&Ctestdoc&Rtest phrase", hdr.getText()); - assertEquals(null, ftr.getText()); + assertNull(ftr.getText()); assertEquals("", hdr.getLeft()); assertEquals("testdoc", hdr.getCenter()); @@ -130,7 +130,7 @@ public final class TestXSSFSheet extends BaseTestXSheet { hdr = (XSSFOddHeader) s2.getHeader(); ftr = (XSSFOddFooter) s2.getFooter(); - assertEquals(null, hdr.getText()); + assertNull(hdr.getText()); assertEquals("&L&F", ftr.getText()); assertEquals("", hdr.getLeft()); @@ -964,15 +964,15 @@ public final class TestXSSFSheet extends BaseTestXSheet { //rows are sorted: {0, 1, 2} assertEquals(4, xrow[0].sizeOfCArray()); assertEquals(1, xrow[0].getR()); - assertTrue(xrow[0].equals(row3.getCTRow())); + assertEquals(xrow[0], row3.getCTRow()); assertEquals(3, xrow[1].sizeOfCArray()); assertEquals(2, xrow[1].getR()); - assertTrue(xrow[1].equals(row2.getCTRow())); + assertEquals(xrow[1], row2.getCTRow()); assertEquals(2, xrow[2].sizeOfCArray()); assertEquals(3, xrow[2].getR()); - assertTrue(xrow[2].equals(row1.getCTRow())); + assertEquals(xrow[2], row1.getCTRow()); CTCell[] xcell = xrow[0].getCArray(); assertEquals("D1", xcell[0].getR()); @@ -1487,7 +1487,7 @@ public final class TestXSSFSheet extends BaseTestXSheet { // Boolean cell = CellUtil.getCell(destRow, col++); assertEquals("[Boolean] F7 cell type", CellType.BOOLEAN, cell.getCellType()); - assertEquals("[Boolean] F7 cell value", true, cell.getBooleanCellValue()); + assertTrue("[Boolean] F7 cell value", cell.getBooleanCellValue()); // String cell = CellUtil.getCell(destRow, col++); @@ -1656,11 +1656,11 @@ public final class TestXSSFSheet extends BaseTestXSheet { col++; cell = CellUtil.getCell(destRow1, col); assertEquals("[Boolean] F10 cell type", CellType.BOOLEAN, cell.getCellType()); - assertEquals("[Boolean] F10 cell value", true, cell.getBooleanCellValue()); + assertTrue("[Boolean] F10 cell value", cell.getBooleanCellValue()); cell = CellUtil.getCell(destRow2, col); assertEquals("[Boolean] F11 cell type", CellType.BOOLEAN, cell.getCellType()); - assertEquals("[Boolean] F11 cell value", false, cell.getBooleanCellValue()); + assertFalse("[Boolean] F11 cell value", cell.getBooleanCellValue()); // String col++; @@ -1922,6 +1922,7 @@ public final class TestXSSFSheet extends BaseTestXSheet { // test regular-colored (non-indexed, ARGB) sheet expected = XSSFColor.from(CTColor.Factory.newInstance(), wb.getStylesSource().getIndexedColors()); + assertNotNull(expected); expected.setARGBHex("FF7F2700"); assertEquals(expected, wb.getSheet("customOrange").getTabColor()); } @@ -1931,7 +1932,7 @@ public final class TestXSSFSheet extends BaseTestXSheet { * See bug #52425 */ @Test - public void testInsertCommentsToClonedSheet() { + public void testInsertCommentsToClonedSheet() throws IOException { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("52425.xlsx"); CreationHelper helper = wb.getCreationHelper(); Sheet sheet2 = wb.createSheet("Sheet 2"); @@ -1942,7 +1943,9 @@ public final class TestXSSFSheet extends BaseTestXSheet { addComments(helper, sheet2); // Adding Comment to cloned Sheet 3 addComments(helper, sheet3); - } + + wb.close(); + } private void addComments(CreationHelper helper, Sheet sheet) { Drawing drawing = sheet.createDrawingPatriarch(); @@ -1986,6 +1989,8 @@ public final class TestXSSFSheet extends BaseTestXSheet { sheet.removeRow(sheet.getRow(commentCellAddress.getRow())); assertEquals("There should not be any comments left!", 0, sheet.getCellComments().size()); + + wb.close(); } @Test diff --git a/src/testcases/org/apache/poi/hssf/util/TestCellReference.java b/src/testcases/org/apache/poi/hssf/util/TestCellReference.java index ee08781e88..c7a7e10aa5 100644 --- a/src/testcases/org/apache/poi/hssf/util/TestCellReference.java +++ b/src/testcases/org/apache/poi/hssf/util/TestCellReference.java @@ -59,7 +59,9 @@ public final class TestCellReference { try { CellReference.convertColStringToIndex("A$B$"); fail("Column reference is invalid and shouldn't be accepted"); - } catch (IllegalArgumentException e) {} + } catch (IllegalArgumentException e) { + // expected here + } } @Test diff --git a/src/testcases/org/apache/poi/ss/util/TestCellReference.java b/src/testcases/org/apache/poi/ss/util/TestCellReference.java index 103d3926fb..9f9f14cbe4 100644 --- a/src/testcases/org/apache/poi/ss/util/TestCellReference.java +++ b/src/testcases/org/apache/poi/ss/util/TestCellReference.java @@ -17,13 +17,19 @@ package org.apache.poi.ss.util; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.Cell; +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 static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @@ -113,7 +119,7 @@ public final class TestCellReference { assertEquals(0, cellReference.getCol()); parts = cellReference.getCellRefParts(); assertNotNull(parts); - assertEquals(null, parts[0]); + assertNull(parts[0]); assertEquals("1", parts[1]); assertEquals("A", parts[2]); @@ -122,7 +128,7 @@ public final class TestCellReference { assertEquals(26, cellReference.getCol()); parts = cellReference.getCellRefParts(); assertNotNull(parts); - assertEquals(null, parts[0]); + assertNull(parts[0]); assertEquals("1", parts[1]); assertEquals("AA", parts[2]); @@ -131,7 +137,7 @@ public final class TestCellReference { assertEquals(26, cellReference.getCol()); parts = cellReference.getCellRefParts(); assertNotNull(parts); - assertEquals(null, parts[0]); + assertNull(parts[0]); assertEquals("100", parts[1]); assertEquals("AA", parts[2]); @@ -140,7 +146,7 @@ public final class TestCellReference { assertEquals(702, cellReference.getCol()); parts = cellReference.getCellRefParts(); assertNotNull(parts); - assertEquals(null, parts[0]); + assertNull(parts[0]); assertEquals("300", parts[1]); assertEquals("AAA", parts[2]); @@ -149,7 +155,7 @@ public final class TestCellReference { assertEquals(26*26+25, cellReference.getCol()); parts = cellReference.getCellRefParts(); assertNotNull(parts); - assertEquals(null, parts[0]); + assertNull(parts[0]); assertEquals("100521", parts[1]); assertEquals("ZZ", parts[2]); @@ -158,7 +164,7 @@ public final class TestCellReference { assertEquals(26*26*26 + 25*26 + 24 - 1, cellReference.getCol()); parts = cellReference.getCellRefParts(); assertNotNull(parts); - assertEquals(null, parts[0]); + assertNull(parts[0]); assertEquals("987", parts[1]); assertEquals("ZYX", parts[2]); @@ -166,7 +172,7 @@ public final class TestCellReference { cellReference = new CellReference(cellRef); parts = cellReference.getCellRefParts(); assertNotNull(parts); - assertEquals(null, parts[0]); + assertNull(parts[0]); assertEquals("10065", parts[1]); assertEquals("AABC", parts[2]); } @@ -350,8 +356,8 @@ public final class TestCellReference { @Test public void getSheetName() { - assertEquals(null, new CellReference("A5").getSheetName()); - assertEquals(null, new CellReference(null, 0, 0, false, false).getSheetName()); + assertNull(new CellReference("A5").getSheetName()); + assertNull(new CellReference(null, 0, 0, false, false).getSheetName()); // FIXME: CellReference is inconsistent assertEquals("", new CellReference("", 0, 0, false, false).getSheetName()); assertEquals("Sheet1", new CellReference("Sheet1!A5").getSheetName()); @@ -372,10 +378,10 @@ public final class TestCellReference { assertEquals("hash code", ref1.hashCode(), ref2.hashCode()); //noinspection ObjectEqualsNull - assertFalse("null", ref1.equals(null)); - assertFalse("3D vs 2D", ref1.equals(new CellReference("A5"))); + assertNotEquals("null", null, ref1); + assertNotEquals("3D vs 2D", ref1, new CellReference("A5")); //noinspection EqualsBetweenInconvertibleTypes - assertFalse("type", ref1.equals(new Integer(0))); + assertNotEquals("type", ref1, new Integer(0)); } @Test @@ -418,6 +424,7 @@ public final class TestCellReference { public void unquotedSheetName() { new CellReference("'Sheet 1!A5"); } + @Test(expected=IllegalArgumentException.class) public void mismatchedQuotesSheetName() { new CellReference("Sheet 1!A5"); @@ -440,6 +447,7 @@ public final class TestCellReference { public void negativeRow() { new CellReference("sheet", -2, 0, false, false); } + @Test(expected=IllegalArgumentException.class) public void negativeColumn() { new CellReference("sheet", 0, -2, false, false); @@ -449,8 +457,24 @@ public final class TestCellReference { public void classifyEmptyStringCellReference() { CellReference.classifyCellReference("", SpreadsheetVersion.EXCEL2007); } + @Test(expected=IllegalArgumentException.class) public void classifyInvalidFirstCharCellReference() { CellReference.classifyCellReference("!A5", SpreadsheetVersion.EXCEL2007); } + + @Test + public void test62828() { + Workbook wb = new HSSFWorkbook(); + final Sheet sheet = wb.createSheet("Ctor test"); + final String sheetName = sheet.getSheetName(); + final Row row = sheet.createRow(0); + final Cell cell = row.createCell(0); + final CellReference goodCellRef = new CellReference(sheetName, cell.getRowIndex(), cell.getColumnIndex(), true, + true); + final CellReference badCellRef = new CellReference(cell); + + assertEquals("'Ctor test'!$A$1", goodCellRef.formatAsString()); + assertEquals("'Ctor test'!A1", badCellRef.formatAsString()); + } } -- 2.39.5