KickJava   Java API By Example, From Geeks To Geeks.

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


1
2
3 /* ====================================================================
4
5    Copyright 2002-2004 Apache Software Foundation
6
7
8
9    Licensed under the Apache License, Version 2.0 (the "License");
10
11    you may not use this file except in compliance with the License.
12
13    You may obtain a copy of the License at
14
15
16
17        http://www.apache.org/licenses/LICENSE-2.0
18
19
20
21    Unless required by applicable law or agreed to in writing, software
22
23    distributed under the License is distributed on an "AS IS" BASIS,
24
25    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26
27    See the License for the specific language governing permissions and
28
29    limitations under the License.
30
31 ==================================================================== */

32
33         
34
35
36
37 package org.apache.poi.hssf.usermodel;
38
39
40
41 import junit.framework.TestCase;
42
43
44
45 import java.io.File JavaDoc;
46
47 import java.io.FileInputStream JavaDoc;
48
49 import java.io.FileOutputStream JavaDoc;
50
51
52
53 import org.apache.poi.util.TempFile;
54
55
56
57 /**
58
59  * Test HSSFRow is okay.
60
61  *
62
63  * @author Glen Stampoultzis (glens at apache.org)
64
65  */

66
67 public class TestHSSFRow
68
69         extends TestCase
70
71 {
72
73     public TestHSSFRow(String JavaDoc s)
74
75     {
76
77         super(s);
78
79     }
80
81
82
83     public void testLastAndFirstColumns()
84
85             throws Exception JavaDoc
86
87     {
88
89         HSSFWorkbook workbook = new HSSFWorkbook();
90
91         HSSFSheet sheet = workbook.createSheet();
92
93         HSSFRow row = sheet.createRow((short) 0);
94
95         assertEquals(-1, row.getFirstCellNum());
96
97         assertEquals(-1, row.getLastCellNum());
98
99
100
101         row.createCell((short) 2);
102
103         assertEquals(2, row.getFirstCellNum());
104
105         assertEquals(2, row.getLastCellNum());
106
107
108
109         row.createCell((short) 1);
110
111         assertEquals(1, row.getFirstCellNum());
112
113         assertEquals(2, row.getLastCellNum());
114
115
116
117     }
118
119
120
121     public void testRemoveCell()
122
123             throws Exception JavaDoc
124
125     {
126
127         HSSFWorkbook workbook = new HSSFWorkbook();
128
129         HSSFSheet sheet = workbook.createSheet();
130
131         HSSFRow row = sheet.createRow((short) 0);
132
133         assertEquals(-1, row.getLastCellNum());
134
135         assertEquals(-1, row.getFirstCellNum());
136
137         row.createCell((short) 1);
138
139         assertEquals(1, row.getLastCellNum());
140
141         assertEquals(1, row.getFirstCellNum());
142
143         row.createCell((short) 3);
144
145         assertEquals(3, row.getLastCellNum());
146
147         assertEquals(1, row.getFirstCellNum());
148
149         row.removeCell(row.getCell((short) 3));
150
151         assertEquals(1, row.getLastCellNum());
152
153         assertEquals(1, row.getFirstCellNum());
154
155         row.removeCell(row.getCell((short) 1));
156
157         assertEquals(-1, row.getLastCellNum());
158
159         assertEquals(-1, row.getFirstCellNum());
160
161
162
163         // check the row record actually writes it out as 0's
164

165         byte[] data = new byte[100];
166
167         row.getRowRecord().serialize(0, data);
168
169         assertEquals(0, data[6]);
170
171         assertEquals(0, data[8]);
172
173
174
175         File JavaDoc file = TempFile.createTempFile("XXX", "XLS");
176
177         FileOutputStream JavaDoc stream = new FileOutputStream JavaDoc(file);
178
179         workbook.write(stream);
180
181         stream.close();
182
183         FileInputStream JavaDoc inputStream = new FileInputStream JavaDoc(file);
184
185         workbook = new HSSFWorkbook(inputStream);
186
187         sheet = workbook.getSheetAt(0);
188
189         stream.close();
190
191         file.delete();
192
193         assertEquals(-1, sheet.getRow((short) 0).getLastCellNum());
194
195         assertEquals(-1, sheet.getRow((short) 0).getFirstCellNum());
196
197
198
199
200
201     }
202
203     
204
205     public void testRowBounds()
206
207             throws Exception JavaDoc
208
209     {
210
211       HSSFWorkbook workbook = new HSSFWorkbook();
212
213       HSSFSheet sheet = workbook.createSheet();
214
215       //Test low row bound
216

217       HSSFRow row = sheet.createRow( (short) 0);
218
219       //Test low row bound exception
220

221       boolean caughtException = false;
222
223       try {
224
225         row = sheet.createRow(-1);
226
227       } catch (IndexOutOfBoundsException JavaDoc ex) {
228
229         caughtException = true;
230
231       }
232
233       assertTrue(caughtException);
234
235       //Test high row bound
236

237       row = sheet.createRow(65535);
238
239       //Test high row bound exception
240

241       caughtException = false;
242
243       try {
244
245         row = sheet.createRow(65536);
246
247       } catch (IndexOutOfBoundsException JavaDoc ex) {
248
249         caughtException = true;
250
251       }
252
253       assertTrue(caughtException);
254
255     }
256
257     
258
259 }
260
261
Popular Tags