KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
24 import org.apache.poi.hssf.model.Sheet;
25 import org.apache.poi.hssf.record.Record;
26 import org.apache.poi.hssf.record.BOFRecord;
27 import org.apache.poi.hssf.record.EOFRecord;
28 import org.apache.poi.hssf.util.CellReference;
29 import org.apache.poi.hssf.util.HSSFColor;
30 import org.apache.poi.util.TempFile;
31
32 import java.io.File JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35
36 import java.util.List JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Date JavaDoc;
39 import java.util.GregorianCalendar JavaDoc;
40
41 /**
42  * Tests various functionity having to do with HSSFCell. For instance support for
43  * paticular datatypes, etc.
44  * @author Andrew C. Oliver (andy at superlinksoftware dot com)
45  * @author Dan Sherman (dsherman at isisph.com)
46  */

47
48 public class TestHSSFCell
49 extends TestCase {
50     public TestHSSFCell(String JavaDoc s) {
51         super(s);
52     }
53
54     /**
55      * test that Boolean and Error types (BoolErrRecord) are supported properly.
56      */

57     public void testBoolErr()
58             throws java.io.IOException JavaDoc {
59         String JavaDoc readFilename = System.getProperty("HSSF.testdata.path");
60
61             File file = TempFile.createTempFile("testBoolErr",".xls");
62             FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
63             HSSFWorkbook wb = new HSSFWorkbook();
64             HSSFSheet s = wb.createSheet("Sheet1");
65             HSSFRow r = null;
66             HSSFCell c = null;
67             r = s.createRow((short)0);
68             c=r.createCell((short)1);
69             //c.setCellType(HSSFCell.CELL_TYPE_BOOLEAN);
70
c.setCellValue(true);
71
72             c=r.createCell((short)2);
73             //c.setCellType(HSSFCell.CELL_TYPE_BOOLEAN);
74
c.setCellValue(false);
75
76             r = s.createRow((short)1);
77             c=r.createCell((short)1);
78             //c.setCellType(HSSFCell.CELL_TYPE_ERROR);
79
c.setCellErrorValue((byte)0);
80
81             c=r.createCell((short)2);
82             //c.setCellType(HSSFCell.CELL_TYPE_ERROR);
83
c.setCellErrorValue((byte)7);
84
85
86             wb.write(out);
87             out.close();
88
89             assertTrue("file exists",file.exists());
90
91             FileInputStream JavaDoc in = new FileInputStream JavaDoc(file);
92             wb = new HSSFWorkbook(in);
93             s = wb.getSheetAt(0);
94             r = s.getRow(0);
95             c = r.getCell((short)1);
96             assertTrue("boolean value 0,1 = true",c.getBooleanCellValue());
97             c = r.getCell((short)2);
98             assertTrue("boolean value 0,2 = false",c.getBooleanCellValue()==false);
99             r = s.getRow(1);
100             c = r.getCell((short)1);
101             assertTrue("boolean value 0,1 = 0",c.getErrorCellValue() == 0);
102             c = r.getCell((short)2);
103             assertTrue("boolean value 0,2 = 7",c.getErrorCellValue() == 7);
104
105             in.close();
106     }
107
108     /**
109     * Checks that the recognition of files using 1904 date windowing
110     * is working properly. Conversion of the date is also an issue,
111     * but there's a separate unit test for that.
112     */

113     public void testDateWindowing() throws Exception JavaDoc {
114         GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(2000,0,1); // Jan. 1, 2000
115
Date JavaDoc date = cal.getTime();
116         String JavaDoc path = System.getProperty("HSSF.testdata.path");
117
118         // first check a file with 1900 Date Windowing
119
String JavaDoc filename = path + "/1900DateWindowing.xls";
120         FileInputStream JavaDoc stream = new FileInputStream JavaDoc(filename);
121         POIFSFileSystem fs = new POIFSFileSystem(stream);
122         HSSFWorkbook workbook = new HSSFWorkbook(fs);
123         HSSFSheet sheet = workbook.getSheetAt(0);
124
125         assertEquals("Date from file using 1900 Date Windowing",
126                         date.getTime(),
127                            sheet.getRow(0).getCell((short)0)
128                               .getDateCellValue().getTime());
129         stream.close();
130         
131         // now check a file with 1904 Date Windowing
132
filename = path + "/1904DateWindowing.xls";
133         stream = new FileInputStream JavaDoc(filename);
134         fs = new POIFSFileSystem(stream);
135         workbook = new HSSFWorkbook(fs);
136         sheet = workbook.getSheetAt(0);
137
138         assertEquals("Date from file using 1904 Date Windowing",
139                         date.getTime(),
140                            sheet.getRow(0).getCell((short)0)
141                               .getDateCellValue().getTime());
142         stream.close();
143     }
144     
145     /**
146      * Tests that the active cell can be correctly read and set
147      */

148     public void testActiveCell() throws Exception JavaDoc
149     {
150         //read in sample
151
String JavaDoc dir = System.getProperty("HSSF.testdata.path");
152         File sample = new File(dir + "/Simple.xls");
153         assertTrue("Simple.xls exists and is readable", sample.canRead());
154         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(sample);
155         HSSFWorkbook book = new HSSFWorkbook(fis);
156         fis.close();
157         
158         //check initial position
159
HSSFSheet umSheet = book.getSheetAt(0);
160         Sheet s = umSheet.getSheet();
161         assertEquals("Initial active cell should be in col 0",
162             (short) 0, s.getActiveCellCol());
163         assertEquals("Initial active cell should be on row 1",
164             1, s.getActiveCellRow());
165         
166         //modify position through HSSFCell
167
HSSFCell cell = umSheet.createRow(3).createCell((short) 2);
168         cell.setAsActiveCell();
169         assertEquals("After modify, active cell should be in col 2",
170             (short) 2, s.getActiveCellCol());
171         assertEquals("After modify, active cell should be on row 3",
172             3, s.getActiveCellRow());
173         
174         //write book to temp file; read and verify that position is serialized
175
File temp = TempFile.createTempFile("testActiveCell", ".xls");
176         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(temp);
177         book.write(fos);
178         fos.close();
179         
180         fis = new FileInputStream JavaDoc(temp);
181         book = new HSSFWorkbook(fis);
182         fis.close();
183         umSheet = book.getSheetAt(0);
184         s = umSheet.getSheet();
185         
186         assertEquals("After serialize, active cell should be in col 2",
187             (short) 2, s.getActiveCellCol());
188         assertEquals("After serialize, active cell should be on row 3",
189             3, s.getActiveCellRow());
190     }
191
192     /**
193      * test that Cell Styles being applied to formulas remain intact
194      */

195     public void testFormulaStyle()
196             throws java.io.IOException JavaDoc {
197         String JavaDoc readFilename = System.getProperty("HSSF.testdata.path");
198
199             File file = TempFile.createTempFile("testFormulaStyle",".xls");
200             FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
201             HSSFWorkbook wb = new HSSFWorkbook();
202             HSSFSheet s = wb.createSheet("Sheet1");
203             HSSFRow r = null;
204             HSSFCell c = null;
205             HSSFCellStyle cs = wb.createCellStyle();
206             HSSFFont f = wb.createFont();
207             f.setFontHeightInPoints((short) 20);
208             f.setColor((short) HSSFColor.RED.index);
209             f.setBoldweight(f.BOLDWEIGHT_BOLD);
210             f.setFontName("Arial Unicode MS");
211             cs.setFillBackgroundColor((short)3);
212             cs.setFont(f);
213             cs.setBorderTop((short)1);
214             cs.setBorderRight((short)1);
215             cs.setBorderLeft((short)1);
216             cs.setBorderBottom((short)1);
217             
218             r = s.createRow((short)0);
219             c=r.createCell((short)0);
220             c.setCellStyle(cs);
221             c.setCellFormula("2*3");
222             
223             wb.write(out);
224             out.close();
225
226             assertTrue("file exists",file.exists());
227
228             FileInputStream JavaDoc in = new FileInputStream JavaDoc(file);
229             wb = new HSSFWorkbook(in);
230             s = wb.getSheetAt(0);
231             r = s.getRow(0);
232             c = r.getCell((short)0);
233             
234             assertTrue("Formula Cell at 0,0", (c.getCellType()==c.CELL_TYPE_FORMULA));
235             cs = c.getCellStyle();
236             
237             assertNotNull("Formula Cell Style", cs);
238             assertTrue("Font Index Matches", (cs.getFontIndex() == f.getIndex()));
239             assertTrue("Top Border", (cs.getBorderTop() == (short)1));
240             assertTrue("Left Border", (cs.getBorderLeft() == (short)1));
241             assertTrue("Right Border", (cs.getBorderRight() == (short)1));
242             assertTrue("Bottom Border", (cs.getBorderBottom() == (short)1));
243             
244             in.close();
245     }
246     
247     public static void main(String JavaDoc [] args) {
248         System.out
249         .println("Testing org.apache.poi.hssf.usermodel.TestHSSFCell");
250         junit.textui.TestRunner.run(TestHSSFCell.class);
251     }
252
253 }
254
255
Popular Tags