KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > model > SheetTest


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

16
17 package org.apache.poi.hssf.model;
18
19 import java.lang.reflect.Field JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.apache.poi.hssf.record.ColumnInfoRecord;
27 import org.apache.poi.hssf.record.MergeCellsRecord;
28 import org.apache.poi.hssf.record.PageBreakRecord;
29 import org.apache.poi.hssf.record.RowRecord;
30 import org.apache.poi.hssf.record.StringRecord;
31
32 /**
33  * @author Tony Poppleton
34  */

35 public class SheetTest extends TestCase
36 {
37     /**
38      * Constructor for SheetTest.
39      * @param arg0
40      */

41     public SheetTest(String JavaDoc arg0)
42     {
43         super(arg0);
44     }
45     
46     public void testAddMergedRegion()
47     {
48         Sheet sheet = Sheet.createSheet();
49         int regionsToAdd = 4096;
50         int startRecords = sheet.getRecords().size();
51         
52         //simple test that adds a load of regions
53
for (int n = 0; n < regionsToAdd; n++)
54         {
55             int index = sheet.addMergedRegion(0, (short) 0, 1, (short) 1);
56             assertTrue("Merged region index expected to be " + n + " got " + index, index == n);
57         }
58         
59         //test all the regions were indeed added
60
assertTrue(sheet.getNumMergedRegions() == regionsToAdd);
61         
62         //test that the regions were spread out over the appropriate number of records
63
int recordsAdded = sheet.getRecords().size() - startRecords;
64         int recordsExpected = regionsToAdd/1027;
65         if ((regionsToAdd % 1027) != 0)
66             recordsExpected++;
67         assertTrue("The " + regionsToAdd + " merged regions should have been spread out over " + recordsExpected + " records, not " + recordsAdded, recordsAdded == recordsExpected);
68     }
69
70     public void testRemoveMergedRegion()
71     {
72         Sheet sheet = Sheet.createSheet();
73         int regionsToAdd = 4096;
74         
75         for (int n = 0; n < regionsToAdd; n++)
76             sheet.addMergedRegion(0, (short) 0, 1, (short) 1);
77             
78         int records = sheet.getRecords().size();
79         
80         //remove a third from the beginning
81
for (int n = 0; n < regionsToAdd/3; n++)
82         {
83             sheet.removeMergedRegion(0);
84             //assert they have been deleted
85
assertTrue("Num of regions should be " + (regionsToAdd - n - 1) + " not " + sheet.getNumMergedRegions(), sheet.getNumMergedRegions() == regionsToAdd - n - 1);
86         }
87         
88         //assert any record removing was done
89
int recordsRemoved = (regionsToAdd/3)/1027; //doesn't work for particular values of regionsToAdd
90
assertTrue("Expected " + recordsRemoved + " record to be removed from the starting " + records + ". Currently there are " + sheet.getRecords().size() + " records", records - sheet.getRecords().size() == recordsRemoved);
91     }
92
93     /**
94      * Bug: 22922 (Reported by Xuemin Guan)
95      * <p>
96      * Remove mergedregion fails when a sheet loses records after an initial CreateSheet
97      * fills up the records.
98      *
99      */

100     public void testMovingMergedRegion() {
101         List JavaDoc records = new ArrayList JavaDoc();
102         
103         MergeCellsRecord merged = new MergeCellsRecord();
104         merged.addArea(0, (short)0, 1, (short)2);
105         records.add(new RowRecord());
106         records.add(new RowRecord());
107         records.add(new RowRecord());
108         records.add(merged);
109         
110         Sheet sheet = Sheet.createSheet(records, 0);
111         sheet.records.remove(0);
112         
113         //stub object to throw off list INDEX operations
114
sheet.removeMergedRegion(0);
115         assertEquals("Should be no more merged regions", 0, sheet.getNumMergedRegions());
116     }
117
118     public void testGetMergedRegionAt()
119     {
120         //TODO
121
}
122
123     public void testGetNumMergedRegions()
124     {
125         //TODO
126
}
127
128     public void testGetCellWidth()
129     {
130         try{
131             Sheet sheet = Sheet.createSheet();
132             ColumnInfoRecord nci = ( ColumnInfoRecord ) sheet.createColInfo();
133     
134             //prepare test model
135
nci.setFirstColumn((short)5);
136             nci.setLastColumn((short)10);
137             nci.setColumnWidth((short)100);
138             Field JavaDoc f = Sheet.class.getDeclaredField("columnSizes");
139             f.setAccessible(true);
140             List JavaDoc columnSizes = new ArrayList JavaDoc();
141             f.set(sheet,columnSizes);
142             columnSizes.add(nci);
143             sheet.records.add(1 + sheet.dimsloc, nci);
144             sheet.dimsloc++;
145     
146             assertEquals((short)100,sheet.getColumnWidth((short)5));
147             assertEquals((short)100,sheet.getColumnWidth((short)6));
148             assertEquals((short)100,sheet.getColumnWidth((short)7));
149             assertEquals((short)100,sheet.getColumnWidth((short)8));
150             assertEquals((short)100,sheet.getColumnWidth((short)9));
151             assertEquals((short)100,sheet.getColumnWidth((short)10));
152
153             sheet.setColumnWidth((short)6,(short)200);
154
155             assertEquals((short)100,sheet.getColumnWidth((short)5));
156             assertEquals((short)200,sheet.getColumnWidth((short)6));
157             assertEquals((short)100,sheet.getColumnWidth((short)7));
158             assertEquals((short)100,sheet.getColumnWidth((short)8));
159             assertEquals((short)100,sheet.getColumnWidth((short)9));
160             assertEquals((short)100,sheet.getColumnWidth((short)10));
161             
162
163         }
164         catch(Exception JavaDoc e){e.printStackTrace();fail(e.getMessage());}
165
166     }
167
168     /**
169      * Makes sure all rows registered for this sheet are aggregated, they were being skipped
170      *
171      */

172     public void testRowAggregation() {
173         List JavaDoc records = new ArrayList JavaDoc();
174         RowRecord row = new RowRecord();
175         row.setRowNumber(0);
176         records.add(row);
177         
178         row = new RowRecord();
179         row.setRowNumber(1);
180         records.add(row);
181
182         records.add(new StringRecord());
183         
184         row = new RowRecord();
185         row.setRowNumber(2);
186         records.add(row);
187         
188         
189         Sheet sheet = Sheet.createSheet(records, 0);
190         assertNotNull("Row [2] was skipped", sheet.getRow(2));
191         
192     }
193     
194     /**
195      * Make sure page break functionality works (in memory)
196      *
197      */

198     public void testRowPageBreaks(){
199         short colFrom = 0;
200         short colTo = 255;
201         
202         Sheet sheet = Sheet.createSheet();
203         sheet.setRowBreak(0, colFrom, colTo);
204         
205         assertTrue("no row break at 0", sheet.isRowBroken(0));
206         assertEquals("1 row break available", 1, sheet.getNumRowBreaks());
207         
208         sheet.setRowBreak(0, colFrom, colTo);
209         sheet.setRowBreak(0, colFrom, colTo);
210
211         assertTrue("no row break at 0", sheet.isRowBroken(0));
212         assertEquals("1 row break available", 1, sheet.getNumRowBreaks());
213         
214         sheet.setRowBreak(10, colFrom, colTo);
215         sheet.setRowBreak(11, colFrom, colTo);
216
217         assertTrue("no row break at 10", sheet.isRowBroken(10));
218         assertTrue("no row break at 11", sheet.isRowBroken(11));
219         assertEquals("3 row break available", 3, sheet.getNumRowBreaks());
220         
221         
222         boolean is10 = false;
223         boolean is0 = false;
224         boolean is11 = false;
225         
226         Iterator JavaDoc iterator = sheet.getRowBreaks();
227         while (iterator.hasNext()) {
228             PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
229             int main = (int)breakItem.main;
230             if (main != 0 && main != 10 && main != 11) fail("Invalid page break");
231             if (main == 0) is0 = true;
232             if (main == 10) is10= true;
233             if (main == 11) is11 = true;
234         }
235         
236         assertTrue("one of the breaks didnt make it", is0 && is10 && is11);
237         
238         sheet.removeRowBreak(11);
239         assertFalse("row should be removed", sheet.isRowBroken(11));
240         
241         sheet.removeRowBreak(0);
242         assertFalse("row should be removed", sheet.isRowBroken(0));
243         
244         sheet.removeRowBreak(10);
245         assertFalse("row should be removed", sheet.isRowBroken(10));
246         
247         assertEquals("no more breaks", 0, sheet.getNumRowBreaks());
248         
249         
250     }
251     
252     /**
253      * Make sure column pag breaks works properly (in-memory)
254      *
255      */

256     public void testColPageBreaks(){
257         short rowFrom = 0;
258         short rowTo = (short)65535;
259         
260         Sheet sheet = Sheet.createSheet();
261         sheet.setColumnBreak((short)0, rowFrom, rowTo);
262         
263         assertTrue("no col break at 0", sheet.isColumnBroken((short)0));
264         assertEquals("1 col break available", 1, sheet.getNumColumnBreaks());
265         
266         sheet.setColumnBreak((short)0, rowFrom, rowTo);
267         
268         assertTrue("no col break at 0", sheet.isColumnBroken((short)0));
269         assertEquals("1 col break available", 1, sheet.getNumColumnBreaks());
270         
271         sheet.setColumnBreak((short)1, rowFrom, rowTo);
272         sheet.setColumnBreak((short)10, rowFrom, rowTo);
273         sheet.setColumnBreak((short)15, rowFrom, rowTo);
274         
275         assertTrue("no col break at 1", sheet.isColumnBroken((short)1));
276         assertTrue("no col break at 10", sheet.isColumnBroken((short)10));
277         assertTrue("no col break at 15", sheet.isColumnBroken((short)15));
278         assertEquals("4 col break available", 4, sheet.getNumColumnBreaks());
279
280         boolean is10 = false;
281         boolean is0 = false;
282         boolean is1 = false;
283         boolean is15 = false;
284         
285         Iterator JavaDoc iterator = sheet.getColumnBreaks();
286         while (iterator.hasNext()) {
287             PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
288             int main = (int)breakItem.main;
289             if (main != 0 && main != 1 && main != 10 && main != 15) fail("Invalid page break");
290             if (main == 0) is0 = true;
291             if (main == 1) is1 = true;
292             if (main == 10) is10= true;
293             if (main == 15) is15 = true;
294         }
295         
296         assertTrue("one of the breaks didnt make it", is0 && is1 && is10 && is15);
297         
298         sheet.removeColumnBreak((short)15);
299         assertFalse("column break should not be there", sheet.isColumnBroken((short)15));
300
301         sheet.removeColumnBreak((short)0);
302         assertFalse("column break should not be there", sheet.isColumnBroken((short)0));
303         
304         sheet.removeColumnBreak((short)1);
305         assertFalse("column break should not be there", sheet.isColumnBroken((short)1));
306         
307         sheet.removeColumnBreak((short)10);
308         assertFalse("column break should not be there", sheet.isColumnBroken((short)10));
309         
310         assertEquals("no more breaks", 0, sheet.getNumColumnBreaks());
311     }
312
313 }
314
315
316
317
Popular Tags