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.

TestBugs.java 58KB

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