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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316
  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 junit.framework.AssertionFailedError;
  17. import org.apache.poi.EncryptedDocumentException;
  18. import org.apache.poi.hssf.HSSFITestDataProvider;
  19. import org.apache.poi.hssf.HSSFTestDataSamples;
  20. import org.apache.poi.hssf.OldExcelFormatException;
  21. import org.apache.poi.hssf.extractor.ExcelExtractor;
  22. import org.apache.poi.hssf.model.InternalSheet;
  23. import org.apache.poi.hssf.model.InternalWorkbook;
  24. import org.apache.poi.hssf.record.*;
  25. import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
  26. import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
  27. import org.apache.poi.hssf.record.aggregates.RecordAggregate;
  28. import org.apache.poi.hssf.record.common.UnicodeString;
  29. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  30. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  31. import org.apache.poi.ss.formula.ptg.Area3DPtg;
  32. import org.apache.poi.ss.formula.ptg.DeletedArea3DPtg;
  33. import org.apache.poi.ss.formula.ptg.Ptg;
  34. import org.apache.poi.ss.usermodel.*;
  35. import org.apache.poi.util.TempFile;
  36. import java.io.*;
  37. import java.util.*;
  38. /**
  39. * Testcases for bugs entered in bugzilla
  40. * the Test name contains the bugzilla bug id
  41. *
  42. * <b>YK: If a bug can be tested in terms of common ss interfaces,
  43. * define the test in the base class {@link BaseTestBugzillaIssues}</b>
  44. *
  45. * @author Avik Sengupta
  46. * @author Yegor Kozlov
  47. */
  48. public final class TestBugs extends BaseTestBugzillaIssues {
  49. public TestBugs() {
  50. super(HSSFITestDataProvider.instance);
  51. }
  52. private static HSSFWorkbook openSample(String sampleFileName) {
  53. return HSSFITestDataProvider.instance.openSampleWorkbook(sampleFileName);
  54. }
  55. private static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {
  56. return HSSFITestDataProvider.instance.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. InternalWorkbook 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. @SuppressWarnings("deprecation")
  883. CellValueRecordInterface[] cvrs = ns.getSheet().getValueRecords();
  884. for (int i = 0; i < cvrs.length; i++) {
  885. CellValueRecordInterface cvr = cvrs[i];
  886. if(cvr instanceof FormulaRecordAggregate) {
  887. FormulaRecordAggregate fr = (FormulaRecordAggregate)cvr;
  888. if(i == 0) {
  889. assertEquals(70164.0, fr.getFormulaRecord().getValue(), 0.0001);
  890. assertNull(fr.getStringRecord());
  891. } else if (i == 1) {
  892. assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
  893. assertNotNull(fr.getStringRecord());
  894. assertEquals("70164", fr.getStringRecord().getString());
  895. } else {
  896. assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
  897. assertNotNull(fr.getStringRecord());
  898. assertEquals("90210", fr.getStringRecord().getString());
  899. }
  900. }
  901. }
  902. assertEquals(3, cvrs.length);
  903. }
  904. private static void confirmCachedValue(double expectedValue, HSSFCell cell) {
  905. assertEquals(HSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
  906. assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cell.getCachedFormulaResultType());
  907. assertEquals(expectedValue, cell.getNumericCellValue(), 0.0);
  908. }
  909. private static void confirmCachedValue(String expectedValue, HSSFCell cell) {
  910. assertEquals(HSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
  911. assertEquals(HSSFCell.CELL_TYPE_STRING, cell.getCachedFormulaResultType());
  912. assertEquals(expectedValue, cell.getRichStringCellValue().getString());
  913. }
  914. /**
  915. * Problem with "Vector Rows", eg a whole
  916. * column which is set to the result of
  917. * {=sin(B1:B9)}(9,1), so that each cell is
  918. * shown to have the contents
  919. * {=sin(B1:B9){9,1)[rownum][0]
  920. * In this sample file, the vector column
  921. * is C, and the data column is B.
  922. *
  923. * For now, blows up with an exception from ExtPtg
  924. * Expected ExpPtg to be converted from Shared to Non-Shared...
  925. */
  926. public void DISABLEDtest43623() {
  927. HSSFWorkbook wb = openSample("43623.xls");
  928. assertEquals(1, wb.getNumberOfSheets());
  929. HSSFSheet s1 = wb.getSheetAt(0);
  930. HSSFCell c1 = s1.getRow(0).getCell(2);
  931. HSSFCell c2 = s1.getRow(1).getCell(2);
  932. HSSFCell c3 = s1.getRow(2).getCell(2);
  933. // These formula contents are a guess...
  934. assertEquals("{=sin(B1:B9){9,1)[0][0]", c1.getCellFormula());
  935. assertEquals("{=sin(B1:B9){9,1)[1][0]", c2.getCellFormula());
  936. assertEquals("{=sin(B1:B9){9,1)[2][0]", c3.getCellFormula());
  937. // Save and re-open, ensure it still works
  938. HSSFWorkbook nwb = writeOutAndReadBack(wb);
  939. HSSFSheet ns1 = nwb.getSheetAt(0);
  940. HSSFCell nc1 = ns1.getRow(0).getCell(2);
  941. HSSFCell nc2 = ns1.getRow(1).getCell(2);
  942. HSSFCell nc3 = ns1.getRow(2).getCell(2);
  943. assertEquals("{=sin(B1:B9){9,1)[0][0]", nc1.getCellFormula());
  944. assertEquals("{=sin(B1:B9){9,1)[1][0]", nc2.getCellFormula());
  945. assertEquals("{=sin(B1:B9){9,1)[2][0]", nc3.getCellFormula());
  946. }
  947. /**
  948. * People are all getting confused about the last
  949. * row and cell number
  950. */
  951. public void test30635() {
  952. HSSFWorkbook wb = new HSSFWorkbook();
  953. HSSFSheet s = wb.createSheet();
  954. // No rows, everything is 0
  955. assertEquals(0, s.getFirstRowNum());
  956. assertEquals(0, s.getLastRowNum());
  957. assertEquals(0, s.getPhysicalNumberOfRows());
  958. // One row, most things are 0, physical is 1
  959. s.createRow(0);
  960. assertEquals(0, s.getFirstRowNum());
  961. assertEquals(0, s.getLastRowNum());
  962. assertEquals(1, s.getPhysicalNumberOfRows());
  963. // And another, things change
  964. s.createRow(4);
  965. assertEquals(0, s.getFirstRowNum());
  966. assertEquals(4, s.getLastRowNum());
  967. assertEquals(2, s.getPhysicalNumberOfRows());
  968. // Now start on cells
  969. HSSFRow r = s.getRow(0);
  970. assertEquals(-1, r.getFirstCellNum());
  971. assertEquals(-1, r.getLastCellNum());
  972. assertEquals(0, r.getPhysicalNumberOfCells());
  973. // Add a cell, things move off -1
  974. r.createCell(0);
  975. assertEquals(0, r.getFirstCellNum());
  976. assertEquals(1, r.getLastCellNum()); // last cell # + 1
  977. assertEquals(1, r.getPhysicalNumberOfCells());
  978. r.createCell(1);
  979. assertEquals(0, r.getFirstCellNum());
  980. assertEquals(2, r.getLastCellNum()); // last cell # + 1
  981. assertEquals(2, r.getPhysicalNumberOfCells());
  982. r.createCell(4);
  983. assertEquals(0, r.getFirstCellNum());
  984. assertEquals(5, r.getLastCellNum()); // last cell # + 1
  985. assertEquals(3, r.getPhysicalNumberOfCells());
  986. }
  987. /**
  988. * Data Tables - ptg 0x2
  989. */
  990. public void test44958() {
  991. HSSFWorkbook wb = openSample("44958.xls");
  992. HSSFSheet s;
  993. HSSFRow r;
  994. HSSFCell c;
  995. // Check the contents of the formulas
  996. // E4 to G9 of sheet 4 make up the table
  997. s = wb.getSheet("OneVariable Table Completed");
  998. r = s.getRow(3);
  999. c = r.getCell(4);
  1000. assertEquals(HSSFCell.CELL_TYPE_FORMULA, c.getCellType());
  1001. // TODO - check the formula once tables and
  1002. // arrays are properly supported
  1003. // E4 to H9 of sheet 5 make up the table
  1004. s = wb.getSheet("TwoVariable Table Example");
  1005. r = s.getRow(3);
  1006. c = r.getCell(4);
  1007. assertEquals(HSSFCell.CELL_TYPE_FORMULA, c.getCellType());
  1008. // TODO - check the formula once tables and
  1009. // arrays are properly supported
  1010. }
  1011. /**
  1012. * 45322: HSSFSheet.autoSizeColumn fails when style.getDataFormat() returns -1
  1013. */
  1014. public void test45322() {
  1015. HSSFWorkbook wb = openSample("44958.xls");
  1016. HSSFSheet sh = wb.getSheetAt(0);
  1017. for(short i=0; i < 30; i++) sh.autoSizeColumn(i);
  1018. }
  1019. /**
  1020. * We used to add too many UncalcRecords to sheets
  1021. * with diagrams on. Don't any more
  1022. */
  1023. public void test45414() {
  1024. HSSFWorkbook wb = openSample("WithThreeCharts.xls");
  1025. wb.getSheetAt(0).setForceFormulaRecalculation(true);
  1026. wb.getSheetAt(1).setForceFormulaRecalculation(false);
  1027. wb.getSheetAt(2).setForceFormulaRecalculation(true);
  1028. // Write out and back in again
  1029. // This used to break
  1030. HSSFWorkbook nwb = writeOutAndReadBack(wb);
  1031. // Check now set as it should be
  1032. assertTrue(nwb.getSheetAt(0).getForceFormulaRecalculation());
  1033. assertFalse(nwb.getSheetAt(1).getForceFormulaRecalculation());
  1034. assertTrue(nwb.getSheetAt(2).getForceFormulaRecalculation());
  1035. }
  1036. /**
  1037. * Very hidden sheets not displaying as such
  1038. */
  1039. public void test45761() {
  1040. HSSFWorkbook wb = openSample("45761.xls");
  1041. assertEquals(3, wb.getNumberOfSheets());
  1042. assertFalse(wb.isSheetHidden(0));
  1043. assertFalse(wb.isSheetVeryHidden(0));
  1044. assertTrue(wb.isSheetHidden(1));
  1045. assertFalse(wb.isSheetVeryHidden(1));
  1046. assertFalse(wb.isSheetHidden(2));
  1047. assertTrue(wb.isSheetVeryHidden(2));
  1048. // Change 0 to be very hidden, and re-load
  1049. wb.setSheetHidden(0, HSSFWorkbook.SHEET_STATE_VERY_HIDDEN);
  1050. HSSFWorkbook nwb = writeOutAndReadBack(wb);
  1051. assertFalse(nwb.isSheetHidden(0));
  1052. assertTrue(nwb.isSheetVeryHidden(0));
  1053. assertTrue(nwb.isSheetHidden(1));
  1054. assertFalse(nwb.isSheetVeryHidden(1));
  1055. assertFalse(nwb.isSheetHidden(2));
  1056. assertTrue(nwb.isSheetVeryHidden(2));
  1057. }
  1058. /**
  1059. * The resolution for bug 45777 assumed that the maximum text length in a header / footer
  1060. * record was 256 bytes. This assumption appears to be wrong. Since the fix for bug 47244,
  1061. * POI now supports header / footer text lengths beyond 256 bytes.
  1062. */
  1063. public void test45777() {
  1064. HSSFWorkbook wb = new HSSFWorkbook();
  1065. HSSFSheet s = wb.createSheet();
  1066. char[] cc248 = new char[248];
  1067. Arrays.fill(cc248, 'x');
  1068. String s248 = new String(cc248);
  1069. String s249 = s248 + "1";
  1070. String s250 = s248 + "12";
  1071. String s251 = s248 + "123";
  1072. assertEquals(248, s248.length());
  1073. assertEquals(249, s249.length());
  1074. assertEquals(250, s250.length());
  1075. assertEquals(251, s251.length());
  1076. // Try on headers
  1077. s.getHeader().setCenter(s248);
  1078. assertEquals(254, s.getHeader().getRawText().length());
  1079. writeOutAndReadBack(wb);
  1080. s.getHeader().setCenter(s251);
  1081. assertEquals(257, s.getHeader().getRawText().length());
  1082. writeOutAndReadBack(wb);
  1083. try {
  1084. s.getHeader().setCenter(s250); // 256 bytes required
  1085. } catch(IllegalArgumentException e) {
  1086. throw new AssertionFailedError("Identified bug 47244b - header can be more than 256 bytes");
  1087. }
  1088. try {
  1089. s.getHeader().setCenter(s251); // 257 bytes required
  1090. } catch(IllegalArgumentException e) {
  1091. throw new AssertionFailedError("Identified bug 47244b - header can be more than 256 bytes");
  1092. }
  1093. // Now try on footers
  1094. s.getFooter().setCenter(s248);
  1095. assertEquals(254, s.getFooter().getRawText().length());
  1096. writeOutAndReadBack(wb);
  1097. s.getFooter().setCenter(s251);
  1098. assertEquals(257, s.getFooter().getRawText().length());
  1099. writeOutAndReadBack(wb);
  1100. try {
  1101. s.getFooter().setCenter(s250); // 256 bytes required
  1102. } catch(IllegalArgumentException e) {
  1103. throw new AssertionFailedError("Identified bug 47244b - footer can be more than 256 bytes");
  1104. }
  1105. try {
  1106. s.getFooter().setCenter(s251); // 257 bytes required
  1107. } catch(IllegalArgumentException e) {
  1108. throw new AssertionFailedError("Identified bug 47244b - footer can be more than 256 bytes");
  1109. }
  1110. }
  1111. /**
  1112. * Charts with long titles
  1113. */
  1114. public void test45784() {
  1115. // This used to break
  1116. HSSFWorkbook wb = openSample("45784.xls");
  1117. assertEquals(1, wb.getNumberOfSheets());
  1118. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  1119. }
  1120. /**
  1121. * Cell background colours
  1122. */
  1123. public void test45492() {
  1124. HSSFWorkbook wb = openSample("45492.xls");
  1125. HSSFSheet s = wb.getSheetAt(0);
  1126. HSSFRow r = s.getRow(0);
  1127. HSSFPalette p = wb.getCustomPalette();
  1128. HSSFCell auto = r.getCell(0);
  1129. HSSFCell grey = r.getCell(1);
  1130. HSSFCell red = r.getCell(2);
  1131. HSSFCell blue = r.getCell(3);
  1132. HSSFCell green = r.getCell(4);
  1133. assertEquals(64, auto.getCellStyle().getFillForegroundColor());
  1134. assertEquals(64, auto.getCellStyle().getFillBackgroundColor());
  1135. assertEquals("0:0:0", p.getColor(64).getHexString());
  1136. assertEquals(22, grey.getCellStyle().getFillForegroundColor());
  1137. assertEquals(64, grey.getCellStyle().getFillBackgroundColor());
  1138. assertEquals("C0C0:C0C0:C0C0", p.getColor(22).getHexString());
  1139. assertEquals(10, red.getCellStyle().getFillForegroundColor());
  1140. assertEquals(64, red.getCellStyle().getFillBackgroundColor());
  1141. assertEquals("FFFF:0:0", p.getColor(10).getHexString());
  1142. assertEquals(12, blue.getCellStyle().getFillForegroundColor());
  1143. assertEquals(64, blue.getCellStyle().getFillBackgroundColor());
  1144. assertEquals("0:0:FFFF", p.getColor(12).getHexString());
  1145. assertEquals(11, green.getCellStyle().getFillForegroundColor());
  1146. assertEquals(64, green.getCellStyle().getFillBackgroundColor());
  1147. assertEquals("0:FFFF:0", p.getColor(11).getHexString());
  1148. }
  1149. /**
  1150. * ContinueRecord after EOF
  1151. */
  1152. public void test46137() {
  1153. // This used to break
  1154. HSSFWorkbook wb = openSample("46137.xls");
  1155. assertEquals(7, wb.getNumberOfSheets());
  1156. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  1157. }
  1158. /**
  1159. * Odd POIFS blocks issue:
  1160. * block[ 44 ] already removed from org.apache.poi.poifs.storage.BlockListImpl.remove
  1161. */
  1162. public void test45290() {
  1163. HSSFWorkbook wb = openSample("45290.xls");
  1164. assertEquals(1, wb.getNumberOfSheets());
  1165. }
  1166. /**
  1167. * In POI-2.5 user reported exception when parsing a name with a custom VBA function:
  1168. * =MY_VBA_FUNCTION("lskdjflsk")
  1169. */
  1170. public void test30070() {
  1171. HSSFWorkbook wb = openSample("30070.xls"); //contains custom VBA function 'Commission'
  1172. HSSFSheet sh = wb.getSheetAt(0);
  1173. HSSFCell cell = sh.getRow(0).getCell(1);
  1174. //B1 uses VBA in the formula
  1175. assertEquals("Commission(A1)", cell.getCellFormula());
  1176. //name sales_1 refers to Commission(Sheet0!$A$1)
  1177. int idx = wb.getNameIndex("sales_1");
  1178. assertTrue(idx != -1);
  1179. HSSFName name = wb.getNameAt(idx);
  1180. assertEquals("Commission(Sheet0!$A$1)", name.getRefersToFormula());
  1181. }
  1182. /**
  1183. * The link formulas which is referring to other books cannot be taken (the bug existed prior to POI-3.2)
  1184. * Expected:
  1185. *
  1186. * [link_sub.xls]Sheet1!$A$1
  1187. * [link_sub.xls]Sheet1!$A$2
  1188. * [link_sub.xls]Sheet1!$A$3
  1189. *
  1190. * POI-3.1 output:
  1191. *
  1192. * Sheet1!$A$1
  1193. * Sheet1!$A$2
  1194. * Sheet1!$A$3
  1195. *
  1196. */
  1197. public void test27364() {
  1198. HSSFWorkbook wb = openSample("27364.xls");
  1199. HSSFSheet sheet = wb.getSheetAt(0);
  1200. assertEquals("[link_sub.xls]Sheet1!$A$1", sheet.getRow(0).getCell(0).getCellFormula());
  1201. assertEquals("[link_sub.xls]Sheet1!$A$2", sheet.getRow(1).getCell(0).getCellFormula());
  1202. assertEquals("[link_sub.xls]Sheet1!$A$3", sheet.getRow(2).getCell(0).getCellFormula());
  1203. }
  1204. /**
  1205. * Similar to bug#27364:
  1206. * HSSFCell.getCellFormula() fails with references to external workbooks
  1207. */
  1208. public void test31661() {
  1209. HSSFWorkbook wb = openSample("31661.xls");
  1210. HSSFSheet sheet = wb.getSheetAt(0);
  1211. HSSFCell cell = sheet.getRow(11).getCell(10); //K11
  1212. assertEquals("+'[GM Budget.xls]8085.4450'!$B$2", cell.getCellFormula());
  1213. }
  1214. /**
  1215. * Incorrect handling of non-ISO 8859-1 characters in Windows ANSII Code Page 1252
  1216. */
  1217. public void test27394() {
  1218. HSSFWorkbook wb = openSample("27394.xls");
  1219. assertEquals("\u0161\u017E", wb.getSheetName(0));
  1220. assertEquals("\u0161\u017E\u010D\u0148\u0159", wb.getSheetName(1));
  1221. HSSFSheet sheet = wb.getSheetAt(0);
  1222. assertEquals("\u0161\u017E", sheet.getRow(0).getCell(0).getStringCellValue());
  1223. assertEquals("\u0161\u017E\u010D\u0148\u0159", sheet.getRow(1).getCell(0).getStringCellValue());
  1224. }
  1225. /**
  1226. * Multiple calls of HSSFWorkbook.write result in corrupted xls
  1227. */
  1228. public void test32191() throws IOException {
  1229. HSSFWorkbook wb = openSample("27394.xls");
  1230. ByteArrayOutputStream out = new ByteArrayOutputStream();
  1231. wb.write(out);
  1232. out.close();
  1233. int size1 = out.size();
  1234. out = new ByteArrayOutputStream();
  1235. wb.write(out);
  1236. out.close();
  1237. int size2 = out.size();
  1238. assertEquals(size1, size2);
  1239. out = new ByteArrayOutputStream();
  1240. wb.write(out);
  1241. out.close();
  1242. int size3 = out.size();
  1243. assertEquals(size2, size3);
  1244. }
  1245. /**
  1246. * java.io.IOException: block[ 0 ] already removed
  1247. * (is an excel 95 file though)
  1248. */
  1249. public void test46904() {
  1250. try {
  1251. openSample("46904.xls");
  1252. fail();
  1253. } catch(OldExcelFormatException e) {
  1254. assertTrue(e.getMessage().startsWith(
  1255. "The supplied spreadsheet seems to be Excel"
  1256. ));
  1257. }
  1258. }
  1259. /**
  1260. * java.lang.NegativeArraySizeException reading long
  1261. * non-unicode data for a name record
  1262. */
  1263. public void test47034() {
  1264. HSSFWorkbook wb = openSample("47034.xls");
  1265. assertEquals(893, wb.getNumberOfNames());
  1266. assertEquals("Matthew\\Matthew11_1\\Matthew2331_1\\Matthew2351_1\\Matthew2361_1___lab", wb.getNameName(300));
  1267. }
  1268. /**
  1269. * HSSFRichTextString.length() returns negative for really long strings.
  1270. * The test file was created in OpenOffice 3.0 as Excel does not allow cell text longer than 32,767 characters
  1271. */
  1272. public void test46368() {
  1273. HSSFWorkbook wb = openSample("46368.xls");
  1274. HSSFSheet s = wb.getSheetAt(0);
  1275. HSSFCell cell1 = s.getRow(0).getCell(0);
  1276. assertEquals(32770, cell1.getStringCellValue().length());
  1277. HSSFCell cell2 = s.getRow(2).getCell(0);
  1278. assertEquals(32766, cell2.getStringCellValue().length());
  1279. }
  1280. /**
  1281. * Short records on certain sheets with charts in them
  1282. */
  1283. public void test48180() {
  1284. HSSFWorkbook wb = openSample("48180.xls");
  1285. HSSFSheet s = wb.getSheetAt(0);
  1286. HSSFCell cell1 = s.getRow(0).getCell(0);
  1287. assertEquals("test ", cell1.getStringCellValue().toString());
  1288. HSSFCell cell2 = s.getRow(0).getCell(1);
  1289. assertEquals(1.0, cell2.getNumericCellValue(), 0.0);
  1290. }
  1291. /**
  1292. * POI 3.5 beta 7 can not read excel file contain list box (Form Control)
  1293. */
  1294. public void test47701() {
  1295. openSample("47701.xls");
  1296. }
  1297. public void test48026() {
  1298. openSample("48026.xls");
  1299. }
  1300. public void test47251() {
  1301. openSample("47251.xls");
  1302. }
  1303. /**
  1304. * Round trip a file with an unusual UnicodeString/ExtRst record parts
  1305. */
  1306. public void test47847() throws Exception {
  1307. HSSFWorkbook wb = openSample("47847.xls");
  1308. assertEquals(3, wb.getNumberOfSheets());
  1309. // Find the SST record
  1310. UnicodeString withExt = wb.getWorkbook().getSSTString(0);
  1311. UnicodeString withoutExt = wb.getWorkbook().getSSTString(31);
  1312. assertEquals("O:Alloc:Qty", withExt.getString());
  1313. assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004);
  1314. assertEquals("RT", withoutExt.getString());
  1315. assertTrue((withoutExt.getOptionFlags() & 0x0004) == 0x0000);
  1316. // Something about continues...
  1317. // Write out and re-read
  1318. wb = writeOutAndReadBack(wb);
  1319. assertEquals(3, wb.getNumberOfSheets());
  1320. // Check it's the same now
  1321. withExt = wb.getWorkbook().getSSTString(0);
  1322. withoutExt = wb.getWorkbook().getSSTString(31);
  1323. assertEquals("O:Alloc:Qty", withExt.getString());
  1324. assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004);
  1325. assertEquals("RT", withoutExt.getString());
  1326. assertTrue((withoutExt.getOptionFlags() & 0x0004) == 0x0000);
  1327. }
  1328. /**
  1329. * Problem with cloning a sheet with a chart
  1330. * contained in it.
  1331. */
  1332. public void test49096() throws Exception {
  1333. HSSFWorkbook wb = openSample("49096.xls");
  1334. assertEquals(1, wb.getNumberOfSheets());
  1335. assertNotNull(wb.getSheetAt(0));
  1336. wb.cloneSheet(0);
  1337. assertEquals(2, wb.getNumberOfSheets());
  1338. wb = writeOutAndReadBack(wb);
  1339. assertEquals(2, wb.getNumberOfSheets());
  1340. }
  1341. /**
  1342. * Newly created sheets need to get a
  1343. * proper TabID, otherwise print setup
  1344. * gets confused on them.
  1345. * Also ensure that print setup refs are
  1346. * by reference not value
  1347. */
  1348. public void test46664() throws Exception {
  1349. HSSFWorkbook wb = new HSSFWorkbook();
  1350. HSSFSheet sheet = wb.createSheet("new_sheet");
  1351. HSSFRow row = sheet.createRow((short)0);
  1352. row.createCell(0).setCellValue(new HSSFRichTextString("Column A"));
  1353. row.createCell(1).setCellValue(new HSSFRichTextString("Column B"));
  1354. row.createCell(2).setCellValue(new HSSFRichTextString("Column C"));
  1355. row.createCell(3).setCellValue(new HSSFRichTextString("Column D"));
  1356. row.createCell(4).setCellValue(new HSSFRichTextString("Column E"));
  1357. row.createCell(5).setCellValue(new HSSFRichTextString("Column F"));
  1358. //set print area from column a to column c (on first row)
  1359. wb.setPrintArea(
  1360. 0, //sheet index
  1361. 0, //start column
  1362. 2, //end column
  1363. 0, //start row
  1364. 0 //end row
  1365. );
  1366. wb = writeOutAndReadBack(wb);
  1367. // Ensure the tab index
  1368. TabIdRecord tr = null;
  1369. for(Record r : wb.getWorkbook().getRecords()) {
  1370. if(r instanceof TabIdRecord) {
  1371. tr = (TabIdRecord)r;
  1372. }
  1373. }
  1374. assertNotNull(tr);
  1375. assertEquals(1, tr._tabids.length);
  1376. assertEquals(0, tr._tabids[0]);
  1377. // Ensure the print setup
  1378. assertEquals("new_sheet!$A$1:$C$1", wb.getPrintArea(0));
  1379. assertEquals("new_sheet!$A$1:$C$1", wb.getName("Print_Area").getRefersToFormula());
  1380. // Needs reference not value
  1381. NameRecord nr = wb.getWorkbook().getNameRecord(
  1382. wb.getNameIndex("Print_Area")
  1383. );
  1384. assertEquals("Print_Area", nr.getNameText());
  1385. assertEquals(1, nr.getNameDefinition().length);
  1386. assertEquals(
  1387. "new_sheet!$A$1:$C$1",
  1388. ((Area3DPtg)nr.getNameDefinition()[0]).toFormulaString(HSSFEvaluationWorkbook.create(wb))
  1389. );
  1390. assertEquals('R', nr.getNameDefinition()[0].getRVAType());
  1391. }
  1392. /**
  1393. * Problems with formula references to
  1394. * sheets via URLs
  1395. */
  1396. public void test45970() throws Exception {
  1397. HSSFWorkbook wb = openSample("FormulaRefs.xls");
  1398. assertEquals(3, wb.getNumberOfSheets());
  1399. HSSFSheet s = wb.getSheetAt(0);
  1400. HSSFRow row;
  1401. row = s.getRow(0);
  1402. assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
  1403. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1404. row = s.getRow(1);
  1405. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1406. assertEquals("B1", row.getCell(1).getCellFormula());
  1407. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1408. row = s.getRow(2);
  1409. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1410. assertEquals("Sheet1!B1", row.getCell(1).getCellFormula());
  1411. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1412. row = s.getRow(3);
  1413. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1414. assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
  1415. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1416. row = s.getRow(4);
  1417. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1418. assertEquals("'[$http://gagravarr.org/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
  1419. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1420. // Change 4
  1421. row.getCell(1).setCellFormula("'[$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2");
  1422. row.getCell(1).setCellValue(123.0);
  1423. // Add 5
  1424. row = s.createRow(5);
  1425. row.createCell(1, Cell.CELL_TYPE_FORMULA);
  1426. row.getCell(1).setCellFormula("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1");
  1427. row.getCell(1).setCellValue(234.0);
  1428. // Re-test
  1429. wb = writeOutAndReadBack(wb);
  1430. s = wb.getSheetAt(0);
  1431. row = s.getRow(0);
  1432. assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
  1433. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1434. row = s.getRow(1);
  1435. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1436. assertEquals("B1", row.getCell(1).getCellFormula());
  1437. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1438. row = s.getRow(2);
  1439. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1440. assertEquals("Sheet1!B1", row.getCell(1).getCellFormula());
  1441. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1442. row = s.getRow(3);
  1443. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1444. assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
  1445. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1446. // TODO - Fix these so they work...
  1447. if(1==2) {
  1448. row = s.getRow(4);
  1449. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1450. assertEquals("'[$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2", row.getCell(1).getCellFormula());
  1451. assertEquals(123.0, row.getCell(1).getNumericCellValue());
  1452. row = s.getRow(5);
  1453. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1454. assertEquals("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
  1455. assertEquals(234.0, row.getCell(1).getNumericCellValue());
  1456. }
  1457. }
  1458. /**
  1459. * Test for a file with NameRecord with NameCommentRecord comments
  1460. */
  1461. public void test49185() throws Exception {
  1462. HSSFWorkbook wb = openSample("49185.xls");
  1463. Name name = wb.getName("foobarName");
  1464. assertEquals("This is a comment", name.getComment());
  1465. // Rename the name, comment comes with it
  1466. name.setNameName("ChangedName");
  1467. assertEquals("This is a comment", name.getComment());
  1468. // Save and re-check
  1469. wb = writeOutAndReadBack(wb);
  1470. name = wb.getName("ChangedName");
  1471. assertEquals("This is a comment", name.getComment());
  1472. // Now try to change it
  1473. name.setComment("Changed Comment");
  1474. assertEquals("Changed Comment", name.getComment());
  1475. // Save and re-check
  1476. wb = writeOutAndReadBack(wb);
  1477. name = wb.getName("ChangedName");
  1478. assertEquals("Changed Comment", name.getComment());
  1479. }
  1480. /**
  1481. * Vertically aligned text
  1482. */
  1483. public void test49524() throws Exception {
  1484. HSSFWorkbook wb = openSample("49524.xls");
  1485. Sheet s = wb.getSheetAt(0);
  1486. Row r = s.getRow(0);
  1487. Cell rotated = r.getCell(0);
  1488. Cell normal = r.getCell(1);
  1489. // Check the current ones
  1490. assertEquals(0, normal.getCellStyle().getRotation());
  1491. assertEquals(0xff, rotated.getCellStyle().getRotation());
  1492. // Add a new style, also rotated
  1493. CellStyle cs = wb.createCellStyle();
  1494. cs.setRotation((short)0xff);
  1495. Cell nc = r.createCell(2);
  1496. nc.setCellValue("New Rotated Text");
  1497. nc.setCellStyle(cs);
  1498. assertEquals(0xff, nc.getCellStyle().getRotation());
  1499. // Write out and read back
  1500. wb = writeOutAndReadBack(wb);
  1501. // Re-check
  1502. s = wb.getSheetAt(0);
  1503. r = s.getRow(0);
  1504. rotated = r.getCell(0);
  1505. normal = r.getCell(1);
  1506. nc = r.getCell(2);
  1507. assertEquals(0, normal.getCellStyle().getRotation());
  1508. assertEquals(0xff, rotated.getCellStyle().getRotation());
  1509. assertEquals(0xff, nc.getCellStyle().getRotation());
  1510. }
  1511. /**
  1512. * Setting the user style name on custom styles
  1513. */
  1514. public void test49689() throws Exception {
  1515. HSSFWorkbook wb = new HSSFWorkbook();
  1516. HSSFSheet s = wb.createSheet("Test");
  1517. HSSFRow r = s.createRow(0);
  1518. HSSFCell c = r.createCell(0);
  1519. HSSFCellStyle cs1 = wb.createCellStyle();
  1520. HSSFCellStyle cs2 = wb.createCellStyle();
  1521. HSSFCellStyle cs3 = wb.createCellStyle();
  1522. assertEquals(21, cs1.getIndex());
  1523. cs1.setUserStyleName("Testing");
  1524. assertEquals(22, cs2.getIndex());
  1525. cs2.setUserStyleName("Testing 2");
  1526. assertEquals(23, cs3.getIndex());
  1527. cs3.setUserStyleName("Testing 3");
  1528. // Set one
  1529. c.setCellStyle(cs1);
  1530. // Write out and read back
  1531. wb = writeOutAndReadBack(wb);
  1532. // Re-check
  1533. assertEquals("Testing", wb.getCellStyleAt((short)21).getUserStyleName());
  1534. assertEquals("Testing 2", wb.getCellStyleAt((short)22).getUserStyleName());
  1535. assertEquals("Testing 3", wb.getCellStyleAt((short)23).getUserStyleName());
  1536. }
  1537. public void test49751() {
  1538. HSSFWorkbook wb = openSample("49751.xls");
  1539. short numCellStyles = wb.getNumCellStyles();
  1540. List<String> namedStyles = Arrays.asList(
  1541. "20% - Accent1", "20% - Accent2", "20% - Accent3", "20% - Accent4", "20% - Accent5",
  1542. "20% - Accent6", "40% - Accent1", "40% - Accent2", "40% - Accent3", "40% - Accent4",
  1543. "40% - Accent5", "40% - Accent6", "60% - Accent1", "60% - Accent2", "60% - Accent3",
  1544. "60% - Accent4", "60% - Accent5", "60% - Accent6", "Accent1", "Accent2", "Accent3",
  1545. "Accent4", "Accent5", "Accent6", "Bad", "Calculation", "Check Cell", "Explanatory Text",
  1546. "Good", "Heading 1", "Heading 2", "Heading 3", "Heading 4", "Input", "Linked Cell",
  1547. "Neutral", "Note", "Output", "Title", "Total", "Warning Text");
  1548. List<String> collecteddStyles = new ArrayList<String>();
  1549. for (short i = 0; i < numCellStyles; i++) {
  1550. HSSFCellStyle cellStyle = wb.getCellStyleAt(i);
  1551. String styleName = cellStyle.getUserStyleName();
  1552. if (styleName != null) {
  1553. collecteddStyles.add(styleName);
  1554. }
  1555. }
  1556. assertTrue(namedStyles.containsAll(collecteddStyles));
  1557. }
  1558. /**
  1559. * Regression with the PageSettingsBlock
  1560. */
  1561. public void test49931() throws Exception {
  1562. HSSFWorkbook wb = openSample("49931.xls");
  1563. assertEquals(1, wb.getNumberOfSheets());
  1564. assertEquals("Foo", wb.getSheetAt(0).getRow(0).getCell(0).getRichStringCellValue().toString());
  1565. }
  1566. /**
  1567. * Missing left/right/centre options on a footer
  1568. */
  1569. public void test48325() throws Exception {
  1570. HSSFWorkbook wb = openSample("48325.xls");
  1571. HSSFSheet sh = wb.getSheetAt(0);
  1572. HSSFFooter f = sh.getFooter();
  1573. // Will show as the centre, as that is what excel does
  1574. // with an invalid footer lacking left/right/centre details
  1575. assertEquals("Left text should be empty", "", f.getLeft());
  1576. assertEquals("Right text should be empty", "", f.getRight());
  1577. assertEquals(
  1578. "Center text should contain the illegal value",
  1579. "BlahBlah blah blah ", f.getCenter()
  1580. );
  1581. }
  1582. /**
  1583. * IllegalStateException received when creating Data validation in sheet with macro
  1584. */
  1585. public void test50020() throws Exception {
  1586. HSSFWorkbook wb = openSample("50020.xls");
  1587. writeOutAndReadBack(wb);
  1588. }
  1589. public void test50426() throws Exception {
  1590. HSSFWorkbook wb = openSample("50426.xls");
  1591. writeOutAndReadBack(wb);
  1592. }
  1593. /**
  1594. * Last row number when shifting rows
  1595. */
  1596. public void test50416LastRowNumber() {
  1597. // Create the workbook with 1 sheet which contains 3 rows
  1598. HSSFWorkbook workbook = new HSSFWorkbook();
  1599. Sheet sheet = workbook.createSheet("Bug50416");
  1600. Row row1 = sheet.createRow(0);
  1601. Cell cellA_1 = row1.createCell(0,Cell.CELL_TYPE_STRING);
  1602. cellA_1.setCellValue("Cell A,1");
  1603. Row row2 = sheet.createRow(1);
  1604. Cell cellA_2 = row2.createCell(0,Cell.CELL_TYPE_STRING);
  1605. cellA_2.setCellValue("Cell A,2");
  1606. Row row3 = sheet.createRow(2);
  1607. Cell cellA_3 = row3.createCell(0,Cell.CELL_TYPE_STRING);
  1608. cellA_3.setCellValue("Cell A,3");
  1609. // Test the last Row number it currently correct
  1610. assertEquals(2, sheet.getLastRowNum());
  1611. // Shift the first row to the end
  1612. sheet.shiftRows(0, 0, 3);
  1613. assertEquals(3, sheet.getLastRowNum());
  1614. assertEquals(-1, sheet.getRow(0).getLastCellNum());
  1615. assertEquals("Cell A,2", sheet.getRow(1).getCell(0).getStringCellValue());
  1616. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1617. assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
  1618. // Shift the 2nd row up to the first one
  1619. sheet.shiftRows(1, 1, -1);
  1620. assertEquals(3, sheet.getLastRowNum());
  1621. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1622. assertEquals(-1, sheet.getRow(1).getLastCellNum());
  1623. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1624. assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
  1625. // Shift the 4th row up into the gap in the 3rd row
  1626. sheet.shiftRows(3, 3, -2);
  1627. assertEquals(2, sheet.getLastRowNum());
  1628. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1629. assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
  1630. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1631. assertEquals(-1, sheet.getRow(3).getLastCellNum());
  1632. // Now zap the empty 4th row - won't do anything
  1633. sheet.removeRow(sheet.getRow(3));
  1634. // Test again the last row number which should be 2
  1635. assertEquals(2, sheet.getLastRowNum());
  1636. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1637. assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
  1638. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1639. }
  1640. /**
  1641. * If you send a file between Excel and OpenOffice enough, something
  1642. * will turn the "General" format into "GENERAL"
  1643. */
  1644. public void test50756() throws Exception {
  1645. HSSFWorkbook wb = openSample("50756.xls");
  1646. HSSFSheet s = wb.getSheetAt(0);
  1647. HSSFRow r17 = s.getRow(16);
  1648. HSSFRow r18 = s.getRow(17);
  1649. HSSFDataFormatter df = new HSSFDataFormatter();
  1650. assertEquals(10.0, r17.getCell(1).getNumericCellValue());
  1651. assertEquals(20.0, r17.getCell(2).getNumericCellValue());
  1652. assertEquals(20.0, r17.getCell(3).getNumericCellValue());
  1653. assertEquals("GENERAL", r17.getCell(1).getCellStyle().getDataFormatString());
  1654. assertEquals("GENERAL", r17.getCell(2).getCellStyle().getDataFormatString());
  1655. assertEquals("GENERAL", r17.getCell(3).getCellStyle().getDataFormatString());
  1656. assertEquals("10", df.formatCellValue(r17.getCell(1)));
  1657. assertEquals("20", df.formatCellValue(r17.getCell(2)));
  1658. assertEquals("20", df.formatCellValue(r17.getCell(3)));
  1659. assertEquals(16.0, r18.getCell(1).getNumericCellValue());
  1660. assertEquals(35.0, r18.getCell(2).getNumericCellValue());
  1661. assertEquals(123.0, r18.getCell(3).getNumericCellValue());
  1662. assertEquals("GENERAL", r18.getCell(1).getCellStyle().getDataFormatString());
  1663. assertEquals("GENERAL", r18.getCell(2).getCellStyle().getDataFormatString());
  1664. assertEquals("GENERAL", r18.getCell(3).getCellStyle().getDataFormatString());
  1665. assertEquals("16", df.formatCellValue(r18.getCell(1)));
  1666. assertEquals("35", df.formatCellValue(r18.getCell(2)));
  1667. assertEquals("123", df.formatCellValue(r18.getCell(3)));
  1668. }
  1669. /**
  1670. * A protected sheet with comments, when written out by
  1671. * POI, ends up upsetting excel.
  1672. * TODO Identify the cause and add extra asserts for
  1673. * the bit excel cares about
  1674. */
  1675. public void test50833() throws Exception {
  1676. HSSFWorkbook wb = openSample("50833.xls");
  1677. HSSFSheet s = wb.getSheetAt(0);
  1678. assertEquals("Sheet1", s.getSheetName());
  1679. assertEquals(false, s.getProtect());
  1680. HSSFCell c = s.getRow(0).getCell(0);
  1681. assertEquals("test cell value", c.getRichStringCellValue().getString());
  1682. HSSFComment cmt = c.getCellComment();
  1683. assertNotNull(cmt);
  1684. assertEquals("Robert Lawrence", cmt.getAuthor());
  1685. assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
  1686. // Reload
  1687. wb = writeOutAndReadBack(wb);
  1688. s = wb.getSheetAt(0);
  1689. c = s.getRow(0).getCell(0);
  1690. // Re-check the comment
  1691. cmt = c.getCellComment();
  1692. assertNotNull(cmt);
  1693. assertEquals("Robert Lawrence", cmt.getAuthor());
  1694. assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
  1695. // TODO Identify what excel doesn't like, and check for that
  1696. }
  1697. public void test50779() throws Exception {
  1698. HSSFWorkbook wb1 = openSample("50779_1.xls");
  1699. writeOutAndReadBack(wb1);
  1700. HSSFWorkbook wb2 = openSample("50779_2.xls");
  1701. writeOutAndReadBack(wb2);
  1702. }
  1703. /**
  1704. * The spec says that ChartEndObjectRecord has 6 reserved
  1705. * bytes on the end, but we sometimes find files without...
  1706. */
  1707. public void test50939() throws Exception {
  1708. HSSFWorkbook wb = openSample("50939.xls");
  1709. assertEquals(2, wb.getNumberOfSheets());
  1710. }
  1711. public void test49219() throws Exception {
  1712. HSSFWorkbook wb = openSample("49219.xls");
  1713. assertEquals(1, wb.getNumberOfSheets());
  1714. assertEquals("DGATE", wb.getSheetAt(0).getRow(1).getCell(0).getStringCellValue());
  1715. }
  1716. public void test48968() throws Exception {
  1717. HSSFWorkbook wb = openSample("48968.xls");
  1718. assertEquals(1, wb.getNumberOfSheets());
  1719. DataFormatter fmt = new DataFormatter();
  1720. // Check the dates
  1721. HSSFSheet s = wb.getSheetAt(0);
  1722. Cell cell_d20110325 = s.getRow(0).getCell(0);
  1723. Cell cell_d19000102 = s.getRow(11).getCell(0);
  1724. Cell cell_d19000100 = s.getRow(21).getCell(0);
  1725. assertEquals(s.getRow(0).getCell(3).getStringCellValue(), fmt.formatCellValue(cell_d20110325));
  1726. assertEquals(s.getRow(11).getCell(3).getStringCellValue(), fmt.formatCellValue(cell_d19000102));
  1727. // There is no such thing as 00/01/1900...
  1728. assertEquals("00/01/1900 06:14:24", s.getRow(21).getCell(3).getStringCellValue());
  1729. assertEquals("31/12/1899 06:14:24", fmt.formatCellValue(cell_d19000100));
  1730. // Check the cached values
  1731. assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
  1732. assertEquals(11.0, s.getRow(5).getCell(0).getNumericCellValue());
  1733. assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
  1734. assertEquals(39.0, s.getRow(6).getCell(0).getNumericCellValue());
  1735. assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
  1736. assertEquals(54.0, s.getRow(7).getCell(0).getNumericCellValue());
  1737. // Re-evaulate and check
  1738. HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  1739. assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
  1740. assertEquals(11.0, s.getRow(5).getCell(0).getNumericCellValue());
  1741. assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
  1742. assertEquals(39.0, s.getRow(6).getCell(0).getNumericCellValue());
  1743. assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
  1744. assertEquals(54.0, s.getRow(7).getCell(0).getNumericCellValue());
  1745. // Push the time forward a bit and check
  1746. double date = s.getRow(0).getCell(0).getNumericCellValue();
  1747. s.getRow(0).getCell(0).setCellValue(date + 1.26);
  1748. HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  1749. assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
  1750. assertEquals(11.0+6.0, s.getRow(5).getCell(0).getNumericCellValue());
  1751. assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
  1752. assertEquals(39.0+14.0+1, s.getRow(6).getCell(0).getNumericCellValue());
  1753. assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
  1754. assertEquals(54.0+24.0-60, s.getRow(7).getCell(0).getNumericCellValue());
  1755. }
  1756. /**
  1757. * HLookup and VLookup with optional arguments
  1758. */
  1759. public void test51024() throws Exception {
  1760. HSSFWorkbook wb = new HSSFWorkbook();
  1761. HSSFSheet s = wb.createSheet();
  1762. HSSFRow r1 = s.createRow(0);
  1763. HSSFRow r2 = s.createRow(1);
  1764. r1.createCell(0).setCellValue("v A1");
  1765. r2.createCell(0).setCellValue("v A2");
  1766. r1.createCell(1).setCellValue("v B1");
  1767. HSSFCell c = r1.createCell(4);
  1768. HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
  1769. c.setCellFormula("VLOOKUP(\"v A1\", A1:B2, 1)");
  1770. assertEquals("v A1", eval.evaluate(c).getStringValue());
  1771. c.setCellFormula("VLOOKUP(\"v A1\", A1:B2, 1, 1)");
  1772. assertEquals("v A1", eval.evaluate(c).getStringValue());
  1773. c.setCellFormula("VLOOKUP(\"v A1\", A1:B2, 1, )");
  1774. assertEquals("v A1", eval.evaluate(c).getStringValue());
  1775. c.setCellFormula("HLOOKUP(\"v A1\", A1:B2, 1)");
  1776. assertEquals("v A1", eval.evaluate(c).getStringValue());
  1777. c.setCellFormula("HLOOKUP(\"v A1\", A1:B2, 1, 1)");
  1778. assertEquals("v A1", eval.evaluate(c).getStringValue());
  1779. c.setCellFormula("HLOOKUP(\"v A1\", A1:B2, 1, )");
  1780. assertEquals("v A1", eval.evaluate(c).getStringValue());
  1781. }
  1782. /**
  1783. * Mixture of Ascii and Unicode strings in a
  1784. * NameComment record
  1785. */
  1786. public void test51143() throws Exception {
  1787. HSSFWorkbook wb = openSample("51143.xls");
  1788. assertEquals(1, wb.getNumberOfSheets());
  1789. wb = writeOutAndReadBack(wb);
  1790. assertEquals(1, wb.getNumberOfSheets());
  1791. }
  1792. /**
  1793. * File with exactly 256 data blocks (+header block)
  1794. * shouldn't break on POIFS loading
  1795. */
  1796. public void test51461() throws Exception {
  1797. byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51461.xls");
  1798. HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem(
  1799. new ByteArrayInputStream(data)).getRoot(), false);
  1800. HSSFWorkbook wbNPOIFS = new HSSFWorkbook(new NPOIFSFileSystem(
  1801. new ByteArrayInputStream(data)).getRoot(), false);
  1802. assertEquals(2, wbPOIFS.getNumberOfSheets());
  1803. assertEquals(2, wbNPOIFS.getNumberOfSheets());
  1804. }
  1805. /**
  1806. * Large row numbers and NPOIFS vs POIFS
  1807. */
  1808. public void test51535() throws Exception {
  1809. byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51535.xls");
  1810. HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem(
  1811. new ByteArrayInputStream(data)).getRoot(), false);
  1812. HSSFWorkbook wbNPOIFS = new HSSFWorkbook(new NPOIFSFileSystem(
  1813. new ByteArrayInputStream(data)).getRoot(), false);
  1814. for(HSSFWorkbook wb : new HSSFWorkbook[] {wbPOIFS, wbNPOIFS}) {
  1815. assertEquals(3, wb.getNumberOfSheets());
  1816. // Check directly
  1817. HSSFSheet s = wb.getSheetAt(0);
  1818. assertEquals("Top Left Cell", s.getRow(0).getCell(0).getStringCellValue());
  1819. assertEquals("Top Right Cell", s.getRow(0).getCell(255).getStringCellValue());
  1820. assertEquals("Bottom Left Cell", s.getRow(65535).getCell(0).getStringCellValue());
  1821. assertEquals("Bottom Right Cell", s.getRow(65535).getCell(255).getStringCellValue());
  1822. // Extract and check
  1823. ExcelExtractor ex = new ExcelExtractor(wb);
  1824. String text = ex.getText();
  1825. assertTrue(text.contains("Top Left Cell"));
  1826. assertTrue(text.contains("Top Right Cell"));
  1827. assertTrue(text.contains("Bottom Left Cell"));
  1828. assertTrue(text.contains("Bottom Right Cell"));
  1829. }
  1830. }
  1831. public void test51670() {
  1832. HSSFWorkbook wb = openSample("51670.xls");
  1833. writeOutAndReadBack(wb);
  1834. }
  1835. /**
  1836. * Sum across multiple workbooks
  1837. * eg =SUM($Sheet2.A1:$Sheet3.A1)
  1838. * DISABLED - We currently get the formula wrong, and mis-evaluate
  1839. */
  1840. public void DISABLEDtest48703() {
  1841. HSSFWorkbook wb = openSample("48703.xls");
  1842. assertEquals(3, wb.getNumberOfSheets());
  1843. // Check reading the formula
  1844. Sheet sheet = wb.getSheetAt(0);
  1845. Row r = sheet.getRow(0);
  1846. Cell c = r.getCell(0);
  1847. assertEquals("SUM(Sheet2!A1:Sheet3!A1)", c.getCellFormula());
  1848. assertEquals(4.0, c.getNumericCellValue());
  1849. // Check the evaluated result
  1850. HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
  1851. eval.evaluateFormulaCell(c);
  1852. assertEquals(4.0, c.getNumericCellValue());
  1853. }
  1854. /**
  1855. * Normally encrypted files have BOF then FILEPASS, but
  1856. * some may squeeze a WRITEPROTECT in the middle
  1857. */
  1858. public void test51832() {
  1859. try {
  1860. openSample("51832.xls");
  1861. fail("Encrypted file");
  1862. } catch(EncryptedDocumentException e) {
  1863. // Good
  1864. }
  1865. }
  1866. public void test49896() {
  1867. HSSFWorkbook wb = openSample("49896.xls");
  1868. HSSFCell cell = wb.getSheetAt(0).getRow(1).getCell(1);
  1869. String PATH_SEPARATOR = System.getProperty("file.separator");
  1870. assertEquals("VLOOKUP(A2,'[C:Documents and Settings" + PATH_SEPARATOR+"Yegor"+PATH_SEPARATOR
  1871. +"My Documents"+PATH_SEPARATOR+"csco.xls]Sheet1'!$A$2:$B$3,2,FALSE)",
  1872. cell.getCellFormula());
  1873. }
  1874. public void test49529() throws Exception {
  1875. // user code reported in Bugzilla #49529
  1876. HSSFWorkbook workbook = openSample("49529.xls");
  1877. workbook.getSheetAt(0).createDrawingPatriarch();
  1878. // prior to the fix the line below failed with
  1879. // java.lang.IllegalStateException: EOF - next record not available
  1880. workbook.cloneSheet(0);
  1881. // make sure we are still readable
  1882. writeOutAndReadBack(workbook);
  1883. }
  1884. public void test51675(){
  1885. final List<Short> list = new ArrayList<Short>();
  1886. HSSFWorkbook workbook = openSample("51675.xls");
  1887. HSSFSheet sh = workbook.getSheetAt(0);
  1888. InternalSheet ish = HSSFTestHelper.getSheetForTest(sh);
  1889. PageSettingsBlock psb = (PageSettingsBlock) ish.getRecords().get(13);
  1890. psb.visitContainedRecords(new RecordAggregate.RecordVisitor() {
  1891. public void visitRecord(Record r) {
  1892. list.add(r.getSid());
  1893. }
  1894. });
  1895. assertTrue(list.get(list.size()-1).intValue() == UnknownRecord.BITMAP_00E9);
  1896. assertTrue(list.get(list.size()-2).intValue() == UnknownRecord.HEADER_FOOTER_089C);
  1897. }
  1898. public void test52272(){
  1899. HSSFWorkbook wb = new HSSFWorkbook();
  1900. HSSFSheet sh = wb.createSheet();
  1901. HSSFPatriarch p = sh.createDrawingPatriarch();
  1902. HSSFSimpleShape s = p.createSimpleShape(new HSSFClientAnchor());
  1903. s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
  1904. HSSFSheet sh2 = wb.cloneSheet(0);
  1905. assertNotNull(sh2.getDrawingPatriarch());
  1906. }
  1907. public void test53432(){
  1908. Workbook wb = new HSSFWorkbook(); //or new HSSFWorkbook();
  1909. wb.addPicture(new byte[]{123,22}, Workbook.PICTURE_TYPE_JPEG);
  1910. assertEquals(wb.getAllPictures().size(), 1);
  1911. wb = new HSSFWorkbook();
  1912. wb = writeOutAndReadBack((HSSFWorkbook) wb);
  1913. assertEquals(wb.getAllPictures().size(), 0);
  1914. wb.addPicture(new byte[]{123,22}, Workbook.PICTURE_TYPE_JPEG);
  1915. assertEquals(wb.getAllPictures().size(), 1);
  1916. wb = writeOutAndReadBack((HSSFWorkbook) wb);
  1917. assertEquals(wb.getAllPictures().size(), 1);
  1918. }
  1919. public void test46250(){
  1920. Workbook wb = openSample("46250.xls");
  1921. Sheet sh = wb.getSheet("Template");
  1922. Sheet cSh = wb.cloneSheet(wb.getSheetIndex(sh));
  1923. HSSFPatriarch patriarch = (HSSFPatriarch) cSh.createDrawingPatriarch();
  1924. HSSFTextbox tb = (HSSFTextbox) patriarch.getChildren().get(2);
  1925. tb.setString(new HSSFRichTextString("POI test"));
  1926. tb.setAnchor(new HSSFClientAnchor(0,0,0,0,(short)0,0,(short)10,10));
  1927. wb = writeOutAndReadBack((HSSFWorkbook) wb);
  1928. }
  1929. public void test53404(){
  1930. Workbook wb = openSample("53404.xls");
  1931. Sheet sheet = wb.getSheet("test-sheet");
  1932. int rowCount = sheet.getLastRowNum() + 1;
  1933. int newRows = 5;
  1934. for (int r = rowCount; r < rowCount + newRows; r++) {
  1935. Row row = sheet.createRow((short) r);
  1936. row.createCell(0).setCellValue(1.03 * (r + 7));
  1937. row.createCell(1).setCellValue(new Date());
  1938. row.createCell(2).setCellValue(Calendar.getInstance());
  1939. row.createCell(3).setCellValue(String.format("row:%d/col:%d", r, 3));
  1940. row.createCell(4).setCellValue(true);
  1941. row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);
  1942. row.createCell(6).setCellValue("added cells.");
  1943. }
  1944. wb = writeOutAndReadBack((HSSFWorkbook) wb);
  1945. }
  1946. }