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

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