From 22bdfcf39996980ab46cbfb3340a58861a89097a Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Fri, 28 Dec 2018 19:53:13 +0000 Subject: [PATCH] Run Cell-tests on HSSF as well and make handling of null-values consistent across implementations git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1849881 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/usermodel/HSSFCell.java | 10 +++++ .../poi/ss/usermodel/BaseTestXCell.java | 14 ------ .../poi/xssf/streaming/TestSXSSFCell.java | 12 ------ .../apache/poi/ss/usermodel/BaseTestCell.java | 43 +++++++++++++++++++ 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 4bdb85501d..35c234a449 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -494,6 +494,11 @@ public class HSSFCell implements Cell { */ public void setCellValue(Date value) { + if(value == null) { + setCellType(CellType.BLANK); + return; + } + setCellValue(HSSFDateUtil.getExcelDate(value, _book.getWorkbook().isUsing1904DateWindowing())); } @@ -514,6 +519,11 @@ public class HSSFCell implements Cell { */ public void setCellValue(Calendar value) { + if(value == null) { + setCellType(CellType.BLANK); + return; + } + setCellValue( HSSFDateUtil.getExcelDate(value, _book.getWorkbook().isUsing1904DateWindowing()) ); } diff --git a/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java b/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java index 0a471419ca..28d2d48936 100644 --- a/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java +++ b/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java @@ -57,18 +57,4 @@ public abstract class BaseTestXCell extends BaseTestCell { assertEquals("???<>\t\n\u00a0 &\"POI\'\u2122", wb2.getSheetAt(0).getRow(0).getCell(0).getStringCellValue()); wb2.close(); } - - @Test - public void testSetNullValues() throws IOException { - Workbook wb = _testDataProvider.createWorkbook(); - Cell cell = wb.createSheet("test").createRow(0).createCell(0); - - cell.setCellValue((Calendar)null); - cell.setCellValue((Date)null); - cell.setCellValue((String)null); - cell.setCellValue((RichTextString) null); - cell.setCellValue((String)null); - - wb.close(); - } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java index 31617253df..c027123a8a 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java @@ -83,16 +83,4 @@ public class TestSXSSFCell extends BaseTestXCell { swb.close(); } } - - @Test - public void test62216() throws IOException { - try (SXSSFWorkbook wb = new SXSSFWorkbook()) { - Cell instance = wb.createSheet().createRow(0).createCell(0); - String formula = "2"; - instance.setCellFormula(formula); - instance.setCellErrorValue(FormulaError.NAME.getCode()); - - assertEquals(formula, instance.getCellFormula()); - } - } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index 3228bbd0a1..304faaadaf 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -1061,6 +1061,49 @@ public abstract class BaseTestCell { } } + @Test + public void test62216() throws IOException { + try (Workbook wb = _testDataProvider.createWorkbook()) { + Cell instance = wb.createSheet().createRow(0).createCell(0); + String formula = "2"; + instance.setCellFormula(formula); + instance.setCellErrorValue(FormulaError.NAME.getCode()); + + assertEquals(formula, instance.getCellFormula()); + } + } + + @Test + public void testSetNullValues() throws IOException { + Workbook wb = _testDataProvider.createWorkbook(); + Cell cell = wb.createSheet("test").createRow(0).createCell(0); + + cell.setCellValue((Calendar)null); + assertEquals(CellType.BLANK, cell.getCellType()); + assertEquals("", cell.getStringCellValue()); + + cell.setCellValue((Date)null); + assertEquals(CellType.BLANK, cell.getCellType()); + assertEquals("", cell.getStringCellValue()); + + cell.setCellValue((String)null); + assertEquals(CellType.BLANK, cell.getCellType()); + assertEquals("", cell.getStringCellValue()); + + assertEquals(CellType.BLANK, cell.getCellType()); + assertEquals("", cell.getStringCellValue()); + + cell.setCellValue((RichTextString) null); + assertEquals(CellType.BLANK, cell.getCellType()); + assertEquals("", cell.getStringCellValue()); + + cell.setCellValue((String)null); + assertEquals(CellType.BLANK, cell.getCellType()); + assertEquals("", cell.getStringCellValue()); + + wb.close(); + } + @Test public void testFormulaSetValueDoesNotChangeType() throws IOException { try (Workbook wb = _testDataProvider.createWorkbook()) {