KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
20  * TestCellStyle.java
21  *
22  * Created on December 11, 2001, 5:51 PM
23  */

24 package org.apache.poi.hssf.usermodel;
25
26 import java.io.*;
27
28 import java.util.*;
29
30 import junit.framework.*;
31 import org.apache.poi.util.TempFile;
32
33 /**
34  * Class to test cell styling functionality
35  *
36  * @author Andrew C. Oliver
37  */

38
39 public class TestCellStyle
40     extends TestCase
41 {
42
43     /** Creates a new instance of TestCellStyle */
44
45     public TestCellStyle(String JavaDoc name)
46     {
47         super(name);
48     }
49
50     /**
51      * TEST NAME: Test Write Sheet Font <P>
52      * OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with fonts.<P>
53      * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects
54      * Last row, first row is tested against the correct values (99,0).<P>
55      * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good.
56      * HSSFSheet last row or first row is incorrect. <P>
57      *
58      */

59
60     public void testWriteSheetFont()
61         throws IOException
62     {
63         File file = TempFile.createTempFile("testWriteSheetFont",
64                                                     ".xls");
65         FileOutputStream out = new FileOutputStream(file);
66         HSSFWorkbook wb = new HSSFWorkbook();
67         HSSFSheet s = wb.createSheet();
68         HSSFRow r = null;
69         HSSFCell c = null;
70         HSSFFont fnt = wb.createFont();
71         HSSFCellStyle cs = wb.createCellStyle();
72
73         fnt.setColor(HSSFFont.COLOR_RED);
74         fnt.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
75         cs.setFont(fnt);
76         for (short rownum = ( short ) 0; rownum < 100; rownum++)
77         {
78             r = s.createRow(rownum);
79
80             // r.setRowNum(( short ) rownum);
81
for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2)
82             {
83                 c = r.createCell(cellnum);
84                 c.setCellValue(rownum * 10000 + cellnum
85                                + ((( double ) rownum / 1000)
86                                   + (( double ) cellnum / 10000)));
87                 c = r.createCell(( short ) (cellnum + 1));
88                 c.setCellValue("TEST");
89                 c.setCellStyle(cs);
90             }
91         }
92         wb.write(out);
93         out.close();
94         SanityChecker sanityChecker = new SanityChecker();
95         sanityChecker.checkHSSFWorkbook(wb);
96         assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
97         assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
98
99         // assert((s.getLastRowNum() == 99));
100
}
101
102     /**
103      * Tests that is creating a file with a date or an calendar works correctly.
104      */

105     public void testDataStyle()
106             throws Exception JavaDoc
107     {
108         File file = TempFile.createTempFile("testWriteSheetStyleDate",
109                                                     ".xls");
110         FileOutputStream out = new FileOutputStream(file);
111         HSSFWorkbook wb = new HSSFWorkbook();
112         HSSFSheet s = wb.createSheet();
113         HSSFCellStyle cs = wb.createCellStyle();
114         HSSFRow row = s.createRow((short)0);
115
116         // with Date:
117
HSSFCell cell = row.createCell((short)1);
118         cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
119         cell.setCellStyle(cs);
120         cell.setCellValue(new Date());
121
122         // with Calendar:
123
cell = row.createCell((short)2);
124         cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
125         cell.setCellStyle(cs);
126         Calendar cal = Calendar.getInstance();
127         cal.setTime(new Date());
128         cell.setCellValue(cal);
129
130         wb.write(out);
131         out.close();
132         SanityChecker sanityChecker = new SanityChecker();
133         sanityChecker.checkHSSFWorkbook(wb);
134
135         assertEquals("LAST ROW ", 0, s.getLastRowNum());
136         assertEquals("FIRST ROW ", 0, s.getFirstRowNum());
137
138     }
139
140     /**
141      * TEST NAME: Test Write Sheet Style <P>
142      * OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with colors
143      * and borders.<P>
144      * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects
145      * Last row, first row is tested against the correct values (99,0).<P>
146      * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good.
147      * HSSFSheet last row or first row is incorrect. <P>
148      *
149      */

150
151     public void testWriteSheetStyle()
152         throws IOException
153     {
154         File file = TempFile.createTempFile("testWriteSheetStyle",
155                                                     ".xls");
156         FileOutputStream out = new FileOutputStream(file);
157         HSSFWorkbook wb = new HSSFWorkbook();
158         HSSFSheet s = wb.createSheet();
159         HSSFRow r = null;
160         HSSFCell c = null;
161         HSSFFont fnt = wb.createFont();
162         HSSFCellStyle cs = wb.createCellStyle();
163         HSSFCellStyle cs2 = wb.createCellStyle();
164
165         cs.setBorderBottom(HSSFCellStyle.BORDER_THIN);
166         cs.setBorderLeft(HSSFCellStyle.BORDER_THIN);
167         cs.setBorderRight(HSSFCellStyle.BORDER_THIN);
168         cs.setBorderTop(HSSFCellStyle.BORDER_THIN);
169         cs.setFillForegroundColor(( short ) 0xA);
170         cs.setFillPattern(( short ) 1);
171         fnt.setColor(( short ) 0xf);
172         fnt.setItalic(true);
173         cs2.setFillForegroundColor(( short ) 0x0);
174         cs2.setFillPattern(( short ) 1);
175         cs2.setFont(fnt);
176         for (short rownum = ( short ) 0; rownum < 100; rownum++)
177         {
178             r = s.createRow(rownum);
179
180             // r.setRowNum(( short ) rownum);
181
for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2)
182             {
183                 c = r.createCell(cellnum);
184                 c.setCellValue(rownum * 10000 + cellnum
185                                + ((( double ) rownum / 1000)
186                                   + (( double ) cellnum / 10000)));
187                 c.setCellStyle(cs);
188                 c = r.createCell(( short ) (cellnum + 1));
189                 c.setCellValue("TEST");
190                 c.setCellStyle(cs2);
191             }
192         }
193         wb.write(out);
194         out.close();
195         SanityChecker sanityChecker = new SanityChecker();
196         sanityChecker.checkHSSFWorkbook(wb);
197         assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
198         assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
199
200         // assert((s.getLastRowNum() == 99));
201
}
202
203     public static void main(String JavaDoc [] ignored_args)
204     {
205         System.out
206             .println("Testing org.apache.poi.hssf.usermodel.HSSFCellStyle");
207         junit.textui.TestRunner.run(TestCellStyle.class);
208     }
209 }
210
Popular Tags