KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > TestBugs


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
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 ==================================================================== */

17         
18
19 package org.apache.poi.hssf.usermodel;
20
21 import junit.framework.TestCase;
22 import org.apache.poi.hssf.util.Region;
23 import org.apache.poi.util.TempFile;
24
25 import java.io.*;
26
27
28
29 /**
30  * Testcases for bugs entered in bugzilla
31  * the Test name contains the bugzilla bug id
32  * @author Avik Sengupta
33  */

34
35 public class TestBugs
36 extends TestCase {
37     public TestBugs(String JavaDoc s) {
38         super(s);
39     }
40     
41     /** Test reading AND writing a complicated workbook
42      *Test opening resulting sheet in excel*/

43     public void test15228()
44     throws java.io.IOException JavaDoc {
45         String JavaDoc readFilename = System.getProperty("HSSF.testdata.path");
46         FileInputStream in = new FileInputStream(readFilename+File.separator+"15228.xls");
47         HSSFWorkbook wb = new HSSFWorkbook(in);
48         HSSFSheet s = wb.getSheetAt(0);
49         HSSFRow r = s.createRow(0);
50         HSSFCell c = r.createCell((short)0);
51         c.setCellValue(10);
52         File file = TempFile.createTempFile("test15228",".xls");
53         FileOutputStream out = new FileOutputStream(file);
54         wb.write(out);
55         assertTrue("No exception thrown", true);
56         assertTrue("File Should Exist", file.exists());
57         
58     }
59     
60     public void test13796()
61     throws java.io.IOException JavaDoc {
62         String JavaDoc readFilename = System.getProperty("HSSF.testdata.path");
63         FileInputStream in = new FileInputStream(readFilename+File.separator+"13796.xls");
64         HSSFWorkbook wb = new HSSFWorkbook(in);
65         HSSFSheet s = wb.getSheetAt(0);
66         HSSFRow r = s.createRow(0);
67         HSSFCell c = r.createCell((short)0);
68         c.setCellValue(10);
69         File file = TempFile.createTempFile("test13796",".xls");
70         FileOutputStream out = new FileOutputStream(file);
71         wb.write(out);
72         assertTrue("No exception thrown", true);
73         assertTrue("File Should Exist", file.exists());
74         
75     }
76     /**Test writing a hyperlink
77      * Open resulting sheet in Excel and check that A1 contains a hyperlink*/

78     public void test23094() throws Exception JavaDoc {
79         File file = TempFile.createTempFile("test23094",".xls");
80         FileOutputStream out = new FileOutputStream(file);
81         HSSFWorkbook wb = new HSSFWorkbook();
82         HSSFSheet s = wb.createSheet();
83         HSSFRow r = s.createRow(0);
84         r.createCell((short)0).setCellFormula("HYPERLINK( \"http://jakarta.apache.org\", \"Jakarta\" )");
85         assertTrue("No Exception expected",true);
86         wb.write(out);
87         out.close();
88     }
89     
90      /* test hyperlinks
91       * open resulting file in excel, and check that there is a link to Google
92       **/

93     public void test15353() throws Exception JavaDoc {
94         HSSFWorkbook wb = new HSSFWorkbook();
95         HSSFSheet sheet = wb.createSheet("My sheet");
96         
97         HSSFRow row = sheet.createRow( (short) 0 );
98         HSSFCell cell = row.createCell( (short) 0 );
99         cell.setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
100         
101         // Write out the workbook
102
File f = TempFile.createTempFile("test15353",".xls");
103         FileOutputStream fileOut = new FileOutputStream(f);
104         wb.write(fileOut);
105         fileOut.close();
106     }
107     
108     /** test reading of a formula with a name and a cell ref in one
109      **/

110     public void test14460() throws Exception JavaDoc {
111         String JavaDoc filename = System.getProperty("HSSF.testdata.path");
112         filename=filename+"/14460.xls";
113         FileInputStream in = new FileInputStream(filename);
114         HSSFWorkbook wb = new HSSFWorkbook(in);
115         HSSFSheet sheet = wb.getSheetAt(0);
116         assertTrue("No exception throws", true);
117     }
118     
119     public void test14330() throws Exception JavaDoc {
120         String JavaDoc filedir = System.getProperty("HSSF.testdata.path");
121         String JavaDoc filename=filedir+"/14330-1.xls";
122         FileInputStream in = new FileInputStream(filename);
123         HSSFWorkbook wb = new HSSFWorkbook(in);
124         HSSFSheet sheet = wb.getSheetAt(0);
125         
126         filename=filedir+"/14330-2.xls";
127         in = new FileInputStream(filename);
128         wb = new HSSFWorkbook(in);
129         sheet = wb.getSheetAt(0);
130         assertTrue("No exception throws", true);
131     }
132     
133     /** test rewriting a file with large number of unique strings
134      *open resulting file in Excel to check results!*/

135     public void test15375() {
136         
137         try {
138             String JavaDoc filename = System.getProperty("HSSF.testdata.path");
139             filename=filename+"/15375.xls";
140             FileInputStream in = new FileInputStream(filename);
141             HSSFWorkbook wb = new HSSFWorkbook(in);
142             HSSFSheet sheet = wb.getSheetAt(0);
143             
144             HSSFRow row = sheet.getRow(5);
145             HSSFCell cell = row.getCell((short)3);
146             if (cell == null)
147                 cell = row.createCell((short)3);
148             
149             // Write test
150
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
151             cell.setCellValue("a test");
152             
153             // change existing numeric cell value
154

155             HSSFRow oRow = sheet.getRow(14);
156             HSSFCell oCell = oRow.getCell((short)4);
157             oCell.setCellValue(75);
158             oCell = oRow.getCell((short)5);
159             oCell.setCellValue("0.3");
160             
161             // Write the output to a file
162
File f = TempFile.createTempFile("test15375",".xls");
163             FileOutputStream fileOut = new FileOutputStream(f);
164             wb.write(fileOut);
165             fileOut.close();
166         }
167         catch (java.io.FileNotFoundException JavaDoc ex) {
168             ex.printStackTrace();
169         }
170         catch (java.io.IOException JavaDoc ex) {
171             ex.printStackTrace();
172         }
173         
174     }
175     
176     /** test writing a file with large number of unique strings
177      *open resulting file in Excel to check results!*/

178     
179     public void test15375_2() throws Exception JavaDoc{
180         
181         
182         HSSFWorkbook wb = new HSSFWorkbook();
183         HSSFSheet sheet = wb.createSheet();
184         
185         String JavaDoc tmp1 = null;
186         String JavaDoc tmp2 = null;
187         String JavaDoc tmp3 = null;
188         
189         for (int i = 0; i < 6000; i++) {
190             tmp1 = "Test1" + i;
191             tmp2 = "Test2" + i;
192             tmp3 = "Test3" + i;
193             
194             HSSFRow row = sheet.createRow((short)i);
195             
196             HSSFCell cell = row.createCell((short)0);
197             cell.setCellValue(tmp1);
198             cell = row.createCell((short)1);
199             cell.setCellValue(tmp2);
200             cell = row.createCell((short)2);
201             cell.setCellValue(tmp3);
202         }
203         File f = TempFile.createTempFile("test15375-2",".xls");
204         FileOutputStream fileOut = new FileOutputStream(f);
205         wb.write(fileOut);
206         fileOut.close();
207     }
208     /** another test for the number of unique strings issue
209      *test opening the resulting file in Excel*/

210     public void test22568() {
211         int r=2000;int c=3;
212         
213         HSSFWorkbook wb = new HSSFWorkbook() ;
214         HSSFSheet sheet = wb.createSheet("ExcelTest") ;
215         
216         int col_cnt=0, rw_cnt=0 ;
217         
218         col_cnt = c;
219         rw_cnt = r;
220         
221         HSSFRow rw = null ;
222         HSSFCell cell =null;
223         rw = sheet.createRow((short)0) ;
224         //Header row
225
for(short j=0; j<col_cnt; j++){
226             cell = rw.createCell((short)j) ;
227             cell.setCellValue("Col " + (j+1)) ;
228         }
229         
230         for(int i=1; i<rw_cnt; i++){
231             rw = sheet.createRow((short)i) ;
232             for(short j=0; j<col_cnt; j++){
233                 cell = rw.createCell((short)j) ;
234                 cell.setCellValue("Row:" + (i+1) + ",Column:" +
235                 (j+1)) ;
236             }
237         }
238         
239         sheet.setDefaultColumnWidth((short) 18) ;
240         
241         try {
242             File f = TempFile.createTempFile("test22568",".xls");
243             FileOutputStream out = new FileOutputStream(f) ;
244             wb.write(out) ;
245             
246             out.close() ;
247         }
248         catch(java.io.IOException JavaDoc io_Excp) {
249             System.out.println(io_Excp.getMessage()) ;
250         }
251     }
252     
253     /**Double byte strings*/
254     public void test15556() throws java.io.IOException JavaDoc {
255         
256         String JavaDoc filename = System.getProperty("HSSF.testdata.path");
257         filename=filename+"/15556.xls";
258         FileInputStream in = new FileInputStream(filename);
259         HSSFWorkbook wb = new HSSFWorkbook(in);
260         HSSFSheet sheet = wb.getSheetAt(0);
261         HSSFRow row = sheet.getRow(45);
262         this.assertTrue("Read row fine!" , true);
263         
264     }
265     
266     /*Double byte strings */
267     public void test22742() throws java.io.IOException JavaDoc {
268         String JavaDoc filename = System.getProperty("HSSF.testdata.path");
269         filename=filename+"/22742.xls";
270         FileInputStream in = new FileInputStream(filename);
271         HSSFWorkbook wb = new HSSFWorkbook(in); this.assertTrue("Read workbook!" , true);
272         
273     }
274     /*Double byte strings */
275     public void test12561_1() throws java.io.IOException JavaDoc {
276         
277         String JavaDoc filename = System.getProperty("HSSF.testdata.path");
278         filename=filename+"/12561-1.xls";
279         FileInputStream in = new FileInputStream(filename);
280         HSSFWorkbook wb = new HSSFWorkbook(in);
281         this.assertTrue("Read workbook!" , true);
282         
283     }
284     /*Double byte strings */
285     public void test12561_2() throws java.io.IOException JavaDoc {
286         
287         String JavaDoc filename = System.getProperty("HSSF.testdata.path");
288         filename=filename+"/12561-2.xls";
289         FileInputStream in = new FileInputStream(filename);
290         HSSFWorkbook wb = new HSSFWorkbook(in);
291         this.assertTrue("Read workbook!" , true);
292         
293     }
294     /*Double byte strings
295      File supplied by jubeson*/

296     public void test12843_1() throws java.io.IOException JavaDoc {
297         String JavaDoc filename = System.getProperty("HSSF.testdata.path");
298         filename=filename+"/12843-1.xls";
299         FileInputStream in = new FileInputStream(filename);
300         HSSFWorkbook wb = new HSSFWorkbook(in);
301         this.assertTrue("Read workbook!" , true);
302     }
303     
304     /*Double byte strings
305      File supplied by Paul Chung*/

306     public void test12843_2() throws java.io.IOException JavaDoc {
307         String JavaDoc filename = System.getProperty("HSSF.testdata.path");
308         filename=filename+"/12843-2.xls";
309         FileInputStream in = new FileInputStream(filename);
310         HSSFWorkbook wb = new HSSFWorkbook(in);
311         this.assertTrue("Read workbook!" , true);
312     }
313     
314     /** Reference to Name*/
315     public void test13224() throws java.io.IOException JavaDoc {
316         String JavaDoc filename = System.getProperty("HSSF.testdata.path");
317         filename=filename+"/13224.xls";
318         FileInputStream in = new FileInputStream(filename);
319         HSSFWorkbook wb = new HSSFWorkbook(in);
320         this.assertTrue("Read workbook!" , true);
321         
322     }
323     
324     /** Illegal argument exception - cannot store duplicate value in Map*/
325     public void test19599() throws java.io.IOException JavaDoc {
326         String JavaDoc filename = System.getProperty("HSSF.testdata.path");
327         FileInputStream in = new FileInputStream(filename+"/19599-1.xls");
328         HSSFWorkbook wb = new HSSFWorkbook(in);
329         in = new FileInputStream(filename+"/19599-2.xls");
330         wb = new HSSFWorkbook(in);
331         this.assertTrue("Read workbook, No exceptions" , true);
332         
333     }
334     
335     public void test24215() throws Exception JavaDoc {
336         String JavaDoc filename = System.getProperty("HSSF.testdata.path");
337         HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filename+"/24215.xls"));
338         
339         for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets();sheetIndex++) {
340             HSSFSheet sheet = wb.getSheetAt(sheetIndex);
341             int rows = sheet.getLastRowNum();
342             
343             for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
344                 HSSFRow row = sheet.getRow(rowIndex);
345                 int cells = row.getLastCellNum();
346                 
347                 for (short cellIndex = 0; cellIndex < cells; cellIndex++) {
348                     HSSFCell cell = row.getCell(cellIndex);
349                 }
350             }
351         }
352         assertTrue("No Exceptions while reading file", true);
353     }
354     
355      public void test18800() throws Exception JavaDoc {
356         ByteArrayOutputStream out = new ByteArrayOutputStream();
357         HSSFWorkbook book = new HSSFWorkbook();
358         book.createSheet("TEST");
359         HSSFSheet sheet = book.cloneSheet(0);
360         book.setSheetName(1,"CLONE");
361         sheet.createRow(0).createCell((short)0).setCellValue("Test");
362         book.write(out);
363         
364         book = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
365         sheet = book.getSheet("CLONE");
366         HSSFRow row = sheet.getRow(0);
367         HSSFCell cell = row.getCell((short)0);
368         System.out.println(cell.getStringCellValue());
369     }
370     
371     /**
372      * Merged regions were being removed from the parent in cloned sheets
373      * @throws Exception
374      */

375     public void test22720() throws Exception JavaDoc {
376        HSSFWorkbook workBook = new HSSFWorkbook();
377        workBook.createSheet("TEST");
378        HSSFSheet template = workBook.getSheetAt(0);
379        
380        template.addMergedRegion(new Region(0, (short)0, 1, (short)2));
381        template.addMergedRegion(new Region(1, (short)0, 2, (short)2));
382        
383        HSSFSheet clone = workBook.cloneSheet(0);
384        int originalMerged = template.getNumMergedRegions();
385        assertEquals("2 merged regions", 2, originalMerged);
386
387 // remove merged regions from clone
388
for (int i=template.getNumMergedRegions()-1; i>=0; i--) {
389          clone.removeMergedRegion(i);
390        }
391
392       assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions());
393 // check if template's merged regions are OK
394
if (template.getNumMergedRegions()>0) {
395           // fetch the first merged region...EXCEPTION OCCURS HERE
396
template.getMergedRegionAt(0);
397        }
398        //make sure we dont exception
399

400     }
401     
402     /*Tests read and write of Unicode strings in formula results
403      * bug and testcase submitted by Sompop Kumnoonsate
404      * The file contains THAI unicode characters.
405      */

406     public void testUnicodeStringFormulaRead() throws Exception JavaDoc {
407         
408         String JavaDoc filename = System.getProperty("HSSF.testdata.path");
409         filename=filename+"/25695.xls";
410         FileInputStream in = new FileInputStream(filename);
411         HSSFWorkbook w;
412         w = new HSSFWorkbook(in);
413         in.close();
414
415         HSSFCell a1 = w.getSheetAt(0).getRow(0).getCell((short) 0);
416         HSSFCell a2 = w.getSheetAt(0).getRow(0).getCell((short) 1);
417         HSSFCell b1 = w.getSheetAt(0).getRow(1).getCell((short) 0);
418         HSSFCell b2 = w.getSheetAt(0).getRow(1).getCell((short) 1);
419         HSSFCell c1 = w.getSheetAt(0).getRow(2).getCell((short) 0);
420         HSSFCell c2 = w.getSheetAt(0).getRow(2).getCell((short) 1);
421         HSSFCell d1 = w.getSheetAt(0).getRow(3).getCell((short) 0);
422         HSSFCell d2 = w.getSheetAt(0).getRow(3).getCell((short) 1);
423
424         assertEquals("String Cell value", a1.getStringCellValue(), a2.getStringCellValue());
425         assertEquals("String Cell value", b1.getStringCellValue(), b2.getStringCellValue());
426         assertEquals("String Cell value", c1.getStringCellValue(), c2.getStringCellValue());
427         assertEquals("String Cell value", d1.getStringCellValue(), d2.getStringCellValue());
428
429         File xls = TempFile.createTempFile("testFormulaUnicode", ".xls");
430         FileOutputStream out = new FileOutputStream(xls);
431         w.write(out);
432         out.close();
433         in = new FileInputStream(xls);
434
435         HSSFWorkbook rw = new HSSFWorkbook(in);
436         in.close();
437
438         HSSFCell ra1 = rw.getSheetAt(0).getRow(0).getCell((short) 0);
439         HSSFCell ra2 = rw.getSheetAt(0).getRow(0).getCell((short) 1);
440         HSSFCell rb1 = rw.getSheetAt(0).getRow(1).getCell((short) 0);
441         HSSFCell rb2 = rw.getSheetAt(0).getRow(1).getCell((short) 1);
442         HSSFCell rc1 = rw.getSheetAt(0).getRow(2).getCell((short) 0);
443         HSSFCell rc2 = rw.getSheetAt(0).getRow(2).getCell((short) 1);
444         HSSFCell rd1 = rw.getSheetAt(0).getRow(3).getCell((short) 0);
445         HSSFCell rd2 = rw.getSheetAt(0).getRow(3).getCell((short) 1);
446
447         assertEquals("Re-Written String Cell value", a1.getStringCellValue(), ra1.getStringCellValue());
448         assertEquals("Re-Written String Cell value", b1.getStringCellValue(), rb1.getStringCellValue());
449         assertEquals("Re-Written String Cell value", c1.getStringCellValue(), rc1.getStringCellValue());
450         assertEquals("Re-Written String Cell value", d1.getStringCellValue(), rd1.getStringCellValue());
451         assertEquals("Re-Written Formula String Cell value", a1.getStringCellValue(), ra2.getStringCellValue());
452         assertEquals("Re-Written Formula String Cell value", b1.getStringCellValue(), rb2.getStringCellValue());
453         assertEquals("Re-Written Formula String Cell value", c1.getStringCellValue(), rc2.getStringCellValue());
454         assertEquals("Re-Written Formula String Cell value", d1.getStringCellValue(), rd2.getStringCellValue());
455
456     }
457
458 }
459
460
461
462
Popular Tags