Add test for bug #43623, currently disabled as it makes FormulaParser unhappy
[poi.git] / src / testcases / org / apache / poi / hssf / usermodel / TestBugs.java
blobfde53ba6a3877904ce94e225422fb96f72d34b99
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
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
18 package org.apache.poi.hssf.usermodel;
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.Iterator;
25 import java.util.List;
27 import junit.framework.AssertionFailedError;
28 import junit.framework.TestCase;
30 import org.apache.poi.hssf.HSSFTestDataSamples;
31 import org.apache.poi.hssf.model.Workbook;
32 import org.apache.poi.hssf.record.CellValueRecordInterface;
33 import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord;
34 import org.apache.poi.hssf.record.NameRecord;
35 import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
36 import org.apache.poi.hssf.record.formula.DeletedArea3DPtg;
37 import org.apache.poi.hssf.util.Region;
38 import org.apache.poi.util.TempFile;
40 /**
41 * Testcases for bugs entered in bugzilla
42 * the Test name contains the bugzilla bug id
43 * @author Avik Sengupta
44 * @author Yegor Kozlov
46 public final class TestBugs extends TestCase {
48 private static HSSFWorkbook openSample(String sampleFileName) {
49 return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
52 private static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {
53 return HSSFTestDataSamples.writeOutAndReadBack(original);
56 private static void writeTestOutputFileForViewing(HSSFWorkbook wb, String simpleFileName) {
57 if (true) { // set to false to output test files
58 return;
60 File file;
61 try {
62 file = TempFile.createTempFile(simpleFileName + "#", ".xls");
63 FileOutputStream out = new FileOutputStream(file);
64 wb.write(out);
65 out.close();
66 } catch (IOException e) {
67 throw new RuntimeException(e);
69 if (!file.exists()) {
70 throw new RuntimeException("File was not written");
72 System.out.println("Open file '" + file.getAbsolutePath() + "' in Excel");
75 /** Test reading AND writing a complicated workbook
76 *Test opening resulting sheet in excel*/
77 public void test15228() {
78 HSSFWorkbook wb = openSample("15228.xls");
79 HSSFSheet s = wb.getSheetAt(0);
80 HSSFRow r = s.createRow(0);
81 HSSFCell c = r.createCell((short)0);
82 c.setCellValue(10);
83 writeTestOutputFileForViewing(wb, "test15228");
86 public void test13796() {
87 HSSFWorkbook wb = openSample("13796.xls");
88 HSSFSheet s = wb.getSheetAt(0);
89 HSSFRow r = s.createRow(0);
90 HSSFCell c = r.createCell((short)0);
91 c.setCellValue(10);
92 writeOutAndReadBack(wb);
94 /**Test writing a hyperlink
95 * Open resulting sheet in Excel and check that A1 contains a hyperlink*/
96 public void test23094() {
97 HSSFWorkbook wb = new HSSFWorkbook();
98 HSSFSheet s = wb.createSheet();
99 HSSFRow r = s.createRow(0);
100 r.createCell((short)0).setCellFormula("HYPERLINK( \"http://jakarta.apache.org\", \"Jakarta\" )");
102 writeTestOutputFileForViewing(wb, "test23094");
105 /** test hyperlinks
106 * open resulting file in excel, and check that there is a link to Google
108 public void test15353() {
109 HSSFWorkbook wb = new HSSFWorkbook();
110 HSSFSheet sheet = wb.createSheet("My sheet");
112 HSSFRow row = sheet.createRow( (short) 0 );
113 HSSFCell cell = row.createCell( (short) 0 );
114 cell.setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
116 writeOutAndReadBack(wb);
119 /** test reading of a formula with a name and a cell ref in one
121 public void test14460() {
122 HSSFWorkbook wb = openSample("14460.xls");
123 wb.getSheetAt(0);
126 public void test14330() {
127 HSSFWorkbook wb = openSample("14330-1.xls");
128 wb.getSheetAt(0);
130 wb = openSample("14330-2.xls");
131 wb.getSheetAt(0);
134 private static void setCellText(HSSFCell cell, String text) {
135 cell.setCellValue(new HSSFRichTextString(text));
138 /** test rewriting a file with large number of unique strings
139 *open resulting file in Excel to check results!*/
140 public void test15375() {
141 HSSFWorkbook wb = openSample("15375.xls");
142 HSSFSheet sheet = wb.getSheetAt(0);
144 HSSFRow row = sheet.getRow(5);
145 HSSFCell cell = row.getCell((short)3);
146 if (cell == null)
147 cell = row.createCell((short)3);
149 // Write test
150 cell.setCellType(HSSFCell.CELL_TYPE_STRING);
151 setCellText(cell, "a test");
153 // change existing numeric cell value
155 HSSFRow oRow = sheet.getRow(14);
156 HSSFCell oCell = oRow.getCell((short)4);
157 oCell.setCellValue(75);
158 oCell = oRow.getCell((short)5);
159 setCellText(oCell, "0.3");
161 writeTestOutputFileForViewing(wb, "test15375");
164 /** test writing a file with large number of unique strings
165 *open resulting file in Excel to check results!*/
167 public void test15375_2() throws Exception{
168 HSSFWorkbook wb = new HSSFWorkbook();
169 HSSFSheet sheet = wb.createSheet();
171 String tmp1 = null;
172 String tmp2 = null;
173 String tmp3 = null;
175 for (int i = 0; i < 6000; i++) {
176 tmp1 = "Test1" + i;
177 tmp2 = "Test2" + i;
178 tmp3 = "Test3" + i;
180 HSSFRow row = sheet.createRow((short)i);
182 HSSFCell cell = row.createCell((short)0);
183 setCellText(cell, tmp1);
184 cell = row.createCell((short)1);
185 setCellText(cell, tmp2);
186 cell = row.createCell((short)2);
187 setCellText(cell, tmp3);
189 writeTestOutputFileForViewing(wb, "test15375-2");
191 /** another test for the number of unique strings issue
192 *test opening the resulting file in Excel*/
193 public void test22568() {
194 int r=2000;int c=3;
196 HSSFWorkbook wb = new HSSFWorkbook() ;
197 HSSFSheet sheet = wb.createSheet("ExcelTest") ;
199 int col_cnt=0, rw_cnt=0 ;
201 col_cnt = c;
202 rw_cnt = r;
204 HSSFRow rw = null ;
205 HSSFCell cell =null;
206 rw = sheet.createRow((short)0) ;
207 //Header row
208 for(short j=0; j<col_cnt; j++){
209 cell = rw.createCell(j) ;
210 setCellText(cell, "Col " + (j+1)) ;
213 for(int i=1; i<rw_cnt; i++){
214 rw = sheet.createRow((short)i) ;
215 for(short j=0; j<col_cnt; j++){
216 cell = rw.createCell(j) ;
217 setCellText(cell, "Row:" + (i+1) + ",Column:" + (j+1)) ;
221 sheet.setDefaultColumnWidth((short) 18) ;
223 writeTestOutputFileForViewing(wb, "test22568");
226 /**Double byte strings*/
227 public void test15556() {
229 HSSFWorkbook wb = openSample("15556.xls");
230 HSSFSheet sheet = wb.getSheetAt(0);
231 HSSFRow row = sheet.getRow(45);
232 assertNotNull("Read row fine!" , row);
234 /**Double byte strings */
235 public void test22742() {
236 openSample("22742.xls");
238 /**Double byte strings */
239 public void test12561_1() {
240 openSample("12561-1.xls");
242 /** Double byte strings */
243 public void test12561_2() {
244 openSample("12561-2.xls");
246 /** Double byte strings
247 File supplied by jubeson*/
248 public void test12843_1() {
249 openSample("12843-1.xls");
252 /** Double byte strings
253 File supplied by Paul Chung*/
254 public void test12843_2() {
255 openSample("12843-2.xls");
258 /** Reference to Name*/
259 public void test13224() {
260 openSample("13224.xls");
263 /** Illegal argument exception - cannot store duplicate value in Map*/
264 public void test19599() {
265 openSample("19599-1.xls");
266 openSample("19599-2.xls");
269 public void test24215() {
270 HSSFWorkbook wb = openSample("24215.xls");
272 for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets();sheetIndex++) {
273 HSSFSheet sheet = wb.getSheetAt(sheetIndex);
274 int rows = sheet.getLastRowNum();
276 for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
277 HSSFRow row = sheet.getRow(rowIndex);
278 int cells = row.getLastCellNum();
280 for (short cellIndex = 0; cellIndex < cells; cellIndex++) {
281 row.getCell(cellIndex);
287 public void test18800() {
288 HSSFWorkbook book = new HSSFWorkbook();
289 book.createSheet("TEST");
290 HSSFSheet sheet = book.cloneSheet(0);
291 book.setSheetName(1,"CLONE");
292 sheet.createRow(0).createCell((short)0).setCellValue(new HSSFRichTextString("Test"));
294 book = writeOutAndReadBack(book);
295 sheet = book.getSheet("CLONE");
296 HSSFRow row = sheet.getRow(0);
297 HSSFCell cell = row.getCell((short)0);
298 assertEquals("Test", cell.getRichStringCellValue().getString());
302 * Merged regions were being removed from the parent in cloned sheets
303 * @throws Exception
305 public void test22720() {
306 HSSFWorkbook workBook = new HSSFWorkbook();
307 workBook.createSheet("TEST");
308 HSSFSheet template = workBook.getSheetAt(0);
310 template.addMergedRegion(new Region(0, (short)0, 1, (short)2));
311 template.addMergedRegion(new Region(1, (short)0, 2, (short)2));
313 HSSFSheet clone = workBook.cloneSheet(0);
314 int originalMerged = template.getNumMergedRegions();
315 assertEquals("2 merged regions", 2, originalMerged);
317 // remove merged regions from clone
318 for (int i=template.getNumMergedRegions()-1; i>=0; i--) {
319 clone.removeMergedRegion(i);
322 assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions());
323 // check if template's merged regions are OK
324 if (template.getNumMergedRegions()>0) {
325 // fetch the first merged region...EXCEPTION OCCURS HERE
326 template.getMergedRegionAt(0);
328 //make sure we dont exception
332 /*Tests read and write of Unicode strings in formula results
333 * bug and testcase submitted by Sompop Kumnoonsate
334 * The file contains THAI unicode characters.
336 public void testUnicodeStringFormulaRead() {
338 HSSFWorkbook w = openSample("25695.xls");
340 HSSFCell a1 = w.getSheetAt(0).getRow(0).getCell((short) 0);
341 HSSFCell a2 = w.getSheetAt(0).getRow(0).getCell((short) 1);
342 HSSFCell b1 = w.getSheetAt(0).getRow(1).getCell((short) 0);
343 HSSFCell b2 = w.getSheetAt(0).getRow(1).getCell((short) 1);
344 HSSFCell c1 = w.getSheetAt(0).getRow(2).getCell((short) 0);
345 HSSFCell c2 = w.getSheetAt(0).getRow(2).getCell((short) 1);
346 HSSFCell d1 = w.getSheetAt(0).getRow(3).getCell((short) 0);
347 HSSFCell d2 = w.getSheetAt(0).getRow(3).getCell((short) 1);
349 if (false) {
350 // THAI code page
351 System.out.println("a1="+unicodeString(a1));
352 System.out.println("a2="+unicodeString(a2));
353 // US code page
354 System.out.println("b1="+unicodeString(b1));
355 System.out.println("b2="+unicodeString(b2));
356 // THAI+US
357 System.out.println("c1="+unicodeString(c1));
358 System.out.println("c2="+unicodeString(c2));
359 // US+THAI
360 System.out.println("d1="+unicodeString(d1));
361 System.out.println("d2="+unicodeString(d2));
363 confirmSameCellText(a1, a2);
364 confirmSameCellText(b1, b2);
365 confirmSameCellText(c1, c2);
366 confirmSameCellText(d1, d2);
368 HSSFWorkbook rw = writeOutAndReadBack(w);
370 HSSFCell ra1 = rw.getSheetAt(0).getRow(0).getCell((short) 0);
371 HSSFCell ra2 = rw.getSheetAt(0).getRow(0).getCell((short) 1);
372 HSSFCell rb1 = rw.getSheetAt(0).getRow(1).getCell((short) 0);
373 HSSFCell rb2 = rw.getSheetAt(0).getRow(1).getCell((short) 1);
374 HSSFCell rc1 = rw.getSheetAt(0).getRow(2).getCell((short) 0);
375 HSSFCell rc2 = rw.getSheetAt(0).getRow(2).getCell((short) 1);
376 HSSFCell rd1 = rw.getSheetAt(0).getRow(3).getCell((short) 0);
377 HSSFCell rd2 = rw.getSheetAt(0).getRow(3).getCell((short) 1);
379 confirmSameCellText(a1, ra1);
380 confirmSameCellText(b1, rb1);
381 confirmSameCellText(c1, rc1);
382 confirmSameCellText(d1, rd1);
384 confirmSameCellText(a1, ra2);
385 confirmSameCellText(b1, rb2);
386 confirmSameCellText(c1, rc2);
387 confirmSameCellText(d1, rd2);
390 private static void confirmSameCellText(HSSFCell a, HSSFCell b) {
391 assertEquals(a.getRichStringCellValue().getString(), b.getRichStringCellValue().getString());
393 private static String unicodeString(HSSFCell cell) {
394 String ss = cell.getRichStringCellValue().getString();
395 char s[] = ss.toCharArray();
396 StringBuffer sb = new StringBuffer();
397 for (int x=0;x<s.length;x++) {
398 sb.append("\\u").append(Integer.toHexString(s[x]));
400 return sb.toString();
403 /** Error in opening wb*/
404 public void test32822() {
405 openSample("32822.xls");
407 /**fail to read wb with chart */
408 public void test15573() {
409 openSample("15573.xls");
412 /**names and macros */
413 public void test27852() {
414 HSSFWorkbook wb = openSample("27852.xls");
416 for(int i = 0 ; i < wb.getNumberOfNames(); i++){
417 HSSFName name = wb.getNameAt(i);
418 name.getNameName();
419 name.getReference();
423 public void test28031() {
424 HSSFWorkbook wb = new HSSFWorkbook();
425 HSSFSheet sheet = wb.createSheet();
426 wb.setSheetName(0, "Sheet1");
428 HSSFRow row = sheet.createRow(0);
429 HSSFCell cell = row.createCell((short)0);
430 String formulaText =
431 "IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))";
432 cell.setCellFormula(formulaText);
434 assertEquals(formulaText, cell.getCellFormula());
435 writeTestOutputFileForViewing(wb, "output28031.xls");
438 public void test33082() {
439 openSample("33082.xls");
442 public void test34775() {
443 try {
444 openSample("34775.xls");
445 } catch (NullPointerException e) {
446 throw new AssertionFailedError("identified bug 34775");
450 /** Error when reading then writing ArrayValues in NameRecord's*/
451 public void test37630() {
452 HSSFWorkbook wb = openSample("37630.xls");
453 writeOutAndReadBack(wb);
457 * Bug 25183: org.apache.poi.hssf.usermodel.HSSFSheet.setPropertiesFromSheet
459 public void test25183() {
460 HSSFWorkbook wb = openSample("25183.xls");
461 writeOutAndReadBack(wb);
465 * Bug 26100: 128-character message in IF statement cell causes HSSFWorkbook open failure
467 public void test26100() {
468 HSSFWorkbook wb = openSample("26100.xls");
469 writeOutAndReadBack(wb);
473 * Bug 27933: Unable to use a template (xls) file containing a wmf graphic
475 public void test27933() {
476 HSSFWorkbook wb = openSample("27933.xls");
477 writeOutAndReadBack(wb);
481 * Bug 29206: NPE on HSSFSheet.getRow for blank rows
483 public void test29206() {
484 //the first check with blank workbook
485 HSSFWorkbook wb = new HSSFWorkbook();
486 HSSFSheet sheet = wb.createSheet();
488 for(int i = 1; i < 400; i++) {
489 HSSFRow row = sheet.getRow(i);
490 if(row != null) {
491 row.getCell((short)0);
495 //now check on an existing xls file
496 wb = openSample("Simple.xls");
498 for(int i = 1; i < 400; i++) {
499 HSSFRow row = sheet.getRow(i);
500 if(row != null) {
501 row.getCell((short)0);
507 * Bug 29675: POI 2.5 final corrupts output when starting workbook has a graphic
509 public void test29675() {
510 HSSFWorkbook wb = openSample("29675.xls");
511 writeOutAndReadBack(wb);
515 * Bug 29942: Importing Excel files that have been created by Open Office on Linux
517 public void test29942() {
518 HSSFWorkbook wb = openSample("29942.xls");
520 HSSFSheet sheet = wb.getSheetAt(0);
521 int count = 0;
522 for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
523 HSSFRow row = sheet.getRow(i);
524 if (row != null) {
525 HSSFCell cell = row .getCell((short)0);
526 assertEquals(HSSFCell.CELL_TYPE_STRING, cell.getCellType());
527 count++;
530 assertEquals(85, count); //should read 85 rows
532 writeOutAndReadBack(wb);
536 * Bug 29982: Unable to read spreadsheet when dropdown list cell is selected -
537 * Unable to construct record instance
539 public void test29982() {
540 HSSFWorkbook wb = openSample("29982.xls");
541 writeOutAndReadBack(wb);
545 * Bug 30540: HSSFSheet.setRowBreak throws NullPointerException
547 public void test30540() {
548 HSSFWorkbook wb = openSample("30540.xls");
550 HSSFSheet s = wb.getSheetAt(0);
551 s.setRowBreak(1);
552 writeOutAndReadBack(wb);
556 * Bug 31749: {Need help urgently}[This is critical] workbook.write() corrupts the file......?
558 public void test31749() {
559 HSSFWorkbook wb = openSample("31749.xls");
560 writeOutAndReadBack(wb);
564 * Bug 31979: {urgent help needed .....}poi library does not support form objects properly.
566 public void test31979() {
567 HSSFWorkbook wb = openSample("31979.xls");
568 writeOutAndReadBack(wb);
572 * Bug 35564: HSSFCell.java: NullPtrExc in isGridsPrinted() and getProtect()
573 * when HSSFWorkbook is created from file
575 public void test35564() {
576 HSSFWorkbook wb = openSample("35564.xls");
578 HSSFSheet sheet = wb.getSheetAt( 0 );
579 assertEquals(false, sheet.isGridsPrinted());
580 assertEquals(false, sheet.getProtect());
582 writeOutAndReadBack(wb);
586 * Bug 35565: HSSFCell.java: NullPtrExc in getColumnBreaks() when HSSFWorkbook is created from file
588 public void test35565() {
589 HSSFWorkbook wb = openSample("35565.xls");
591 HSSFSheet sheet = wb.getSheetAt( 0 );
592 assertNotNull(sheet);
593 writeOutAndReadBack(wb);
597 * Bug 37376: Cannot open the saved Excel file if checkbox controls exceed certain limit
599 public void test37376() {
600 HSSFWorkbook wb = openSample("37376.xls");
601 writeOutAndReadBack(wb);
605 * Bug 40285: CellIterator Skips First Column
607 public void test40285() {
608 HSSFWorkbook wb = openSample("40285.xls");
610 HSSFSheet sheet = wb.getSheetAt( 0 );
611 int rownum = 0;
612 for (Iterator it = sheet.rowIterator(); it.hasNext(); rownum++) {
613 HSSFRow row = (HSSFRow)it.next();
614 assertEquals(rownum, row.getRowNum());
615 int cellNum = 0;
616 for (Iterator it2 = row.cellIterator(); it2.hasNext(); cellNum++) {
617 HSSFCell cell = (HSSFCell)it2.next();
618 assertEquals(cellNum, cell.getCellNum());
624 * Bug 40296: HSSFCell.setCellFormula throws
625 * ClassCastException if cell is created using HSSFRow.createCell(short column, int type)
627 public void test40296() {
628 HSSFWorkbook wb = new HSSFWorkbook();
630 HSSFWorkbook workBook = new HSSFWorkbook();
631 HSSFSheet workSheet = workBook.createSheet("Sheet1");
632 HSSFCell cell;
633 HSSFRow row = workSheet.createRow(0);
634 cell = row.createCell((short)0, HSSFCell.CELL_TYPE_NUMERIC);
635 cell.setCellValue(1.0);
636 cell = row.createCell((short)1, HSSFCell.CELL_TYPE_NUMERIC);
637 cell.setCellValue(2.0);
638 cell = row.createCell((short)2, HSSFCell.CELL_TYPE_FORMULA);
639 cell.setCellFormula("SUM(A1:B1)");
641 writeOutAndReadBack(wb);
645 * Test bug 38266: NPE when adding a row break
647 * User's diagnosis:
648 * 1. Manually (i.e., not using POI) create an Excel Workbook, making sure it
649 * contains a sheet that doesn't have any row breaks
650 * 2. Using POI, create a new HSSFWorkbook from the template in step #1
651 * 3. Try adding a row break (via sheet.setRowBreak()) to the sheet mentioned in step #1
652 * 4. Get a NullPointerException
654 public void test38266() {
655 String[] files = {"Simple.xls", "SimpleMultiCell.xls", "duprich1.xls"};
656 for (int i = 0; i < files.length; i++) {
657 HSSFWorkbook wb = openSample(files[i]);
659 HSSFSheet sheet = wb.getSheetAt( 0 );
660 int[] breaks = sheet.getRowBreaks();
661 assertNull(breaks);
663 //add 3 row breaks
664 for (int j = 1; j <= 3; j++) {
665 sheet.setRowBreak(j*20);
670 public void test40738() {
671 HSSFWorkbook wb = openSample("SimpleWithAutofilter.xls");
672 writeOutAndReadBack(wb);
676 * Bug 44200: Sheet not cloneable when Note added to excel cell
678 public void test44200() {
679 HSSFWorkbook wb = openSample("44200.xls");
681 wb.cloneSheet(0);
682 writeOutAndReadBack(wb);
686 * Bug 44201: Sheet not cloneable when validation added to excel cell
688 public void test44201() {
689 HSSFWorkbook wb = openSample("44201.xls");
690 writeOutAndReadBack(wb);
694 * Bug 37684 : Unhandled Continue Record Error
696 public void test37684 () {
697 HSSFWorkbook wb = openSample("37684-1.xls");
698 writeOutAndReadBack(wb);
701 wb = openSample("37684-2.xls");
702 writeOutAndReadBack(wb);
706 * Bug 41139: Constructing HSSFWorkbook is failed,threw threw ArrayIndexOutOfBoundsException for creating UnknownRecord
708 public void test41139() {
709 HSSFWorkbook wb = openSample("41139.xls");
710 writeOutAndReadBack(wb);
714 * Bug 41546: Constructing HSSFWorkbook is failed,
715 * Unknown Ptg in Formula: 0x1a (26)
717 public void test41546() {
718 HSSFWorkbook wb = openSample("41546.xls");
719 assertEquals(1, wb.getNumberOfSheets());
720 wb = writeOutAndReadBack(wb);
721 assertEquals(1, wb.getNumberOfSheets());
725 * Bug 42564: Some files from Access were giving a RecordFormatException
726 * when reading the BOFRecord
728 public void test42564() {
729 HSSFWorkbook wb = openSample("42564.xls");
730 writeOutAndReadBack(wb);
734 * Bug 42564: Some files from Access also have issues
735 * with the NameRecord, once you get past the BOFRecord
736 * issue.
738 public void test42564Alt() {
739 HSSFWorkbook wb = openSample("42564-2.xls");
740 writeOutAndReadBack(wb);
744 * Bug 42618: RecordFormatException reading a file containing
745 * =CHOOSE(2,A2,A3,A4)
747 public void test42618() {
748 HSSFWorkbook wb = openSample("SimpleWithChoose.xls");
749 wb = writeOutAndReadBack(wb);
750 // Check we detect the string properly too
751 HSSFSheet s = wb.getSheetAt(0);
753 // Textual value
754 HSSFRow r1 = s.getRow(0);
755 HSSFCell c1 = r1.getCell((short)1);
756 assertEquals("=CHOOSE(2,A2,A3,A4)", c1.getRichStringCellValue().toString());
758 // Formula Value
759 HSSFRow r2 = s.getRow(1);
760 HSSFCell c2 = r2.getCell((short)1);
761 assertEquals(25, (int)c2.getNumericCellValue());
763 try {
764 assertEquals("CHOOSE(2,A2,A3,A4)", c2.getCellFormula());
765 } catch (IllegalStateException e) {
766 if (e.getMessage().startsWith("Too few arguments")
767 && e.getMessage().indexOf("ConcatPtg") > 0) {
768 throw new AssertionFailedError("identified bug 44306");
774 * Something up with the FileSharingRecord
776 public void test43251() {
778 // Used to blow up with an IllegalArgumentException
779 // when creating a FileSharingRecord
780 HSSFWorkbook wb;
781 try {
782 wb = openSample("43251.xls");
783 } catch (IllegalArgumentException e) {
784 throw new AssertionFailedError("identified bug 43251");
787 assertEquals(1, wb.getNumberOfSheets());
791 * Crystal reports generates files with short
792 * StyleRecords, which is against the spec
794 public void test44471() {
796 // Used to blow up with an ArrayIndexOutOfBounds
797 // when creating a StyleRecord
798 HSSFWorkbook wb;
799 try {
800 wb = openSample("OddStyleRecord.xls");
801 } catch (ArrayIndexOutOfBoundsException e) {
802 throw new AssertionFailedError("Identified bug 44471");
805 assertEquals(1, wb.getNumberOfSheets());
809 * Files with "read only recommended" were giving
810 * grief on the FileSharingRecord
812 public void test44536() {
814 // Used to blow up with an IllegalArgumentException
815 // when creating a FileSharingRecord
816 HSSFWorkbook wb = openSample("ReadOnlyRecommended.xls");
818 // Check read only advised
819 assertEquals(3, wb.getNumberOfSheets());
820 assertTrue(wb.isWriteProtected());
822 // But also check that another wb isn't
823 wb = openSample("SimpleWithChoose.xls");
824 assertFalse(wb.isWriteProtected());
828 * Some files were having problems with the DVRecord,
829 * probably due to dropdowns
831 public void test44593() {
833 // Used to blow up with an IllegalArgumentException
834 // when creating a DVRecord
835 // Now won't, but no idea if this means we have
836 // rubbish in the DVRecord or not...
837 HSSFWorkbook wb;
838 try {
839 wb = openSample("44593.xls");
840 } catch (IllegalArgumentException e) {
841 throw new AssertionFailedError("Identified bug 44593");
844 assertEquals(2, wb.getNumberOfSheets());
848 * Used to give problems due to trying to read a zero
849 * length string, but that's now properly handled
851 public void test44643() {
853 // Used to blow up with an IllegalArgumentException
854 HSSFWorkbook wb;
855 try {
856 wb = openSample("44643.xls");
857 } catch (IllegalArgumentException e) {
858 throw new AssertionFailedError("identified bug 44643");
861 assertEquals(1, wb.getNumberOfSheets());
865 * User reported the wrong number of rows from the
866 * iterator, but we can't replicate that
868 public void test44693() {
870 HSSFWorkbook wb = openSample("44693.xls");
871 HSSFSheet s = wb.getSheetAt(0);
873 // Rows are 1 to 713
874 assertEquals(0, s.getFirstRowNum());
875 assertEquals(712, s.getLastRowNum());
876 assertEquals(713, s.getPhysicalNumberOfRows());
878 // Now check the iterator
879 int rowsSeen = 0;
880 for(Iterator i = s.rowIterator(); i.hasNext(); ) {
881 HSSFRow r = (HSSFRow)i.next();
882 assertNotNull(r);
883 rowsSeen++;
885 assertEquals(713, rowsSeen);
889 * Bug 28774: Excel will crash when opening xls-files with images.
891 public void test28774() {
892 HSSFWorkbook wb = openSample("28774.xls");
893 assertTrue("no errors reading sample xls", true);
894 writeOutAndReadBack(wb);
895 assertTrue("no errors writing sample xls", true);
899 * Had a problem apparently, not sure what as it
900 * works just fine...
902 public void test44891() throws Exception {
903 HSSFWorkbook wb = openSample("44891.xls");
904 assertTrue("no errors reading sample xls", true);
905 writeOutAndReadBack(wb);
906 assertTrue("no errors writing sample xls", true);
910 * Bug 44235: Ms Excel can't open save as excel file
912 * Works fine with poi-3.1-beta1.
914 public void test44235() throws Exception {
915 HSSFWorkbook wb = openSample("44235.xls");
916 assertTrue("no errors reading sample xls", true);
917 writeOutAndReadBack(wb);
918 assertTrue("no errors writing sample xls", true);
922 * Bug 21334: "File error: data may have been lost" with a file
923 * that contains macros and this formula:
924 * {=SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""))>0,1))}
926 public void test21334() {
927 HSSFWorkbook wb = new HSSFWorkbook();
928 HSSFSheet sh = wb.createSheet();
929 HSSFCell cell = sh.createRow(0).createCell((short)0);
930 String formula = "SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"))>0,1))";
931 cell.setCellFormula(formula);
933 HSSFWorkbook wb_sv = writeOutAndReadBack(wb);
934 HSSFCell cell_sv = wb_sv.getSheetAt(0).getRow(0).getCell((short)0);
935 assertEquals(formula, cell_sv.getCellFormula());
938 public void test36947() throws Exception {
939 HSSFWorkbook wb = openSample("36947.xls");
940 assertTrue("no errors reading sample xls", true);
941 writeOutAndReadBack(wb);
942 assertTrue("no errors writing sample xls", true);
946 * Bug 42448: Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69
948 public void test42448(){
949 HSSFWorkbook wb = new HSSFWorkbook();
950 HSSFCell cell = wb.createSheet().createRow(0).createCell((short)0);
951 cell.setCellFormula("SUMPRODUCT(A!C7:A!C67, B8:B68) / B69");
952 assertTrue("no errors parsing formula", true);
955 public void test39634() throws Exception {
956 HSSFWorkbook wb = openSample("39634.xls");
957 assertTrue("no errors reading sample xls", true);
958 writeOutAndReadBack(wb);
959 assertTrue("no errors writing sample xls", true);
963 * Problems with extracting check boxes from
964 * HSSFObjectData
965 * @throws Exception
967 public void test44840() throws Exception {
968 HSSFWorkbook wb = openSample("WithCheckBoxes.xls");
970 // Take a look at the embeded objects
971 List objects = wb.getAllEmbeddedObjects();
972 assertEquals(1, objects.size());
974 HSSFObjectData obj = (HSSFObjectData)objects.get(0);
975 assertNotNull(obj);
977 // Peek inside the underlying record
978 EmbeddedObjectRefSubRecord rec = obj.findObjectRecord();
979 assertNotNull(rec);
981 assertEquals(32, rec.field_1_stream_id_offset);
982 assertEquals(0, rec.field_6_stream_id); // WRONG!
983 assertEquals("Forms.CheckBox.1", rec.field_5_ole_classname);
984 assertEquals(12, rec.remainingBytes.length);
986 // Doesn't have a directory
987 assertFalse(obj.hasDirectoryEntry());
988 assertNotNull(obj.getObjectData());
989 assertEquals(12, obj.getObjectData().length);
990 assertEquals("Forms.CheckBox.1", obj.getOLE2ClassName());
992 try {
993 obj.getDirectory();
994 fail();
995 } catch(FileNotFoundException e) {}
999 * Test that we can delete sheets without
1000 * breaking the build in named ranges
1001 * used for printing stuff.
1002 * Currently broken, as we change the Ptg
1004 public void test30978() throws Exception {
1005 HSSFWorkbook wb = openSample("30978-alt.xls");
1006 assertEquals(1, wb.getNumberOfNames());
1007 assertEquals(3, wb.getNumberOfSheets());
1009 // Check all names fit within range, and use
1010 // DeletedArea3DPtg
1011 Workbook w = wb.getWorkbook();
1012 for(int i=0; i<w.getNumNames(); i++) {
1013 NameRecord r = w.getNameRecord(i);
1014 assertTrue(r.getIndexToSheet() <= wb.getNumberOfSheets());
1016 List nd = r.getNameDefinition();
1017 assertEquals(1, nd.size());
1018 assertTrue(nd.get(0) instanceof DeletedArea3DPtg);
1022 // Delete the 2nd sheet
1023 wb.removeSheetAt(1);
1026 // Re-check
1027 assertEquals(1, wb.getNumberOfNames());
1028 assertEquals(2, wb.getNumberOfSheets());
1030 for(int i=0; i<w.getNumNames(); i++) {
1031 NameRecord r = w.getNameRecord(i);
1032 assertTrue(r.getIndexToSheet() <= wb.getNumberOfSheets());
1034 List nd = r.getNameDefinition();
1035 assertEquals(1, nd.size());
1036 assertTrue(nd.get(0) instanceof DeletedArea3DPtg);
1040 // Save and re-load
1041 wb = writeOutAndReadBack(wb);
1042 w = wb.getWorkbook();
1044 assertEquals(1, wb.getNumberOfNames());
1045 assertEquals(2, wb.getNumberOfSheets());
1047 for(int i=0; i<w.getNumNames(); i++) {
1048 NameRecord r = w.getNameRecord(i);
1049 assertTrue(r.getIndexToSheet() <= wb.getNumberOfSheets());
1051 List nd = r.getNameDefinition();
1052 assertEquals(1, nd.size());
1053 assertTrue(nd.get(0) instanceof DeletedArea3DPtg);
1058 * Test that fonts get added properly
1060 public void test45338() throws Exception {
1061 HSSFWorkbook wb = new HSSFWorkbook();
1062 assertEquals(4, wb.getNumberOfFonts());
1064 HSSFSheet s = wb.createSheet();
1065 s.createRow(0);
1066 s.createRow(1);
1067 HSSFCell c1 = s.getRow(0).createCell((short)0);
1068 HSSFCell c2 = s.getRow(1).createCell((short)0);
1070 assertEquals(4, wb.getNumberOfFonts());
1072 HSSFFont f1 = wb.getFontAt((short)0);
1073 assertEquals(400, f1.getBoldweight());
1075 // Check that asking for the same font
1076 // multiple times gives you the same thing.
1077 // Otherwise, our tests wouldn't work!
1078 assertEquals(
1079 wb.getFontAt((short)0),
1080 wb.getFontAt((short)0)
1082 assertEquals(
1083 wb.getFontAt((short)2),
1084 wb.getFontAt((short)2)
1086 assertTrue(
1087 wb.getFontAt((short)0)
1089 wb.getFontAt((short)2)
1092 // Look for a new font we have
1093 // yet to add
1094 assertNull(
1095 wb.findFont(
1096 (short)11, (short)123, (short)22,
1097 "Thingy", false, true, (short)2, (byte)2
1101 HSSFFont nf = wb.createFont();
1102 assertEquals(5, wb.getNumberOfFonts());
1104 assertEquals(5, nf.getIndex());
1105 assertEquals(nf, wb.getFontAt((short)5));
1107 nf.setBoldweight((short)11);
1108 nf.setColor((short)123);
1109 nf.setFontHeight((short)22);
1110 nf.setFontName("Thingy");
1111 nf.setItalic(false);
1112 nf.setStrikeout(true);
1113 nf.setTypeOffset((short)2);
1114 nf.setUnderline((byte)2);
1116 assertEquals(5, wb.getNumberOfFonts());
1117 assertEquals(nf, wb.getFontAt((short)5));
1119 // Find it now
1120 assertNotNull(
1121 wb.findFont(
1122 (short)11, (short)123, (short)22,
1123 "Thingy", false, true, (short)2, (byte)2
1126 assertEquals(
1128 wb.findFont(
1129 (short)11, (short)123, (short)22,
1130 "Thingy", false, true, (short)2, (byte)2
1131 ).getIndex()
1133 assertEquals(nf,
1134 wb.findFont(
1135 (short)11, (short)123, (short)22,
1136 "Thingy", false, true, (short)2, (byte)2
1142 * From the mailing list - ensure we can handle a formula
1143 * containing a zip code, eg ="70164"
1144 * @throws Exception
1146 public void testZipCodeFormulas() throws Exception {
1147 HSSFWorkbook wb = new HSSFWorkbook();
1148 HSSFSheet s = wb.createSheet();
1149 s.createRow(0);
1150 HSSFCell c1 = s.getRow(0).createCell((short)0);
1151 HSSFCell c2 = s.getRow(0).createCell((short)1);
1152 HSSFCell c3 = s.getRow(0).createCell((short)2);
1154 // As number and string
1155 c1.setCellFormula("70164");
1156 c2.setCellFormula("\"70164\"");
1157 c3.setCellFormula("\"90210\"");
1159 // Check the formulas
1160 assertEquals("70164.0", c1.getCellFormula());
1161 assertEquals("\"70164\"", c2.getCellFormula());
1163 // And check the values - blank
1164 assertEquals(0.0, c1.getNumericCellValue(), 0.00001);
1165 assertEquals("", c1.getRichStringCellValue().getString());
1166 assertEquals(0.0, c2.getNumericCellValue(), 0.00001);
1167 assertEquals("", c2.getRichStringCellValue().getString());
1168 assertEquals(0.0, c3.getNumericCellValue(), 0.00001);
1169 assertEquals("", c3.getRichStringCellValue().getString());
1171 // Try changing the cached value on one of the string
1172 // formula cells, so we can see it updates properly
1173 c3.setCellValue(new HSSFRichTextString("test"));
1174 assertEquals(0.0, c3.getNumericCellValue(), 0.00001);
1175 assertEquals("test", c3.getRichStringCellValue().getString());
1178 // Now evaluate, they should all be changed
1179 HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(s, wb);
1180 eval.setCurrentRow(s.getRow(0));
1181 eval.evaluateFormulaCell(c1);
1182 eval.evaluateFormulaCell(c2);
1183 eval.evaluateFormulaCell(c3);
1185 // Check that the cells now contain
1186 // the correct values
1187 assertEquals(70164.0, c1.getNumericCellValue(), 0.00001);
1188 assertEquals("", c1.getRichStringCellValue().getString());
1189 assertEquals(0.0, c2.getNumericCellValue(), 0.00001);
1190 assertEquals("70164", c2.getRichStringCellValue().getString());
1191 assertEquals(0.0, c3.getNumericCellValue(), 0.00001);
1192 assertEquals("90210", c3.getRichStringCellValue().getString());
1195 // Write and read
1196 HSSFWorkbook nwb = writeOutAndReadBack(wb);
1197 HSSFSheet ns = nwb.getSheetAt(0);
1198 HSSFCell nc1 = ns.getRow(0).getCell((short)0);
1199 HSSFCell nc2 = ns.getRow(0).getCell((short)1);
1200 HSSFCell nc3 = ns.getRow(0).getCell((short)2);
1202 // Re-check
1203 assertEquals(70164.0, nc1.getNumericCellValue(), 0.00001);
1204 assertEquals("", nc1.getRichStringCellValue().getString());
1205 assertEquals(0.0, nc2.getNumericCellValue(), 0.00001);
1206 assertEquals("70164", nc2.getRichStringCellValue().getString());
1207 assertEquals(0.0, nc3.getNumericCellValue(), 0.00001);
1208 assertEquals("90210", nc3.getRichStringCellValue().getString());
1210 // Now check record level stuff too
1211 ns.getSheet().setLoc(0);
1212 int fn = 0;
1213 CellValueRecordInterface cvr;
1214 while((cvr = ns.getSheet().getNextValueRecord()) != null) {
1215 if(cvr instanceof FormulaRecordAggregate) {
1216 FormulaRecordAggregate fr = (FormulaRecordAggregate)cvr;
1218 if(fn == 0) {
1219 assertEquals(70164.0, fr.getFormulaRecord().getValue(), 0.0001);
1220 assertNull(fr.getStringRecord());
1221 } else if (fn == 1) {
1222 assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
1223 assertNotNull(fr.getStringRecord());
1224 assertEquals("70164", fr.getStringRecord().getString());
1225 } else {
1226 assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
1227 assertNotNull(fr.getStringRecord());
1228 assertEquals("90210", fr.getStringRecord().getString());
1231 fn++;
1234 assertEquals(3, fn);
1238 * Problem with "Vector Rows", eg a whole
1239 * column which is set to the result of
1240 * {=sin(B1:B9)}(9,1), so that each cell is
1241 * shown to have the contents
1242 * {=sin(B1:B9){9,1)[rownum][0]
1243 * In this sample file, the vector column
1244 * is C, and the data column is B.
1246 * For now, blows up with an exception from ExtPtg
1247 * Expected ExpPtg to be converted from Shared to Non-Shared...
1249 public void DISABLEDtest43623() throws Exception {
1250 HSSFWorkbook wb = openSample("43623.xls");
1251 assertEquals(1, wb.getNumberOfSheets());
1253 HSSFSheet s1 = wb.getSheetAt(0);
1255 HSSFCell c1 = s1.getRow(0).getCell(2);
1256 HSSFCell c2 = s1.getRow(1).getCell(2);
1257 HSSFCell c3 = s1.getRow(2).getCell(2);
1259 // These formula contents are a guess...
1260 assertEquals("{=sin(B1:B9){9,1)[0][0]", c1.getCellFormula());
1261 assertEquals("{=sin(B1:B9){9,1)[1][0]", c2.getCellFormula());
1262 assertEquals("{=sin(B1:B9){9,1)[2][0]", c3.getCellFormula());
1264 // Save and re-open, ensure it still works
1265 HSSFWorkbook nwb = writeOutAndReadBack(wb);
1266 HSSFSheet ns1 = nwb.getSheetAt(0);
1267 HSSFCell nc1 = ns1.getRow(0).getCell(2);
1268 HSSFCell nc2 = ns1.getRow(1).getCell(2);
1269 HSSFCell nc3 = ns1.getRow(2).getCell(2);
1271 assertEquals("{=sin(B1:B9){9,1)[0][0]", nc1.getCellFormula());
1272 assertEquals("{=sin(B1:B9){9,1)[1][0]", nc2.getCellFormula());
1273 assertEquals("{=sin(B1:B9){9,1)[2][0]", nc3.getCellFormula());