aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/hssf/model/SheetTest.java
blob: fa58834bb2d47e582152d7b829a238da6b5c786e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* ====================================================================
   Copyright 2003-2004   Apache Software Foundation

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.hssf.model;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

import org.apache.poi.hssf.record.ColumnInfoRecord;
import org.apache.poi.hssf.record.MergeCellsRecord;
import org.apache.poi.hssf.record.PageBreakRecord;
import org.apache.poi.hssf.record.RowRecord;
import org.apache.poi.hssf.record.StringRecord;

/**
 * @author Tony Poppleton
 */
public class SheetTest extends TestCase
{
	/**
	 * Constructor for SheetTest.
	 * @param arg0
	 */
	public SheetTest(String arg0)
	{
		super(arg0);
	}
	
	public void testAddMergedRegion()
	{
		Sheet sheet = Sheet.createSheet();
		int regionsToAdd = 4096;
		int startRecords = sheet.getRecords().size();
		
		//simple test that adds a load of regions
		for (int n = 0; n < regionsToAdd; n++)
		{
			int index = sheet.addMergedRegion(0, (short) 0, 1, (short) 1);
			assertTrue("Merged region index expected to be " + n + " got " + index, index == n);
		}
		
		//test all the regions were indeed added 
		assertTrue(sheet.getNumMergedRegions() == regionsToAdd);
		
		//test that the regions were spread out over the appropriate number of records
		int recordsAdded    = sheet.getRecords().size() - startRecords;
		int recordsExpected = regionsToAdd/1027;
		if ((regionsToAdd % 1027) != 0)
			recordsExpected++;
		assertTrue("The " + regionsToAdd + " merged regions should have been spread out over " + recordsExpected + " records, not " + recordsAdded, recordsAdded == recordsExpected);
	}

	public void testRemoveMergedRegion()
	{
		Sheet sheet = Sheet.createSheet();
		int regionsToAdd = 4096;
		
		for (int n = 0; n < regionsToAdd; n++)
			sheet.addMergedRegion(0, (short) 0, 1, (short) 1);
			
		int records = sheet.getRecords().size();
		
		//remove a third from the beginning
		for (int n = 0; n < regionsToAdd/3; n++)
		{
			sheet.removeMergedRegion(0); 
			//assert they have been deleted
			assertTrue("Num of regions should be " + (regionsToAdd - n - 1) + " not " + sheet.getNumMergedRegions(), sheet.getNumMergedRegions() == regionsToAdd - n - 1);
		}
		
		//assert any record removing was done
		int recordsRemoved = (regionsToAdd/3)/1027; //doesn't work for particular values of regionsToAdd
		assertTrue("Expected " + recordsRemoved + " record to be removed from the starting " + records + ".  Currently there are " + sheet.getRecords().size() + " records", records - sheet.getRecords().size() == recordsRemoved);
	}

	/**
	 * Bug: 22922 (Reported by Xuemin Guan)
	 * <p>
	 * Remove mergedregion fails when a sheet loses records after an initial CreateSheet
	 * fills up the records.
	 *
	 */
	public void testMovingMergedRegion() {
		List records = new ArrayList();
		
		MergeCellsRecord merged = new MergeCellsRecord();
		merged.addArea(0, (short)0, 1, (short)2);
		records.add(new RowRecord());
		records.add(new RowRecord());
		records.add(new RowRecord());
		records.add(merged);
		
		Sheet sheet = Sheet.createSheet(records, 0);
		sheet.records.remove(0);
		
		//stub object to throw off list INDEX operations
		sheet.removeMergedRegion(0);
		assertEquals("Should be no more merged regions", 0, sheet.getNumMergedRegions());
	}

	public void testGetMergedRegionAt()
	{
		//TODO
	}

	public void testGetNumMergedRegions()
	{
		//TODO
	}

	public void testGetCellWidth()
	{
		try{
			Sheet sheet = Sheet.createSheet();
			ColumnInfoRecord nci = ( ColumnInfoRecord ) sheet.createColInfo();
	
			//prepare test model
			nci.setFirstColumn((short)5);
			nci.setLastColumn((short)10);
			nci.setColumnWidth((short)100);
			Field f = Sheet.class.getDeclaredField("columnSizes");
			f.setAccessible(true);
			List columnSizes = new ArrayList();
			f.set(sheet,columnSizes);
			columnSizes.add(nci);
			sheet.records.add(1 + sheet.dimsloc, nci);
			sheet.dimsloc++;
	
			assertEquals((short)100,sheet.getColumnWidth((short)5));
			assertEquals((short)100,sheet.getColumnWidth((short)6));
			assertEquals((short)100,sheet.getColumnWidth((short)7));
			assertEquals((short)100,sheet.getColumnWidth((short)8));
			assertEquals((short)100,sheet.getColumnWidth((short)9));
			assertEquals((short)100,sheet.getColumnWidth((short)10));

			sheet.setColumnWidth((short)6,(short)200);

			assertEquals((short)100,sheet.getColumnWidth((short)5));
			assertEquals((short)200,sheet.getColumnWidth((short)6));
			assertEquals((short)100,sheet.getColumnWidth((short)7));
			assertEquals((short)100,sheet.getColumnWidth((short)8));
			assertEquals((short)100,sheet.getColumnWidth((short)9));
			assertEquals((short)100,sheet.getColumnWidth((short)10));
			

		}
		catch(Exception e){e.printStackTrace();fail(e.getMessage());}

	}

	/**
	 * Makes sure all rows registered for this sheet are aggregated, they were being skipped
	 *
	 */
	public void testRowAggregation() {
		List records = new ArrayList();
		RowRecord row = new RowRecord();
		row.setRowNumber(0);		
		records.add(row);
		
		row = new RowRecord();
		row.setRowNumber(1);
		records.add(row);

		records.add(new StringRecord());
		
		row = new RowRecord();
		row.setRowNumber(2);
		records.add(row);
		
		
		Sheet sheet = Sheet.createSheet(records, 0);
		assertNotNull("Row [2] was skipped", sheet.getRow(2));
		
	}
	
	/**
	 * Make sure page break functionality works (in memory)
	 *
	 */
	public void testRowPageBreaks(){
		short colFrom = 0;
		short colTo = 255;
		
		Sheet sheet = Sheet.createSheet();
		sheet.setRowBreak(0, colFrom, colTo);
		
		assertTrue("no row break at 0", sheet.isRowBroken(0));
		assertEquals("1 row break available", 1, sheet.getNumRowBreaks());
		
		sheet.setRowBreak(0, colFrom, colTo);		
		sheet.setRowBreak(0, colFrom, colTo);		

		assertTrue("no row break at 0", sheet.isRowBroken(0));
		assertEquals("1 row break available", 1, sheet.getNumRowBreaks());
		
		sheet.setRowBreak(10, colFrom, colTo);
		sheet.setRowBreak(11, colFrom, colTo);

		assertTrue("no row break at 10", sheet.isRowBroken(10));
		assertTrue("no row break at 11", sheet.isRowBroken(11));
		assertEquals("3 row break available", 3, sheet.getNumRowBreaks());
		
		
		boolean is10 = false;
		boolean is0 = false;
		boolean is11 = false;
		
		Iterator iterator = sheet.getRowBreaks();
		while (iterator.hasNext()) {
			PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
			int main = (int)breakItem.main;
			if (main != 0 && main != 10 && main != 11) fail("Invalid page break");
			if (main == 0) 	is0 = true;
			if (main == 10) is10= true;
			if (main == 11) is11 = true;
		}
		
		assertTrue("one of the breaks didnt make it", is0 && is10 && is11); 
		
		sheet.removeRowBreak(11);
		assertFalse("row should be removed", sheet.isRowBroken(11));
		
		sheet.removeRowBreak(0);
		assertFalse("row should be removed", sheet.isRowBroken(0));
		
		sheet.removeRowBreak(10);
		assertFalse("row should be removed", sheet.isRowBroken(10));
		
		assertEquals("no more breaks", 0, sheet.getNumRowBreaks());
		
		
	}
	
	/**
	 * Make sure column pag breaks works properly (in-memory)
	 *
	 */
	public void testColPageBreaks(){
		short rowFrom = 0;
		short rowTo = (short)65535;
		
		Sheet sheet = Sheet.createSheet();
		sheet.setColumnBreak((short)0, rowFrom, rowTo); 
		
		assertTrue("no col break at 0", sheet.isColumnBroken((short)0));
		assertEquals("1 col break available", 1, sheet.getNumColumnBreaks());		
		
		sheet.setColumnBreak((short)0, rowFrom, rowTo);
		
		assertTrue("no col break at 0", sheet.isColumnBroken((short)0));
		assertEquals("1 col break available", 1, sheet.getNumColumnBreaks());		
		
		sheet.setColumnBreak((short)1, rowFrom, rowTo);
		sheet.setColumnBreak((short)10, rowFrom, rowTo);
		sheet.setColumnBreak((short)15, rowFrom, rowTo);
		
		assertTrue("no col break at 1", sheet.isColumnBroken((short)1));
		assertTrue("no col break at 10", sheet.isColumnBroken((short)10));
		assertTrue("no col break at 15", sheet.isColumnBroken((short)15));
		assertEquals("4 col break available", 4, sheet.getNumColumnBreaks());		

		boolean is10 = false;
		boolean is0 = false;
		boolean is1 = false;
		boolean is15 = false;
		
		Iterator iterator = sheet.getColumnBreaks();
		while (iterator.hasNext()) {
			PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
			int main = (int)breakItem.main;
			if (main != 0 && main != 1 && main != 10 && main != 15) fail("Invalid page break");
			if (main == 0) 	is0 = true;
			if (main == 1) 	is1 = true;
			if (main == 10) is10= true;
			if (main == 15) is15 = true;
		}
		
		assertTrue("one of the breaks didnt make it", is0 && is1 && is10 && is15); 
		
		sheet.removeColumnBreak((short)15);
		assertFalse("column break should not be there", sheet.isColumnBroken((short)15));

		sheet.removeColumnBreak((short)0);
		assertFalse("column break should not be there", sheet.isColumnBroken((short)0));
		
		sheet.removeColumnBreak((short)1);
		assertFalse("column break should not be there", sheet.isColumnBroken((short)1));
		
		sheet.removeColumnBreak((short)10);
		assertFalse("column break should not be there", sheet.isColumnBroken((short)10));
		
		assertEquals("no more breaks", 0, sheet.getNumColumnBreaks());
	}

}