You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseTestCell.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertTrue;
  21. import static org.junit.Assert.fail;
  22. import java.io.IOException;
  23. import java.util.Calendar;
  24. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  25. import org.apache.poi.ss.ITestDataProvider;
  26. import org.apache.poi.ss.SpreadsheetVersion;
  27. import org.apache.poi.util.LocaleUtil;
  28. import org.junit.Test;
  29. import junit.framework.AssertionFailedError;
  30. /**
  31. * Common superclass for testing implementations of
  32. * {@link org.apache.poi.ss.usermodel.Cell}
  33. */
  34. @SuppressWarnings("deprecation")
  35. public abstract class BaseTestCell {
  36. protected final ITestDataProvider _testDataProvider;
  37. /**
  38. * @param testDataProvider an object that provides test data in HSSF / XSSF specific way
  39. */
  40. protected BaseTestCell(ITestDataProvider testDataProvider) {
  41. _testDataProvider = testDataProvider;
  42. }
  43. @Test
  44. public void testSetValues() throws Exception {
  45. Workbook book = _testDataProvider.createWorkbook();
  46. Sheet sheet = book.createSheet("test");
  47. Row row = sheet.createRow(0);
  48. CreationHelper factory = book.getCreationHelper();
  49. Cell cell = row.createCell(0);
  50. cell.setCellValue(1.2);
  51. assertEquals(1.2, cell.getNumericCellValue(), 0.0001);
  52. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  53. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING,
  54. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR);
  55. cell.setCellValue(false);
  56. assertEquals(false, cell.getBooleanCellValue());
  57. assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType());
  58. cell.setCellValue(true);
  59. assertEquals(true, cell.getBooleanCellValue());
  60. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_STRING,
  61. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR);
  62. cell.setCellValue(factory.createRichTextString("Foo"));
  63. assertEquals("Foo", cell.getRichStringCellValue().getString());
  64. assertEquals("Foo", cell.getStringCellValue());
  65. assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
  66. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN,
  67. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR);
  68. cell.setCellValue("345");
  69. assertEquals("345", cell.getRichStringCellValue().getString());
  70. assertEquals("345", cell.getStringCellValue());
  71. assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
  72. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN,
  73. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR);
  74. Calendar c = LocaleUtil.getLocaleCalendar();
  75. c.setTimeInMillis(123456789);
  76. cell.setCellValue(c.getTime());
  77. assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime());
  78. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  79. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING,
  80. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR);
  81. cell.setCellValue(c);
  82. assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime());
  83. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  84. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING,
  85. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR);
  86. cell.setCellErrorValue(FormulaError.NA.getCode());
  87. assertEquals(FormulaError.NA.getCode(), cell.getErrorCellValue());
  88. assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType());
  89. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN,
  90. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_STRING);
  91. book.close();
  92. }
  93. private static void assertProhibitedValueAccess(Cell cell, int ... types){
  94. for(int type : types){
  95. try {
  96. switch (type) {
  97. case Cell.CELL_TYPE_NUMERIC:
  98. cell.getNumericCellValue();
  99. break;
  100. case Cell.CELL_TYPE_STRING:
  101. cell.getStringCellValue();
  102. break;
  103. case Cell.CELL_TYPE_BOOLEAN:
  104. cell.getBooleanCellValue();
  105. break;
  106. case Cell.CELL_TYPE_FORMULA:
  107. cell.getCellFormula();
  108. break;
  109. case Cell.CELL_TYPE_ERROR:
  110. cell.getErrorCellValue();
  111. break;
  112. }
  113. fail("Should get exception when reading cell type (" + type + ").");
  114. } catch (IllegalStateException e){
  115. // expected during successful test
  116. assertTrue(e.getMessage().startsWith("Cannot get a"));
  117. }
  118. }
  119. }
  120. /**
  121. * test that Boolean and Error types (BoolErrRecord) are supported properly.
  122. */
  123. @Test
  124. public void testBoolErr() throws Exception {
  125. Workbook wb1 = _testDataProvider.createWorkbook();
  126. Sheet s = wb1.createSheet("testSheet1");
  127. Row r;
  128. Cell c;
  129. r = s.createRow(0);
  130. c=r.createCell(1);
  131. //c.setCellType(HSSFCell.CELL_TYPE_BOOLEAN);
  132. c.setCellValue(true);
  133. c=r.createCell(2);
  134. //c.setCellType(HSSFCell.CELL_TYPE_BOOLEAN);
  135. c.setCellValue(false);
  136. r = s.createRow(1);
  137. c=r.createCell(1);
  138. //c.setCellType(HSSFCell.CELL_TYPE_ERROR);
  139. c.setCellErrorValue((byte)0);
  140. c=r.createCell(2);
  141. //c.setCellType(HSSFCell.CELL_TYPE_ERROR);
  142. c.setCellErrorValue((byte)7);
  143. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  144. wb1.close();
  145. s = wb2.getSheetAt(0);
  146. r = s.getRow(0);
  147. c = r.getCell(1);
  148. assertTrue("boolean value 0,1 = true",c.getBooleanCellValue());
  149. c = r.getCell(2);
  150. assertTrue("boolean value 0,2 = false",c.getBooleanCellValue()==false);
  151. r = s.getRow(1);
  152. c = r.getCell(1);
  153. assertTrue("boolean value 0,1 = 0",c.getErrorCellValue() == 0);
  154. c = r.getCell(2);
  155. assertTrue("boolean value 0,2 = 7",c.getErrorCellValue() == 7);
  156. wb2.close();
  157. }
  158. /**
  159. * test that Cell Styles being applied to formulas remain intact
  160. */
  161. @Test
  162. public void testFormulaStyle() throws Exception {
  163. Workbook wb1 = _testDataProvider.createWorkbook();
  164. Sheet s = wb1.createSheet("testSheet1");
  165. Row r = null;
  166. Cell c = null;
  167. CellStyle cs = wb1.createCellStyle();
  168. Font f = wb1.createFont();
  169. f.setFontHeightInPoints((short) 20);
  170. f.setColor(IndexedColors.RED.getIndex());
  171. f.setBoldweight(Font.BOLDWEIGHT_BOLD);
  172. f.setFontName("Arial Unicode MS");
  173. cs.setFillBackgroundColor((short)3);
  174. cs.setFont(f);
  175. cs.setBorderTop((short)1);
  176. cs.setBorderRight((short)1);
  177. cs.setBorderLeft((short)1);
  178. cs.setBorderBottom((short)1);
  179. r = s.createRow(0);
  180. c=r.createCell(0);
  181. c.setCellStyle(cs);
  182. c.setCellFormula("2*3");
  183. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  184. wb1.close();
  185. s = wb2.getSheetAt(0);
  186. r = s.getRow(0);
  187. c = r.getCell(0);
  188. assertTrue("Formula Cell at 0,0", (c.getCellType()==Cell.CELL_TYPE_FORMULA));
  189. cs = c.getCellStyle();
  190. assertNotNull("Formula Cell Style", cs);
  191. assertTrue("Font Index Matches", (cs.getFontIndex() == f.getIndex()));
  192. assertTrue("Top Border", (cs.getBorderTop() == (short)1));
  193. assertTrue("Left Border", (cs.getBorderLeft() == (short)1));
  194. assertTrue("Right Border", (cs.getBorderRight() == (short)1));
  195. assertTrue("Bottom Border", (cs.getBorderBottom() == (short)1));
  196. wb2.close();
  197. }
  198. /**tests the toString() method of HSSFCell*/
  199. @Test
  200. public void testToString() throws Exception {
  201. Workbook wb1 = _testDataProvider.createWorkbook();
  202. Row r = wb1.createSheet("Sheet1").createRow(0);
  203. CreationHelper factory = wb1.getCreationHelper();
  204. r.createCell(0).setCellValue(true);
  205. r.createCell(1).setCellValue(1.5);
  206. r.createCell(2).setCellValue(factory.createRichTextString("Astring"));
  207. r.createCell(3).setCellErrorValue((byte)ErrorConstants.ERROR_DIV_0);
  208. r.createCell(4).setCellFormula("A1+B1");
  209. assertEquals("Boolean", "TRUE", r.getCell(0).toString());
  210. assertEquals("Numeric", "1.5", r.getCell(1).toString());
  211. assertEquals("String", "Astring", r.getCell(2).toString());
  212. assertEquals("Error", "#DIV/0!", r.getCell(3).toString());
  213. assertEquals("Formula", "A1+B1", r.getCell(4).toString());
  214. //Write out the file, read it in, and then check cell values
  215. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  216. wb1.close();
  217. r = wb2.getSheetAt(0).getRow(0);
  218. assertEquals("Boolean", "TRUE", r.getCell(0).toString());
  219. assertEquals("Numeric", "1.5", r.getCell(1).toString());
  220. assertEquals("String", "Astring", r.getCell(2).toString());
  221. assertEquals("Error", "#DIV/0!", r.getCell(3).toString());
  222. assertEquals("Formula", "A1+B1", r.getCell(4).toString());
  223. wb2.close();
  224. }
  225. /**
  226. * Test that setting cached formula result keeps the cell type
  227. */
  228. @Test
  229. public void testSetFormulaValue() throws Exception {
  230. Workbook wb = _testDataProvider.createWorkbook();
  231. Sheet s = wb.createSheet();
  232. Row r = s.createRow(0);
  233. Cell c1 = r.createCell(0);
  234. c1.setCellFormula("NA()");
  235. assertEquals(0.0, c1.getNumericCellValue(), 0.0);
  236. assertEquals(Cell.CELL_TYPE_NUMERIC, c1.getCachedFormulaResultType());
  237. c1.setCellValue(10);
  238. assertEquals(10.0, c1.getNumericCellValue(), 0.0);
  239. assertEquals(Cell.CELL_TYPE_FORMULA, c1.getCellType());
  240. assertEquals(Cell.CELL_TYPE_NUMERIC, c1.getCachedFormulaResultType());
  241. Cell c2 = r.createCell(1);
  242. c2.setCellFormula("NA()");
  243. assertEquals(0.0, c2.getNumericCellValue(), 0.0);
  244. assertEquals(Cell.CELL_TYPE_NUMERIC, c2.getCachedFormulaResultType());
  245. c2.setCellValue("I changed!");
  246. assertEquals("I changed!", c2.getStringCellValue());
  247. assertEquals(Cell.CELL_TYPE_FORMULA, c2.getCellType());
  248. assertEquals(Cell.CELL_TYPE_STRING, c2.getCachedFormulaResultType());
  249. //calglin Cell.setCellFormula(null) for a non-formula cell
  250. Cell c3 = r.createCell(2);
  251. c3.setCellFormula(null);
  252. assertEquals(Cell.CELL_TYPE_BLANK, c3.getCellType());
  253. wb.close();
  254. }
  255. private Cell createACell() {
  256. return _testDataProvider.createWorkbook().createSheet("Sheet1").createRow(0).createCell(0);
  257. }
  258. /**
  259. * bug 58452: Copy cell formulas containing unregistered function names
  260. * Make sure that formulas with unknown/unregistered UDFs can be written to and read back from a file.
  261. *
  262. * @throws IOException
  263. */
  264. @Test
  265. public void testFormulaWithUnknownUDF() throws IOException {
  266. final Workbook wb1 = _testDataProvider.createWorkbook();
  267. final FormulaEvaluator evaluator1 = wb1.getCreationHelper().createFormulaEvaluator();
  268. try {
  269. final Cell cell1 = wb1.createSheet().createRow(0).createCell(0);
  270. final String formula = "myFunc(\"arg\")";
  271. cell1.setCellFormula(formula);
  272. confirmFormulaWithUnknownUDF(formula, cell1, evaluator1);
  273. final Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  274. final FormulaEvaluator evaluator2 = wb2.getCreationHelper().createFormulaEvaluator();
  275. try {
  276. final Cell cell2 = wb2.getSheetAt(0).getRow(0).getCell(0);
  277. confirmFormulaWithUnknownUDF(formula, cell2, evaluator2);
  278. } finally {
  279. wb2.close();
  280. }
  281. } finally {
  282. wb1.close();
  283. }
  284. }
  285. private static void confirmFormulaWithUnknownUDF(String expectedFormula, Cell cell, FormulaEvaluator evaluator) {
  286. assertEquals(expectedFormula, cell.getCellFormula());
  287. try {
  288. evaluator.evaluate(cell);
  289. fail("Expected NotImplementedFunctionException/NotImplementedException");
  290. } catch (final org.apache.poi.ss.formula.eval.NotImplementedException e) {
  291. // expected
  292. }
  293. }
  294. @Test
  295. public void testChangeTypeStringToBool() {
  296. Cell cell = createACell();
  297. cell.setCellValue("TRUE");
  298. assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
  299. try {
  300. cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
  301. } catch (ClassCastException e) {
  302. throw new AssertionFailedError(
  303. "Identified bug in conversion of cell from text to boolean");
  304. }
  305. assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType());
  306. assertEquals(true, cell.getBooleanCellValue());
  307. cell.setCellType(Cell.CELL_TYPE_STRING);
  308. assertEquals("TRUE", cell.getRichStringCellValue().getString());
  309. // 'false' text to bool and back
  310. cell.setCellValue("FALSE");
  311. cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
  312. assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType());
  313. assertEquals(false, cell.getBooleanCellValue());
  314. cell.setCellType(Cell.CELL_TYPE_STRING);
  315. assertEquals("FALSE", cell.getRichStringCellValue().getString());
  316. }
  317. @Test
  318. public void testChangeTypeBoolToString() {
  319. Cell cell = createACell();
  320. cell.setCellValue(true);
  321. try {
  322. cell.setCellType(Cell.CELL_TYPE_STRING);
  323. } catch (IllegalStateException e) {
  324. if (e.getMessage().equals("Cannot get a text value from a boolean cell")) {
  325. throw new AssertionFailedError(
  326. "Identified bug in conversion of cell from boolean to text");
  327. }
  328. throw e;
  329. }
  330. assertEquals("TRUE", cell.getRichStringCellValue().getString());
  331. }
  332. @Test
  333. public void testChangeTypeErrorToNumber() {
  334. Cell cell = createACell();
  335. cell.setCellErrorValue((byte)ErrorConstants.ERROR_NAME);
  336. try {
  337. cell.setCellValue(2.5);
  338. } catch (ClassCastException e) {
  339. throw new AssertionFailedError("Identified bug 46479b");
  340. }
  341. assertEquals(2.5, cell.getNumericCellValue(), 0.0);
  342. }
  343. @Test
  344. public void testChangeTypeErrorToBoolean() {
  345. Cell cell = createACell();
  346. cell.setCellErrorValue((byte)ErrorConstants.ERROR_NAME);
  347. cell.setCellValue(true);
  348. try {
  349. cell.getBooleanCellValue();
  350. } catch (IllegalStateException e) {
  351. if (e.getMessage().equals("Cannot get a boolean value from a error cell")) {
  352. throw new AssertionFailedError("Identified bug 46479c");
  353. }
  354. throw e;
  355. }
  356. assertEquals(true, cell.getBooleanCellValue());
  357. }
  358. /**
  359. * Test for a bug observed around svn r886733 when using
  360. * {@link FormulaEvaluator#evaluateInCell(Cell)} with a
  361. * string result type.
  362. */
  363. @Test
  364. public void testConvertStringFormulaCell() {
  365. Cell cellA1 = createACell();
  366. cellA1.setCellFormula("\"abc\"");
  367. // default cached formula result is numeric zero
  368. assertEquals(0.0, cellA1.getNumericCellValue(), 0.0);
  369. FormulaEvaluator fe = cellA1.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
  370. fe.evaluateFormulaCell(cellA1);
  371. assertEquals("abc", cellA1.getStringCellValue());
  372. fe.evaluateInCell(cellA1);
  373. if (cellA1.getStringCellValue().equals("")) {
  374. throw new AssertionFailedError("Identified bug with writing back formula result of type string");
  375. }
  376. assertEquals("abc", cellA1.getStringCellValue());
  377. }
  378. /**
  379. * similar to {@link #testConvertStringFormulaCell()} but checks at a
  380. * lower level that {#link {@link Cell#setCellType(int)} works properly
  381. */
  382. @Test
  383. public void testSetTypeStringOnFormulaCell() {
  384. Cell cellA1 = createACell();
  385. FormulaEvaluator fe = cellA1.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
  386. cellA1.setCellFormula("\"DEF\"");
  387. fe.clearAllCachedResultValues();
  388. fe.evaluateFormulaCell(cellA1);
  389. assertEquals("DEF", cellA1.getStringCellValue());
  390. cellA1.setCellType(Cell.CELL_TYPE_STRING);
  391. assertEquals("DEF", cellA1.getStringCellValue());
  392. cellA1.setCellFormula("25.061");
  393. fe.clearAllCachedResultValues();
  394. fe.evaluateFormulaCell(cellA1);
  395. confirmCannotReadString(cellA1);
  396. assertEquals(25.061, cellA1.getNumericCellValue(), 0.0);
  397. cellA1.setCellType(Cell.CELL_TYPE_STRING);
  398. assertEquals("25.061", cellA1.getStringCellValue());
  399. cellA1.setCellFormula("TRUE");
  400. fe.clearAllCachedResultValues();
  401. fe.evaluateFormulaCell(cellA1);
  402. confirmCannotReadString(cellA1);
  403. assertEquals(true, cellA1.getBooleanCellValue());
  404. cellA1.setCellType(Cell.CELL_TYPE_STRING);
  405. assertEquals("TRUE", cellA1.getStringCellValue());
  406. cellA1.setCellFormula("#NAME?");
  407. fe.clearAllCachedResultValues();
  408. fe.evaluateFormulaCell(cellA1);
  409. confirmCannotReadString(cellA1);
  410. assertEquals(ErrorConstants.ERROR_NAME, cellA1.getErrorCellValue());
  411. cellA1.setCellType(Cell.CELL_TYPE_STRING);
  412. assertEquals("#NAME?", cellA1.getStringCellValue());
  413. }
  414. private static void confirmCannotReadString(Cell cell) {
  415. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_STRING);
  416. }
  417. /**
  418. * Test for bug in convertCellValueToBoolean to make sure that formula results get converted
  419. */
  420. @Test
  421. public void testChangeTypeFormulaToBoolean() {
  422. Cell cell = createACell();
  423. cell.setCellFormula("1=1");
  424. cell.setCellValue(true);
  425. cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
  426. if (cell.getBooleanCellValue() == false) {
  427. throw new AssertionFailedError("Identified bug 46479d");
  428. }
  429. assertEquals(true, cell.getBooleanCellValue());
  430. }
  431. /**
  432. * Bug 40296: HSSFCell.setCellFormula throws
  433. * ClassCastException if cell is created using HSSFRow.createCell(short column, int type)
  434. */
  435. @Test
  436. public void test40296() throws Exception {
  437. Workbook wb1 = _testDataProvider.createWorkbook();
  438. Sheet workSheet = wb1.createSheet("Sheet1");
  439. Cell cell;
  440. Row row = workSheet.createRow(0);
  441. cell = row.createCell(0, Cell.CELL_TYPE_NUMERIC);
  442. cell.setCellValue(1.0);
  443. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  444. assertEquals(1.0, cell.getNumericCellValue(), 0.0);
  445. cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC);
  446. cell.setCellValue(2.0);
  447. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  448. assertEquals(2.0, cell.getNumericCellValue(), 0.0);
  449. cell = row.createCell(2, Cell.CELL_TYPE_FORMULA);
  450. cell.setCellFormula("SUM(A1:B1)");
  451. assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
  452. assertEquals("SUM(A1:B1)", cell.getCellFormula());
  453. //serialize and check again
  454. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  455. wb1.close();
  456. row = wb2.getSheetAt(0).getRow(0);
  457. cell = row.getCell(0);
  458. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  459. assertEquals(1.0, cell.getNumericCellValue(), 0.0);
  460. cell = row.getCell(1);
  461. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  462. assertEquals(2.0, cell.getNumericCellValue(), 0.0);
  463. cell = row.getCell(2);
  464. assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
  465. assertEquals("SUM(A1:B1)", cell.getCellFormula());
  466. wb2.close();
  467. }
  468. @Test
  469. public void testSetStringInFormulaCell_bug44606() throws Exception {
  470. Workbook wb = _testDataProvider.createWorkbook();
  471. Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
  472. cell.setCellFormula("B1&C1");
  473. cell.setCellValue(wb.getCreationHelper().createRichTextString("hello"));
  474. wb.close();
  475. }
  476. /**
  477. * Make sure that cell.setCellType(Cell.CELL_TYPE_BLANK) preserves the cell style
  478. */
  479. @Test
  480. public void testSetBlank_bug47028() throws Exception {
  481. Workbook wb = _testDataProvider.createWorkbook();
  482. CellStyle style = wb.createCellStyle();
  483. Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
  484. cell.setCellStyle(style);
  485. int i1 = cell.getCellStyle().getIndex();
  486. cell.setCellType(Cell.CELL_TYPE_BLANK);
  487. int i2 = cell.getCellStyle().getIndex();
  488. assertEquals(i1, i2);
  489. wb.close();
  490. }
  491. /**
  492. * Excel's implementation of floating number arithmetic does not fully adhere to IEEE 754:
  493. *
  494. * From http://support.microsoft.com/kb/78113:
  495. *
  496. * <ul>
  497. * <li> Positive/Negative Infinities:
  498. * Infinities occur when you divide by 0. Excel does not support infinities, rather,
  499. * it gives a #DIV/0! error in these cases.
  500. * </li>
  501. * <li>
  502. * Not-a-Number (NaN):
  503. * NaN is used to represent invalid operations (such as infinity/infinity,
  504. * infinity-infinity, or the square root of -1). NaNs allow a program to
  505. * continue past an invalid operation. Excel instead immediately generates
  506. * an error such as #NUM! or #DIV/0!.
  507. * </li>
  508. * </ul>
  509. */
  510. @Test
  511. public void testNanAndInfinity() throws Exception {
  512. Workbook wb1 = _testDataProvider.createWorkbook();
  513. Sheet workSheet = wb1.createSheet("Sheet1");
  514. Row row = workSheet.createRow(0);
  515. Cell cell0 = row.createCell(0);
  516. cell0.setCellValue(Double.NaN);
  517. assertEquals("Double.NaN should change cell type to CELL_TYPE_ERROR", Cell.CELL_TYPE_ERROR, cell0.getCellType());
  518. assertEquals("Double.NaN should change cell value to #NUM!", ErrorConstants.ERROR_NUM, cell0.getErrorCellValue());
  519. Cell cell1 = row.createCell(1);
  520. cell1.setCellValue(Double.POSITIVE_INFINITY);
  521. assertEquals("Double.POSITIVE_INFINITY should change cell type to CELL_TYPE_ERROR", Cell.CELL_TYPE_ERROR, cell1.getCellType());
  522. assertEquals("Double.POSITIVE_INFINITY should change cell value to #DIV/0!", ErrorConstants.ERROR_DIV_0, cell1.getErrorCellValue());
  523. Cell cell2 = row.createCell(2);
  524. cell2.setCellValue(Double.NEGATIVE_INFINITY);
  525. assertEquals("Double.NEGATIVE_INFINITY should change cell type to CELL_TYPE_ERROR", Cell.CELL_TYPE_ERROR, cell2.getCellType());
  526. assertEquals("Double.NEGATIVE_INFINITY should change cell value to #DIV/0!", ErrorConstants.ERROR_DIV_0, cell2.getErrorCellValue());
  527. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  528. wb1.close();
  529. row = wb2.getSheetAt(0).getRow(0);
  530. cell0 = row.getCell(0);
  531. assertEquals(Cell.CELL_TYPE_ERROR, cell0.getCellType());
  532. assertEquals(ErrorConstants.ERROR_NUM, cell0.getErrorCellValue());
  533. cell1 = row.getCell(1);
  534. assertEquals(Cell.CELL_TYPE_ERROR, cell1.getCellType());
  535. assertEquals(ErrorConstants.ERROR_DIV_0, cell1.getErrorCellValue());
  536. cell2 = row.getCell(2);
  537. assertEquals(Cell.CELL_TYPE_ERROR, cell2.getCellType());
  538. assertEquals(ErrorConstants.ERROR_DIV_0, cell2.getErrorCellValue());
  539. wb2.close();
  540. }
  541. @Test
  542. public void testDefaultStyleProperties() throws Exception {
  543. Workbook wb1 = _testDataProvider.createWorkbook();
  544. Cell cell = wb1.createSheet("Sheet1").createRow(0).createCell(0);
  545. CellStyle style = cell.getCellStyle();
  546. assertTrue(style.getLocked());
  547. assertFalse(style.getHidden());
  548. assertEquals(0, style.getIndention());
  549. assertEquals(0, style.getFontIndex());
  550. assertEquals(0, style.getAlignment());
  551. assertEquals(0, style.getDataFormat());
  552. assertEquals(false, style.getWrapText());
  553. CellStyle style2 = wb1.createCellStyle();
  554. assertTrue(style2.getLocked());
  555. assertFalse(style2.getHidden());
  556. style2.setLocked(false);
  557. style2.setHidden(true);
  558. assertFalse(style2.getLocked());
  559. assertTrue(style2.getHidden());
  560. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  561. wb1.close();
  562. cell = wb2.getSheetAt(0).getRow(0).getCell(0);
  563. style = cell.getCellStyle();
  564. assertFalse(style2.getLocked());
  565. assertTrue(style2.getHidden());
  566. style2.setLocked(true);
  567. style2.setHidden(false);
  568. assertTrue(style2.getLocked());
  569. assertFalse(style2.getHidden());
  570. wb2.close();
  571. }
  572. @Test
  573. public void testBug55658SetNumericValue() throws Exception {
  574. Workbook wb1 = _testDataProvider.createWorkbook();
  575. Sheet sh = wb1.createSheet();
  576. Row row = sh.createRow(0);
  577. Cell cell = row.createCell(0);
  578. cell.setCellValue(Integer.valueOf(23));
  579. cell.setCellValue("some");
  580. cell = row.createCell(1);
  581. cell.setCellValue(Integer.valueOf(23));
  582. cell.setCellValue("24");
  583. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  584. wb1.close();
  585. assertEquals("some", wb2.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
  586. assertEquals("24", wb2.getSheetAt(0).getRow(0).getCell(1).getStringCellValue());
  587. wb2.close();
  588. }
  589. @Test
  590. public void testRemoveHyperlink() throws Exception {
  591. Workbook wb1 = _testDataProvider.createWorkbook();
  592. Sheet sh = wb1.createSheet("test");
  593. Row row = sh.createRow(0);
  594. CreationHelper helper = wb1.getCreationHelper();
  595. Cell cell1 = row.createCell(1);
  596. Hyperlink link1 = helper.createHyperlink(Hyperlink.LINK_URL);
  597. cell1.setHyperlink(link1);
  598. assertNotNull(cell1.getHyperlink());
  599. cell1.removeHyperlink();
  600. assertNull(cell1.getHyperlink());
  601. Cell cell2 = row.createCell(0);
  602. Hyperlink link2 = helper.createHyperlink(Hyperlink.LINK_URL);
  603. cell2.setHyperlink(link2);
  604. assertNotNull(cell2.getHyperlink());
  605. cell2.setHyperlink(null);
  606. assertNull(cell2.getHyperlink());
  607. Cell cell3 = row.createCell(2);
  608. Hyperlink link3 = helper.createHyperlink(Hyperlink.LINK_URL);
  609. link3.setAddress("http://poi.apache.org/");
  610. cell3.setHyperlink(link3);
  611. assertNotNull(cell3.getHyperlink());
  612. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  613. wb1.close();
  614. assertNotNull(wb2);
  615. cell1 = wb2.getSheet("test").getRow(0).getCell(1);
  616. assertNull(cell1.getHyperlink());
  617. cell2 = wb2.getSheet("test").getRow(0).getCell(0);
  618. assertNull(cell2.getHyperlink());
  619. cell3 = wb2.getSheet("test").getRow(0).getCell(2);
  620. assertNotNull(cell3.getHyperlink());
  621. wb2.close();
  622. }
  623. /**
  624. * Cell with the formula that returns error must return error code(There was
  625. * an problem that cell could not return error value form formula cell).
  626. * @throws IOException
  627. */
  628. @Test
  629. public void testGetErrorCellValueFromFormulaCell() throws IOException {
  630. Workbook wb = _testDataProvider.createWorkbook();
  631. try {
  632. Sheet sheet = wb.createSheet();
  633. Row row = sheet.createRow(0);
  634. Cell cell = row.createCell(0);
  635. cell.setCellFormula("SQRT(-1)");
  636. wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(cell);
  637. assertEquals(36, cell.getErrorCellValue());
  638. } finally {
  639. wb.close();
  640. }
  641. }
  642. @Test
  643. public void testSetRemoveStyle() throws Exception {
  644. Workbook wb = _testDataProvider.createWorkbook();
  645. Sheet sheet = wb.createSheet();
  646. Row row = sheet.createRow(0);
  647. Cell cell = row.createCell(0);
  648. // different default style indexes for HSSF and XSSF/SXSSF
  649. CellStyle defaultStyle = wb.getCellStyleAt(wb instanceof HSSFWorkbook ? (short)15 : (short)0);
  650. // Starts out with the default style
  651. assertEquals(defaultStyle, cell.getCellStyle());
  652. // Create some styles, no change
  653. CellStyle style1 = wb.createCellStyle();
  654. CellStyle style2 = wb.createCellStyle();
  655. style1.setDataFormat((short)2);
  656. style2.setDataFormat((short)3);
  657. assertEquals(defaultStyle, cell.getCellStyle());
  658. // Apply one, changes
  659. cell.setCellStyle(style1);
  660. assertEquals(style1, cell.getCellStyle());
  661. // Apply the other, changes
  662. cell.setCellStyle(style2);
  663. assertEquals(style2, cell.getCellStyle());
  664. // Remove, goes back to default
  665. cell.setCellStyle(null);
  666. assertEquals(defaultStyle, cell.getCellStyle());
  667. // Add back, returns
  668. cell.setCellStyle(style2);
  669. assertEquals(style2, cell.getCellStyle());
  670. wb.close();
  671. }
  672. @Test
  673. public void test57008() throws IOException {
  674. Workbook wb1 = _testDataProvider.createWorkbook();
  675. Sheet sheet = wb1.createSheet();
  676. Row row0 = sheet.createRow(0);
  677. Cell cell0 = row0.createCell(0);
  678. cell0.setCellValue("row 0, cell 0 _x0046_ without changes");
  679. Cell cell1 = row0.createCell(1);
  680. cell1.setCellValue("row 0, cell 1 _x005fx0046_ with changes");
  681. Cell cell2 = row0.createCell(2);
  682. cell2.setCellValue("hgh_x0041_**_x0100_*_x0101_*_x0190_*_x0200_*_x0300_*_x0427_*");
  683. checkUnicodeValues(wb1);
  684. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  685. checkUnicodeValues(wb2);
  686. wb2.close();
  687. wb1.close();
  688. }
  689. /**
  690. * Setting a cell value of a null RichTextString should set
  691. * the cell to Blank, test case for 58558
  692. */
  693. @Test
  694. public void testSetCellValueNullRichTextString() throws IOException {
  695. Workbook wb = _testDataProvider.createWorkbook();
  696. Sheet sheet = wb.createSheet();
  697. Cell cell = sheet.createRow(0).createCell(0);
  698. RichTextString nullStr = null;
  699. cell.setCellValue(nullStr);
  700. assertEquals("", cell.getStringCellValue());
  701. assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType());
  702. cell = sheet.createRow(0).createCell(1);
  703. cell.setCellValue(1.2d);
  704. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  705. cell.setCellValue(nullStr);
  706. assertEquals("", cell.getStringCellValue());
  707. assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType());
  708. cell = sheet.createRow(0).createCell(1);
  709. cell.setCellValue(wb.getCreationHelper().createRichTextString("Test"));
  710. assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
  711. cell.setCellValue(nullStr);
  712. assertEquals("", cell.getStringCellValue());
  713. assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType());
  714. wb.close();
  715. }
  716. private void checkUnicodeValues(Workbook wb) {
  717. assertEquals((wb instanceof HSSFWorkbook ? "row 0, cell 0 _x0046_ without changes" : "row 0, cell 0 F without changes"),
  718. wb.getSheetAt(0).getRow(0).getCell(0).toString());
  719. assertEquals((wb instanceof HSSFWorkbook ? "row 0, cell 1 _x005fx0046_ with changes" : "row 0, cell 1 _x005fx0046_ with changes"),
  720. wb.getSheetAt(0).getRow(0).getCell(1).toString());
  721. assertEquals((wb instanceof HSSFWorkbook ? "hgh_x0041_**_x0100_*_x0101_*_x0190_*_x0200_*_x0300_*_x0427_*" : "hghA**\u0100*\u0101*\u0190*\u0200*\u0300*\u0427*"),
  722. wb.getSheetAt(0).getRow(0).getCell(2).toString());
  723. }
  724. /**
  725. * The maximum length of cell contents (text) is 32,767 characters.
  726. * @throws IOException
  727. */
  728. @Test
  729. public void testMaxTextLength() throws IOException{
  730. Workbook wb = _testDataProvider.createWorkbook();
  731. Sheet sheet = wb.createSheet();
  732. Cell cell = sheet.createRow(0).createCell(0);
  733. int maxlen = wb instanceof HSSFWorkbook ?
  734. SpreadsheetVersion.EXCEL97.getMaxTextLength()
  735. : SpreadsheetVersion.EXCEL2007.getMaxTextLength();
  736. assertEquals(32767, maxlen);
  737. StringBuffer b = new StringBuffer() ;
  738. // 32767 is okay
  739. for( int i = 0 ; i < maxlen ; i++ )
  740. {
  741. b.append( "X" ) ;
  742. }
  743. cell.setCellValue(b.toString());
  744. b.append("X");
  745. // 32768 produces an invalid XLS file
  746. try {
  747. cell.setCellValue(b.toString());
  748. fail("Expected exception");
  749. } catch (IllegalArgumentException e){
  750. assertEquals("The maximum length of cell contents (text) is 32,767 characters", e.getMessage());
  751. }
  752. wb.close();
  753. }
  754. }