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 52KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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.apache.poi.ss.usermodel.FormulaError.forInt;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNotNull;
  20. import static org.junit.Assert.assertNull;
  21. import static org.junit.Assert.assertTrue;
  22. import static org.junit.Assert.fail;
  23. import static org.mockito.Mockito.doCallRealMethod;
  24. import static org.mockito.Mockito.mock;
  25. import static org.mockito.Mockito.verify;
  26. import java.io.IOException;
  27. import java.nio.charset.StandardCharsets;
  28. import java.util.Calendar;
  29. import java.util.Date;
  30. import java.util.GregorianCalendar;
  31. import java.util.Locale;
  32. import java.util.TimeZone;
  33. import org.apache.poi.common.usermodel.HyperlinkType;
  34. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  35. import org.apache.poi.ss.ITestDataProvider;
  36. import org.apache.poi.ss.SpreadsheetVersion;
  37. import org.apache.poi.ss.util.CellRangeAddress;
  38. import org.apache.poi.util.LocaleUtil;
  39. import org.junit.Test;
  40. /**
  41. * Common superclass for testing implementations of
  42. * {@link org.apache.poi.ss.usermodel.Cell}
  43. */
  44. public abstract class BaseTestCell {
  45. protected final ITestDataProvider _testDataProvider;
  46. /**
  47. * @param testDataProvider an object that provides test data in HSSF / XSSF specific way
  48. */
  49. protected BaseTestCell(ITestDataProvider testDataProvider) {
  50. _testDataProvider = testDataProvider;
  51. }
  52. @Test
  53. public void testSetValues() throws Exception {
  54. Workbook book = _testDataProvider.createWorkbook();
  55. Sheet sheet = book.createSheet("test");
  56. Row row = sheet.createRow(0);
  57. CreationHelper factory = book.getCreationHelper();
  58. Cell cell = row.createCell(0);
  59. cell.setCellValue(1.2);
  60. assertEquals(1.2, cell.getNumericCellValue(), 0.0001);
  61. assertEquals(CellType.NUMERIC, cell.getCellType());
  62. assertProhibitedValueAccess(cell, CellType.BOOLEAN, CellType.STRING,
  63. CellType.FORMULA, CellType.ERROR);
  64. cell.setCellValue(false);
  65. assertFalse(cell.getBooleanCellValue());
  66. assertEquals(CellType.BOOLEAN, cell.getCellType());
  67. cell.setCellValue(true);
  68. assertTrue(cell.getBooleanCellValue());
  69. assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.STRING, CellType.BOOLEAN,
  70. CellType.FORMULA, CellType.ERROR);
  71. cell.setCellValue(factory.createRichTextString("Foo"));
  72. assertEquals("Foo", cell.getRichStringCellValue().getString());
  73. assertEquals("Foo", cell.getStringCellValue());
  74. assertEquals(CellType.STRING, cell.getCellType());
  75. assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.BOOLEAN,
  76. CellType.FORMULA, CellType.ERROR);
  77. cell.setCellValue("345");
  78. assertEquals("345", cell.getRichStringCellValue().getString());
  79. assertEquals("345", cell.getStringCellValue());
  80. assertEquals(CellType.STRING, cell.getCellType());
  81. assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.BOOLEAN,
  82. CellType.FORMULA, CellType.ERROR);
  83. Calendar c = LocaleUtil.getLocaleCalendar();
  84. c.setTimeInMillis(123456789);
  85. cell.setCellValue(c.getTime());
  86. assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime());
  87. assertEquals(CellType.NUMERIC, cell.getCellType());
  88. assertProhibitedValueAccess(cell, CellType.BOOLEAN, CellType.STRING,
  89. CellType.FORMULA, CellType.ERROR);
  90. cell.setCellValue(c);
  91. assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime());
  92. assertEquals(CellType.NUMERIC, cell.getCellType());
  93. assertProhibitedValueAccess(cell, CellType.BOOLEAN, CellType.STRING,
  94. CellType.FORMULA, CellType.ERROR);
  95. cell.setCellErrorValue(FormulaError.NA.getCode());
  96. assertEquals(FormulaError.NA.getCode(), cell.getErrorCellValue());
  97. assertEquals(CellType.ERROR, cell.getCellType());
  98. assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.BOOLEAN,
  99. CellType.FORMULA, CellType.STRING);
  100. book.close();
  101. }
  102. private static void assertProhibitedValueAccess(Cell cell, CellType ... types) {
  103. for(CellType type : types){
  104. try {
  105. switch (type) {
  106. case NUMERIC:
  107. cell.getNumericCellValue();
  108. break;
  109. case STRING:
  110. cell.getStringCellValue();
  111. break;
  112. case BOOLEAN:
  113. cell.getBooleanCellValue();
  114. break;
  115. case FORMULA:
  116. cell.getCellFormula();
  117. break;
  118. case ERROR:
  119. cell.getErrorCellValue();
  120. break;
  121. default:
  122. fail("Should get exception when reading cell type (" + type + ").");
  123. }
  124. } catch (IllegalStateException e){
  125. // expected during successful test
  126. assertTrue(e.getMessage().startsWith("Cannot get a"));
  127. }
  128. }
  129. }
  130. /**
  131. * test that Boolean (BoolErrRecord) are supported properly.
  132. */
  133. @Test
  134. public void testBool() throws IOException {
  135. Workbook wb1 = _testDataProvider.createWorkbook();
  136. Sheet s = wb1.createSheet("testSheet1");
  137. Row r;
  138. Cell c;
  139. // B1
  140. r = s.createRow(0);
  141. c=r.createCell(1);
  142. assertEquals(0, c.getRowIndex());
  143. assertEquals(1, c.getColumnIndex());
  144. c.setCellValue(true);
  145. assertTrue("B1 value", c.getBooleanCellValue());
  146. // C1
  147. c=r.createCell(2);
  148. assertEquals(0, c.getRowIndex());
  149. assertEquals(2, c.getColumnIndex());
  150. c.setCellValue(false);
  151. assertFalse("C1 value", c.getBooleanCellValue());
  152. // Make sure values are saved and re-read correctly.
  153. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  154. wb1.close();
  155. s = wb2.getSheet("testSheet1");
  156. r = s.getRow(0);
  157. assertEquals("Row 1 should have 2 cells", 2, r.getPhysicalNumberOfCells());
  158. c = r.getCell(1);
  159. assertEquals(0, c.getRowIndex());
  160. assertEquals(1, c.getColumnIndex());
  161. assertEquals(CellType.BOOLEAN, c.getCellType());
  162. assertTrue("B1 value", c.getBooleanCellValue());
  163. c = r.getCell(2);
  164. assertEquals(0, c.getRowIndex());
  165. assertEquals(2, c.getColumnIndex());
  166. assertEquals(CellType.BOOLEAN, c.getCellType());
  167. assertFalse("C1 value", c.getBooleanCellValue());
  168. wb2.close();
  169. }
  170. /**
  171. * test that Error types (BoolErrRecord) are supported properly.
  172. * @see #testBool
  173. */
  174. @Test
  175. public void testErr() throws IOException {
  176. Workbook wb1 = _testDataProvider.createWorkbook();
  177. Sheet s = wb1.createSheet("testSheet1");
  178. Row r;
  179. Cell c;
  180. // B1
  181. r = s.createRow(0);
  182. c=r.createCell(1);
  183. assertEquals(0, c.getRowIndex());
  184. assertEquals(1, c.getColumnIndex());
  185. c.setCellErrorValue(FormulaError.NULL.getCode());
  186. assertEquals("B1 value == #NULL!", FormulaError.NULL.getCode(), c.getErrorCellValue());
  187. // C1
  188. c=r.createCell(2);
  189. assertEquals(0, c.getRowIndex());
  190. assertEquals(2, c.getColumnIndex());
  191. c.setCellErrorValue(FormulaError.DIV0.getCode());
  192. assertEquals("C1 value == #DIV/0!", FormulaError.DIV0.getCode(), c.getErrorCellValue());
  193. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  194. wb1.close();
  195. s = wb2.getSheet("testSheet1");
  196. r = s.getRow(0);
  197. assertEquals("Row 1 should have 2 cells", 2, r.getPhysicalNumberOfCells());
  198. c = r.getCell(1);
  199. assertEquals(0, c.getRowIndex());
  200. assertEquals(1, c.getColumnIndex());
  201. assertEquals(CellType.ERROR, c.getCellType());
  202. assertEquals("B1 value == #NULL!", FormulaError.NULL.getCode(), c.getErrorCellValue());
  203. c = r.getCell(2);
  204. assertEquals(0, c.getRowIndex());
  205. assertEquals(2, c.getColumnIndex());
  206. assertEquals(CellType.ERROR, c.getCellType());
  207. assertEquals("C1 value == #DIV/0!", FormulaError.DIV0.getCode(), c.getErrorCellValue());
  208. wb2.close();
  209. }
  210. /**
  211. * test that Cell Styles being applied to formulas remain intact
  212. */
  213. @Test
  214. public void testFormulaStyle() throws Exception {
  215. Workbook wb1 = _testDataProvider.createWorkbook();
  216. Sheet s = wb1.createSheet("testSheet1");
  217. Row r;
  218. Cell c;
  219. CellStyle cs = wb1.createCellStyle();
  220. Font f = wb1.createFont();
  221. f.setFontHeightInPoints((short) 20);
  222. f.setColor(IndexedColors.RED.getIndex());
  223. f.setBold(true);
  224. f.setFontName("Arial Unicode MS");
  225. cs.setFillBackgroundColor((short)3);
  226. cs.setFont(f);
  227. cs.setBorderTop(BorderStyle.THIN);
  228. cs.setBorderRight(BorderStyle.THIN);
  229. cs.setBorderLeft(BorderStyle.THIN);
  230. cs.setBorderBottom(BorderStyle.THIN);
  231. r = s.createRow(0);
  232. c=r.createCell(0);
  233. c.setCellStyle(cs);
  234. c.setCellFormula("2*3");
  235. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  236. wb1.close();
  237. s = wb2.getSheetAt(0);
  238. r = s.getRow(0);
  239. c = r.getCell(0);
  240. assertEquals("Formula Cell at 0,0", CellType.FORMULA, c.getCellType());
  241. cs = c.getCellStyle();
  242. assertNotNull("Formula Cell Style", cs);
  243. assertEquals("Font Index Matches", f.getIndexAsInt(), cs.getFontIndexAsInt());
  244. assertEquals("Top Border", BorderStyle.THIN, cs.getBorderTop());
  245. assertEquals("Left Border", BorderStyle.THIN, cs.getBorderLeft());
  246. assertEquals("Right Border", BorderStyle.THIN, cs.getBorderRight());
  247. assertEquals("Bottom Border", BorderStyle.THIN, cs.getBorderBottom());
  248. wb2.close();
  249. }
  250. /**tests the toString() method of HSSFCell*/
  251. @Test
  252. public void testToString() throws Exception {
  253. Workbook wb1 = _testDataProvider.createWorkbook();
  254. Row r = wb1.createSheet("Sheet1").createRow(0);
  255. CreationHelper factory = wb1.getCreationHelper();
  256. r.createCell(0).setCellValue(false);
  257. r.createCell(1).setCellValue(true);
  258. r.createCell(2).setCellValue(1.5);
  259. r.createCell(3).setCellValue(factory.createRichTextString("Astring"));
  260. r.createCell(4).setCellErrorValue(FormulaError.DIV0.getCode());
  261. r.createCell(5).setCellFormula("A1+B1");
  262. r.createCell(6); // blank
  263. // create date-formatted cell
  264. Calendar c = LocaleUtil.getLocaleCalendar();
  265. c.set(2010, Calendar.FEBRUARY, 2, 0, 0, 0);
  266. r.createCell(7).setCellValue(c);
  267. CellStyle dateStyle = wb1.createCellStyle();
  268. short formatId = wb1.getCreationHelper().createDataFormat().getFormat("m/d/yy h:mm"); // any date format will do
  269. dateStyle.setDataFormat(formatId);
  270. r.getCell(7).setCellStyle(dateStyle);
  271. assertEquals("Boolean", "FALSE", r.getCell(0).toString());
  272. assertEquals("Boolean", "TRUE", r.getCell(1).toString());
  273. assertEquals("Numeric", "1.5", r.getCell(2).toString());
  274. assertEquals("String", "Astring", r.getCell(3).toString());
  275. assertEquals("Error", "#DIV/0!", r.getCell(4).toString());
  276. assertEquals("Formula", "A1+B1", r.getCell(5).toString());
  277. assertEquals("Blank", "", r.getCell(6).toString());
  278. // toString on a date-formatted cell displays dates as dd-MMM-yyyy, which has locale problems with the month
  279. String dateCell1 = r.getCell(7).toString();
  280. assertTrue("Date (Day)", dateCell1.startsWith("02-"));
  281. assertTrue("Date (Year)", dateCell1.endsWith("-2010"));
  282. //Write out the file, read it in, and then check cell values
  283. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  284. wb1.close();
  285. r = wb2.getSheetAt(0).getRow(0);
  286. assertEquals("Boolean", "FALSE", r.getCell(0).toString());
  287. assertEquals("Boolean", "TRUE", r.getCell(1).toString());
  288. assertEquals("Numeric", "1.5", r.getCell(2).toString());
  289. assertEquals("String", "Astring", r.getCell(3).toString());
  290. assertEquals("Error", "#DIV/0!", r.getCell(4).toString());
  291. assertEquals("Formula", "A1+B1", r.getCell(5).toString());
  292. assertEquals("Blank", "", r.getCell(6).toString());
  293. String dateCell2 = r.getCell(7).toString();
  294. assertEquals("Date", dateCell1, dateCell2);
  295. wb2.close();
  296. }
  297. /**
  298. * Test that setting cached formula result keeps the cell type
  299. */
  300. @Test
  301. public void testSetFormulaValue() throws Exception {
  302. Workbook wb = _testDataProvider.createWorkbook();
  303. Sheet s = wb.createSheet();
  304. Row r = s.createRow(0);
  305. Cell c1 = r.createCell(0);
  306. c1.setCellFormula("NA()");
  307. assertEquals(0.0, c1.getNumericCellValue(), 0.0);
  308. assertEquals(CellType.NUMERIC, c1.getCachedFormulaResultType());
  309. c1.setCellValue(10);
  310. assertEquals(10.0, c1.getNumericCellValue(), 0.0);
  311. assertEquals(CellType.FORMULA, c1.getCellType());
  312. assertEquals(CellType.NUMERIC, c1.getCachedFormulaResultType());
  313. Cell c2 = r.createCell(1);
  314. c2.setCellFormula("NA()");
  315. assertEquals(0.0, c2.getNumericCellValue(), 0.0);
  316. assertEquals(CellType.NUMERIC, c2.getCachedFormulaResultType());
  317. c2.setCellValue("I changed!");
  318. assertEquals("I changed!", c2.getStringCellValue());
  319. assertEquals(CellType.FORMULA, c2.getCellType());
  320. assertEquals(CellType.STRING, c2.getCachedFormulaResultType());
  321. //calglin Cell.setCellFormula(null) for a non-formula cell
  322. Cell c3 = r.createCell(2);
  323. c3.setCellFormula(null);
  324. assertEquals(CellType.BLANK, c3.getCellType());
  325. wb.close();
  326. }
  327. private Cell createACell(Workbook wb) {
  328. return wb.createSheet("Sheet1").createRow(0).createCell(0);
  329. }
  330. /**
  331. * bug 58452: Copy cell formulas containing unregistered function names
  332. * Make sure that formulas with unknown/unregistered UDFs can be written to and read back from a file.
  333. */
  334. @Test
  335. public void testFormulaWithUnknownUDF() throws IOException {
  336. final Workbook wb1 = _testDataProvider.createWorkbook();
  337. final FormulaEvaluator evaluator1 = wb1.getCreationHelper().createFormulaEvaluator();
  338. try {
  339. final Cell cell1 = wb1.createSheet().createRow(0).createCell(0);
  340. final String formula = "myFunc(\"arg\")";
  341. cell1.setCellFormula(formula);
  342. confirmFormulaWithUnknownUDF(formula, cell1, evaluator1);
  343. final Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  344. final FormulaEvaluator evaluator2 = wb2.getCreationHelper().createFormulaEvaluator();
  345. try {
  346. final Cell cell2 = wb2.getSheetAt(0).getRow(0).getCell(0);
  347. confirmFormulaWithUnknownUDF(formula, cell2, evaluator2);
  348. } finally {
  349. wb2.close();
  350. }
  351. } finally {
  352. wb1.close();
  353. }
  354. }
  355. private static void confirmFormulaWithUnknownUDF(String expectedFormula, Cell cell, FormulaEvaluator evaluator) {
  356. assertEquals(expectedFormula, cell.getCellFormula());
  357. try {
  358. evaluator.evaluate(cell);
  359. fail("Expected NotImplementedFunctionException/NotImplementedException");
  360. } catch (final org.apache.poi.ss.formula.eval.NotImplementedException e) {
  361. // expected
  362. }
  363. }
  364. @Test
  365. public void testChangeTypeStringToBool() throws IOException {
  366. Workbook wb = _testDataProvider.createWorkbook();
  367. Cell cell = createACell(wb);
  368. cell.setCellValue("TRUE");
  369. assertEquals(CellType.STRING, cell.getCellType());
  370. // test conversion of cell from text to boolean
  371. cell.setCellType(CellType.BOOLEAN);
  372. assertEquals(CellType.BOOLEAN, cell.getCellType());
  373. assertTrue(cell.getBooleanCellValue());
  374. cell.setCellType(CellType.STRING);
  375. assertEquals("TRUE", cell.getRichStringCellValue().getString());
  376. // 'false' text to bool and back
  377. cell.setCellValue("FALSE");
  378. cell.setCellType(CellType.BOOLEAN);
  379. assertEquals(CellType.BOOLEAN, cell.getCellType());
  380. assertFalse(cell.getBooleanCellValue());
  381. cell.setCellType(CellType.STRING);
  382. assertEquals("FALSE", cell.getRichStringCellValue().getString());
  383. wb.close();
  384. }
  385. @Test
  386. public void testChangeTypeBoolToString() throws IOException {
  387. Workbook wb = _testDataProvider.createWorkbook();
  388. Cell cell = createACell(wb);
  389. cell.setCellValue(true);
  390. // test conversion of cell from boolean to text
  391. cell.setCellType(CellType.STRING);
  392. assertEquals("TRUE", cell.getRichStringCellValue().getString());
  393. wb.close();
  394. }
  395. @Test
  396. public void testChangeTypeErrorToNumber() throws IOException {
  397. Workbook wb = _testDataProvider.createWorkbook();
  398. Cell cell = createACell(wb);
  399. cell.setCellErrorValue(FormulaError.NAME.getCode());
  400. try {
  401. cell.setCellValue(2.5);
  402. } catch (ClassCastException e) {
  403. fail("Identified bug 46479b");
  404. }
  405. assertEquals(2.5, cell.getNumericCellValue(), 0.0);
  406. wb.close();
  407. }
  408. @Test
  409. public void testChangeTypeErrorToBoolean() throws IOException {
  410. Workbook wb = _testDataProvider.createWorkbook();
  411. Cell cell = createACell(wb);
  412. cell.setCellErrorValue(FormulaError.NAME.getCode());
  413. cell.setCellValue(true);
  414. // Identify bug 46479c
  415. assertTrue(cell.getBooleanCellValue());
  416. wb.close();
  417. }
  418. /**
  419. * Test for a bug observed around svn r886733 when using
  420. * {@link FormulaEvaluator#evaluateInCell(Cell)} with a
  421. * string result type.
  422. */
  423. @Test
  424. public void testConvertStringFormulaCell() throws IOException {
  425. Workbook wb = _testDataProvider.createWorkbook();
  426. Cell cellA1 = createACell(wb);
  427. cellA1.setCellFormula("\"abc\"");
  428. // default cached formula result is numeric zero
  429. assertEquals(0.0, cellA1.getNumericCellValue(), 0.0);
  430. FormulaEvaluator fe = cellA1.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
  431. fe.evaluateFormulaCell(cellA1);
  432. assertEquals("abc", cellA1.getStringCellValue());
  433. fe.evaluateInCell(cellA1);
  434. assertFalse("Identified bug with writing back formula result of type string", cellA1.getStringCellValue().isEmpty());
  435. assertEquals("abc", cellA1.getStringCellValue());
  436. wb.close();
  437. }
  438. /**
  439. * similar to {@link #testConvertStringFormulaCell()} but checks at a
  440. * lower level that {#link {@link Cell#setCellType(CellType)} works properly
  441. */
  442. @Test
  443. public void testSetTypeStringOnFormulaCell() throws IOException {
  444. Workbook wb = _testDataProvider.createWorkbook();
  445. Cell cellA1 = createACell(wb);
  446. FormulaEvaluator fe = cellA1.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
  447. cellA1.setCellFormula("\"DEF\"");
  448. fe.clearAllCachedResultValues();
  449. fe.evaluateFormulaCell(cellA1);
  450. assertEquals("DEF", cellA1.getStringCellValue());
  451. cellA1.setCellType(CellType.STRING);
  452. assertEquals("DEF", cellA1.getStringCellValue());
  453. cellA1.setCellFormula("25.061");
  454. fe.clearAllCachedResultValues();
  455. fe.evaluateFormulaCell(cellA1);
  456. confirmCannotReadString(cellA1);
  457. assertEquals(25.061, cellA1.getNumericCellValue(), 0.0);
  458. cellA1.setCellType(CellType.STRING);
  459. assertEquals("25.061", cellA1.getStringCellValue());
  460. cellA1.setCellFormula("TRUE");
  461. fe.clearAllCachedResultValues();
  462. fe.evaluateFormulaCell(cellA1);
  463. confirmCannotReadString(cellA1);
  464. assertTrue(cellA1.getBooleanCellValue());
  465. cellA1.setCellType(CellType.STRING);
  466. assertEquals("TRUE", cellA1.getStringCellValue());
  467. cellA1.setCellFormula("#NAME?");
  468. fe.clearAllCachedResultValues();
  469. fe.evaluateFormulaCell(cellA1);
  470. confirmCannotReadString(cellA1);
  471. assertEquals(FormulaError.NAME, forInt(cellA1.getErrorCellValue()));
  472. cellA1.setCellType(CellType.STRING);
  473. assertEquals("#NAME?", cellA1.getStringCellValue());
  474. wb.close();
  475. }
  476. private static void confirmCannotReadString(Cell cell) {
  477. assertProhibitedValueAccess(cell, CellType.STRING);
  478. }
  479. /**
  480. * Test for bug in convertCellValueToBoolean to make sure that formula results get converted
  481. */
  482. @Test
  483. public void testChangeTypeFormulaToBoolean() throws IOException {
  484. Workbook wb = _testDataProvider.createWorkbook();
  485. Cell cell = createACell(wb);
  486. cell.setCellFormula("1=1");
  487. cell.setCellValue(true);
  488. cell.setCellType(CellType.BOOLEAN);
  489. assertTrue("Identified bug 46479d", cell.getBooleanCellValue());
  490. assertTrue(cell.getBooleanCellValue());
  491. wb.close();
  492. }
  493. /**
  494. * Bug 40296: HSSFCell.setCellFormula throws
  495. * ClassCastException if cell is created using HSSFRow.createCell(short column, int type)
  496. */
  497. @Test
  498. public void test40296() throws Exception {
  499. Workbook wb1 = _testDataProvider.createWorkbook();
  500. Sheet workSheet = wb1.createSheet("Sheet1");
  501. Cell cell;
  502. Row row = workSheet.createRow(0);
  503. cell = row.createCell(0, CellType.NUMERIC);
  504. cell.setCellValue(1.0);
  505. assertEquals(CellType.NUMERIC, cell.getCellType());
  506. assertEquals(1.0, cell.getNumericCellValue(), 0.0);
  507. cell = row.createCell(1, CellType.NUMERIC);
  508. cell.setCellValue(2.0);
  509. assertEquals(CellType.NUMERIC, cell.getCellType());
  510. assertEquals(2.0, cell.getNumericCellValue(), 0.0);
  511. cell = row.createCell(2, CellType.FORMULA);
  512. cell.setCellFormula("SUM(A1:B1)");
  513. assertEquals(CellType.FORMULA, cell.getCellType());
  514. assertEquals("SUM(A1:B1)", cell.getCellFormula());
  515. //serialize and check again
  516. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  517. wb1.close();
  518. row = wb2.getSheetAt(0).getRow(0);
  519. cell = row.getCell(0);
  520. assertEquals(CellType.NUMERIC, cell.getCellType());
  521. assertEquals(1.0, cell.getNumericCellValue(), 0.0);
  522. cell = row.getCell(1);
  523. assertEquals(CellType.NUMERIC, cell.getCellType());
  524. assertEquals(2.0, cell.getNumericCellValue(), 0.0);
  525. cell = row.getCell(2);
  526. assertEquals(CellType.FORMULA, cell.getCellType());
  527. assertEquals("SUM(A1:B1)", cell.getCellFormula());
  528. wb2.close();
  529. }
  530. @Test
  531. public void testSetStringInFormulaCell_bug44606() throws Exception {
  532. Workbook wb = _testDataProvider.createWorkbook();
  533. Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
  534. cell.setCellFormula("B1&C1");
  535. assertEquals(CellType.FORMULA, cell.getCellType());
  536. cell.setCellValue(wb.getCreationHelper().createRichTextString("hello"));
  537. assertEquals(CellType.FORMULA, cell.getCellType());
  538. wb.close();
  539. }
  540. /**
  541. * Make sure that cell.setBlank() preserves the cell style
  542. */
  543. @Test
  544. public void testSetBlank_bug47028() throws Exception {
  545. Workbook wb = _testDataProvider.createWorkbook();
  546. CellStyle style = wb.createCellStyle();
  547. Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
  548. cell.setCellStyle(style);
  549. int i1 = cell.getCellStyle().getIndex();
  550. cell.setBlank();
  551. int i2 = cell.getCellStyle().getIndex();
  552. assertEquals(i1, i2);
  553. wb.close();
  554. }
  555. /**
  556. * Excel's implementation of floating number arithmetic does not fully adhere to IEEE 754:
  557. *
  558. * From http://support.microsoft.com/kb/78113:
  559. *
  560. * <ul>
  561. * <li> Positive/Negative Infinities:
  562. * Infinities occur when you divide by 0. Excel does not support infinities, rather,
  563. * it gives a #DIV/0! error in these cases.
  564. * </li>
  565. * <li>
  566. * Not-a-Number (NaN):
  567. * NaN is used to represent invalid operations (such as infinity/infinity,
  568. * infinity-infinity, or the square root of -1). NaNs allow a program to
  569. * continue past an invalid operation. Excel instead immediately generates
  570. * an error such as #NUM! or #DIV/0!.
  571. * </li>
  572. * </ul>
  573. */
  574. @Test
  575. public void testNanAndInfinity() throws Exception {
  576. Workbook wb1 = _testDataProvider.createWorkbook();
  577. Sheet workSheet = wb1.createSheet("Sheet1");
  578. Row row = workSheet.createRow(0);
  579. Cell cell0 = row.createCell(0);
  580. cell0.setCellValue(Double.NaN);
  581. assertEquals("Double.NaN should change cell type to CellType#ERROR", CellType.ERROR, cell0.getCellType());
  582. assertEquals("Double.NaN should change cell value to #NUM!", FormulaError.NUM, forInt(cell0.getErrorCellValue()));
  583. Cell cell1 = row.createCell(1);
  584. cell1.setCellValue(Double.POSITIVE_INFINITY);
  585. assertEquals("Double.POSITIVE_INFINITY should change cell type to CellType#ERROR", CellType.ERROR, cell1.getCellType());
  586. assertEquals("Double.POSITIVE_INFINITY should change cell value to #DIV/0!", FormulaError.DIV0, forInt(cell1.getErrorCellValue()));
  587. Cell cell2 = row.createCell(2);
  588. cell2.setCellValue(Double.NEGATIVE_INFINITY);
  589. assertEquals("Double.NEGATIVE_INFINITY should change cell type to CellType#ERROR", CellType.ERROR, cell2.getCellType());
  590. assertEquals("Double.NEGATIVE_INFINITY should change cell value to #DIV/0!", FormulaError.DIV0, forInt(cell2.getErrorCellValue()));
  591. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  592. wb1.close();
  593. row = wb2.getSheetAt(0).getRow(0);
  594. cell0 = row.getCell(0);
  595. assertEquals(CellType.ERROR, cell0.getCellType());
  596. assertEquals(FormulaError.NUM, forInt(cell0.getErrorCellValue()));
  597. cell1 = row.getCell(1);
  598. assertEquals(CellType.ERROR, cell1.getCellType());
  599. assertEquals(FormulaError.DIV0, forInt(cell1.getErrorCellValue()));
  600. cell2 = row.getCell(2);
  601. assertEquals(CellType.ERROR, cell2.getCellType());
  602. assertEquals(FormulaError.DIV0, forInt(cell2.getErrorCellValue()));
  603. wb2.close();
  604. }
  605. @Test
  606. public void testDefaultStyleProperties() throws Exception {
  607. Workbook wb1 = _testDataProvider.createWorkbook();
  608. Cell cell = wb1.createSheet("Sheet1").createRow(0).createCell(0);
  609. CellStyle style = cell.getCellStyle();
  610. assertTrue(style.getLocked());
  611. assertFalse(style.getHidden());
  612. assertEquals(0, style.getIndention());
  613. assertEquals(0, style.getFontIndexAsInt());
  614. assertEquals(HorizontalAlignment.GENERAL, style.getAlignment());
  615. assertEquals(0, style.getDataFormat());
  616. assertFalse(style.getWrapText());
  617. CellStyle style2 = wb1.createCellStyle();
  618. assertTrue(style2.getLocked());
  619. assertFalse(style2.getHidden());
  620. style2.setLocked(false);
  621. style2.setHidden(true);
  622. assertFalse(style2.getLocked());
  623. assertTrue(style2.getHidden());
  624. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  625. wb1.close();
  626. cell = wb2.getSheetAt(0).getRow(0).getCell(0);
  627. style = cell.getCellStyle();
  628. assertFalse(style2.getLocked());
  629. assertTrue(style2.getHidden());
  630. assertTrue(style.getLocked());
  631. assertFalse(style.getHidden());
  632. style2.setLocked(true);
  633. style2.setHidden(false);
  634. assertTrue(style2.getLocked());
  635. assertFalse(style2.getHidden());
  636. wb2.close();
  637. }
  638. @Test
  639. public void testBug55658SetNumericValue() throws Exception {
  640. Workbook wb1 = _testDataProvider.createWorkbook();
  641. Sheet sh = wb1.createSheet();
  642. Row row = sh.createRow(0);
  643. Cell cell = row.createCell(0);
  644. cell.setCellValue(Integer.valueOf(23));
  645. cell.setCellValue("some");
  646. cell = row.createCell(1);
  647. cell.setCellValue(Integer.valueOf(23));
  648. cell.setCellValue("24");
  649. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  650. wb1.close();
  651. assertEquals("some", wb2.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
  652. assertEquals("24", wb2.getSheetAt(0).getRow(0).getCell(1).getStringCellValue());
  653. wb2.close();
  654. }
  655. @Test
  656. public void testRemoveHyperlink() throws Exception {
  657. Workbook wb1 = _testDataProvider.createWorkbook();
  658. Sheet sh = wb1.createSheet("test");
  659. Row row = sh.createRow(0);
  660. CreationHelper helper = wb1.getCreationHelper();
  661. Cell cell1 = row.createCell(1);
  662. Hyperlink link1 = helper.createHyperlink(HyperlinkType.URL);
  663. cell1.setHyperlink(link1);
  664. assertNotNull(cell1.getHyperlink());
  665. cell1.removeHyperlink();
  666. assertNull(cell1.getHyperlink());
  667. Cell cell2 = row.createCell(0);
  668. Hyperlink link2 = helper.createHyperlink(HyperlinkType.URL);
  669. cell2.setHyperlink(link2);
  670. assertNotNull(cell2.getHyperlink());
  671. cell2.setHyperlink(null);
  672. assertNull(cell2.getHyperlink());
  673. Cell cell3 = row.createCell(2);
  674. Hyperlink link3 = helper.createHyperlink(HyperlinkType.URL);
  675. link3.setAddress("http://poi.apache.org/");
  676. cell3.setHyperlink(link3);
  677. assertNotNull(cell3.getHyperlink());
  678. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  679. wb1.close();
  680. assertNotNull(wb2);
  681. cell1 = wb2.getSheet("test").getRow(0).getCell(1);
  682. assertNull(cell1.getHyperlink());
  683. cell2 = wb2.getSheet("test").getRow(0).getCell(0);
  684. assertNull(cell2.getHyperlink());
  685. cell3 = wb2.getSheet("test").getRow(0).getCell(2);
  686. assertNotNull(cell3.getHyperlink());
  687. wb2.close();
  688. }
  689. /**
  690. * Cell with the formula that returns error must return error code(There was
  691. * an problem that cell could not return error value form formula cell).
  692. */
  693. @Test
  694. public void testGetErrorCellValueFromFormulaCell() throws IOException {
  695. try (Workbook wb = _testDataProvider.createWorkbook()) {
  696. Sheet sheet = wb.createSheet();
  697. Row row = sheet.createRow(0);
  698. Cell cell = row.createCell(0);
  699. cell.setCellFormula("SQRT(-1)");
  700. wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(cell);
  701. assertEquals(36, cell.getErrorCellValue());
  702. }
  703. }
  704. @Test
  705. public void testSetRemoveStyle() throws Exception {
  706. Workbook wb = _testDataProvider.createWorkbook();
  707. Sheet sheet = wb.createSheet();
  708. Row row = sheet.createRow(0);
  709. Cell cell = row.createCell(0);
  710. // different default style indexes for HSSF and XSSF/SXSSF
  711. CellStyle defaultStyle = wb.getCellStyleAt(wb instanceof HSSFWorkbook ? (short)15 : (short)0);
  712. // Starts out with the default style
  713. assertEquals(defaultStyle, cell.getCellStyle());
  714. // Create some styles, no change
  715. CellStyle style1 = wb.createCellStyle();
  716. CellStyle style2 = wb.createCellStyle();
  717. style1.setDataFormat((short)2);
  718. style2.setDataFormat((short)3);
  719. assertEquals(defaultStyle, cell.getCellStyle());
  720. // Apply one, changes
  721. cell.setCellStyle(style1);
  722. assertEquals(style1, cell.getCellStyle());
  723. // Apply the other, changes
  724. cell.setCellStyle(style2);
  725. assertEquals(style2, cell.getCellStyle());
  726. // Remove, goes back to default
  727. cell.setCellStyle(null);
  728. assertEquals(defaultStyle, cell.getCellStyle());
  729. // Add back, returns
  730. cell.setCellStyle(style2);
  731. assertEquals(style2, cell.getCellStyle());
  732. wb.close();
  733. }
  734. @Test
  735. public void test57008() throws IOException {
  736. Workbook wb1 = _testDataProvider.createWorkbook();
  737. Sheet sheet = wb1.createSheet();
  738. Row row0 = sheet.createRow(0);
  739. Cell cell0 = row0.createCell(0);
  740. cell0.setCellValue("row 0, cell 0 _x0046_ without changes");
  741. Cell cell1 = row0.createCell(1);
  742. cell1.setCellValue("row 0, cell 1 _x005fx0046_ with changes");
  743. Cell cell2 = row0.createCell(2);
  744. cell2.setCellValue("hgh_x0041_**_x0100_*_x0101_*_x0190_*_x0200_*_x0300_*_x0427_*");
  745. checkUnicodeValues(wb1);
  746. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  747. checkUnicodeValues(wb2);
  748. wb2.close();
  749. wb1.close();
  750. }
  751. /**
  752. * Setting a cell value of a null RichTextString should set
  753. * the cell to Blank, test case for 58558
  754. */
  755. @SuppressWarnings("ConstantConditions")
  756. @Test
  757. public void testSetCellValueNullRichTextString() throws IOException {
  758. Workbook wb = _testDataProvider.createWorkbook();
  759. Sheet sheet = wb.createSheet();
  760. Cell cell = sheet.createRow(0).createCell(0);
  761. RichTextString nullStr = null;
  762. cell.setCellValue(nullStr);
  763. assertEquals("", cell.getStringCellValue());
  764. assertEquals(CellType.BLANK, cell.getCellType());
  765. cell = sheet.createRow(0).createCell(1);
  766. cell.setCellValue(1.2d);
  767. assertEquals(CellType.NUMERIC, cell.getCellType());
  768. cell.setCellValue(nullStr);
  769. assertEquals("", cell.getStringCellValue());
  770. assertEquals(CellType.BLANK, cell.getCellType());
  771. cell = sheet.createRow(0).createCell(1);
  772. cell.setCellValue(wb.getCreationHelper().createRichTextString("Test"));
  773. assertEquals(CellType.STRING, cell.getCellType());
  774. cell.setCellValue(nullStr);
  775. assertEquals("", cell.getStringCellValue());
  776. assertEquals(CellType.BLANK, cell.getCellType());
  777. wb.close();
  778. }
  779. private void checkUnicodeValues(Workbook wb) {
  780. assertEquals((wb instanceof HSSFWorkbook ? "row 0, cell 0 _x0046_ without changes" : "row 0, cell 0 F without changes"),
  781. wb.getSheetAt(0).getRow(0).getCell(0).toString());
  782. assertEquals((wb instanceof HSSFWorkbook ? "row 0, cell 1 _x005fx0046_ with changes" : "row 0, cell 1 _x005fx0046_ with changes"),
  783. wb.getSheetAt(0).getRow(0).getCell(1).toString());
  784. assertEquals((wb instanceof HSSFWorkbook ? "hgh_x0041_**_x0100_*_x0101_*_x0190_*_x0200_*_x0300_*_x0427_*" : "hghA**\u0100*\u0101*\u0190*\u0200*\u0300*\u0427*"),
  785. wb.getSheetAt(0).getRow(0).getCell(2).toString());
  786. }
  787. /**
  788. * The maximum length of cell contents (text) is 32,767 characters.
  789. */
  790. @Test
  791. public void testMaxTextLength() throws IOException{
  792. Workbook wb = _testDataProvider.createWorkbook();
  793. Sheet sheet = wb.createSheet();
  794. Cell cell = sheet.createRow(0).createCell(0);
  795. int maxlen = wb instanceof HSSFWorkbook ?
  796. SpreadsheetVersion.EXCEL97.getMaxTextLength()
  797. : SpreadsheetVersion.EXCEL2007.getMaxTextLength();
  798. assertEquals(32767, maxlen);
  799. StringBuilder b = new StringBuilder() ;
  800. // 32767 is okay
  801. for( int i = 0 ; i < maxlen ; i++ )
  802. {
  803. b.append( "X" ) ;
  804. }
  805. cell.setCellValue(b.toString());
  806. b.append("X");
  807. // 32768 produces an invalid XLS file
  808. try {
  809. cell.setCellValue(b.toString());
  810. fail("Expected exception");
  811. } catch (IllegalArgumentException e){
  812. assertEquals("The maximum length of cell contents (text) is 32767 characters", e.getMessage());
  813. }
  814. wb.close();
  815. }
  816. /**
  817. * Tests that the setAsActiveCell and getActiveCell function pairs work together
  818. */
  819. @Test
  820. public void setAsActiveCell() throws IOException {
  821. Workbook wb = _testDataProvider.createWorkbook();
  822. Sheet sheet = wb.createSheet();
  823. Row row = sheet.createRow(0);
  824. Cell A1 = row.createCell(0);
  825. Cell B1 = row.createCell(1);
  826. A1.setAsActiveCell();
  827. assertEquals(A1.getAddress(), sheet.getActiveCell());
  828. B1.setAsActiveCell();
  829. assertEquals(B1.getAddress(), sheet.getActiveCell());
  830. wb.close();
  831. }
  832. @Test
  833. public void getCellComment() throws IOException {
  834. Workbook wb = _testDataProvider.createWorkbook();
  835. Sheet sheet = wb.createSheet();
  836. CreationHelper factory = wb.getCreationHelper();
  837. Row row = sheet.createRow(0);
  838. Cell cell = row.createCell(1);
  839. // cell does not have a comment
  840. assertNull(cell.getCellComment());
  841. // add a cell comment
  842. ClientAnchor anchor = factory.createClientAnchor();
  843. anchor.setCol1(cell.getColumnIndex());
  844. anchor.setCol2(cell.getColumnIndex()+1);
  845. anchor.setRow1(row.getRowNum());
  846. anchor.setRow2(row.getRowNum()+3);
  847. Drawing<?> drawing = sheet.createDrawingPatriarch();
  848. Comment comment = drawing.createCellComment(anchor);
  849. RichTextString str = factory.createRichTextString("Hello, World!");
  850. comment.setString(str);
  851. comment.setAuthor("Apache POI");
  852. cell.setCellComment(comment);
  853. // ideally assertSame, but XSSFCell creates a new XSSFCellComment wrapping the same bean for every call to getCellComment.
  854. assertEquals(comment, cell.getCellComment());
  855. wb.close();
  856. }
  857. @Test
  858. public void testSetErrorValue() throws Exception {
  859. try (Workbook wb = _testDataProvider.createWorkbook()) {
  860. Sheet sheet = wb.createSheet();
  861. Row row = sheet.createRow(0);
  862. Cell cell = row.createCell(0);
  863. cell.setCellFormula("A2");
  864. cell.setCellErrorValue(FormulaError.NAME.getCode());
  865. assertEquals("Should still be a formula even after we set an error value",
  866. CellType.FORMULA, cell.getCellType());
  867. assertEquals("Should still be a formula even after we set an error value",
  868. CellType.ERROR, cell.getCachedFormulaResultType());
  869. assertEquals("A2", cell.getCellFormula());
  870. try {
  871. cell.getNumericCellValue();
  872. fail("Should catch exception here");
  873. } catch (IllegalStateException e) {
  874. // expected here
  875. }
  876. try {
  877. cell.getStringCellValue();
  878. fail("Should catch exception here");
  879. } catch (IllegalStateException e) {
  880. // expected here
  881. }
  882. try {
  883. cell.getRichStringCellValue();
  884. fail("Should catch exception here");
  885. } catch (IllegalStateException e) {
  886. // expected here
  887. }
  888. try {
  889. cell.getDateCellValue();
  890. fail("Should catch exception here");
  891. } catch (IllegalStateException e) {
  892. // expected here
  893. }
  894. assertEquals(FormulaError.NAME.getCode(), cell.getErrorCellValue());
  895. assertNull(cell.getHyperlink());
  896. }
  897. }
  898. @Test
  899. public void test62216() throws IOException {
  900. try (Workbook wb = _testDataProvider.createWorkbook()) {
  901. Cell instance = wb.createSheet().createRow(0).createCell(0);
  902. String formula = "2";
  903. instance.setCellFormula(formula);
  904. instance.setCellErrorValue(FormulaError.NAME.getCode());
  905. assertEquals(formula, instance.getCellFormula());
  906. }
  907. }
  908. @Test
  909. public void testSetNullValues() throws IOException {
  910. Workbook wb = _testDataProvider.createWorkbook();
  911. Cell cell = wb.createSheet("test").createRow(0).createCell(0);
  912. cell.setCellValue((Calendar)null);
  913. assertEquals(CellType.BLANK, cell.getCellType());
  914. assertEquals("", cell.getStringCellValue());
  915. cell.setCellValue((Date)null);
  916. assertEquals(CellType.BLANK, cell.getCellType());
  917. assertEquals("", cell.getStringCellValue());
  918. cell.setCellValue((String)null);
  919. assertEquals(CellType.BLANK, cell.getCellType());
  920. assertEquals("", cell.getStringCellValue());
  921. assertEquals(CellType.BLANK, cell.getCellType());
  922. assertEquals("", cell.getStringCellValue());
  923. cell.setCellValue((RichTextString) null);
  924. assertEquals(CellType.BLANK, cell.getCellType());
  925. assertEquals("", cell.getStringCellValue());
  926. cell.setCellValue((String)null);
  927. assertEquals(CellType.BLANK, cell.getCellType());
  928. assertEquals("", cell.getStringCellValue());
  929. wb.close();
  930. }
  931. @Test
  932. public void testFormulaSetValueDoesNotChangeType() throws IOException {
  933. try (Workbook wb = _testDataProvider.createWorkbook()) {
  934. Sheet sheet = wb.createSheet();
  935. Row row = sheet.createRow(0);
  936. Cell cell = row.createCell(0);
  937. cell.setCellFormula("SQRT(-1)");
  938. assertEquals(CellType.FORMULA, cell.getCellType());
  939. cell.setCellValue(new Date());
  940. assertEquals(CellType.FORMULA, cell.getCellType());
  941. cell.setCellValue(GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT));
  942. assertEquals(CellType.FORMULA, cell.getCellType());
  943. cell.setCellValue(1.0);
  944. assertEquals(CellType.FORMULA, cell.getCellType());
  945. cell.setCellValue("test");
  946. assertEquals(CellType.FORMULA, cell.getCellType());
  947. cell.setCellValue(wb.getCreationHelper().createRichTextString("test"));
  948. assertEquals(CellType.FORMULA, cell.getCellType());
  949. cell.setCellValue(false);
  950. assertEquals(CellType.FORMULA, cell.getCellType());
  951. }
  952. }
  953. @Test
  954. public void testGetNumericCellValueOnABlankCellReturnsZero() {
  955. Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
  956. assertEquals(CellType.BLANK, cell.getCellType());
  957. assertEquals(0, cell.getNumericCellValue(), 0);
  958. }
  959. @Test
  960. public void getDateCellValue_returnsNull_onABlankCell() {
  961. Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
  962. assertEquals(CellType.BLANK, cell.getCellType());
  963. Date result = cell.getDateCellValue();
  964. assertNull(result);
  965. }
  966. @Test
  967. public void getBooleanCellValue_returnsFalse_onABlankCell() {
  968. Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
  969. assertEquals(CellType.BLANK, cell.getCellType());
  970. boolean result = cell.getBooleanCellValue();
  971. assertFalse(result);
  972. }
  973. @Test
  974. public void setStringCellValue_ifThrows_shallNotChangeCell() {
  975. Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
  976. final double value = 2.78;
  977. cell.setCellValue(value);
  978. assertEquals(CellType.NUMERIC, cell.getCellType());
  979. int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1;
  980. String badStringValue = new String(new byte[badLength], StandardCharsets.UTF_8);
  981. try {
  982. cell.setCellValue(badStringValue);
  983. } catch (IllegalArgumentException e) {
  984. // no-op, expected to throw but we need to assert something more
  985. }
  986. assertEquals(CellType.NUMERIC, cell.getCellType());
  987. assertEquals(value, cell.getNumericCellValue(), 0);
  988. }
  989. @Test
  990. public void setStringCellValueWithRichTextString_ifThrows_shallNotChangeCell() {
  991. Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
  992. final double value = 2.78;
  993. cell.setCellValue(value);
  994. assertEquals(CellType.NUMERIC, cell.getCellType());
  995. int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1;
  996. RichTextString badStringValue = cell.getSheet().getWorkbook().getCreationHelper().
  997. createRichTextString(new String(new byte[badLength], StandardCharsets.UTF_8));
  998. try {
  999. cell.setCellValue(badStringValue);
  1000. } catch (IllegalArgumentException e) {
  1001. // no-op, expected to throw but we need to assert something more
  1002. }
  1003. assertEquals(CellType.NUMERIC, cell.getCellType());
  1004. assertEquals(value, cell.getNumericCellValue(), 0);
  1005. }
  1006. @Test(expected = IllegalArgumentException.class)
  1007. public void setCellType_null_throwsIAE() {
  1008. Cell cell = getInstance();
  1009. cell.setCellType(null);
  1010. }
  1011. @Test(expected = IllegalArgumentException.class)
  1012. public void setCellType_NONE_throwsIAE() {
  1013. Cell cell = getInstance();
  1014. cell.setCellType(CellType._NONE);
  1015. }
  1016. @Test
  1017. public void setBlank_removesArrayFormula_ifCellIsPartOfAnArrayFormulaGroupContainingOnlyThisCell() {
  1018. Cell cell = getInstance();
  1019. cell.getSheet().setArrayFormula("1", CellRangeAddress.valueOf("A1"));
  1020. cell.setCellValue("foo");
  1021. assertTrue(cell.isPartOfArrayFormulaGroup());
  1022. assertEquals("1", cell.getCellFormula());
  1023. cell.setBlank();
  1024. assertEquals(CellType.BLANK, cell.getCellType());
  1025. assertFalse(cell.isPartOfArrayFormulaGroup());
  1026. }
  1027. @Test(expected = IllegalStateException.class)
  1028. public void setBlank_throwsISE_ifCellIsPartOfAnArrayFormulaGroupContainingOtherCells() {
  1029. Cell cell = getInstance();
  1030. cell.getSheet().setArrayFormula("1", CellRangeAddress.valueOf("A1:B1"));
  1031. cell.setCellValue("foo");
  1032. cell.setBlank();
  1033. }
  1034. @Test(expected = IllegalStateException.class)
  1035. public void setCellFormula_throwsISE_ifCellIsPartOfAnArrayFormulaGroupContainingOtherCells() {
  1036. Cell cell = getInstance();
  1037. cell.getSheet().setArrayFormula("1", CellRangeAddress.valueOf("A1:B1"));
  1038. assertTrue(cell.isPartOfArrayFormulaGroup());
  1039. assertEquals(CellType.FORMULA, cell.getCellType());
  1040. cell.setCellFormula("1");
  1041. }
  1042. @Test
  1043. public void removeFormula_preservesValue() {
  1044. Cell cell = getInstance();
  1045. cell.setCellFormula("#DIV/0!");
  1046. cell.setCellValue(true);
  1047. cell.removeFormula();
  1048. assertEquals(CellType.BOOLEAN, cell.getCellType());
  1049. assertTrue(cell.getBooleanCellValue());
  1050. cell.setCellFormula("#DIV/0!");
  1051. cell.setCellValue(2);
  1052. cell.removeFormula();
  1053. assertEquals(CellType.NUMERIC, cell.getCellType());
  1054. assertEquals(2, cell.getNumericCellValue(), 0);
  1055. cell.setCellFormula("#DIV/0!");
  1056. cell.setCellValue("foo");
  1057. cell.removeFormula();
  1058. assertEquals(CellType.STRING, cell.getCellType());
  1059. assertEquals("foo", cell.getStringCellValue());
  1060. cell.setCellFormula("#DIV/0!");
  1061. cell.setCellErrorValue(FormulaError.NUM.getCode());
  1062. cell.removeFormula();
  1063. assertEquals(CellType.ERROR, cell.getCellType());
  1064. assertEquals(FormulaError.NUM.getCode(), cell.getErrorCellValue());
  1065. }
  1066. @Test
  1067. public void removeFormula_turnsCellToBlank_whenFormulaWasASingleCellArrayFormula() {
  1068. Cell cell = getInstance();
  1069. cell.getSheet().setArrayFormula("#DIV/0!", CellRangeAddress.valueOf("A1"));
  1070. cell.setCellValue(true);
  1071. cell.removeFormula();
  1072. assertEquals(CellType.BLANK, cell.getCellType());
  1073. cell.getSheet().setArrayFormula("#DIV/0!", CellRangeAddress.valueOf("A1"));
  1074. cell.setCellValue(2);
  1075. cell.removeFormula();
  1076. assertEquals(CellType.BLANK, cell.getCellType());
  1077. cell.getSheet().setArrayFormula("#DIV/0!", CellRangeAddress.valueOf("A1"));
  1078. cell.setCellValue(true);
  1079. cell.removeFormula();
  1080. assertEquals(CellType.BLANK, cell.getCellType());
  1081. cell.getSheet().setArrayFormula("#DIV/0!", CellRangeAddress.valueOf("A1"));
  1082. cell.setCellErrorValue(FormulaError.NUM.getCode());
  1083. cell.removeFormula();
  1084. assertEquals(CellType.BLANK, cell.getCellType());
  1085. }
  1086. @Test
  1087. public void setCellFormula_onABlankCell_setsValueToZero() {
  1088. Cell cell = getInstance();
  1089. cell.setCellFormula("\"foo\"");
  1090. assertEquals(CellType.FORMULA, cell.getCellType());
  1091. assertEquals(CellType.NUMERIC, cell.getCachedFormulaResultType());
  1092. assertEquals(0, cell.getNumericCellValue(), 0);
  1093. }
  1094. @Test
  1095. public void setCellFormula_onANonBlankCell_preservesTheValue() {
  1096. Cell cell = getInstance();
  1097. cell.setCellValue(true);
  1098. cell.setCellFormula("\"foo\"");
  1099. assertEquals(CellType.FORMULA, cell.getCellType());
  1100. assertEquals(CellType.BOOLEAN, cell.getCachedFormulaResultType());
  1101. assertTrue(cell.getBooleanCellValue());
  1102. }
  1103. @Test
  1104. public void setCellFormula_onAFormulaCell_changeFormula_preservesTheValue() {
  1105. Cell cell = getInstance();
  1106. cell.setCellFormula("\"foo\"");
  1107. cell.setCellValue(true);
  1108. assertEquals(CellType.FORMULA, cell.getCellType());
  1109. assertEquals(CellType.BOOLEAN, cell.getCachedFormulaResultType());
  1110. assertTrue(cell.getBooleanCellValue());
  1111. cell.setCellFormula("\"bar\"");
  1112. assertEquals(CellType.FORMULA, cell.getCellType());
  1113. assertEquals(CellType.BOOLEAN, cell.getCachedFormulaResultType());
  1114. assertTrue(cell.getBooleanCellValue());
  1115. }
  1116. @Test
  1117. public void setCellFormula_onASingleCellArrayFormulaCell_preservesTheValue() {
  1118. Cell cell = getInstance();
  1119. cell.getSheet().setArrayFormula("\"foo\"", CellRangeAddress.valueOf("A1"));
  1120. cell.setCellValue(true);
  1121. assertTrue(cell.isPartOfArrayFormulaGroup());
  1122. assertEquals(CellType.FORMULA, cell.getCellType());
  1123. assertEquals(CellType.BOOLEAN, cell.getCachedFormulaResultType());
  1124. assertTrue(cell.getBooleanCellValue());
  1125. cell.getSheet().setArrayFormula("\"bar\"", CellRangeAddress.valueOf("A1"));
  1126. assertEquals(CellType.FORMULA, cell.getCellType());
  1127. assertEquals(CellType.BOOLEAN, cell.getCachedFormulaResultType());
  1128. assertTrue(cell.getBooleanCellValue());
  1129. }
  1130. @Test(expected = IllegalArgumentException.class)
  1131. public void setCellType_FORMULA_onANonFormulaCell_throwsIllegalArgumentException() {
  1132. Cell cell = getInstance();
  1133. cell.setCellType(CellType.FORMULA);
  1134. }
  1135. @Test
  1136. public void setCellType_FORMULA_onAFormulaCell_doesNothing() {
  1137. Cell cell = getInstance();
  1138. cell.setCellFormula("3");
  1139. cell.setCellValue("foo");
  1140. cell.setCellType(CellType.FORMULA);
  1141. assertEquals(CellType.FORMULA, cell.getCellType());
  1142. assertEquals(CellType.STRING, cell.getCachedFormulaResultType());
  1143. assertEquals("foo", cell.getStringCellValue());
  1144. }
  1145. @Test
  1146. public void setCellType_FORMULA_onAnArrayFormulaCell_doesNothing() {
  1147. Cell cell = getInstance();
  1148. cell.getSheet().setArrayFormula("3", CellRangeAddress.valueOf("A1:A2"));
  1149. cell.setCellValue("foo");
  1150. cell.setCellType(CellType.FORMULA);
  1151. assertEquals(CellType.FORMULA, cell.getCellType());
  1152. assertEquals(CellType.STRING, cell.getCachedFormulaResultType());
  1153. assertEquals("foo", cell.getStringCellValue());
  1154. }
  1155. @Test
  1156. public final void setBlank_delegatesTo_setCellType_BLANK() {
  1157. Cell cell = mock(CellBase.class);
  1158. doCallRealMethod().when(cell).setBlank();
  1159. cell.setBlank();
  1160. verify(cell).setCellType(CellType.BLANK);
  1161. }
  1162. private Cell getInstance() {
  1163. return _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
  1164. }
  1165. }