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

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