diff options
author | Dominik Stadler <centic@apache.org> | 2015-01-05 14:03:37 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2015-01-05 14:03:37 +0000 |
commit | 959fecbb442723fab05b8b6601c5305c5ed107a1 (patch) | |
tree | a71fb723044bc54d43cedc9d7d37b95a0049bd7b /src/testcases | |
parent | 7100931eae6b36f35332df9bfccbcc08dcbc812b (diff) | |
download | poi-959fecbb442723fab05b8b6601c5305c5ed107a1.tar.gz poi-959fecbb442723fab05b8b6601c5305c5ed107a1.zip |
Move some tests to the base test class where applicapple
Introduce class BaseTestXCell to collect tests for XSSFCell and SXSSFCell that do not work for HSSFCell
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1649527 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases')
-rw-r--r-- | src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java | 115 | ||||
-rw-r--r-- | src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java | 118 |
2 files changed, 154 insertions, 79 deletions
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java index 31a1d972a4..36a48f46f6 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java @@ -34,8 +34,9 @@ import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.BaseTestCell; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.ErrorConstants; -import org.apache.poi.ss.usermodel.Hyperlink; +import org.apache.poi.ss.usermodel.FormulaError; import org.apache.poi.ss.usermodel.RichTextString; +import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; @@ -51,6 +52,7 @@ public final class TestHSSFCell extends BaseTestCell { public TestHSSFCell() { super(HSSFITestDataProvider.instance); } + /** * Checks that the recognition of files using 1904 date windowing * is working properly. Conversion of the date is also an issue, @@ -189,27 +191,31 @@ public final class TestHSSFCell extends BaseTestCell { // // fos.close(); - wb = _testDataProvider.writeOutAndReadBack(wb); - - assertEquals(1, ((HSSFSheet)wb.getSheetAt(0)).getSheet().getActiveCellRow()); - assertEquals(3, ((HSSFSheet)wb.getSheetAt(0)).getSheet().getActiveCellCol()); + Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb); + wb.close(); + + assertEquals(1, ((HSSFSheet)wbBack.getSheetAt(0)).getSheet().getActiveCellRow()); + assertEquals(3, ((HSSFSheet)wbBack.getSheetAt(0)).getSheet().getActiveCellCol()); - wb.getSheetAt(0).getRow(3).getCell(3).setAsActiveCell(); + wbBack.getSheetAt(0).getRow(3).getCell(3).setAsActiveCell(); - assertEquals(3, ((HSSFSheet)wb.getSheetAt(0)).getSheet().getActiveCellRow()); - assertEquals(3, ((HSSFSheet)wb.getSheetAt(0)).getSheet().getActiveCellCol()); + assertEquals(3, ((HSSFSheet)wbBack.getSheetAt(0)).getSheet().getActiveCellRow()); + assertEquals(3, ((HSSFSheet)wbBack.getSheetAt(0)).getSheet().getActiveCellCol()); // fos = new FileOutputStream("/tmp/56114a.xls"); // -// wb.write(fos); +// wbBack.write(fos); // // fos.close(); - wb = _testDataProvider.writeOutAndReadBack(wb); + Workbook wbBack2 = _testDataProvider.writeOutAndReadBack(wbBack); + wbBack.close(); - assertEquals(3, ((HSSFSheet)wb.getSheetAt(0)).getSheet().getActiveCellRow()); - assertEquals(3, ((HSSFSheet)wb.getSheetAt(0)).getSheet().getActiveCellCol()); + assertEquals(3, ((HSSFSheet)wbBack2.getSheetAt(0)).getSheet().getActiveCellRow()); + assertEquals(3, ((HSSFSheet)wbBack2.getSheetAt(0)).getSheet().getActiveCellCol()); + wbBack2.close(); } + /** * Test reading hyperlinks */ @@ -254,26 +260,6 @@ public final class TestHSSFCell extends BaseTestCell { assertEquals(1, link2.getFirstColumn()); } - public void testRemoveHyperlink() { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - HSSFRow row = sheet.createRow(0); - - HSSFCell cell1 = row.createCell(1); - HSSFHyperlink link1 = new HSSFHyperlink(Hyperlink.LINK_URL); - assertNotNull(link1); - cell1.removeHyperlink(); - assertNull(cell1.getHyperlink()); - - HSSFCell cell2 = row.createCell(0); - HSSFHyperlink link2 = new HSSFHyperlink(Hyperlink.LINK_URL); - assertNotNull(link2); - cell2.setHyperlink(null); - assertNull(cell2.getHyperlink()); - - HSSFTestDataSamples.writeOutAndReadBack(wb); - } - /** * Test to ensure we can only assign cell styles that belong * to our workbook, and not those from other workbooks. @@ -324,16 +310,19 @@ public final class TestHSSFCell extends BaseTestCell { * the {@link StringRecord} following the {@link FormulaRecord} after the result type had been * changed to number/boolean/error. Excel silently ignores the extra record, but some POI * versions (prior to bug 46213 / r717883) crash instead. + * @throws IOException */ - public void testCachedTypeChange() { - HSSFSheet sheet = new HSSFWorkbook().createSheet("Sheet1"); - HSSFCell cell = sheet.createRow(0).createCell(0); + public void testCachedTypeChange() throws IOException { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet("Sheet1"); + Cell cell = sheet.createRow(0).createCell(0); cell.setCellFormula("A1"); cell.setCellValue("abc"); confirmStringRecord(sheet, true); cell.setCellValue(123); Record[] recs = RecordInspector.getRecords(sheet, 0); if (recs.length == 28 && recs[23] instanceof StringRecord) { + wb.close(); throw new AssertionFailedError("Identified bug - leftover StringRecord"); } confirmStringRecord(sheet, false); @@ -349,6 +338,7 @@ public final class TestHSSFCell extends BaseTestCell { confirmStringRecord(sheet, true); cell.setCellValue(false); confirmStringRecord(sheet, false); + wb.close(); } private static void confirmStringRecord(HSSFSheet sheet, boolean isPresent) { @@ -368,9 +358,11 @@ public final class TestHSSFCell extends BaseTestCell { /** * The maximum length of cell contents (text) is 32,767 characters. + * @throws IOException */ - public void testMaxTextLength(){ - HSSFSheet sheet = new HSSFWorkbook().createSheet(); + public void testMaxTextLength() throws IOException{ + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); HSSFCell cell = sheet.createRow(0).createCell(0); int maxlen = SpreadsheetVersion.EXCEL97.getMaxTextLength(); @@ -393,6 +385,7 @@ public final class TestHSSFCell extends BaseTestCell { } catch (IllegalArgumentException e){ assertEquals("The maximum length of cell contents (text) is 32,767 characters", e.getMessage()); } + wb.close(); } /** @@ -440,11 +433,11 @@ public final class TestHSSFCell extends BaseTestCell { cell.removeCellComment(); } - public void testCellType() { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - HSSFRow row = sheet.createRow(0); - HSSFCell cell = row.createCell(0); + public void testCellType() throws IOException { + Workbook wb = _testDataProvider.createWorkbook(); + Sheet sheet = wb.createSheet(); + Row row = sheet.createRow(0); + Cell cell = row.createCell(0); cell.setCellType(Cell.CELL_TYPE_BLANK); assertNull(null, cell.getDateCellValue()); @@ -460,6 +453,7 @@ public final class TestHSSFCell extends BaseTestCell { cell.setCellType(Cell.CELL_TYPE_BOOLEAN); assertEquals("TRUE", cell.toString()); cell.setCellType(Cell.CELL_TYPE_BOOLEAN); + cell.setCellValue("" + FormulaError.VALUE.name()); cell.setCellType(Cell.CELL_TYPE_ERROR); assertEquals("#VALUE!", cell.toString()); cell.setCellType(Cell.CELL_TYPE_ERROR); @@ -479,41 +473,6 @@ public final class TestHSSFCell extends BaseTestCell { cell.setCellValue((String)null); cell.setCellValue((RichTextString)null); - } - - public void testSetRemoveStyle() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - HSSFRow row = sheet.createRow(0); - HSSFCell cell = row.createCell(0); - - HSSFCellStyle defaultStyle = wb.getCellStyleAt((short)15); - - // Starts out with the default style - assertEquals(defaultStyle, cell.getCellStyle()); - - // Create some styles, no change - HSSFCellStyle style1 = wb.createCellStyle(); - HSSFCellStyle style2 = wb.createCellStyle(); - style1.setDataFormat((short)2); - style2.setDataFormat((short)3); - - assertEquals(defaultStyle, cell.getCellStyle()); - - // Apply one, changes - cell.setCellStyle(style1); - assertEquals(style1, cell.getCellStyle()); - - // Apply the other, changes - cell.setCellStyle(style2); - assertEquals(style2, cell.getCellStyle()); - - // Remove, goes back to default - cell.setCellStyle(null); - assertEquals(defaultStyle, cell.getCellStyle()); - - // Add back, returns - cell.setCellStyle(style2); - assertEquals(style2, cell.getCellStyle()); + wb.close(); } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index a93c330fd9..35e95ab049 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -17,15 +17,17 @@ package org.apache.poi.ss.usermodel; +import java.io.IOException; import java.util.Calendar; import junit.framework.AssertionFailedError; import junit.framework.TestCase; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.ITestDataProvider; /** - * Common superclass for testing implementatiosn of + * Common superclass for testing implementations of * {@link org.apache.poi.ss.usermodel.Cell} */ public abstract class BaseTestCell extends TestCase { @@ -579,4 +581,118 @@ public abstract class BaseTestCell extends TestCase { assertFalse(style2.getHidden()); } + public void testBug55658SetNumericValue(){ + Workbook wb = _testDataProvider.createWorkbook(); + Sheet sh = wb.createSheet(); + Row row = sh.createRow(0); + Cell cell = row.createCell(0); + cell.setCellValue(Integer.valueOf(23)); + + cell.setCellValue("some"); + + cell = row.createCell(1); + cell.setCellValue(Integer.valueOf(23)); + + cell.setCellValue("24"); + + wb = _testDataProvider.writeOutAndReadBack(wb); + + assertEquals("some", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue()); + assertEquals("24", wb.getSheetAt(0).getRow(0).getCell(1).getStringCellValue()); + } + + public void testRemoveHyperlink(){ + Workbook wb = _testDataProvider.createWorkbook(); + Sheet sh = wb.createSheet("test"); + Row row = sh.createRow(0); + CreationHelper helper = wb.getCreationHelper(); + + Cell cell1 = row.createCell(1); + Hyperlink link1 = helper.createHyperlink(Hyperlink.LINK_URL); + cell1.setHyperlink(link1); + assertNotNull(cell1.getHyperlink()); + cell1.removeHyperlink(); + assertNull(cell1.getHyperlink()); + + Cell cell2 = row.createCell(0); + Hyperlink link2 = helper.createHyperlink(Hyperlink.LINK_URL); + cell2.setHyperlink(link2); + assertNotNull(cell2.getHyperlink()); + cell2.setHyperlink(null); + assertNull(cell2.getHyperlink()); + + Cell cell3 = row.createCell(2); + Hyperlink link3 = helper.createHyperlink(Hyperlink.LINK_URL); + link3.setAddress("http://poi.apache.org/"); + cell3.setHyperlink(link3); + assertNotNull(cell3.getHyperlink()); + + Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb); + assertNotNull(wbBack); + + cell1 = wbBack.getSheet("test").getRow(0).getCell(1); + assertNull(cell1.getHyperlink()); + cell2 = wbBack.getSheet("test").getRow(0).getCell(0); + assertNull(cell2.getHyperlink()); + cell3 = wbBack.getSheet("test").getRow(0).getCell(2); + assertNotNull(cell3.getHyperlink()); + } + + /** + * 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 + */ + public void testGetErrorCellValueFromFormulaCell() throws IOException { + Workbook wb = _testDataProvider.createWorkbook(); + try { + 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(); + } + } + + public void testSetRemoveStyle() throws Exception { + Workbook wb = _testDataProvider.createWorkbook(); + Sheet sheet = wb.createSheet(); + Row row = sheet.createRow(0); + Cell cell = row.createCell(0); + + // different default style indexes for HSSF and XSSF/SXSSF + CellStyle defaultStyle = wb.getCellStyleAt(wb instanceof HSSFWorkbook ? (short)15 : (short)0); + + // Starts out with the default style + assertEquals(defaultStyle, cell.getCellStyle()); + + // Create some styles, no change + CellStyle style1 = wb.createCellStyle(); + CellStyle style2 = wb.createCellStyle(); + style1.setDataFormat((short)2); + style2.setDataFormat((short)3); + + assertEquals(defaultStyle, cell.getCellStyle()); + + // Apply one, changes + cell.setCellStyle(style1); + assertEquals(style1, cell.getCellStyle()); + + // Apply the other, changes + cell.setCellStyle(style2); + assertEquals(style2, cell.getCellStyle()); + + // Remove, goes back to default + cell.setCellStyle(null); + assertEquals(defaultStyle, cell.getCellStyle()); + + // Add back, returns + cell.setCellStyle(style2); + assertEquals(style2, cell.getCellStyle()); + + wb.close(); + } } |