Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TestBugs.java 69KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039
  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.hssf.usermodel;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.File;
  18. import java.io.FileNotFoundException;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import junit.framework.AssertionFailedError;
  26. import org.apache.poi.hssf.HSSFITestDataProvider;
  27. import org.apache.poi.hssf.HSSFTestDataSamples;
  28. import org.apache.poi.hssf.OldExcelFormatException;
  29. import org.apache.poi.hssf.model.InternalWorkbook;
  30. import org.apache.poi.hssf.record.CellValueRecordInterface;
  31. import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord;
  32. import org.apache.poi.hssf.record.NameRecord;
  33. import org.apache.poi.hssf.record.Record;
  34. import org.apache.poi.hssf.record.TabIdRecord;
  35. import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
  36. import org.apache.poi.hssf.record.common.UnicodeString;
  37. import org.apache.poi.ss.formula.ptg.Area3DPtg;
  38. import org.apache.poi.ss.formula.ptg.DeletedArea3DPtg;
  39. import org.apache.poi.ss.formula.ptg.Ptg;
  40. import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
  41. import org.apache.poi.ss.usermodel.Cell;
  42. import org.apache.poi.ss.usermodel.CellStyle;
  43. import org.apache.poi.ss.usermodel.Name;
  44. import org.apache.poi.ss.usermodel.Row;
  45. import org.apache.poi.ss.usermodel.Sheet;
  46. import org.apache.poi.util.TempFile;
  47. /**
  48. * Testcases for bugs entered in bugzilla
  49. * the Test name contains the bugzilla bug id
  50. *
  51. * <b>YK: If a bug can be tested in terms of common ss interfaces,
  52. * define the test in the base class {@link BaseTestBugzillaIssues}</b>
  53. *
  54. * @author Avik Sengupta
  55. * @author Yegor Kozlov
  56. */
  57. public final class TestBugs extends BaseTestBugzillaIssues {
  58. public TestBugs() {
  59. super(HSSFITestDataProvider.instance);
  60. }
  61. private static HSSFWorkbook openSample(String sampleFileName) {
  62. return HSSFITestDataProvider.instance.openSampleWorkbook(sampleFileName);
  63. }
  64. private static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {
  65. return HSSFITestDataProvider.instance.writeOutAndReadBack(original);
  66. }
  67. private static void writeTestOutputFileForViewing(HSSFWorkbook wb, String simpleFileName) {
  68. if (true) { // set to false to output test files
  69. return;
  70. }
  71. System.setProperty("poi.keep.tmp.files", "true");
  72. File file;
  73. try {
  74. file = TempFile.createTempFile(simpleFileName + "#", ".xls");
  75. FileOutputStream out = new FileOutputStream(file);
  76. wb.write(out);
  77. out.close();
  78. } catch (IOException e) {
  79. throw new RuntimeException(e);
  80. }
  81. if (!file.exists()) {
  82. throw new RuntimeException("File was not written");
  83. }
  84. System.out.println("Open file '" + file.getAbsolutePath() + "' in Excel");
  85. }
  86. /** Test reading AND writing a complicated workbook
  87. *Test opening resulting sheet in excel*/
  88. public void test15228() {
  89. HSSFWorkbook wb = openSample("15228.xls");
  90. HSSFSheet s = wb.getSheetAt(0);
  91. HSSFRow r = s.createRow(0);
  92. HSSFCell c = r.createCell(0);
  93. c.setCellValue(10);
  94. writeTestOutputFileForViewing(wb, "test15228");
  95. }
  96. public void test13796() {
  97. HSSFWorkbook wb = openSample("13796.xls");
  98. HSSFSheet s = wb.getSheetAt(0);
  99. HSSFRow r = s.createRow(0);
  100. HSSFCell c = r.createCell(0);
  101. c.setCellValue(10);
  102. writeOutAndReadBack(wb);
  103. }
  104. /** test hyperlinks
  105. * open resulting file in excel, and check that there is a link to Google
  106. */
  107. public void test15353() {
  108. HSSFWorkbook wb = new HSSFWorkbook();
  109. HSSFSheet sheet = wb.createSheet("My sheet");
  110. HSSFRow row = sheet.createRow( 0 );
  111. HSSFCell cell = row.createCell( 0 );
  112. cell.setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
  113. writeOutAndReadBack(wb);
  114. }
  115. /** test reading of a formula with a name and a cell ref in one
  116. **/
  117. public void test14460() {
  118. HSSFWorkbook wb = openSample("14460.xls");
  119. wb.getSheetAt(0);
  120. }
  121. public void test14330() {
  122. HSSFWorkbook wb = openSample("14330-1.xls");
  123. wb.getSheetAt(0);
  124. wb = openSample("14330-2.xls");
  125. wb.getSheetAt(0);
  126. }
  127. private static void setCellText(HSSFCell cell, String text) {
  128. cell.setCellValue(new HSSFRichTextString(text));
  129. }
  130. /** test rewriting a file with large number of unique strings
  131. *open resulting file in Excel to check results!*/
  132. public void test15375() {
  133. HSSFWorkbook wb = openSample("15375.xls");
  134. HSSFSheet sheet = wb.getSheetAt(0);
  135. HSSFRow row = sheet.getRow(5);
  136. HSSFCell cell = row.getCell(3);
  137. if (cell == null)
  138. cell = row.createCell(3);
  139. // Write test
  140. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  141. setCellText(cell, "a test");
  142. // change existing numeric cell value
  143. HSSFRow oRow = sheet.getRow(14);
  144. HSSFCell oCell = oRow.getCell(4);
  145. oCell.setCellValue(75);
  146. oCell = oRow.getCell(5);
  147. setCellText(oCell, "0.3");
  148. writeTestOutputFileForViewing(wb, "test15375");
  149. }
  150. /**
  151. * test writing a file with large number of unique strings,
  152. * open resulting file in Excel to check results!
  153. */
  154. public void test15375_2() {
  155. baseTest15375(6000);
  156. }
  157. /**Double byte strings*/
  158. public void test15556() {
  159. HSSFWorkbook wb = openSample("15556.xls");
  160. HSSFSheet sheet = wb.getSheetAt(0);
  161. HSSFRow row = sheet.getRow(45);
  162. assertNotNull("Read row fine!" , row);
  163. }
  164. /**Double byte strings */
  165. public void test22742() {
  166. openSample("22742.xls");
  167. }
  168. /**Double byte strings */
  169. public void test12561_1() {
  170. openSample("12561-1.xls");
  171. }
  172. /** Double byte strings */
  173. public void test12561_2() {
  174. openSample("12561-2.xls");
  175. }
  176. /** Double byte strings
  177. File supplied by jubeson*/
  178. public void test12843_1() {
  179. openSample("12843-1.xls");
  180. }
  181. /** Double byte strings
  182. File supplied by Paul Chung*/
  183. public void test12843_2() {
  184. openSample("12843-2.xls");
  185. }
  186. /** Reference to Name*/
  187. public void test13224() {
  188. openSample("13224.xls");
  189. }
  190. /** Illegal argument exception - cannot store duplicate value in Map*/
  191. public void test19599() {
  192. openSample("19599-1.xls");
  193. openSample("19599-2.xls");
  194. }
  195. public void test24215() {
  196. HSSFWorkbook wb = openSample("24215.xls");
  197. for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets();sheetIndex++) {
  198. HSSFSheet sheet = wb.getSheetAt(sheetIndex);
  199. int rows = sheet.getLastRowNum();
  200. for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
  201. HSSFRow row = sheet.getRow(rowIndex);
  202. int cells = row.getLastCellNum();
  203. for (int cellIndex = 0; cellIndex < cells; cellIndex++) {
  204. row.getCell(cellIndex);
  205. }
  206. }
  207. }
  208. }
  209. /**Tests read and write of Unicode strings in formula results
  210. * bug and testcase submitted by Sompop Kumnoonsate
  211. * The file contains THAI unicode characters.
  212. */
  213. public void testUnicodeStringFormulaRead() {
  214. HSSFWorkbook w = openSample("25695.xls");
  215. HSSFCell a1 = w.getSheetAt(0).getRow(0).getCell(0);
  216. HSSFCell a2 = w.getSheetAt(0).getRow(0).getCell(1);
  217. HSSFCell b1 = w.getSheetAt(0).getRow(1).getCell(0);
  218. HSSFCell b2 = w.getSheetAt(0).getRow(1).getCell(1);
  219. HSSFCell c1 = w.getSheetAt(0).getRow(2).getCell(0);
  220. HSSFCell c2 = w.getSheetAt(0).getRow(2).getCell(1);
  221. HSSFCell d1 = w.getSheetAt(0).getRow(3).getCell(0);
  222. HSSFCell d2 = w.getSheetAt(0).getRow(3).getCell(1);
  223. if (false) {
  224. // THAI code page
  225. System.out.println("a1="+unicodeString(a1));
  226. System.out.println("a2="+unicodeString(a2));
  227. // US code page
  228. System.out.println("b1="+unicodeString(b1));
  229. System.out.println("b2="+unicodeString(b2));
  230. // THAI+US
  231. System.out.println("c1="+unicodeString(c1));
  232. System.out.println("c2="+unicodeString(c2));
  233. // US+THAI
  234. System.out.println("d1="+unicodeString(d1));
  235. System.out.println("d2="+unicodeString(d2));
  236. }
  237. confirmSameCellText(a1, a2);
  238. confirmSameCellText(b1, b2);
  239. confirmSameCellText(c1, c2);
  240. confirmSameCellText(d1, d2);
  241. HSSFWorkbook rw = writeOutAndReadBack(w);
  242. HSSFCell ra1 = rw.getSheetAt(0).getRow(0).getCell(0);
  243. HSSFCell ra2 = rw.getSheetAt(0).getRow(0).getCell(1);
  244. HSSFCell rb1 = rw.getSheetAt(0).getRow(1).getCell(0);
  245. HSSFCell rb2 = rw.getSheetAt(0).getRow(1).getCell(1);
  246. HSSFCell rc1 = rw.getSheetAt(0).getRow(2).getCell(0);
  247. HSSFCell rc2 = rw.getSheetAt(0).getRow(2).getCell(1);
  248. HSSFCell rd1 = rw.getSheetAt(0).getRow(3).getCell(0);
  249. HSSFCell rd2 = rw.getSheetAt(0).getRow(3).getCell(1);
  250. confirmSameCellText(a1, ra1);
  251. confirmSameCellText(b1, rb1);
  252. confirmSameCellText(c1, rc1);
  253. confirmSameCellText(d1, rd1);
  254. confirmSameCellText(a1, ra2);
  255. confirmSameCellText(b1, rb2);
  256. confirmSameCellText(c1, rc2);
  257. confirmSameCellText(d1, rd2);
  258. }
  259. private static void confirmSameCellText(HSSFCell a, HSSFCell b) {
  260. assertEquals(a.getRichStringCellValue().getString(), b.getRichStringCellValue().getString());
  261. }
  262. private static String unicodeString(HSSFCell cell) {
  263. String ss = cell.getRichStringCellValue().getString();
  264. char s[] = ss.toCharArray();
  265. StringBuffer sb = new StringBuffer();
  266. for (int x=0;x<s.length;x++) {
  267. sb.append("\\u").append(Integer.toHexString(s[x]));
  268. }
  269. return sb.toString();
  270. }
  271. /** Error in opening wb*/
  272. public void test32822() {
  273. openSample("32822.xls");
  274. }
  275. /**fail to read wb with chart */
  276. public void test15573() {
  277. openSample("15573.xls");
  278. }
  279. /**names and macros */
  280. public void test27852() {
  281. HSSFWorkbook wb = openSample("27852.xls");
  282. for(int i = 0 ; i < wb.getNumberOfNames(); i++){
  283. HSSFName name = wb.getNameAt(i);
  284. name.getNameName();
  285. if (name.isFunctionName()) {
  286. continue;
  287. }
  288. name.getRefersToFormula();
  289. }
  290. }
  291. public void test33082() {
  292. openSample("33082.xls");
  293. }
  294. public void test34775() {
  295. try {
  296. openSample("34775.xls");
  297. } catch (NullPointerException e) {
  298. throw new AssertionFailedError("identified bug 34775");
  299. }
  300. }
  301. /** Error when reading then writing ArrayValues in NameRecord's*/
  302. public void test37630() {
  303. HSSFWorkbook wb = openSample("37630.xls");
  304. writeOutAndReadBack(wb);
  305. }
  306. /**
  307. * Bug 25183: org.apache.poi.hssf.usermodel.HSSFSheet.setPropertiesFromSheet
  308. */
  309. public void test25183() {
  310. HSSFWorkbook wb = openSample("25183.xls");
  311. writeOutAndReadBack(wb);
  312. }
  313. /**
  314. * Bug 26100: 128-character message in IF statement cell causes HSSFWorkbook open failure
  315. */
  316. public void test26100() {
  317. HSSFWorkbook wb = openSample("26100.xls");
  318. writeOutAndReadBack(wb);
  319. }
  320. /**
  321. * Bug 27933: Unable to use a template (xls) file containing a wmf graphic
  322. */
  323. public void test27933() {
  324. HSSFWorkbook wb = openSample("27933.xls");
  325. writeOutAndReadBack(wb);
  326. }
  327. /**
  328. * Bug 29206: NPE on HSSFSheet.getRow for blank rows
  329. */
  330. public void test29206() {
  331. //the first check with blank workbook
  332. HSSFWorkbook wb = openSample("Simple.xls");
  333. HSSFSheet sheet = wb.createSheet();
  334. for(int i = 1; i < 400; i++) {
  335. HSSFRow row = sheet.getRow(i);
  336. if(row != null) {
  337. row.getCell(0);
  338. }
  339. }
  340. }
  341. /**
  342. * Bug 29675: POI 2.5 final corrupts output when starting workbook has a graphic
  343. */
  344. public void test29675() {
  345. HSSFWorkbook wb = openSample("29675.xls");
  346. writeOutAndReadBack(wb);
  347. }
  348. /**
  349. * Bug 29942: Importing Excel files that have been created by Open Office on Linux
  350. */
  351. public void test29942() {
  352. HSSFWorkbook wb = openSample("29942.xls");
  353. HSSFSheet sheet = wb.getSheetAt(0);
  354. int count = 0;
  355. for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
  356. HSSFRow row = sheet.getRow(i);
  357. if (row != null) {
  358. HSSFCell cell = row .getCell(0);
  359. assertEquals(HSSFCell.CELL_TYPE_STRING, cell.getCellType());
  360. count++;
  361. }
  362. }
  363. assertEquals(85, count); //should read 85 rows
  364. writeOutAndReadBack(wb);
  365. }
  366. /**
  367. * Bug 29982: Unable to read spreadsheet when dropdown list cell is selected -
  368. * Unable to construct record instance
  369. */
  370. public void test29982() {
  371. HSSFWorkbook wb = openSample("29982.xls");
  372. writeOutAndReadBack(wb);
  373. }
  374. /**
  375. * Bug 30540: HSSFSheet.setRowBreak throws NullPointerException
  376. */
  377. public void test30540() {
  378. HSSFWorkbook wb = openSample("30540.xls");
  379. HSSFSheet s = wb.getSheetAt(0);
  380. s.setRowBreak(1);
  381. writeOutAndReadBack(wb);
  382. }
  383. /**
  384. * Bug 31749: {Need help urgently}[This is critical] workbook.write() corrupts the file......?
  385. */
  386. public void test31749() {
  387. HSSFWorkbook wb = openSample("31749.xls");
  388. writeOutAndReadBack(wb);
  389. }
  390. /**
  391. * Bug 31979: {urgent help needed .....}poi library does not support form objects properly.
  392. */
  393. public void test31979() {
  394. HSSFWorkbook wb = openSample("31979.xls");
  395. writeOutAndReadBack(wb);
  396. }
  397. /**
  398. * Bug 35564: HSSFCell.java: NullPtrExc in isGridsPrinted() and getProtect()
  399. * when HSSFWorkbook is created from file
  400. */
  401. public void test35564() {
  402. HSSFWorkbook wb = openSample("35564.xls");
  403. HSSFSheet sheet = wb.getSheetAt( 0 );
  404. assertEquals(false, sheet.isGridsPrinted());
  405. assertEquals(false, sheet.getProtect());
  406. writeOutAndReadBack(wb);
  407. }
  408. /**
  409. * Bug 35565: HSSFCell.java: NullPtrExc in getColumnBreaks() when HSSFWorkbook is created from file
  410. */
  411. public void test35565() {
  412. HSSFWorkbook wb = openSample("35565.xls");
  413. HSSFSheet sheet = wb.getSheetAt( 0 );
  414. assertNotNull(sheet);
  415. writeOutAndReadBack(wb);
  416. }
  417. /**
  418. * Bug 37376: Cannot open the saved Excel file if checkbox controls exceed certain limit
  419. */
  420. public void test37376() {
  421. HSSFWorkbook wb = openSample("37376.xls");
  422. writeOutAndReadBack(wb);
  423. }
  424. /**
  425. * Bug 40285: CellIterator Skips First Column
  426. */
  427. public void test40285() {
  428. HSSFWorkbook wb = openSample("40285.xls");
  429. HSSFSheet sheet = wb.getSheetAt( 0 );
  430. int rownum = 0;
  431. for (Iterator it = sheet.rowIterator(); it.hasNext(); rownum++) {
  432. HSSFRow row = (HSSFRow)it.next();
  433. assertEquals(rownum, row.getRowNum());
  434. int cellNum = 0;
  435. for (Iterator it2 = row.cellIterator(); it2.hasNext(); cellNum++) {
  436. HSSFCell cell = (HSSFCell)it2.next();
  437. assertEquals(cellNum, cell.getColumnIndex());
  438. }
  439. }
  440. }
  441. /**
  442. * Test bug 38266: NPE when adding a row break
  443. *
  444. * User's diagnosis:
  445. * 1. Manually (i.e., not using POI) create an Excel Workbook, making sure it
  446. * contains a sheet that doesn't have any row breaks
  447. * 2. Using POI, create a new HSSFWorkbook from the template in step #1
  448. * 3. Try adding a row break (via sheet.setRowBreak()) to the sheet mentioned in step #1
  449. * 4. Get a NullPointerException
  450. */
  451. public void test38266() {
  452. String[] files = {"Simple.xls", "SimpleMultiCell.xls", "duprich1.xls"};
  453. for (int i = 0; i < files.length; i++) {
  454. HSSFWorkbook wb = openSample(files[i]);
  455. HSSFSheet sheet = wb.getSheetAt( 0 );
  456. int[] breaks = sheet.getRowBreaks();
  457. assertEquals(0, breaks.length);
  458. //add 3 row breaks
  459. for (int j = 1; j <= 3; j++) {
  460. sheet.setRowBreak(j*20);
  461. }
  462. }
  463. }
  464. public void test40738() {
  465. HSSFWorkbook wb = openSample("SimpleWithAutofilter.xls");
  466. writeOutAndReadBack(wb);
  467. }
  468. /**
  469. * Bug 44200: Sheet not cloneable when Note added to excel cell
  470. */
  471. public void test44200() {
  472. HSSFWorkbook wb = openSample("44200.xls");
  473. wb.cloneSheet(0);
  474. writeOutAndReadBack(wb);
  475. }
  476. /**
  477. * Bug 44201: Sheet not cloneable when validation added to excel cell
  478. */
  479. public void test44201() {
  480. HSSFWorkbook wb = openSample("44201.xls");
  481. writeOutAndReadBack(wb);
  482. }
  483. /**
  484. * Bug 37684 : Unhandled Continue Record Error
  485. */
  486. public void test37684 () {
  487. HSSFWorkbook wb = openSample("37684-1.xls");
  488. writeOutAndReadBack(wb);
  489. wb = openSample("37684-2.xls");
  490. writeOutAndReadBack(wb);
  491. }
  492. /**
  493. * Bug 41139: Constructing HSSFWorkbook is failed,threw threw ArrayIndexOutOfBoundsException for creating UnknownRecord
  494. */
  495. public void test41139() {
  496. HSSFWorkbook wb = openSample("41139.xls");
  497. writeOutAndReadBack(wb);
  498. }
  499. /**
  500. * Bug 41546: Constructing HSSFWorkbook is failed,
  501. * Unknown Ptg in Formula: 0x1a (26)
  502. */
  503. public void test41546() {
  504. HSSFWorkbook wb = openSample("41546.xls");
  505. assertEquals(1, wb.getNumberOfSheets());
  506. wb = writeOutAndReadBack(wb);
  507. assertEquals(1, wb.getNumberOfSheets());
  508. }
  509. /**
  510. * Bug 42564: Some files from Access were giving a RecordFormatException
  511. * when reading the BOFRecord
  512. */
  513. public void test42564() {
  514. HSSFWorkbook wb = openSample("ex42564-21435.xls");
  515. writeOutAndReadBack(wb);
  516. }
  517. /**
  518. * Bug 42564: Some files from Access also have issues
  519. * with the NameRecord, once you get past the BOFRecord
  520. * issue.
  521. */
  522. public void test42564Alt() {
  523. HSSFWorkbook wb = openSample("ex42564-21503.xls");
  524. writeOutAndReadBack(wb);
  525. }
  526. /**
  527. * Bug 42618: RecordFormatException reading a file containing
  528. * =CHOOSE(2,A2,A3,A4)
  529. */
  530. public void test42618() {
  531. HSSFWorkbook wb = openSample("SimpleWithChoose.xls");
  532. wb = writeOutAndReadBack(wb);
  533. // Check we detect the string properly too
  534. HSSFSheet s = wb.getSheetAt(0);
  535. // Textual value
  536. HSSFRow r1 = s.getRow(0);
  537. HSSFCell c1 = r1.getCell(1);
  538. assertEquals("=CHOOSE(2,A2,A3,A4)", c1.getRichStringCellValue().toString());
  539. // Formula Value
  540. HSSFRow r2 = s.getRow(1);
  541. HSSFCell c2 = r2.getCell(1);
  542. assertEquals(25, (int)c2.getNumericCellValue());
  543. try {
  544. assertEquals("CHOOSE(2,A2,A3,A4)", c2.getCellFormula());
  545. } catch (IllegalStateException e) {
  546. if (e.getMessage().startsWith("Too few arguments")
  547. && e.getMessage().indexOf("ConcatPtg") > 0) {
  548. throw new AssertionFailedError("identified bug 44306");
  549. }
  550. }
  551. }
  552. /**
  553. * Something up with the FileSharingRecord
  554. */
  555. public void test43251() {
  556. // Used to blow up with an IllegalArgumentException
  557. // when creating a FileSharingRecord
  558. HSSFWorkbook wb;
  559. try {
  560. wb = openSample("43251.xls");
  561. } catch (IllegalArgumentException e) {
  562. throw new AssertionFailedError("identified bug 43251");
  563. }
  564. assertEquals(1, wb.getNumberOfSheets());
  565. }
  566. /**
  567. * Crystal reports generates files with short
  568. * StyleRecords, which is against the spec
  569. */
  570. public void test44471() {
  571. // Used to blow up with an ArrayIndexOutOfBounds
  572. // when creating a StyleRecord
  573. HSSFWorkbook wb;
  574. try {
  575. wb = openSample("OddStyleRecord.xls");
  576. } catch (ArrayIndexOutOfBoundsException e) {
  577. throw new AssertionFailedError("Identified bug 44471");
  578. }
  579. assertEquals(1, wb.getNumberOfSheets());
  580. }
  581. /**
  582. * Files with "read only recommended" were giving
  583. * grief on the FileSharingRecord
  584. */
  585. public void test44536() {
  586. // Used to blow up with an IllegalArgumentException
  587. // when creating a FileSharingRecord
  588. HSSFWorkbook wb = openSample("ReadOnlyRecommended.xls");
  589. // Check read only advised
  590. assertEquals(3, wb.getNumberOfSheets());
  591. assertTrue(wb.isWriteProtected());
  592. // But also check that another wb isn't
  593. wb = openSample("SimpleWithChoose.xls");
  594. assertFalse(wb.isWriteProtected());
  595. }
  596. /**
  597. * Some files were having problems with the DVRecord,
  598. * probably due to dropdowns
  599. */
  600. public void test44593() {
  601. // Used to blow up with an IllegalArgumentException
  602. // when creating a DVRecord
  603. // Now won't, but no idea if this means we have
  604. // rubbish in the DVRecord or not...
  605. HSSFWorkbook wb;
  606. try {
  607. wb = openSample("44593.xls");
  608. } catch (IllegalArgumentException e) {
  609. throw new AssertionFailedError("Identified bug 44593");
  610. }
  611. assertEquals(2, wb.getNumberOfSheets());
  612. }
  613. /**
  614. * Used to give problems due to trying to read a zero
  615. * length string, but that's now properly handled
  616. */
  617. public void test44643() {
  618. // Used to blow up with an IllegalArgumentException
  619. HSSFWorkbook wb;
  620. try {
  621. wb = openSample("44643.xls");
  622. } catch (IllegalArgumentException e) {
  623. throw new AssertionFailedError("identified bug 44643");
  624. }
  625. assertEquals(1, wb.getNumberOfSheets());
  626. }
  627. /**
  628. * User reported the wrong number of rows from the
  629. * iterator, but we can't replicate that
  630. */
  631. public void test44693() {
  632. HSSFWorkbook wb = openSample("44693.xls");
  633. HSSFSheet s = wb.getSheetAt(0);
  634. // Rows are 1 to 713
  635. assertEquals(0, s.getFirstRowNum());
  636. assertEquals(712, s.getLastRowNum());
  637. assertEquals(713, s.getPhysicalNumberOfRows());
  638. // Now check the iterator
  639. int rowsSeen = 0;
  640. for(Iterator i = s.rowIterator(); i.hasNext(); ) {
  641. HSSFRow r = (HSSFRow)i.next();
  642. assertNotNull(r);
  643. rowsSeen++;
  644. }
  645. assertEquals(713, rowsSeen);
  646. }
  647. /**
  648. * Bug 28774: Excel will crash when opening xls-files with images.
  649. */
  650. public void test28774() {
  651. HSSFWorkbook wb = openSample("28774.xls");
  652. assertTrue("no errors reading sample xls", true);
  653. writeOutAndReadBack(wb);
  654. assertTrue("no errors writing sample xls", true);
  655. }
  656. /**
  657. * Had a problem apparently, not sure what as it
  658. * works just fine...
  659. */
  660. public void test44891() {
  661. HSSFWorkbook wb = openSample("44891.xls");
  662. assertTrue("no errors reading sample xls", true);
  663. writeOutAndReadBack(wb);
  664. assertTrue("no errors writing sample xls", true);
  665. }
  666. /**
  667. * Bug 44235: Ms Excel can't open save as excel file
  668. *
  669. * Works fine with poi-3.1-beta1.
  670. */
  671. public void test44235() {
  672. HSSFWorkbook wb = openSample("44235.xls");
  673. assertTrue("no errors reading sample xls", true);
  674. writeOutAndReadBack(wb);
  675. assertTrue("no errors writing sample xls", true);
  676. }
  677. public void test36947() {
  678. HSSFWorkbook wb = openSample("36947.xls");
  679. assertTrue("no errors reading sample xls", true);
  680. writeOutAndReadBack(wb);
  681. assertTrue("no errors writing sample xls", true);
  682. }
  683. public void test39634() {
  684. HSSFWorkbook wb = openSample("39634.xls");
  685. assertTrue("no errors reading sample xls", true);
  686. writeOutAndReadBack(wb);
  687. assertTrue("no errors writing sample xls", true);
  688. }
  689. /**
  690. * Problems with extracting check boxes from
  691. * HSSFObjectData
  692. * @throws Exception
  693. */
  694. public void test44840() {
  695. HSSFWorkbook wb = openSample("WithCheckBoxes.xls");
  696. // Take a look at the embedded objects
  697. List objects = wb.getAllEmbeddedObjects();
  698. assertEquals(1, objects.size());
  699. HSSFObjectData obj = (HSSFObjectData)objects.get(0);
  700. assertNotNull(obj);
  701. // Peek inside the underlying record
  702. EmbeddedObjectRefSubRecord rec = obj.findObjectRecord();
  703. assertNotNull(rec);
  704. // assertEquals(32, rec.field_1_stream_id_offset);
  705. assertEquals(0, rec.getStreamId().intValue()); // WRONG!
  706. assertEquals("Forms.CheckBox.1", rec.getOLEClassName());
  707. assertEquals(12, rec.getObjectData().length);
  708. // Doesn't have a directory
  709. assertFalse(obj.hasDirectoryEntry());
  710. assertNotNull(obj.getObjectData());
  711. assertEquals(12, obj.getObjectData().length);
  712. assertEquals("Forms.CheckBox.1", obj.getOLE2ClassName());
  713. try {
  714. obj.getDirectory();
  715. fail();
  716. } catch(FileNotFoundException e) {
  717. // expected during successful test
  718. } catch (IOException e) {
  719. throw new RuntimeException(e);
  720. }
  721. }
  722. /**
  723. * Test that we can delete sheets without
  724. * breaking the build in named ranges
  725. * used for printing stuff.
  726. */
  727. public void test30978() {
  728. HSSFWorkbook wb = openSample("30978-alt.xls");
  729. assertEquals(1, wb.getNumberOfNames());
  730. assertEquals(3, wb.getNumberOfSheets());
  731. // Check all names fit within range, and use
  732. // DeletedArea3DPtg
  733. InternalWorkbook w = wb.getWorkbook();
  734. for(int i=0; i<w.getNumNames(); i++) {
  735. NameRecord r = w.getNameRecord(i);
  736. assertTrue(r.getSheetNumber() <= wb.getNumberOfSheets());
  737. Ptg[] nd = r.getNameDefinition();
  738. assertEquals(1, nd.length);
  739. assertTrue(nd[0] instanceof DeletedArea3DPtg);
  740. }
  741. // Delete the 2nd sheet
  742. wb.removeSheetAt(1);
  743. // Re-check
  744. assertEquals(1, wb.getNumberOfNames());
  745. assertEquals(2, wb.getNumberOfSheets());
  746. for(int i=0; i<w.getNumNames(); i++) {
  747. NameRecord r = w.getNameRecord(i);
  748. assertTrue(r.getSheetNumber() <= wb.getNumberOfSheets());
  749. Ptg[] nd = r.getNameDefinition();
  750. assertEquals(1, nd.length);
  751. assertTrue(nd[0] instanceof DeletedArea3DPtg);
  752. }
  753. // Save and re-load
  754. wb = writeOutAndReadBack(wb);
  755. w = wb.getWorkbook();
  756. assertEquals(1, wb.getNumberOfNames());
  757. assertEquals(2, wb.getNumberOfSheets());
  758. for(int i=0; i<w.getNumNames(); i++) {
  759. NameRecord r = w.getNameRecord(i);
  760. assertTrue(r.getSheetNumber() <= wb.getNumberOfSheets());
  761. Ptg[] nd = r.getNameDefinition();
  762. assertEquals(1, nd.length);
  763. assertTrue(nd[0] instanceof DeletedArea3DPtg);
  764. }
  765. }
  766. /**
  767. * Test that fonts get added properly
  768. */
  769. public void test45338() {
  770. HSSFWorkbook wb = new HSSFWorkbook();
  771. assertEquals(4, wb.getNumberOfFonts());
  772. HSSFSheet s = wb.createSheet();
  773. s.createRow(0);
  774. s.createRow(1);
  775. s.getRow(0).createCell(0);
  776. s.getRow(1).createCell(0);
  777. assertEquals(4, wb.getNumberOfFonts());
  778. HSSFFont f1 = wb.getFontAt((short)0);
  779. assertEquals(400, f1.getBoldweight());
  780. // Check that asking for the same font
  781. // multiple times gives you the same thing.
  782. // Otherwise, our tests wouldn't work!
  783. assertEquals(
  784. wb.getFontAt((short)0),
  785. wb.getFontAt((short)0)
  786. );
  787. assertEquals(
  788. wb.getFontAt((short)2),
  789. wb.getFontAt((short)2)
  790. );
  791. assertTrue(
  792. wb.getFontAt((short)0)
  793. !=
  794. wb.getFontAt((short)2)
  795. );
  796. // Look for a new font we have
  797. // yet to add
  798. assertNull(
  799. wb.findFont(
  800. (short)11, (short)123, (short)22,
  801. "Thingy", false, true, (short)2, (byte)2
  802. )
  803. );
  804. HSSFFont nf = wb.createFont();
  805. assertEquals(5, wb.getNumberOfFonts());
  806. assertEquals(5, nf.getIndex());
  807. assertEquals(nf, wb.getFontAt((short)5));
  808. nf.setBoldweight((short)11);
  809. nf.setColor((short)123);
  810. nf.setFontHeight((short)22);
  811. nf.setFontName("Thingy");
  812. nf.setItalic(false);
  813. nf.setStrikeout(true);
  814. nf.setTypeOffset((short)2);
  815. nf.setUnderline((byte)2);
  816. assertEquals(5, wb.getNumberOfFonts());
  817. assertEquals(nf, wb.getFontAt((short)5));
  818. // Find it now
  819. assertNotNull(
  820. wb.findFont(
  821. (short)11, (short)123, (short)22,
  822. "Thingy", false, true, (short)2, (byte)2
  823. )
  824. );
  825. assertEquals(
  826. 5,
  827. wb.findFont(
  828. (short)11, (short)123, (short)22,
  829. "Thingy", false, true, (short)2, (byte)2
  830. ).getIndex()
  831. );
  832. assertEquals(nf,
  833. wb.findFont(
  834. (short)11, (short)123, (short)22,
  835. "Thingy", false, true, (short)2, (byte)2
  836. )
  837. );
  838. }
  839. /**
  840. * From the mailing list - ensure we can handle a formula
  841. * containing a zip code, eg ="70164"
  842. */
  843. public void testZipCodeFormulas() {
  844. HSSFWorkbook wb = new HSSFWorkbook();
  845. HSSFSheet s = wb.createSheet();
  846. s.createRow(0);
  847. HSSFCell c1 = s.getRow(0).createCell(0);
  848. HSSFCell c2 = s.getRow(0).createCell(1);
  849. HSSFCell c3 = s.getRow(0).createCell(2);
  850. // As number and string
  851. c1.setCellFormula("70164");
  852. c2.setCellFormula("\"70164\"");
  853. c3.setCellFormula("\"90210\"");
  854. // Check the formulas
  855. assertEquals("70164", c1.getCellFormula());
  856. assertEquals("\"70164\"", c2.getCellFormula());
  857. // And check the values - blank
  858. confirmCachedValue(0.0, c1);
  859. confirmCachedValue(0.0, c2);
  860. confirmCachedValue(0.0, c3);
  861. // Try changing the cached value on one of the string
  862. // formula cells, so we can see it updates properly
  863. c3.setCellValue(new HSSFRichTextString("test"));
  864. confirmCachedValue("test", c3);
  865. try {
  866. c3.getNumericCellValue();
  867. throw new AssertionFailedError("exception should have been thrown");
  868. } catch (IllegalStateException e) {
  869. assertEquals("Cannot get a numeric value from a text formula cell", e.getMessage());
  870. }
  871. // Now evaluate, they should all be changed
  872. HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
  873. eval.evaluateFormulaCell(c1);
  874. eval.evaluateFormulaCell(c2);
  875. eval.evaluateFormulaCell(c3);
  876. // Check that the cells now contain
  877. // the correct values
  878. confirmCachedValue(70164.0, c1);
  879. confirmCachedValue("70164", c2);
  880. confirmCachedValue("90210", c3);
  881. // Write and read
  882. HSSFWorkbook nwb = writeOutAndReadBack(wb);
  883. HSSFSheet ns = nwb.getSheetAt(0);
  884. HSSFCell nc1 = ns.getRow(0).getCell(0);
  885. HSSFCell nc2 = ns.getRow(0).getCell(1);
  886. HSSFCell nc3 = ns.getRow(0).getCell(2);
  887. // Re-check
  888. confirmCachedValue(70164.0, nc1);
  889. confirmCachedValue("70164", nc2);
  890. confirmCachedValue("90210", nc3);
  891. CellValueRecordInterface[] cvrs = ns.getSheet().getValueRecords();
  892. for (int i = 0; i < cvrs.length; i++) {
  893. CellValueRecordInterface cvr = cvrs[i];
  894. if(cvr instanceof FormulaRecordAggregate) {
  895. FormulaRecordAggregate fr = (FormulaRecordAggregate)cvr;
  896. if(i == 0) {
  897. assertEquals(70164.0, fr.getFormulaRecord().getValue(), 0.0001);
  898. assertNull(fr.getStringRecord());
  899. } else if (i == 1) {
  900. assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
  901. assertNotNull(fr.getStringRecord());
  902. assertEquals("70164", fr.getStringRecord().getString());
  903. } else {
  904. assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
  905. assertNotNull(fr.getStringRecord());
  906. assertEquals("90210", fr.getStringRecord().getString());
  907. }
  908. }
  909. }
  910. assertEquals(3, cvrs.length);
  911. }
  912. private static void confirmCachedValue(double expectedValue, HSSFCell cell) {
  913. assertEquals(HSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
  914. assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cell.getCachedFormulaResultType());
  915. assertEquals(expectedValue, cell.getNumericCellValue(), 0.0);
  916. }
  917. private static void confirmCachedValue(String expectedValue, HSSFCell cell) {
  918. assertEquals(HSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
  919. assertEquals(HSSFCell.CELL_TYPE_STRING, cell.getCachedFormulaResultType());
  920. assertEquals(expectedValue, cell.getRichStringCellValue().getString());
  921. }
  922. /**
  923. * Problem with "Vector Rows", eg a whole
  924. * column which is set to the result of
  925. * {=sin(B1:B9)}(9,1), so that each cell is
  926. * shown to have the contents
  927. * {=sin(B1:B9){9,1)[rownum][0]
  928. * In this sample file, the vector column
  929. * is C, and the data column is B.
  930. *
  931. * For now, blows up with an exception from ExtPtg
  932. * Expected ExpPtg to be converted from Shared to Non-Shared...
  933. */
  934. public void DISABLEDtest43623() {
  935. HSSFWorkbook wb = openSample("43623.xls");
  936. assertEquals(1, wb.getNumberOfSheets());
  937. HSSFSheet s1 = wb.getSheetAt(0);
  938. HSSFCell c1 = s1.getRow(0).getCell(2);
  939. HSSFCell c2 = s1.getRow(1).getCell(2);
  940. HSSFCell c3 = s1.getRow(2).getCell(2);
  941. // These formula contents are a guess...
  942. assertEquals("{=sin(B1:B9){9,1)[0][0]", c1.getCellFormula());
  943. assertEquals("{=sin(B1:B9){9,1)[1][0]", c2.getCellFormula());
  944. assertEquals("{=sin(B1:B9){9,1)[2][0]", c3.getCellFormula());
  945. // Save and re-open, ensure it still works
  946. HSSFWorkbook nwb = writeOutAndReadBack(wb);
  947. HSSFSheet ns1 = nwb.getSheetAt(0);
  948. HSSFCell nc1 = ns1.getRow(0).getCell(2);
  949. HSSFCell nc2 = ns1.getRow(1).getCell(2);
  950. HSSFCell nc3 = ns1.getRow(2).getCell(2);
  951. assertEquals("{=sin(B1:B9){9,1)[0][0]", nc1.getCellFormula());
  952. assertEquals("{=sin(B1:B9){9,1)[1][0]", nc2.getCellFormula());
  953. assertEquals("{=sin(B1:B9){9,1)[2][0]", nc3.getCellFormula());
  954. }
  955. /**
  956. * People are all getting confused about the last
  957. * row and cell number
  958. */
  959. public void test30635() {
  960. HSSFWorkbook wb = new HSSFWorkbook();
  961. HSSFSheet s = wb.createSheet();
  962. // No rows, everything is 0
  963. assertEquals(0, s.getFirstRowNum());
  964. assertEquals(0, s.getLastRowNum());
  965. assertEquals(0, s.getPhysicalNumberOfRows());
  966. // One row, most things are 0, physical is 1
  967. s.createRow(0);
  968. assertEquals(0, s.getFirstRowNum());
  969. assertEquals(0, s.getLastRowNum());
  970. assertEquals(1, s.getPhysicalNumberOfRows());
  971. // And another, things change
  972. s.createRow(4);
  973. assertEquals(0, s.getFirstRowNum());
  974. assertEquals(4, s.getLastRowNum());
  975. assertEquals(2, s.getPhysicalNumberOfRows());
  976. // Now start on cells
  977. HSSFRow r = s.getRow(0);
  978. assertEquals(-1, r.getFirstCellNum());
  979. assertEquals(-1, r.getLastCellNum());
  980. assertEquals(0, r.getPhysicalNumberOfCells());
  981. // Add a cell, things move off -1
  982. r.createCell(0);
  983. assertEquals(0, r.getFirstCellNum());
  984. assertEquals(1, r.getLastCellNum()); // last cell # + 1
  985. assertEquals(1, r.getPhysicalNumberOfCells());
  986. r.createCell(1);
  987. assertEquals(0, r.getFirstCellNum());
  988. assertEquals(2, r.getLastCellNum()); // last cell # + 1
  989. assertEquals(2, r.getPhysicalNumberOfCells());
  990. r.createCell(4);
  991. assertEquals(0, r.getFirstCellNum());
  992. assertEquals(5, r.getLastCellNum()); // last cell # + 1
  993. assertEquals(3, r.getPhysicalNumberOfCells());
  994. }
  995. /**
  996. * Data Tables - ptg 0x2
  997. */
  998. public void test44958() {
  999. HSSFWorkbook wb = openSample("44958.xls");
  1000. HSSFSheet s;
  1001. HSSFRow r;
  1002. HSSFCell c;
  1003. // Check the contents of the formulas
  1004. // E4 to G9 of sheet 4 make up the table
  1005. s = wb.getSheet("OneVariable Table Completed");
  1006. r = s.getRow(3);
  1007. c = r.getCell(4);
  1008. assertEquals(HSSFCell.CELL_TYPE_FORMULA, c.getCellType());
  1009. // TODO - check the formula once tables and
  1010. // arrays are properly supported
  1011. // E4 to H9 of sheet 5 make up the table
  1012. s = wb.getSheet("TwoVariable Table Example");
  1013. r = s.getRow(3);
  1014. c = r.getCell(4);
  1015. assertEquals(HSSFCell.CELL_TYPE_FORMULA, c.getCellType());
  1016. // TODO - check the formula once tables and
  1017. // arrays are properly supported
  1018. }
  1019. /**
  1020. * 45322: HSSFSheet.autoSizeColumn fails when style.getDataFormat() returns -1
  1021. */
  1022. public void test45322() {
  1023. HSSFWorkbook wb = openSample("44958.xls");
  1024. HSSFSheet sh = wb.getSheetAt(0);
  1025. for(short i=0; i < 30; i++) sh.autoSizeColumn(i);
  1026. }
  1027. /**
  1028. * We used to add too many UncalcRecords to sheets
  1029. * with diagrams on. Don't any more
  1030. */
  1031. public void test45414() {
  1032. HSSFWorkbook wb = openSample("WithThreeCharts.xls");
  1033. wb.getSheetAt(0).setForceFormulaRecalculation(true);
  1034. wb.getSheetAt(1).setForceFormulaRecalculation(false);
  1035. wb.getSheetAt(2).setForceFormulaRecalculation(true);
  1036. // Write out and back in again
  1037. // This used to break
  1038. HSSFWorkbook nwb = writeOutAndReadBack(wb);
  1039. // Check now set as it should be
  1040. assertTrue(nwb.getSheetAt(0).getForceFormulaRecalculation());
  1041. assertFalse(nwb.getSheetAt(1).getForceFormulaRecalculation());
  1042. assertTrue(nwb.getSheetAt(2).getForceFormulaRecalculation());
  1043. }
  1044. /**
  1045. * Very hidden sheets not displaying as such
  1046. */
  1047. public void test45761() {
  1048. HSSFWorkbook wb = openSample("45761.xls");
  1049. assertEquals(3, wb.getNumberOfSheets());
  1050. assertFalse(wb.isSheetHidden(0));
  1051. assertFalse(wb.isSheetVeryHidden(0));
  1052. assertTrue(wb.isSheetHidden(1));
  1053. assertFalse(wb.isSheetVeryHidden(1));
  1054. assertFalse(wb.isSheetHidden(2));
  1055. assertTrue(wb.isSheetVeryHidden(2));
  1056. // Change 0 to be very hidden, and re-load
  1057. wb.setSheetHidden(0, HSSFWorkbook.SHEET_STATE_VERY_HIDDEN);
  1058. HSSFWorkbook nwb = writeOutAndReadBack(wb);
  1059. assertFalse(nwb.isSheetHidden(0));
  1060. assertTrue(nwb.isSheetVeryHidden(0));
  1061. assertTrue(nwb.isSheetHidden(1));
  1062. assertFalse(nwb.isSheetVeryHidden(1));
  1063. assertFalse(nwb.isSheetHidden(2));
  1064. assertTrue(nwb.isSheetVeryHidden(2));
  1065. }
  1066. /**
  1067. * The resolution for bug 45777 assumed that the maximum text length in a header / footer
  1068. * record was 256 bytes. This assumption appears to be wrong. Since the fix for bug 47244,
  1069. * POI now supports header / footer text lengths beyond 256 bytes.
  1070. */
  1071. public void test45777() {
  1072. HSSFWorkbook wb = new HSSFWorkbook();
  1073. HSSFSheet s = wb.createSheet();
  1074. char[] cc248 = new char[248];
  1075. Arrays.fill(cc248, 'x');
  1076. String s248 = new String(cc248);
  1077. String s249 = s248 + "1";
  1078. String s250 = s248 + "12";
  1079. String s251 = s248 + "123";
  1080. assertEquals(248, s248.length());
  1081. assertEquals(249, s249.length());
  1082. assertEquals(250, s250.length());
  1083. assertEquals(251, s251.length());
  1084. // Try on headers
  1085. s.getHeader().setCenter(s248);
  1086. assertEquals(254, s.getHeader().getRawText().length());
  1087. writeOutAndReadBack(wb);
  1088. s.getHeader().setCenter(s251);
  1089. assertEquals(257, s.getHeader().getRawText().length());
  1090. writeOutAndReadBack(wb);
  1091. try {
  1092. s.getHeader().setCenter(s250); // 256 bytes required
  1093. } catch(IllegalArgumentException e) {
  1094. throw new AssertionFailedError("Identified bug 47244b - header can be more than 256 bytes");
  1095. }
  1096. try {
  1097. s.getHeader().setCenter(s251); // 257 bytes required
  1098. } catch(IllegalArgumentException e) {
  1099. throw new AssertionFailedError("Identified bug 47244b - header can be more than 256 bytes");
  1100. }
  1101. // Now try on footers
  1102. s.getFooter().setCenter(s248);
  1103. assertEquals(254, s.getFooter().getRawText().length());
  1104. writeOutAndReadBack(wb);
  1105. s.getFooter().setCenter(s251);
  1106. assertEquals(257, s.getFooter().getRawText().length());
  1107. writeOutAndReadBack(wb);
  1108. try {
  1109. s.getFooter().setCenter(s250); // 256 bytes required
  1110. } catch(IllegalArgumentException e) {
  1111. throw new AssertionFailedError("Identified bug 47244b - footer can be more than 256 bytes");
  1112. }
  1113. try {
  1114. s.getFooter().setCenter(s251); // 257 bytes required
  1115. } catch(IllegalArgumentException e) {
  1116. throw new AssertionFailedError("Identified bug 47244b - footer can be more than 256 bytes");
  1117. }
  1118. }
  1119. /**
  1120. * Charts with long titles
  1121. */
  1122. public void test45784() {
  1123. // This used to break
  1124. HSSFWorkbook wb = openSample("45784.xls");
  1125. assertEquals(1, wb.getNumberOfSheets());
  1126. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  1127. }
  1128. /**
  1129. * Cell background colours
  1130. */
  1131. public void test45492() {
  1132. HSSFWorkbook wb = openSample("45492.xls");
  1133. HSSFSheet s = wb.getSheetAt(0);
  1134. HSSFRow r = s.getRow(0);
  1135. HSSFPalette p = wb.getCustomPalette();
  1136. HSSFCell auto = r.getCell(0);
  1137. HSSFCell grey = r.getCell(1);
  1138. HSSFCell red = r.getCell(2);
  1139. HSSFCell blue = r.getCell(3);
  1140. HSSFCell green = r.getCell(4);
  1141. assertEquals(64, auto.getCellStyle().getFillForegroundColor());
  1142. assertEquals(64, auto.getCellStyle().getFillBackgroundColor());
  1143. assertEquals("0:0:0", p.getColor(64).getHexString());
  1144. assertEquals(22, grey.getCellStyle().getFillForegroundColor());
  1145. assertEquals(64, grey.getCellStyle().getFillBackgroundColor());
  1146. assertEquals("C0C0:C0C0:C0C0", p.getColor(22).getHexString());
  1147. assertEquals(10, red.getCellStyle().getFillForegroundColor());
  1148. assertEquals(64, red.getCellStyle().getFillBackgroundColor());
  1149. assertEquals("FFFF:0:0", p.getColor(10).getHexString());
  1150. assertEquals(12, blue.getCellStyle().getFillForegroundColor());
  1151. assertEquals(64, blue.getCellStyle().getFillBackgroundColor());
  1152. assertEquals("0:0:FFFF", p.getColor(12).getHexString());
  1153. assertEquals(11, green.getCellStyle().getFillForegroundColor());
  1154. assertEquals(64, green.getCellStyle().getFillBackgroundColor());
  1155. assertEquals("0:FFFF:0", p.getColor(11).getHexString());
  1156. }
  1157. /**
  1158. * ContinueRecord after EOF
  1159. */
  1160. public void test46137() {
  1161. // This used to break
  1162. HSSFWorkbook wb = openSample("46137.xls");
  1163. assertEquals(7, wb.getNumberOfSheets());
  1164. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  1165. }
  1166. /**
  1167. * Odd POIFS blocks issue:
  1168. * block[ 44 ] already removed from org.apache.poi.poifs.storage.BlockListImpl.remove
  1169. */
  1170. public void test45290() {
  1171. HSSFWorkbook wb = openSample("45290.xls");
  1172. assertEquals(1, wb.getNumberOfSheets());
  1173. }
  1174. /**
  1175. * In POI-2.5 user reported exception when parsing a name with a custom VBA function:
  1176. * =MY_VBA_FUNCTION("lskdjflsk")
  1177. */
  1178. public void test30070() {
  1179. HSSFWorkbook wb = openSample("30070.xls"); //contains custom VBA function 'Commission'
  1180. HSSFSheet sh = wb.getSheetAt(0);
  1181. HSSFCell cell = sh.getRow(0).getCell(1);
  1182. //B1 uses VBA in the formula
  1183. assertEquals("Commission(A1)", cell.getCellFormula());
  1184. //name sales_1 refers to Commission(Sheet0!$A$1)
  1185. int idx = wb.getNameIndex("sales_1");
  1186. assertTrue(idx != -1);
  1187. HSSFName name = wb.getNameAt(idx);
  1188. assertEquals("Commission(Sheet0!$A$1)", name.getRefersToFormula());
  1189. }
  1190. /**
  1191. * The link formulas which is referring to other books cannot be taken (the bug existed prior to POI-3.2)
  1192. * Expected:
  1193. *
  1194. * [link_sub.xls]Sheet1!$A$1
  1195. * [link_sub.xls]Sheet1!$A$2
  1196. * [link_sub.xls]Sheet1!$A$3
  1197. *
  1198. * POI-3.1 output:
  1199. *
  1200. * Sheet1!$A$1
  1201. * Sheet1!$A$2
  1202. * Sheet1!$A$3
  1203. *
  1204. */
  1205. public void test27364() {
  1206. HSSFWorkbook wb = openSample("27364.xls");
  1207. HSSFSheet sheet = wb.getSheetAt(0);
  1208. assertEquals("[link_sub.xls]Sheet1!$A$1", sheet.getRow(0).getCell(0).getCellFormula());
  1209. assertEquals("[link_sub.xls]Sheet1!$A$2", sheet.getRow(1).getCell(0).getCellFormula());
  1210. assertEquals("[link_sub.xls]Sheet1!$A$3", sheet.getRow(2).getCell(0).getCellFormula());
  1211. }
  1212. /**
  1213. * Similar to bug#27364:
  1214. * HSSFCell.getCellFormula() fails with references to external workbooks
  1215. */
  1216. public void test31661() {
  1217. HSSFWorkbook wb = openSample("31661.xls");
  1218. HSSFSheet sheet = wb.getSheetAt(0);
  1219. HSSFCell cell = sheet.getRow(11).getCell(10); //K11
  1220. assertEquals("+'[GM Budget.xls]8085.4450'!$B$2", cell.getCellFormula());
  1221. }
  1222. /**
  1223. * Incorrect handling of non-ISO 8859-1 characters in Windows ANSII Code Page 1252
  1224. */
  1225. public void test27394() {
  1226. HSSFWorkbook wb = openSample("27394.xls");
  1227. assertEquals("\u0161\u017E", wb.getSheetName(0));
  1228. assertEquals("\u0161\u017E\u010D\u0148\u0159", wb.getSheetName(1));
  1229. HSSFSheet sheet = wb.getSheetAt(0);
  1230. assertEquals("\u0161\u017E", sheet.getRow(0).getCell(0).getStringCellValue());
  1231. assertEquals("\u0161\u017E\u010D\u0148\u0159", sheet.getRow(1).getCell(0).getStringCellValue());
  1232. }
  1233. /**
  1234. * Multiple calls of HSSFWorkbook.write result in corrupted xls
  1235. */
  1236. public void test32191() throws IOException {
  1237. HSSFWorkbook wb = openSample("27394.xls");
  1238. ByteArrayOutputStream out = new ByteArrayOutputStream();
  1239. wb.write(out);
  1240. out.close();
  1241. int size1 = out.size();
  1242. out = new ByteArrayOutputStream();
  1243. wb.write(out);
  1244. out.close();
  1245. int size2 = out.size();
  1246. assertEquals(size1, size2);
  1247. out = new ByteArrayOutputStream();
  1248. wb.write(out);
  1249. out.close();
  1250. int size3 = out.size();
  1251. assertEquals(size2, size3);
  1252. }
  1253. /**
  1254. * java.io.IOException: block[ 0 ] already removed
  1255. * (is an excel 95 file though)
  1256. */
  1257. public void test46904() {
  1258. try {
  1259. openSample("46904.xls");
  1260. fail();
  1261. } catch(OldExcelFormatException e) {
  1262. assertTrue(e.getMessage().startsWith(
  1263. "The supplied spreadsheet seems to be Excel"
  1264. ));
  1265. }
  1266. }
  1267. /**
  1268. * java.lang.NegativeArraySizeException reading long
  1269. * non-unicode data for a name record
  1270. */
  1271. public void test47034() {
  1272. HSSFWorkbook wb = openSample("47034.xls");
  1273. assertEquals(893, wb.getNumberOfNames());
  1274. assertEquals("Matthew\\Matthew11_1\\Matthew2331_1\\Matthew2351_1\\Matthew2361_1___lab", wb.getNameName(300));
  1275. }
  1276. /**
  1277. * HSSFRichTextString.length() returns negative for really long strings.
  1278. * The test file was created in OpenOffice 3.0 as Excel does not allow cell text longer than 32,767 characters
  1279. */
  1280. public void test46368() {
  1281. HSSFWorkbook wb = openSample("46368.xls");
  1282. HSSFSheet s = wb.getSheetAt(0);
  1283. HSSFCell cell1 = s.getRow(0).getCell(0);
  1284. assertEquals(32770, cell1.getStringCellValue().length());
  1285. HSSFCell cell2 = s.getRow(2).getCell(0);
  1286. assertEquals(32766, cell2.getStringCellValue().length());
  1287. }
  1288. /**
  1289. * Short records on certain sheets with charts in them
  1290. */
  1291. public void test48180() {
  1292. HSSFWorkbook wb = openSample("48180.xls");
  1293. HSSFSheet s = wb.getSheetAt(0);
  1294. HSSFCell cell1 = s.getRow(0).getCell(0);
  1295. assertEquals("test ", cell1.getStringCellValue().toString());
  1296. HSSFCell cell2 = s.getRow(0).getCell(1);
  1297. assertEquals(1.0, cell2.getNumericCellValue(), 0.0);
  1298. }
  1299. /**
  1300. * POI 3.5 beta 7 can not read excel file contain list box (Form Control)
  1301. */
  1302. public void test47701() {
  1303. openSample("47701.xls");
  1304. }
  1305. public void test48026() {
  1306. openSample("48026.xls");
  1307. }
  1308. public void test47251() {
  1309. openSample("47251.xls");
  1310. }
  1311. /**
  1312. * Round trip a file with an unusual UnicodeString/ExtRst record parts
  1313. */
  1314. public void test47847() throws Exception {
  1315. HSSFWorkbook wb = openSample("47847.xls");
  1316. assertEquals(3, wb.getNumberOfSheets());
  1317. // Find the SST record
  1318. UnicodeString withExt = wb.getWorkbook().getSSTString(0);
  1319. UnicodeString withoutExt = wb.getWorkbook().getSSTString(31);
  1320. assertEquals("O:Alloc:Qty", withExt.getString());
  1321. assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004);
  1322. assertEquals("RT", withoutExt.getString());
  1323. assertTrue((withoutExt.getOptionFlags() & 0x0004) == 0x0000);
  1324. // Something about continues...
  1325. // Write out and re-read
  1326. wb = writeOutAndReadBack(wb);
  1327. assertEquals(3, wb.getNumberOfSheets());
  1328. // Check it's the same now
  1329. withExt = wb.getWorkbook().getSSTString(0);
  1330. withoutExt = wb.getWorkbook().getSSTString(31);
  1331. assertEquals("O:Alloc:Qty", withExt.getString());
  1332. assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004);
  1333. assertEquals("RT", withoutExt.getString());
  1334. assertTrue((withoutExt.getOptionFlags() & 0x0004) == 0x0000);
  1335. }
  1336. /**
  1337. * Problem with cloning a sheet with a chart
  1338. * contained in it.
  1339. */
  1340. public void test49096() throws Exception {
  1341. HSSFWorkbook wb = openSample("49096.xls");
  1342. assertEquals(1, wb.getNumberOfSheets());
  1343. assertNotNull(wb.getSheetAt(0));
  1344. wb.cloneSheet(0);
  1345. assertEquals(2, wb.getNumberOfSheets());
  1346. wb = writeOutAndReadBack(wb);
  1347. assertEquals(2, wb.getNumberOfSheets());
  1348. }
  1349. /**
  1350. * Newly created sheets need to get a
  1351. * proper TabID, otherwise print setup
  1352. * gets confused on them.
  1353. * Also ensure that print setup refs are
  1354. * by reference not value
  1355. */
  1356. public void test46664() throws Exception {
  1357. HSSFWorkbook wb = new HSSFWorkbook();
  1358. HSSFSheet sheet = wb.createSheet("new_sheet");
  1359. HSSFRow row = sheet.createRow((short)0);
  1360. row.createCell(0).setCellValue(new HSSFRichTextString("Column A"));
  1361. row.createCell(1).setCellValue(new HSSFRichTextString("Column B"));
  1362. row.createCell(2).setCellValue(new HSSFRichTextString("Column C"));
  1363. row.createCell(3).setCellValue(new HSSFRichTextString("Column D"));
  1364. row.createCell(4).setCellValue(new HSSFRichTextString("Column E"));
  1365. row.createCell(5).setCellValue(new HSSFRichTextString("Column F"));
  1366. //set print area from column a to column c (on first row)
  1367. wb.setPrintArea(
  1368. 0, //sheet index
  1369. 0, //start column
  1370. 2, //end column
  1371. 0, //start row
  1372. 0 //end row
  1373. );
  1374. wb = writeOutAndReadBack(wb);
  1375. // Ensure the tab index
  1376. TabIdRecord tr = null;
  1377. for(Record r : wb.getWorkbook().getRecords()) {
  1378. if(r instanceof TabIdRecord) {
  1379. tr = (TabIdRecord)r;
  1380. }
  1381. }
  1382. assertNotNull(tr);
  1383. assertEquals(1, tr._tabids.length);
  1384. assertEquals(0, tr._tabids[0]);
  1385. // Ensure the print setup
  1386. assertEquals("new_sheet!$A$1:$C$1", wb.getPrintArea(0));
  1387. assertEquals("new_sheet!$A$1:$C$1", wb.getName("Print_Area").getRefersToFormula());
  1388. // Needs reference not value
  1389. NameRecord nr = wb.getWorkbook().getNameRecord(
  1390. wb.getNameIndex("Print_Area")
  1391. );
  1392. assertEquals("Print_Area", nr.getNameText());
  1393. assertEquals(1, nr.getNameDefinition().length);
  1394. assertEquals(
  1395. "new_sheet!$A$1:$C$1",
  1396. ((Area3DPtg)nr.getNameDefinition()[0]).toFormulaString(HSSFEvaluationWorkbook.create(wb))
  1397. );
  1398. assertEquals('R', nr.getNameDefinition()[0].getRVAType());
  1399. }
  1400. /**
  1401. * Problems with formula references to
  1402. * sheets via URLs
  1403. */
  1404. public void test45970() throws Exception {
  1405. HSSFWorkbook wb = openSample("FormulaRefs.xls");
  1406. assertEquals(3, wb.getNumberOfSheets());
  1407. HSSFSheet s = wb.getSheetAt(0);
  1408. HSSFRow row;
  1409. row = s.getRow(0);
  1410. assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
  1411. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1412. row = s.getRow(1);
  1413. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1414. assertEquals("B1", row.getCell(1).getCellFormula());
  1415. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1416. row = s.getRow(2);
  1417. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1418. assertEquals("Sheet1!B1", row.getCell(1).getCellFormula());
  1419. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1420. row = s.getRow(3);
  1421. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1422. assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
  1423. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1424. row = s.getRow(4);
  1425. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1426. assertEquals("'[\u0005$http://gagravarr.org/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
  1427. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1428. // Change 4
  1429. row.getCell(1).setCellFormula("'[\u0005$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2");
  1430. row.getCell(1).setCellValue(123.0);
  1431. // Add 5
  1432. row = s.createRow(5);
  1433. row.createCell(1, Cell.CELL_TYPE_FORMULA);
  1434. row.getCell(1).setCellFormula("'[\u0005$http://example.com/FormulaRefs.xls]Sheet1'!B1");
  1435. row.getCell(1).setCellValue(234.0);
  1436. // Re-test
  1437. wb = writeOutAndReadBack(wb);
  1438. s = wb.getSheetAt(0);
  1439. row = s.getRow(0);
  1440. assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
  1441. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1442. row = s.getRow(1);
  1443. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1444. assertEquals("B1", row.getCell(1).getCellFormula());
  1445. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1446. row = s.getRow(2);
  1447. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1448. assertEquals("Sheet1!B1", row.getCell(1).getCellFormula());
  1449. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1450. row = s.getRow(3);
  1451. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1452. assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
  1453. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1454. // TODO - Fix these so they work...
  1455. if(1==2) {
  1456. row = s.getRow(4);
  1457. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1458. assertEquals("'[\u0005$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2", row.getCell(1).getCellFormula());
  1459. assertEquals(123.0, row.getCell(1).getNumericCellValue());
  1460. row = s.getRow(5);
  1461. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1462. assertEquals("'[\u0005$http://example.com/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
  1463. assertEquals(234.0, row.getCell(1).getNumericCellValue());
  1464. }
  1465. }
  1466. /**
  1467. * Test for a file with NameRecord with NameCommentRecord comments
  1468. */
  1469. public void test49185() throws Exception {
  1470. HSSFWorkbook wb = openSample("49185.xls");
  1471. Name name = wb.getName("foobarName");
  1472. assertEquals("This is a comment", name.getComment());
  1473. // Rename the name, comment comes with it
  1474. name.setNameName("ChangedName");
  1475. assertEquals("This is a comment", name.getComment());
  1476. // Save and re-check
  1477. wb = writeOutAndReadBack(wb);
  1478. name = wb.getName("ChangedName");
  1479. assertEquals("This is a comment", name.getComment());
  1480. // Now try to change it
  1481. name.setComment("Changed Comment");
  1482. assertEquals("Changed Comment", name.getComment());
  1483. // Save and re-check
  1484. wb = writeOutAndReadBack(wb);
  1485. name = wb.getName("ChangedName");
  1486. assertEquals("Changed Comment", name.getComment());
  1487. }
  1488. /**
  1489. * Vertically aligned text
  1490. */
  1491. public void test49524() throws Exception {
  1492. HSSFWorkbook wb = openSample("49524.xls");
  1493. Sheet s = wb.getSheetAt(0);
  1494. Row r = s.getRow(0);
  1495. Cell rotated = r.getCell(0);
  1496. Cell normal = r.getCell(1);
  1497. // Check the current ones
  1498. assertEquals(0, normal.getCellStyle().getRotation());
  1499. assertEquals(0xff, rotated.getCellStyle().getRotation());
  1500. // Add a new style, also rotated
  1501. CellStyle cs = wb.createCellStyle();
  1502. cs.setRotation((short)0xff);
  1503. Cell nc = r.createCell(2);
  1504. nc.setCellValue("New Rotated Text");
  1505. nc.setCellStyle(cs);
  1506. assertEquals(0xff, nc.getCellStyle().getRotation());
  1507. // Write out and read back
  1508. wb = writeOutAndReadBack(wb);
  1509. // Re-check
  1510. s = wb.getSheetAt(0);
  1511. r = s.getRow(0);
  1512. rotated = r.getCell(0);
  1513. normal = r.getCell(1);
  1514. nc = r.getCell(2);
  1515. assertEquals(0, normal.getCellStyle().getRotation());
  1516. assertEquals(0xff, rotated.getCellStyle().getRotation());
  1517. assertEquals(0xff, nc.getCellStyle().getRotation());
  1518. }
  1519. /**
  1520. * Setting the user style name on custom styles
  1521. */
  1522. public void test49689() throws Exception {
  1523. HSSFWorkbook wb = new HSSFWorkbook();
  1524. HSSFSheet s = wb.createSheet("Test");
  1525. HSSFRow r = s.createRow(0);
  1526. HSSFCell c = r.createCell(0);
  1527. HSSFCellStyle cs1 = wb.createCellStyle();
  1528. HSSFCellStyle cs2 = wb.createCellStyle();
  1529. HSSFCellStyle cs3 = wb.createCellStyle();
  1530. assertEquals(21, cs1.getIndex());
  1531. cs1.setUserStyleName("Testing");
  1532. assertEquals(22, cs2.getIndex());
  1533. cs2.setUserStyleName("Testing 2");
  1534. assertEquals(23, cs3.getIndex());
  1535. cs3.setUserStyleName("Testing 3");
  1536. // Set one
  1537. c.setCellStyle(cs1);
  1538. // Write out and read back
  1539. wb = writeOutAndReadBack(wb);
  1540. // Re-check
  1541. assertEquals("Testing", wb.getCellStyleAt((short)21).getUserStyleName());
  1542. assertEquals("Testing 2", wb.getCellStyleAt((short)22).getUserStyleName());
  1543. assertEquals("Testing 3", wb.getCellStyleAt((short)23).getUserStyleName());
  1544. }
  1545. public void test49751() {
  1546. HSSFWorkbook wb = openSample("49751.xls");
  1547. short numCellStyles = wb.getNumCellStyles();
  1548. List<String> namedStyles = Arrays.asList(
  1549. "20% - Accent1", "20% - Accent2", "20% - Accent3", "20% - Accent4", "20% - Accent5",
  1550. "20% - Accent6", "40% - Accent1", "40% - Accent2", "40% - Accent3", "40% - Accent4",
  1551. "40% - Accent5", "40% - Accent6", "60% - Accent1", "60% - Accent2", "60% - Accent3",
  1552. "60% - Accent4", "60% - Accent5", "60% - Accent6", "Accent1", "Accent2", "Accent3",
  1553. "Accent4", "Accent5", "Accent6", "Bad", "Calculation", "Check Cell", "Explanatory Text",
  1554. "Good", "Heading 1", "Heading 2", "Heading 3", "Heading 4", "Input", "Linked Cell",
  1555. "Neutral", "Note", "Output", "Title", "Total", "Warning Text");
  1556. List<String> collecteddStyles = new ArrayList<String>();
  1557. for (short i = 0; i < numCellStyles; i++) {
  1558. HSSFCellStyle cellStyle = wb.getCellStyleAt(i);
  1559. String styleName = cellStyle.getUserStyleName();
  1560. if (styleName != null) {
  1561. collecteddStyles.add(styleName);
  1562. }
  1563. }
  1564. assertTrue(namedStyles.containsAll(collecteddStyles));
  1565. }
  1566. /**
  1567. * Regression with the PageSettingsBlock
  1568. */
  1569. public void test49931() throws Exception {
  1570. HSSFWorkbook wb = openSample("49931.xls");
  1571. assertEquals(1, wb.getNumberOfSheets());
  1572. assertEquals("Foo", wb.getSheetAt(0).getRow(0).getCell(0).getRichStringCellValue().toString());
  1573. }
  1574. /**
  1575. * Missing left/right/centre options on a footer
  1576. */
  1577. public void test48325() throws Exception {
  1578. HSSFWorkbook wb = openSample("48325.xls");
  1579. HSSFSheet sh = wb.getSheetAt(0);
  1580. HSSFFooter f = sh.getFooter();
  1581. // Will show as the centre, as that is what excel does
  1582. // with an invalid footer lacking left/right/centre details
  1583. assertEquals("Left text should be empty", "", f.getLeft());
  1584. assertEquals("Right text should be empty", "", f.getRight());
  1585. assertEquals(
  1586. "Center text should contain the illegal value",
  1587. "BlahBlah blah blah ", f.getCenter()
  1588. );
  1589. }
  1590. /**
  1591. * IllegalStateException received when creating Data validation in sheet with macro
  1592. */
  1593. public void test50020() throws Exception {
  1594. HSSFWorkbook wb = openSample("50020.xls");
  1595. writeOutAndReadBack(wb);
  1596. }
  1597. public void test50426() throws Exception {
  1598. HSSFWorkbook wb = openSample("50426.xls");
  1599. writeOutAndReadBack(wb);
  1600. }
  1601. /**
  1602. * Last row number when shifting rows
  1603. */
  1604. public void test50416LastRowNumber() {
  1605. // Create the workbook with 1 sheet which contains 3 rows
  1606. HSSFWorkbook workbook = new HSSFWorkbook();
  1607. Sheet sheet = workbook.createSheet("Bug50416");
  1608. Row row1 = sheet.createRow(0);
  1609. Cell cellA_1 = row1.createCell(0,Cell.CELL_TYPE_STRING);
  1610. cellA_1.setCellValue("Cell A,1");
  1611. Row row2 = sheet.createRow(1);
  1612. Cell cellA_2 = row2.createCell(0,Cell.CELL_TYPE_STRING);
  1613. cellA_2.setCellValue("Cell A,2");
  1614. Row row3 = sheet.createRow(2);
  1615. Cell cellA_3 = row3.createCell(0,Cell.CELL_TYPE_STRING);
  1616. cellA_3.setCellValue("Cell A,3");
  1617. // Test the last Row number it currently correct
  1618. assertEquals(2, sheet.getLastRowNum());
  1619. // Shift the first row to the end
  1620. sheet.shiftRows(0, 0, 3);
  1621. assertEquals(3, sheet.getLastRowNum());
  1622. assertEquals(-1, sheet.getRow(0).getLastCellNum());
  1623. assertEquals("Cell A,2", sheet.getRow(1).getCell(0).getStringCellValue());
  1624. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1625. assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
  1626. // Shift the 2nd row up to the first one
  1627. sheet.shiftRows(1, 1, -1);
  1628. assertEquals(3, sheet.getLastRowNum());
  1629. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1630. assertEquals(-1, sheet.getRow(1).getLastCellNum());
  1631. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1632. assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
  1633. // Shift the 4th row up into the gap in the 3rd row
  1634. sheet.shiftRows(3, 3, -2);
  1635. assertEquals(2, sheet.getLastRowNum());
  1636. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1637. assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
  1638. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1639. assertEquals(-1, sheet.getRow(3).getLastCellNum());
  1640. // Now zap the empty 4th row - won't do anything
  1641. sheet.removeRow(sheet.getRow(3));
  1642. // Test again the last row number which should be 2
  1643. assertEquals(2, sheet.getLastRowNum());
  1644. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1645. assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
  1646. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1647. }
  1648. /**
  1649. * If you send a file between Excel and OpenOffice enough, something
  1650. * will turn the "General" format into "GENERAL"
  1651. */
  1652. public void test50756() throws Exception {
  1653. HSSFWorkbook wb = openSample("50756.xls");
  1654. HSSFSheet s = wb.getSheetAt(0);
  1655. HSSFRow r17 = s.getRow(16);
  1656. HSSFRow r18 = s.getRow(17);
  1657. HSSFDataFormatter df = new HSSFDataFormatter();
  1658. assertEquals(10.0, r17.getCell(1).getNumericCellValue());
  1659. assertEquals(20.0, r17.getCell(2).getNumericCellValue());
  1660. assertEquals(20.0, r17.getCell(3).getNumericCellValue());
  1661. assertEquals("GENERAL", r17.getCell(1).getCellStyle().getDataFormatString());
  1662. assertEquals("GENERAL", r17.getCell(2).getCellStyle().getDataFormatString());
  1663. assertEquals("GENERAL", r17.getCell(3).getCellStyle().getDataFormatString());
  1664. assertEquals("10", df.formatCellValue(r17.getCell(1)));
  1665. assertEquals("20", df.formatCellValue(r17.getCell(2)));
  1666. assertEquals("20", df.formatCellValue(r17.getCell(3)));
  1667. assertEquals(16.0, r18.getCell(1).getNumericCellValue());
  1668. assertEquals(35.0, r18.getCell(2).getNumericCellValue());
  1669. assertEquals(123.0, r18.getCell(3).getNumericCellValue());
  1670. assertEquals("GENERAL", r18.getCell(1).getCellStyle().getDataFormatString());
  1671. assertEquals("GENERAL", r18.getCell(2).getCellStyle().getDataFormatString());
  1672. assertEquals("GENERAL", r18.getCell(3).getCellStyle().getDataFormatString());
  1673. assertEquals("16", df.formatCellValue(r18.getCell(1)));
  1674. assertEquals("35", df.formatCellValue(r18.getCell(2)));
  1675. assertEquals("123", df.formatCellValue(r18.getCell(3)));
  1676. }
  1677. /**
  1678. * A protected sheet with comments, when written out by
  1679. * POI, ends up upsetting excel.
  1680. * TODO Identify the cause and add extra asserts for
  1681. * the bit excel cares about
  1682. */
  1683. public void test50833() throws Exception {
  1684. HSSFWorkbook wb = openSample("50833.xls");
  1685. HSSFSheet s = wb.getSheetAt(0);
  1686. assertEquals("Sheet1", s.getSheetName());
  1687. assertEquals(false, s.getProtect());
  1688. HSSFCell c = s.getRow(0).getCell(0);
  1689. assertEquals("test cell value", c.getRichStringCellValue().getString());
  1690. HSSFComment cmt = c.getCellComment();
  1691. assertNotNull(cmt);
  1692. assertEquals("Robert Lawrence", cmt.getAuthor());
  1693. assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
  1694. // Reload
  1695. wb = writeOutAndReadBack(wb);
  1696. s = wb.getSheetAt(0);
  1697. c = s.getRow(0).getCell(0);
  1698. // Re-check the comment
  1699. cmt = c.getCellComment();
  1700. assertNotNull(cmt);
  1701. assertEquals("Robert Lawrence", cmt.getAuthor());
  1702. assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
  1703. // TODO Identify what excel doesn't like, and check for that
  1704. }
  1705. public void test50779() throws Exception {
  1706. HSSFWorkbook wb1 = openSample("50779_1.xls");
  1707. writeOutAndReadBack(wb1);
  1708. HSSFWorkbook wb2 = openSample("50779_2.xls");
  1709. writeOutAndReadBack(wb2);
  1710. }
  1711. /**
  1712. * The spec says that ChartEndObjectRecord has 6 reserved
  1713. * bytes on the end, but we sometimes find files without...
  1714. */
  1715. public void test50939() throws Exception {
  1716. HSSFWorkbook wb = openSample("50939.xls");
  1717. assertEquals(2, wb.getNumberOfSheets());
  1718. }
  1719. }