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

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