From: Josh Micich Date: Sat, 11 Apr 2009 17:53:39 +0000 (+0000) Subject: Improvements to Test[HX]SSFCell. Small fix to XSSFCell to get one more common test... X-Git-Tag: REL_3_5_BETA6~61 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3514cbb38c2ac8e5a74fc7f312f955cb155c834e;p=poi.git Improvements to Test[HX]SSFCell. Small fix to XSSFCell to get one more common test working. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@764225 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 07d09fdbe8..f0ad410adf 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -858,6 +858,10 @@ public final class XSSFCell implements Cell { */ private boolean convertCellValueToBoolean() { int cellType = getCellType(); + + if (cellType == CELL_TYPE_FORMULA) { + cellType = getBaseCellType(false); + } switch (cellType) { case CELL_TYPE_BOOLEAN: @@ -870,7 +874,6 @@ public final class XSSFCell implements Cell { case CELL_TYPE_NUMERIC: return Double.parseDouble(cell.getV()) != 0; - case CELL_TYPE_FORMULA: case CELL_TYPE_ERROR: case CELL_TYPE_BLANK: return false; 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 f73368650c..b851d8827e 100755 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java @@ -17,8 +17,7 @@ package org.apache.poi.xssf.usermodel; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.ss.ITestDataProvider; +import org.apache.poi.ss.usermodel.BaseTestCell; import org.apache.poi.xssf.XSSFITestDataProvider; /** @@ -26,21 +25,7 @@ import org.apache.poi.xssf.XSSFITestDataProvider; */ public final class TestXSSFCell extends BaseTestCell { - @Override - protected ITestDataProvider getTestDataProvider(){ - return XSSFITestDataProvider.getInstance(); - } - - public void testChangeCellType() { - //for performance reasons combine baseTestChangeType* together - Workbook wb = getTestDataProvider().createWorkbook(); - Row row = wb.createSheet().createRow(0); - baseTestChangeTypeStringToBool(row.createCell(0)); - baseTestChangeTypeBoolToString(row.createCell(1)); - baseTestChangeTypeErrorToNumber(row.createCell(2)); - baseTestChangeTypeErrorToBoolean(row.createCell(3)); - - //TODO: works in HSSF but fails in XSSF - //baseTestChangeTypeFormulaToBoolean(row.createCell(4)); - } + public TestXSSFCell() { + super(XSSFITestDataProvider.getInstance()); + } } \ No newline at end of file diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java index 2e95772216..fdad33fec2 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java @@ -21,14 +21,15 @@ import java.util.Date; import java.util.GregorianCalendar; import junit.framework.AssertionFailedError; + import org.apache.poi.hssf.HSSFITestDataProvider; import org.apache.poi.hssf.model.Sheet; import org.apache.poi.hssf.record.DBCellRecord; import org.apache.poi.hssf.record.FormulaRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.StringRecord; -import org.apache.poi.ss.usermodel.ErrorConstants; import org.apache.poi.ss.usermodel.BaseTestCell; +import org.apache.poi.ss.usermodel.ErrorConstants; /** * Tests various functionality having to do with {@link HSSFCell}. For instance support for @@ -38,12 +39,12 @@ import org.apache.poi.ss.usermodel.BaseTestCell; * @author Alex Jacoby (ajacoby at gmail.com) */ public final class TestHSSFCell extends BaseTestCell { - - @Override - protected HSSFITestDataProvider getTestDataProvider(){ - return HSSFITestDataProvider.getInstance(); - } - + + private static final HSSFITestDataProvider _hssfDP = HSSFITestDataProvider.getInstance(); + + public TestHSSFCell() { + super(HSSFITestDataProvider.getInstance()); + } /** * Checks that the recognition of files using 1904 date windowing * is working properly. Conversion of the date is also an issue, @@ -54,7 +55,7 @@ public final class TestHSSFCell extends BaseTestCell { Date date = cal.getTime(); // first check a file with 1900 Date Windowing - HSSFWorkbook workbook = getTestDataProvider().openSampleWorkbook("1900DateWindowing.xls"); + HSSFWorkbook workbook = _hssfDP.openSampleWorkbook("1900DateWindowing.xls"); HSSFSheet sheet = workbook.getSheetAt(0); assertEquals("Date from file using 1900 Date Windowing", @@ -62,7 +63,7 @@ public final class TestHSSFCell extends BaseTestCell { sheet.getRow(0).getCell(0).getDateCellValue().getTime()); // now check a file with 1904 Date Windowing - workbook = getTestDataProvider().openSampleWorkbook("1904DateWindowing.xls"); + workbook = _hssfDP.openSampleWorkbook("1904DateWindowing.xls"); sheet = workbook.getSheetAt(0); assertEquals("Date from file using 1904 Date Windowing", @@ -70,6 +71,8 @@ public final class TestHSSFCell extends BaseTestCell { sheet.getRow(0).getCell(0).getDateCellValue().getTime()); } + + /** * Checks that dates are properly written to both types of files: * those with 1900 and 1904 date windowing. Note that if the @@ -82,19 +85,19 @@ public final class TestHSSFCell extends BaseTestCell { // first check a file with 1900 Date Windowing HSSFWorkbook wb; - wb = getTestDataProvider().openSampleWorkbook("1900DateWindowing.xls"); + wb = _hssfDP.openSampleWorkbook("1900DateWindowing.xls"); setCell(wb, 0, 1, date); - wb = getTestDataProvider().writeOutAndReadBack(wb); + wb = _hssfDP.writeOutAndReadBack(wb); assertEquals("Date from file using 1900 Date Windowing", date.getTime(), readCell(wb, 0, 1).getTime()); // now check a file with 1904 Date Windowing - wb = getTestDataProvider().openSampleWorkbook("1904DateWindowing.xls"); + wb = _hssfDP.openSampleWorkbook("1904DateWindowing.xls"); setCell(wb, 0, 1, date); - wb = getTestDataProvider().writeOutAndReadBack(wb); + wb = _hssfDP.writeOutAndReadBack(wb); assertEquals("Date from file using 1900 Date Windowing", date.getTime(), readCell(wb, 0, 1).getTime()); @@ -123,7 +126,7 @@ public final class TestHSSFCell extends BaseTestCell { */ public void testActiveCell() { //read in sample - HSSFWorkbook book = getTestDataProvider().openSampleWorkbook("Simple.xls"); + HSSFWorkbook book = _hssfDP.openSampleWorkbook("Simple.xls"); //check initial position HSSFSheet umSheet = book.getSheetAt(0); @@ -142,7 +145,7 @@ public final class TestHSSFCell extends BaseTestCell { 3, s.getActiveCellRow()); //write book to temp file; read and verify that position is serialized - book = getTestDataProvider().writeOutAndReadBack(book); + book = _hssfDP.writeOutAndReadBack(book); umSheet = book.getSheetAt(0); s = umSheet.getSheet(); @@ -158,7 +161,7 @@ public final class TestHSSFCell extends BaseTestCell { */ public void testWithHyperlink() { - HSSFWorkbook wb = getTestDataProvider().openSampleWorkbook("WithHyperlink.xls"); + HSSFWorkbook wb = _hssfDP.openSampleWorkbook("WithHyperlink.xls"); HSSFSheet sheet = wb.getSheetAt(0); HSSFCell cell = sheet.getRow(4).getCell(0); @@ -176,7 +179,7 @@ public final class TestHSSFCell extends BaseTestCell { */ public void testWithTwoHyperlinks() { - HSSFWorkbook wb = getTestDataProvider().openSampleWorkbook("WithTwoHyperLinks.xls"); + HSSFWorkbook wb = _hssfDP.openSampleWorkbook("WithTwoHyperLinks.xls"); HSSFSheet sheet = wb.getSheetAt(0); @@ -196,17 +199,6 @@ public final class TestHSSFCell extends BaseTestCell { assertEquals(8, link2.getFirstRow()); assertEquals(1, link2.getFirstColumn()); } - - public void testSetStringInFormulaCell_bug44606() { - HSSFWorkbook wb = getTestDataProvider().createWorkbook(); - HSSFCell cell = wb.createSheet("Sheet1").createRow(0).createCell(0); - cell.setCellFormula("B1&C1"); - try { - cell.setCellValue(new HSSFRichTextString("hello")); - } catch (ClassCastException e) { - throw new AssertionFailedError("Identified bug 44606"); - } - } /** * Test to ensure we can only assign cell styles that belong @@ -253,34 +245,6 @@ public final class TestHSSFCell extends BaseTestCell { } } - public void testChangeTypeStringToBool() { - HSSFCell cell = new HSSFWorkbook().createSheet("Sheet1").createRow(0).createCell(0); - baseTestChangeTypeStringToBool(cell); - } - - public void testChangeTypeBoolToString() { - HSSFCell cell = new HSSFWorkbook().createSheet("Sheet1").createRow(0).createCell(0); - baseTestChangeTypeBoolToString(cell); - } - - public void testChangeTypeErrorToNumber_bug46479() { - HSSFCell cell = new HSSFWorkbook().createSheet("Sheet1").createRow(0).createCell(0); - baseTestChangeTypeErrorToNumber(cell); - } - - public void testChangeTypeErrorToBoolean_bug46479() { - HSSFCell cell = new HSSFWorkbook().createSheet("Sheet1").createRow(0).createCell(0); - baseTestChangeTypeErrorToBoolean(cell); - } - - /** - * Test for bug in convertCellValueToBoolean to make sure that formula results get converted - */ - public void testChangeTypeFormulaToBoolean_bug46479() { - HSSFCell cell = new HSSFWorkbook().createSheet("Sheet1").createRow(0).createCell(0); - baseTestChangeTypeFormulaToBoolean(cell); - } - /** * Test for small bug observable around r736460 (prior to version 3.5). POI fails to remove * the {@link StringRecord} following the {@link FormulaRecord} after the result type had been diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index c5259feef2..5649c950e8 100755 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -17,12 +17,13 @@ package org.apache.poi.ss.usermodel; -import junit.framework.TestCase; +import java.util.Calendar; + import junit.framework.AssertionFailedError; -import org.apache.poi.ss.ITestDataProvider; +import junit.framework.TestCase; -import java.util.Date; -import java.util.Calendar; +import org.apache.poi.hssf.usermodel.HSSFRichTextString; +import org.apache.poi.ss.ITestDataProvider; /** * Common superclass for testing implementatiosn of @@ -30,240 +31,249 @@ import java.util.Calendar; */ public abstract class BaseTestCell extends TestCase { - /** - * @return an object that provides test data in HSSF / XSSF specific way - */ - protected abstract ITestDataProvider getTestDataProvider(); - - public void testSetValues() { - Workbook book = getTestDataProvider().createWorkbook(); - Sheet sheet = book.createSheet("test"); - Row row = sheet.createRow(0); - - CreationHelper factory = book.getCreationHelper(); - Cell cell = row.createCell(0); - - cell.setCellValue(1.2); - assertEquals(1.2, cell.getNumericCellValue(), 0.0001); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); - - cell.setCellValue(false); - assertEquals(false, cell.getBooleanCellValue()); - assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType()); - cell.setCellValue(true); - assertEquals(true, cell.getBooleanCellValue()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_STRING, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); - - cell.setCellValue(factory.createRichTextString("Foo")); - assertEquals("Foo", cell.getRichStringCellValue().getString()); - assertEquals("Foo", cell.getStringCellValue()); - assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); - - cell.setCellValue("345"); - assertEquals("345", cell.getRichStringCellValue().getString()); - assertEquals("345", cell.getStringCellValue()); - assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); - - Calendar c = Calendar.getInstance(); - c.setTimeInMillis(123456789); - cell.setCellValue(c.getTime()); - assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime()); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); - - cell.setCellValue(c); - assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime()); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); - - cell.setCellErrorValue(FormulaError.NA.getCode()); - assertEquals(FormulaError.NA.getCode(), cell.getErrorCellValue()); - assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType()); - assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN, - Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_STRING); - } - - private void assertProhibitedValueAccess(Cell cell, int ... types){ - for(int type : types){ - try { - switch (type) { - case Cell.CELL_TYPE_NUMERIC: - cell.getNumericCellValue(); - fail(); - break; - case Cell.CELL_TYPE_STRING: - cell.getStringCellValue(); - fail(); - break; - case Cell.CELL_TYPE_BOOLEAN: - cell.getBooleanCellValue(); - fail(); - break; - case Cell.CELL_TYPE_FORMULA: - cell.getCellFormula(); - fail(); - break; - case Cell.CELL_TYPE_ERROR: - cell.getErrorCellValue(); - fail(); - break; - } - } catch (IllegalStateException e){ - ; - } - } - } - - /** - * test that Boolean and Error types (BoolErrRecord) are supported properly. - */ - public void testBoolErr() { - - Workbook wb = getTestDataProvider().createWorkbook(); - Sheet s = wb.createSheet("testSheet1"); - Row r = null; - Cell c = null; - r = s.createRow(0); - c=r.createCell(1); - //c.setCellType(HSSFCell.CELL_TYPE_BOOLEAN); - c.setCellValue(true); - - c=r.createCell(2); - //c.setCellType(HSSFCell.CELL_TYPE_BOOLEAN); - c.setCellValue(false); - - r = s.createRow(1); - c=r.createCell(1); - //c.setCellType(HSSFCell.CELL_TYPE_ERROR); - c.setCellErrorValue((byte)0); - - c=r.createCell(2); - //c.setCellType(HSSFCell.CELL_TYPE_ERROR); - c.setCellErrorValue((byte)7); - - wb = getTestDataProvider().writeOutAndReadBack(wb); - s = wb.getSheetAt(0); - r = s.getRow(0); - c = r.getCell(1); - assertTrue("boolean value 0,1 = true",c.getBooleanCellValue()); - c = r.getCell(2); - assertTrue("boolean value 0,2 = false",c.getBooleanCellValue()==false); - r = s.getRow(1); - c = r.getCell(1); - assertTrue("boolean value 0,1 = 0",c.getErrorCellValue() == 0); - c = r.getCell(2); - assertTrue("boolean value 0,2 = 7",c.getErrorCellValue() == 7); - } - - /** - * test that Cell Styles being applied to formulas remain intact - */ - public void testFormulaStyle() { - - Workbook wb = getTestDataProvider().createWorkbook(); - Sheet s = wb.createSheet("testSheet1"); - Row r = null; - Cell c = null; - CellStyle cs = wb.createCellStyle(); - Font f = wb.createFont(); - f.setFontHeightInPoints((short) 20); - f.setColor(IndexedColors.RED.getIndex()); - f.setBoldweight(f.BOLDWEIGHT_BOLD); - f.setFontName("Arial Unicode MS"); - cs.setFillBackgroundColor((short)3); - cs.setFont(f); - cs.setBorderTop((short)1); - cs.setBorderRight((short)1); - cs.setBorderLeft((short)1); - cs.setBorderBottom((short)1); - - r = s.createRow(0); - c=r.createCell(0); - c.setCellStyle(cs); - c.setCellFormula("2*3"); - - wb = getTestDataProvider().writeOutAndReadBack(wb); - s = wb.getSheetAt(0); - r = s.getRow(0); - c = r.getCell(0); - - assertTrue("Formula Cell at 0,0", (c.getCellType()==c.CELL_TYPE_FORMULA)); - cs = c.getCellStyle(); - - assertNotNull("Formula Cell Style", cs); - assertTrue("Font Index Matches", (cs.getFontIndex() == f.getIndex())); - assertTrue("Top Border", (cs.getBorderTop() == (short)1)); - assertTrue("Left Border", (cs.getBorderLeft() == (short)1)); - assertTrue("Right Border", (cs.getBorderRight() == (short)1)); - assertTrue("Bottom Border", (cs.getBorderBottom() == (short)1)); - } - - /**tests the toString() method of HSSFCell*/ - public void testToString() { - Workbook wb = getTestDataProvider().createWorkbook(); - Row r = wb.createSheet("Sheet1").createRow(0); - CreationHelper factory = wb.getCreationHelper(); - - r.createCell(0).setCellValue(true); - r.createCell(1).setCellValue(1.5); - r.createCell(2).setCellValue(factory.createRichTextString("Astring")); - r.createCell(3).setCellErrorValue((byte)ErrorConstants.ERROR_DIV_0); - r.createCell(4).setCellFormula("A1+B1"); - - assertEquals("Boolean", "TRUE", r.getCell(0).toString()); - assertEquals("Numeric", "1.5", r.getCell(1).toString()); - assertEquals("String", "Astring", r.getCell(2).toString()); - assertEquals("Error", "#DIV/0!", r.getCell(3).toString()); - assertEquals("Formula", "A1+B1", r.getCell(4).toString()); - - //Write out the file, read it in, and then check cell values - wb = getTestDataProvider().writeOutAndReadBack(wb); - - r = wb.getSheetAt(0).getRow(0); - assertEquals("Boolean", "TRUE", r.getCell(0).toString()); - assertEquals("Numeric", "1.5", r.getCell(1).toString()); - assertEquals("String", "Astring", r.getCell(2).toString()); - assertEquals("Error", "#DIV/0!", r.getCell(3).toString()); - assertEquals("Formula", "A1+B1", r.getCell(4).toString()); - } - - /** - * Test that setting cached formula result keeps the cell type - */ - public void testSetFormulaValue() { - Workbook wb = getTestDataProvider().createWorkbook(); - Sheet s = wb.createSheet(); - Row r = s.createRow(0); - - Cell c1 = r.createCell(0); - c1.setCellFormula("NA()"); - assertEquals(0.0, c1.getNumericCellValue()); - assertEquals(Cell.CELL_TYPE_NUMERIC, c1.getCachedFormulaResultType()); - c1.setCellValue(10); - assertEquals(10.0, c1.getNumericCellValue()); - assertEquals(Cell.CELL_TYPE_FORMULA, c1.getCellType()); - assertEquals(Cell.CELL_TYPE_NUMERIC, c1.getCachedFormulaResultType()); - - Cell c2 = r.createCell(1); - c2.setCellFormula("NA()"); - assertEquals(0.0, c2.getNumericCellValue()); - assertEquals(Cell.CELL_TYPE_NUMERIC, c2.getCachedFormulaResultType()); - c2.setCellValue("I changed!"); - assertEquals("I changed!", c2.getStringCellValue()); - assertEquals(Cell.CELL_TYPE_FORMULA, c2.getCellType()); - assertEquals(Cell.CELL_TYPE_STRING, c2.getCachedFormulaResultType()); - } - - public void baseTestChangeTypeStringToBool(Cell cell) { + private final ITestDataProvider _testDataProvider; + + /** + * @param testDataProvider an object that provides test data in HSSF / XSSF specific way + */ + protected BaseTestCell(ITestDataProvider testDataProvider) { + _testDataProvider = testDataProvider; + } + + public final void testSetValues() { + Workbook book = _testDataProvider.createWorkbook(); + Sheet sheet = book.createSheet("test"); + Row row = sheet.createRow(0); + + CreationHelper factory = book.getCreationHelper(); + Cell cell = row.createCell(0); + + cell.setCellValue(1.2); + assertEquals(1.2, cell.getNumericCellValue(), 0.0001); + assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING, + Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); + + cell.setCellValue(false); + assertEquals(false, cell.getBooleanCellValue()); + assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType()); + cell.setCellValue(true); + assertEquals(true, cell.getBooleanCellValue()); + assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_STRING, + Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); + + cell.setCellValue(factory.createRichTextString("Foo")); + assertEquals("Foo", cell.getRichStringCellValue().getString()); + assertEquals("Foo", cell.getStringCellValue()); + assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); + assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN, + Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); + + cell.setCellValue("345"); + assertEquals("345", cell.getRichStringCellValue().getString()); + assertEquals("345", cell.getStringCellValue()); + assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); + assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN, + Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); + + Calendar c = Calendar.getInstance(); + c.setTimeInMillis(123456789); + cell.setCellValue(c.getTime()); + assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime()); + assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING, + Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); + + cell.setCellValue(c); + assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime()); + assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING, + Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR); + + cell.setCellErrorValue(FormulaError.NA.getCode()); + assertEquals(FormulaError.NA.getCode(), cell.getErrorCellValue()); + assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType()); + assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN, + Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_STRING); + } + + private static void assertProhibitedValueAccess(Cell cell, int ... types){ + for(int type : types){ + try { + switch (type) { + case Cell.CELL_TYPE_NUMERIC: + cell.getNumericCellValue(); + fail(); + break; + case Cell.CELL_TYPE_STRING: + cell.getStringCellValue(); + fail(); + break; + case Cell.CELL_TYPE_BOOLEAN: + cell.getBooleanCellValue(); + fail(); + break; + case Cell.CELL_TYPE_FORMULA: + cell.getCellFormula(); + fail(); + break; + case Cell.CELL_TYPE_ERROR: + cell.getErrorCellValue(); + fail(); + break; + } + } catch (IllegalStateException e){ + ; + } + } + } + + /** + * test that Boolean and Error types (BoolErrRecord) are supported properly. + */ + public final void testBoolErr() { + + Workbook wb = _testDataProvider.createWorkbook(); + Sheet s = wb.createSheet("testSheet1"); + Row r; + Cell c; + r = s.createRow(0); + c=r.createCell(1); + //c.setCellType(HSSFCell.CELL_TYPE_BOOLEAN); + c.setCellValue(true); + + c=r.createCell(2); + //c.setCellType(HSSFCell.CELL_TYPE_BOOLEAN); + c.setCellValue(false); + + r = s.createRow(1); + c=r.createCell(1); + //c.setCellType(HSSFCell.CELL_TYPE_ERROR); + c.setCellErrorValue((byte)0); + + c=r.createCell(2); + //c.setCellType(HSSFCell.CELL_TYPE_ERROR); + c.setCellErrorValue((byte)7); + + wb = _testDataProvider.writeOutAndReadBack(wb); + s = wb.getSheetAt(0); + r = s.getRow(0); + c = r.getCell(1); + assertTrue("boolean value 0,1 = true",c.getBooleanCellValue()); + c = r.getCell(2); + assertTrue("boolean value 0,2 = false",c.getBooleanCellValue()==false); + r = s.getRow(1); + c = r.getCell(1); + assertTrue("boolean value 0,1 = 0",c.getErrorCellValue() == 0); + c = r.getCell(2); + assertTrue("boolean value 0,2 = 7",c.getErrorCellValue() == 7); + } + + /** + * test that Cell Styles being applied to formulas remain intact + */ + public final void testFormulaStyle() { + + Workbook wb = _testDataProvider.createWorkbook(); + Sheet s = wb.createSheet("testSheet1"); + Row r = null; + Cell c = null; + CellStyle cs = wb.createCellStyle(); + Font f = wb.createFont(); + f.setFontHeightInPoints((short) 20); + f.setColor(IndexedColors.RED.getIndex()); + f.setBoldweight(f.BOLDWEIGHT_BOLD); + f.setFontName("Arial Unicode MS"); + cs.setFillBackgroundColor((short)3); + cs.setFont(f); + cs.setBorderTop((short)1); + cs.setBorderRight((short)1); + cs.setBorderLeft((short)1); + cs.setBorderBottom((short)1); + + r = s.createRow(0); + c=r.createCell(0); + c.setCellStyle(cs); + c.setCellFormula("2*3"); + + wb = _testDataProvider.writeOutAndReadBack(wb); + s = wb.getSheetAt(0); + r = s.getRow(0); + c = r.getCell(0); + + assertTrue("Formula Cell at 0,0", (c.getCellType()==c.CELL_TYPE_FORMULA)); + cs = c.getCellStyle(); + + assertNotNull("Formula Cell Style", cs); + assertTrue("Font Index Matches", (cs.getFontIndex() == f.getIndex())); + assertTrue("Top Border", (cs.getBorderTop() == (short)1)); + assertTrue("Left Border", (cs.getBorderLeft() == (short)1)); + assertTrue("Right Border", (cs.getBorderRight() == (short)1)); + assertTrue("Bottom Border", (cs.getBorderBottom() == (short)1)); + } + + /**tests the toString() method of HSSFCell*/ + public final void testToString() { + Workbook wb = _testDataProvider.createWorkbook(); + Row r = wb.createSheet("Sheet1").createRow(0); + CreationHelper factory = wb.getCreationHelper(); + + r.createCell(0).setCellValue(true); + r.createCell(1).setCellValue(1.5); + r.createCell(2).setCellValue(factory.createRichTextString("Astring")); + r.createCell(3).setCellErrorValue((byte)ErrorConstants.ERROR_DIV_0); + r.createCell(4).setCellFormula("A1+B1"); + + assertEquals("Boolean", "TRUE", r.getCell(0).toString()); + assertEquals("Numeric", "1.5", r.getCell(1).toString()); + assertEquals("String", "Astring", r.getCell(2).toString()); + assertEquals("Error", "#DIV/0!", r.getCell(3).toString()); + assertEquals("Formula", "A1+B1", r.getCell(4).toString()); + + //Write out the file, read it in, and then check cell values + wb = _testDataProvider.writeOutAndReadBack(wb); + + r = wb.getSheetAt(0).getRow(0); + assertEquals("Boolean", "TRUE", r.getCell(0).toString()); + assertEquals("Numeric", "1.5", r.getCell(1).toString()); + assertEquals("String", "Astring", r.getCell(2).toString()); + assertEquals("Error", "#DIV/0!", r.getCell(3).toString()); + assertEquals("Formula", "A1+B1", r.getCell(4).toString()); + } + + /** + * Test that setting cached formula result keeps the cell type + */ + public final void testSetFormulaValue() { + Workbook wb = _testDataProvider.createWorkbook(); + Sheet s = wb.createSheet(); + Row r = s.createRow(0); + + Cell c1 = r.createCell(0); + c1.setCellFormula("NA()"); + assertEquals(0.0, c1.getNumericCellValue(), 0.0); + assertEquals(Cell.CELL_TYPE_NUMERIC, c1.getCachedFormulaResultType()); + c1.setCellValue(10); + assertEquals(10.0, c1.getNumericCellValue(), 0.0); + assertEquals(Cell.CELL_TYPE_FORMULA, c1.getCellType()); + assertEquals(Cell.CELL_TYPE_NUMERIC, c1.getCachedFormulaResultType()); + + Cell c2 = r.createCell(1); + c2.setCellFormula("NA()"); + assertEquals(0.0, c2.getNumericCellValue(), 0.0); + assertEquals(Cell.CELL_TYPE_NUMERIC, c2.getCachedFormulaResultType()); + c2.setCellValue("I changed!"); + assertEquals("I changed!", c2.getStringCellValue()); + assertEquals(Cell.CELL_TYPE_FORMULA, c2.getCellType()); + assertEquals(Cell.CELL_TYPE_STRING, c2.getCachedFormulaResultType()); + } + private Cell createACell() { + return _testDataProvider.createWorkbook().createSheet("Sheet1").createRow(0).createCell(0); + } + + + public final void testChangeTypeStringToBool() { + Cell cell = createACell(); cell.setCellValue("TRUE"); assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType()); @@ -288,98 +298,112 @@ public abstract class BaseTestCell extends TestCase { assertEquals("FALSE", cell.getRichStringCellValue().getString()); } - public void baseTestChangeTypeBoolToString(Cell cell) { - cell.setCellValue(true); - try { - cell.setCellType(Cell.CELL_TYPE_STRING); - } catch (IllegalStateException e) { - if (e.getMessage().equals("Cannot get a text value from a boolean cell")) { - throw new AssertionFailedError( - "Identified bug in conversion of cell from boolean to text"); - } - throw e; - } - assertEquals("TRUE", cell.getRichStringCellValue().getString()); - } - - public void baseTestChangeTypeErrorToNumber(Cell cell) { - cell.setCellErrorValue((byte)ErrorConstants.ERROR_NAME); - try { - cell.setCellValue(2.5); - } catch (ClassCastException e) { - throw new AssertionFailedError("Identified bug 46479b"); - } - assertEquals(2.5, cell.getNumericCellValue(), 0.0); - } - - public void baseTestChangeTypeErrorToBoolean(Cell cell) { - cell.setCellErrorValue((byte)ErrorConstants.ERROR_NAME); - cell.setCellValue(true); - try { - cell.getBooleanCellValue(); - } catch (IllegalStateException e) { - if (e.getMessage().equals("Cannot get a boolean value from a error cell")) { - - throw new AssertionFailedError("Identified bug 46479c"); - } - throw e; - } - assertEquals(true, cell.getBooleanCellValue()); - } - - /** - * Test for bug in convertCellValueToBoolean to make sure that formula results get converted - */ - public void baseTestChangeTypeFormulaToBoolean(Cell cell) { - cell.setCellFormula("1=1"); - cell.setCellValue(true); - cell.setCellType(Cell.CELL_TYPE_BOOLEAN); - if (cell.getBooleanCellValue() == false) { - throw new AssertionFailedError("Identified bug 46479d"); - } - assertEquals(true, cell.getBooleanCellValue()); - } - - /** - * Bug 40296: HSSFCell.setCellFormula throws - * ClassCastException if cell is created using HSSFRow.createCell(short column, int type) - */ - public void test40296() { - Workbook wb = getTestDataProvider().createWorkbook(); - Sheet workSheet = wb.createSheet("Sheet1"); - Cell cell; - Row row = workSheet.createRow(0); - - cell = row.createCell(0, Cell.CELL_TYPE_NUMERIC); - cell.setCellValue(1.0); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - assertEquals(1.0, cell.getNumericCellValue()); - - cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC); - cell.setCellValue(2.0); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - assertEquals(2.0, cell.getNumericCellValue()); - - cell = row.createCell(2, Cell.CELL_TYPE_FORMULA); - cell.setCellFormula("SUM(A1:B1)"); - assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); - assertEquals("SUM(A1:B1)", cell.getCellFormula()); - - //serialize and check again - wb = getTestDataProvider().writeOutAndReadBack(wb); - row = wb.getSheetAt(0).getRow(0); - cell = row.getCell(0); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - assertEquals(1.0, cell.getNumericCellValue()); - - cell = row.getCell(1); - assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); - assertEquals(2.0, cell.getNumericCellValue()); - - cell = row.getCell(2); - assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); - assertEquals("SUM(A1:B1)", cell.getCellFormula()); - } - - -} \ No newline at end of file + public final void testChangeTypeBoolToString() { + Cell cell = createACell(); + + cell.setCellValue(true); + try { + cell.setCellType(Cell.CELL_TYPE_STRING); + } catch (IllegalStateException e) { + if (e.getMessage().equals("Cannot get a text value from a boolean cell")) { + throw new AssertionFailedError( + "Identified bug in conversion of cell from boolean to text"); + } + throw e; + } + assertEquals("TRUE", cell.getRichStringCellValue().getString()); + } + + public final void testChangeTypeErrorToNumber() { + Cell cell = createACell(); + cell.setCellErrorValue((byte)ErrorConstants.ERROR_NAME); + try { + cell.setCellValue(2.5); + } catch (ClassCastException e) { + throw new AssertionFailedError("Identified bug 46479b"); + } + assertEquals(2.5, cell.getNumericCellValue(), 0.0); + } + + public final void testChangeTypeErrorToBoolean() { + Cell cell = createACell(); + cell.setCellErrorValue((byte)ErrorConstants.ERROR_NAME); + cell.setCellValue(true); + try { + cell.getBooleanCellValue(); + } catch (IllegalStateException e) { + if (e.getMessage().equals("Cannot get a boolean value from a error cell")) { + + throw new AssertionFailedError("Identified bug 46479c"); + } + throw e; + } + assertEquals(true, cell.getBooleanCellValue()); + } + + /** + * Test for bug in convertCellValueToBoolean to make sure that formula results get converted + */ + public void testChangeTypeFormulaToBoolean() { + Cell cell = createACell(); + cell.setCellFormula("1=1"); + cell.setCellValue(true); + cell.setCellType(Cell.CELL_TYPE_BOOLEAN); + if (cell.getBooleanCellValue() == false) { + throw new AssertionFailedError("Identified bug 46479d"); + } + assertEquals(true, cell.getBooleanCellValue()); + } + + /** + * Bug 40296: HSSFCell.setCellFormula throws + * ClassCastException if cell is created using HSSFRow.createCell(short column, int type) + */ + public void test40296() { + Workbook wb = _testDataProvider.createWorkbook(); + Sheet workSheet = wb.createSheet("Sheet1"); + Cell cell; + Row row = workSheet.createRow(0); + + cell = row.createCell(0, Cell.CELL_TYPE_NUMERIC); + cell.setCellValue(1.0); + assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertEquals(1.0, cell.getNumericCellValue(), 0.0); + + cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC); + cell.setCellValue(2.0); + assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertEquals(2.0, cell.getNumericCellValue(), 0.0); + + cell = row.createCell(2, Cell.CELL_TYPE_FORMULA); + cell.setCellFormula("SUM(A1:B1)"); + assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); + assertEquals("SUM(A1:B1)", cell.getCellFormula()); + + //serialize and check again + wb = _testDataProvider.writeOutAndReadBack(wb); + row = wb.getSheetAt(0).getRow(0); + cell = row.getCell(0); + assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertEquals(1.0, cell.getNumericCellValue(), 0.0); + + cell = row.getCell(1); + assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertEquals(2.0, cell.getNumericCellValue(), 0.0); + + cell = row.getCell(2); + assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType()); + assertEquals("SUM(A1:B1)", cell.getCellFormula()); + } + + public final void testSetStringInFormulaCell_bug44606() { + Workbook wb = _testDataProvider.createWorkbook(); + Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0); + cell.setCellFormula("B1&C1"); + try { + cell.setCellValue(new HSSFRichTextString("hello")); + } catch (ClassCastException e) { + throw new AssertionFailedError("Identified bug 44606"); + } + } +}