1 /* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================== */
18 package org
.apache
.poi
.hssf
.model
;
20 import java
.lang
.reflect
.Field
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Iterator
;
23 import java
.util
.List
;
25 import junit
.framework
.TestCase
;
27 import org
.apache
.poi
.hssf
.record
.ColumnInfoRecord
;
28 import org
.apache
.poi
.hssf
.record
.MergeCellsRecord
;
29 import org
.apache
.poi
.hssf
.record
.PageBreakRecord
;
30 import org
.apache
.poi
.hssf
.record
.RowRecord
;
31 import org
.apache
.poi
.hssf
.record
.StringRecord
;
34 * @author Tony Poppleton
36 public class TestSheetAdditional
extends TestCase
39 * Constructor for SheetTest.
42 public TestSheetAdditional(String arg0
)
47 public void testAddMergedRegion()
49 Sheet sheet
= Sheet
.createSheet();
50 int regionsToAdd
= 4096;
51 int startRecords
= sheet
.getRecords().size();
53 //simple test that adds a load of regions
54 for (int n
= 0; n
< regionsToAdd
; n
++)
56 int index
= sheet
.addMergedRegion(0, (short) 0, 1, (short) 1);
57 assertTrue("Merged region index expected to be " + n
+ " got " + index
, index
== n
);
60 //test all the regions were indeed added
61 assertTrue(sheet
.getNumMergedRegions() == regionsToAdd
);
63 //test that the regions were spread out over the appropriate number of records
64 int recordsAdded
= sheet
.getRecords().size() - startRecords
;
65 int recordsExpected
= regionsToAdd
/1027;
66 if ((regionsToAdd
% 1027) != 0)
68 assertTrue("The " + regionsToAdd
+ " merged regions should have been spread out over " + recordsExpected
+ " records, not " + recordsAdded
, recordsAdded
== recordsExpected
);
70 // Check we can't add one with invalud date
72 sheet
.addMergedRegion(10, (short)10, 9, (short)12);
74 } catch(IllegalArgumentException e
) {}
76 sheet
.addMergedRegion(10, (short)10, 12, (short)9);
78 } catch(IllegalArgumentException e
) {}
81 public void testRemoveMergedRegion()
83 Sheet sheet
= Sheet
.createSheet();
84 int regionsToAdd
= 4096;
86 for (int n
= 0; n
< regionsToAdd
; n
++)
87 sheet
.addMergedRegion(0, (short) 0, 1, (short) 1);
89 int records
= sheet
.getRecords().size();
91 //remove a third from the beginning
92 for (int n
= 0; n
< regionsToAdd
/3; n
++)
94 sheet
.removeMergedRegion(0);
95 //assert they have been deleted
96 assertTrue("Num of regions should be " + (regionsToAdd
- n
- 1) + " not " + sheet
.getNumMergedRegions(), sheet
.getNumMergedRegions() == regionsToAdd
- n
- 1);
99 //assert any record removing was done
100 int recordsRemoved
= (regionsToAdd
/3)/1027; //doesn't work for particular values of regionsToAdd
101 assertTrue("Expected " + recordsRemoved
+ " record to be removed from the starting " + records
+ ". Currently there are " + sheet
.getRecords().size() + " records", records
- sheet
.getRecords().size() == recordsRemoved
);
105 * Bug: 22922 (Reported by Xuemin Guan)
107 * Remove mergedregion fails when a sheet loses records after an initial CreateSheet
108 * fills up the records.
111 public void testMovingMergedRegion() {
112 List records
= new ArrayList();
114 MergeCellsRecord merged
= new MergeCellsRecord();
115 merged
.addArea(0, (short)0, 1, (short)2);
116 records
.add(new RowRecord());
117 records
.add(new RowRecord());
118 records
.add(new RowRecord());
121 Sheet sheet
= Sheet
.createSheet(records
, 0);
122 sheet
.records
.remove(0);
124 //stub object to throw off list INDEX operations
125 sheet
.removeMergedRegion(0);
126 assertEquals("Should be no more merged regions", 0, sheet
.getNumMergedRegions());
129 public void testGetMergedRegionAt()
134 public void testGetNumMergedRegions()
139 public void DISBALEDtestGetCellWidth() throws Exception
141 Sheet sheet
= Sheet
.createSheet();
142 ColumnInfoRecord nci
= ( ColumnInfoRecord
) sheet
.createColInfo();
144 // Prepare test model
145 nci
.setFirstColumn((short)5);
146 nci
.setLastColumn((short)10);
147 nci
.setColumnWidth((short)100);
150 f
= Sheet
.class.getDeclaredField("columnSizes");
151 f
.setAccessible(true);
152 List columnSizes
= new ArrayList();
153 f
.set(sheet
,columnSizes
);
154 columnSizes
.add(nci
);
155 sheet
.records
.add(1 + sheet
.dimsloc
, nci
);
158 assertEquals((short)100,sheet
.getColumnWidth((short)5));
159 assertEquals((short)100,sheet
.getColumnWidth((short)6));
160 assertEquals((short)100,sheet
.getColumnWidth((short)7));
161 assertEquals((short)100,sheet
.getColumnWidth((short)8));
162 assertEquals((short)100,sheet
.getColumnWidth((short)9));
163 assertEquals((short)100,sheet
.getColumnWidth((short)10));
165 sheet
.setColumnWidth((short)6,(short)200);
167 assertEquals((short)100,sheet
.getColumnWidth((short)5));
168 assertEquals((short)200,sheet
.getColumnWidth((short)6));
169 assertEquals((short)100,sheet
.getColumnWidth((short)7));
170 assertEquals((short)100,sheet
.getColumnWidth((short)8));
171 assertEquals((short)100,sheet
.getColumnWidth((short)9));
172 assertEquals((short)100,sheet
.getColumnWidth((short)10));
176 * Makes sure all rows registered for this sheet are aggregated, they were being skipped
179 public void testRowAggregation() {
180 List records
= new ArrayList();
181 RowRecord row
= new RowRecord();
185 row
= new RowRecord();
189 records
.add(new StringRecord());
191 row
= new RowRecord();
196 Sheet sheet
= Sheet
.createSheet(records
, 0);
197 assertNotNull("Row [2] was skipped", sheet
.getRow(2));
202 * Make sure page break functionality works (in memory)
205 public void testRowPageBreaks(){
209 Sheet sheet
= Sheet
.createSheet();
210 sheet
.setRowBreak(0, colFrom
, colTo
);
212 assertTrue("no row break at 0", sheet
.isRowBroken(0));
213 assertEquals("1 row break available", 1, sheet
.getNumRowBreaks());
215 sheet
.setRowBreak(0, colFrom
, colTo
);
216 sheet
.setRowBreak(0, colFrom
, colTo
);
218 assertTrue("no row break at 0", sheet
.isRowBroken(0));
219 assertEquals("1 row break available", 1, sheet
.getNumRowBreaks());
221 sheet
.setRowBreak(10, colFrom
, colTo
);
222 sheet
.setRowBreak(11, colFrom
, colTo
);
224 assertTrue("no row break at 10", sheet
.isRowBroken(10));
225 assertTrue("no row break at 11", sheet
.isRowBroken(11));
226 assertEquals("3 row break available", 3, sheet
.getNumRowBreaks());
229 boolean is10
= false;
231 boolean is11
= false;
233 Iterator iterator
= sheet
.getRowBreaks();
234 while (iterator
.hasNext()) {
235 PageBreakRecord
.Break breakItem
= (PageBreakRecord
.Break
)iterator
.next();
236 int main
= (int)breakItem
.main
;
237 if (main
!= 0 && main
!= 10 && main
!= 11) fail("Invalid page break");
238 if (main
== 0) is0
= true;
239 if (main
== 10) is10
= true;
240 if (main
== 11) is11
= true;
243 assertTrue("one of the breaks didnt make it", is0
&& is10
&& is11
);
245 sheet
.removeRowBreak(11);
246 assertFalse("row should be removed", sheet
.isRowBroken(11));
248 sheet
.removeRowBreak(0);
249 assertFalse("row should be removed", sheet
.isRowBroken(0));
251 sheet
.removeRowBreak(10);
252 assertFalse("row should be removed", sheet
.isRowBroken(10));
254 assertEquals("no more breaks", 0, sheet
.getNumRowBreaks());
260 * Make sure column pag breaks works properly (in-memory)
263 public void testColPageBreaks(){
265 short rowTo
= (short)65535;
267 Sheet sheet
= Sheet
.createSheet();
268 sheet
.setColumnBreak((short)0, rowFrom
, rowTo
);
270 assertTrue("no col break at 0", sheet
.isColumnBroken((short)0));
271 assertEquals("1 col break available", 1, sheet
.getNumColumnBreaks());
273 sheet
.setColumnBreak((short)0, rowFrom
, rowTo
);
275 assertTrue("no col break at 0", sheet
.isColumnBroken((short)0));
276 assertEquals("1 col break available", 1, sheet
.getNumColumnBreaks());
278 sheet
.setColumnBreak((short)1, rowFrom
, rowTo
);
279 sheet
.setColumnBreak((short)10, rowFrom
, rowTo
);
280 sheet
.setColumnBreak((short)15, rowFrom
, rowTo
);
282 assertTrue("no col break at 1", sheet
.isColumnBroken((short)1));
283 assertTrue("no col break at 10", sheet
.isColumnBroken((short)10));
284 assertTrue("no col break at 15", sheet
.isColumnBroken((short)15));
285 assertEquals("4 col break available", 4, sheet
.getNumColumnBreaks());
287 boolean is10
= false;
290 boolean is15
= false;
292 Iterator iterator
= sheet
.getColumnBreaks();
293 while (iterator
.hasNext()) {
294 PageBreakRecord
.Break breakItem
= (PageBreakRecord
.Break
)iterator
.next();
295 int main
= (int)breakItem
.main
;
296 if (main
!= 0 && main
!= 1 && main
!= 10 && main
!= 15) fail("Invalid page break");
297 if (main
== 0) is0
= true;
298 if (main
== 1) is1
= true;
299 if (main
== 10) is10
= true;
300 if (main
== 15) is15
= true;
303 assertTrue("one of the breaks didnt make it", is0
&& is1
&& is10
&& is15
);
305 sheet
.removeColumnBreak((short)15);
306 assertFalse("column break should not be there", sheet
.isColumnBroken((short)15));
308 sheet
.removeColumnBreak((short)0);
309 assertFalse("column break should not be there", sheet
.isColumnBroken((short)0));
311 sheet
.removeColumnBreak((short)1);
312 assertFalse("column break should not be there", sheet
.isColumnBroken((short)1));
314 sheet
.removeColumnBreak((short)10);
315 assertFalse("column break should not be there", sheet
.isColumnBroken((short)10));
317 assertEquals("no more breaks", 0, sheet
.getNumColumnBreaks());