From 959fecbb442723fab05b8b6601c5305c5ed107a1 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Mon, 5 Jan 2015 14:03:37 +0000 Subject: [PATCH] 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 --- .../java/org/apache/poi/util/OOXMLLite.java | 2 +- .../poi/ss/usermodel/BaseTestXCell.java | 79 ++++++++++++ .../poi/xssf/streaming/TestSXSSFCell.java | 115 +++-------------- .../poi/xssf/usermodel/TestXSSFCell.java | 63 ++-------- .../poi/hssf/usermodel/TestHSSFCell.java | 115 ++++++----------- .../apache/poi/ss/usermodel/BaseTestCell.java | 118 +++++++++++++++++- 6 files changed, 265 insertions(+), 227 deletions(-) create mode 100644 src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java diff --git a/src/ooxml/java/org/apache/poi/util/OOXMLLite.java b/src/ooxml/java/org/apache/poi/util/OOXMLLite.java index 4cbfdbdf3c..3b8dc32203 100644 --- a/src/ooxml/java/org/apache/poi/util/OOXMLLite.java +++ b/src/ooxml/java/org/apache/poi/util/OOXMLLite.java @@ -89,7 +89,7 @@ public final class OOXMLLite { //collect unit tests System.out.println("Collecting unit tests from " + _testDir); collectTests(_testDir, _testDir, lst, ".+.class$", - ".+(TestUnfixedBugs|MemoryUsage|TestDataProvider|TestDataSamples|All.+Tests|ZipFileAssert|PkiTestUtils|TestCellFormatPart\\$\\d|TestSignatureInfo\\$\\d).class"); + ".+(BaseTestXCell|TestUnfixedBugs|MemoryUsage|TestDataProvider|TestDataSamples|All.+Tests|ZipFileAssert|PkiTestUtils|TestCellFormatPart\\$\\d|TestSignatureInfo\\$\\d).class"); System.out.println("Found " + lst.size() + " classes"); //run tests diff --git a/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java b/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java new file mode 100644 index 0000000000..ba55f070a1 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/ss/usermodel/BaseTestXCell.java @@ -0,0 +1,79 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.usermodel; + +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.ss.ITestDataProvider; +import org.apache.poi.xssf.SXSSFITestDataProvider; +import org.apache.poi.xssf.XSSFITestDataProvider; +import org.apache.poi.xssf.streaming.SXSSFCell; +import org.apache.poi.xssf.usermodel.XSSFCell; + +/** + * Class for combined testing of XML-specific functionality of + * {@link XSSFCell} and {@link SXSSFCell}. + * + * Any test that is applicable for {@link HSSFCell} as well should go into + * the common base class {@link BaseTestCell}. + */ +public abstract class BaseTestXCell extends BaseTestCell { + protected BaseTestXCell(ITestDataProvider testDataProvider) { + super(testDataProvider); + } + + public void testXmlEncoding(){ + Workbook wb = _testDataProvider.createWorkbook(); + Sheet sh = wb.createSheet(); + Row row = sh.createRow(0); + Cell cell = row.createCell(0); + String sval = "\u0000\u0002\u0012<>\t\n\u00a0 &\"POI\'\u2122"; + cell.setCellValue(sval); + + wb = _testDataProvider.writeOutAndReadBack(wb); + + // invalid characters are replaced with question marks + assertEquals("???<>\t\n\u00a0 &\"POI\'\u2122", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue()); + + } + + public void testEncodingbeloAscii(){ + Workbook xwb = XSSFITestDataProvider.instance.createWorkbook(); + Cell xCell = xwb.createSheet().createRow(0).createCell(0); + + Workbook swb = SXSSFITestDataProvider.instance.createWorkbook(); + Cell sCell = swb.createSheet().createRow(0).createCell(0); + + StringBuffer sb = new StringBuffer(); + // test all possible characters + for(int i = 0; i < Character.MAX_VALUE; i++) sb.append((char)i) ; + + String str = sb.toString(); + + xCell.setCellValue(str); + assertEquals(str, xCell.getStringCellValue()); + sCell.setCellValue(str); + assertEquals(str, sCell.getStringCellValue()); + + xwb = XSSFITestDataProvider.instance.writeOutAndReadBack(xwb); + swb = SXSSFITestDataProvider.instance.writeOutAndReadBack(swb); + xCell = xwb.getSheetAt(0).createRow(0).createCell(0); + sCell = swb.getSheetAt(0).createRow(0).createCell(0); + + assertEquals(xCell.getStringCellValue(), sCell.getStringCellValue()); + } +} 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 754f7e2a85..fd4399fc42 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java @@ -23,30 +23,25 @@ import java.io.IOException; import javax.xml.namespace.QName; -import org.apache.poi.ss.usermodel.BaseTestCell; +import org.apache.poi.ss.usermodel.BaseTestXCell; import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CreationHelper; -import org.apache.poi.ss.usermodel.Hyperlink; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.SXSSFITestDataProvider; -import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.xmlbeans.XmlCursor; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; /** - * + * Tests various functionality having to do with {@link SXSSFCell}. For instance support for + * particular datatypes, etc. */ -public class TestSXSSFCell extends BaseTestCell { +public class TestSXSSFCell extends BaseTestXCell { public TestSXSSFCell() { super(SXSSFITestDataProvider.instance); } - @Override public void tearDown(){ SXSSFITestDataProvider.instance.cleanup(); @@ -81,47 +76,20 @@ public class TestSXSSFCell extends BaseTestCell { "Only XSSFCells can be evaluated.", e.getMessage()); } } - - public void testXmlEncoding(){ - Workbook wb = _testDataProvider.createWorkbook(); - Sheet sh = wb.createSheet(); - Row row = sh.createRow(0); - Cell cell = row.createCell(0); - String sval = "\u0000\u0002\u0012<>\t\n\u00a0 &\"POI\'\u2122"; - cell.setCellValue(sval); - - wb = _testDataProvider.writeOutAndReadBack(wb); - - // invalid characters are replaced with question marks - assertEquals("???<>\t\n\u00a0 &\"POI\'\u2122", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue()); - - } - - public void testEncodingbeloAscii(){ - Workbook xwb = new XSSFWorkbook(); - Cell xCell = xwb.createSheet().createRow(0).createCell(0); - - Workbook swb = SXSSFITestDataProvider.instance.createWorkbook(); - Cell sCell = swb.createSheet().createRow(0).createCell(0); - - StringBuffer sb = new StringBuffer(); - // test all possible characters - for(int i = 0; i < Character.MAX_VALUE; i++) sb.append((char)i) ; - - String str = sb.toString(); - - xCell.setCellValue(str); - assertEquals(str, xCell.getStringCellValue()); - sCell.setCellValue(str); - assertEquals(str, sCell.getStringCellValue()); - - xwb = XSSFITestDataProvider.instance.writeOutAndReadBack(xwb); - swb = SXSSFITestDataProvider.instance.writeOutAndReadBack(swb); - xCell = xwb.getSheetAt(0).createRow(0).createCell(0); - sCell = swb.getSheetAt(0).createRow(0).createCell(0); - - assertEquals(xCell.getStringCellValue(), sCell.getStringCellValue()); - + + /** + * this test involves evaluation of formulas which isn't supported for SXSSF + */ + @Override + public void testGetErrorCellValueFromFormulaCell() { + try { + super.testConvertStringFormulaCell(); + fail("expected exception"); + } catch (IllegalArgumentException e){ + assertEquals( + "Unexpected type of cell: class org.apache.poi.xssf.streaming.SXSSFCell. " + + "Only XSSFCells can be evaluated.", e.getMessage()); + } } public void testPreserveSpaces() throws IOException { @@ -133,13 +101,13 @@ public class TestSXSSFCell extends BaseTestCell { "\n\nPOI \n", }; for(String str : samplesWithSpaces){ - Workbook swb = new SXSSFWorkbook(); + Workbook swb = _testDataProvider.createWorkbook(); Cell sCell = swb.createSheet().createRow(0).createCell(0); sCell.setCellValue(str); assertEquals(sCell.getStringCellValue(), str); // read back as XSSF and check that xml:spaces="preserve" is set - XSSFWorkbook xwb = (XSSFWorkbook)SXSSFITestDataProvider.instance.writeOutAndReadBack(swb); + XSSFWorkbook xwb = (XSSFWorkbook)_testDataProvider.writeOutAndReadBack(swb); XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0); CTRst is = xCell.getCTCell().getIs(); @@ -150,47 +118,4 @@ public class TestSXSSFCell extends BaseTestCell { assertEquals("expected xml:spaces=\"preserve\" \"" + str + "\"", "preserve", t); } } - - public void testBug55658SetNumericValue(){ - Workbook wb = new SXSSFWorkbook(); - 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()); - - _testDataProvider.writeOutAndReadBack(wb); - } } 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 e48f7705e8..9740e416d5 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java @@ -19,10 +19,10 @@ package org.apache.poi.xssf.usermodel; import java.io.IOException; -import org.apache.poi.ss.usermodel.BaseTestCell; +import org.apache.poi.ss.usermodel.BaseTestXCell; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DataFormatter; -import org.apache.poi.ss.usermodel.Hyperlink; +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; @@ -37,7 +37,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType; /** * @author Yegor Kozlov */ -public final class TestXSSFCell extends BaseTestCell { +public final class TestXSSFCell extends BaseTestXCell { public TestXSSFCell() { super(XSSFITestDataProvider.instance); @@ -98,28 +98,28 @@ public final class TestXSSFCell extends BaseTestCell { */ public void test47278() { XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.createWorkbook(); - XSSFSheet sheet = wb.createSheet(); - XSSFRow row = sheet.createRow(0); + Sheet sheet = wb.createSheet(); + Row row = sheet.createRow(0); SharedStringsTable sst = wb.getSharedStringSource(); assertEquals(0, sst.getCount()); //case 1. cell.setCellValue(new XSSFRichTextString((String)null)); - XSSFCell cell_0 = row.createCell(0); - XSSFRichTextString str = new XSSFRichTextString((String)null); + Cell cell_0 = row.createCell(0); + RichTextString str = new XSSFRichTextString((String)null); assertNull(str.getString()); cell_0.setCellValue(str); assertEquals(0, sst.getCount()); - assertEquals(XSSFCell.CELL_TYPE_BLANK, cell_0.getCellType()); + assertEquals(Cell.CELL_TYPE_BLANK, cell_0.getCellType()); //case 2. cell.setCellValue((String)null); - XSSFCell cell_1 = row.createCell(1); + Cell cell_1 = row.createCell(1); cell_1.setCellValue((String)null); assertEquals(0, sst.getCount()); - assertEquals(XSSFCell.CELL_TYPE_BLANK, cell_1.getCellType()); + assertEquals(Cell.CELL_TYPE_BLANK, cell_1.getCellType()); } public void testFormulaString() throws IOException { - XSSFWorkbook wb = new XSSFWorkbook(); + XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.createWorkbook(); try { XSSFCell cell = wb.createSheet().createRow(0).createCell(0); CTCell ctCell = cell.getCTCell(); //low-level bean holding cell's xml @@ -184,25 +184,6 @@ public final class TestXSSFCell extends BaseTestCell { assertEquals(null, cell.getCellStyle()); } - /** - * 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 { - XSSFWorkbook wb = new XSSFWorkbook(); - try { - XSSFSheet sheet = wb.createSheet(); - XSSFRow row = sheet.createRow(0); - XSSFCell cell = row.createCell(0); - cell.setCellFormula("SQRT(-1)"); - wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(cell); - assertEquals(36, cell.getErrorCellValue()); - } finally { - wb.close(); - } - } - public void testMissingRAttribute() { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); @@ -386,28 +367,6 @@ public final class TestXSSFCell extends BaseTestCell { } } - public void testRemoveHyperlink() { - final Workbook wb = new XSSFWorkbook(); - final Sheet sheet = wb.createSheet(); - Row row = sheet.createRow(0); - - Cell cell1 = row.createCell(1); - Hyperlink link1 = new XSSFHyperlink(Hyperlink.LINK_URL); - cell1.setHyperlink(link1); - assertNotNull(cell1.getHyperlink()); - cell1.removeHyperlink(); - assertNull(cell1.getHyperlink()); - - Cell cell2 = row.createCell(0); - Hyperlink link2 = new XSSFHyperlink(Hyperlink.LINK_URL); - cell2.setHyperlink(link2); - assertNotNull(cell2.getHyperlink()); - cell2.setHyperlink(null); - assertNull(cell2.getHyperlink()); - - XSSFTestDataSamples.writeOutAndReadBack(wb); - } - public void testBug56644ReturnNull() throws IOException { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56644.xlsx"); try { 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); + Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb); + wb.close(); + + assertEquals(1, ((HSSFSheet)wbBack.getSheetAt(0)).getSheet().getActiveCellRow()); + assertEquals(3, ((HSSFSheet)wbBack.getSheetAt(0)).getSheet().getActiveCellCol()); - assertEquals(1, ((HSSFSheet)wb.getSheetAt(0)).getSheet().getActiveCellRow()); - assertEquals(3, ((HSSFSheet)wb.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(); + } }