001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.model;
019:
020: import junit.framework.TestCase;
021: import org.apache.poi.hssf.record.*;
022: import org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate;
023: import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate;
024: import org.apache.poi.hssf.record.aggregates.ValueRecordsAggregate;
025:
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: /**
031: * Unit test for the Sheet class.
032: *
033: * @author Glen Stampoultzis (glens at apache.org)
034: */
035: public class TestSheet extends TestCase {
036: public void testCreateSheet() throws Exception {
037: // Check we're adding row and cell aggregates
038: List records = new ArrayList();
039: records.add(new BOFRecord());
040: records.add(new DimensionsRecord());
041: records.add(new EOFRecord());
042: Sheet sheet = Sheet.createSheet(records, 0, 0);
043:
044: int pos = 0;
045: assertTrue(sheet.records.get(pos++) instanceof BOFRecord);
046: assertTrue(sheet.records.get(pos++) instanceof ColumnInfoRecordsAggregate);
047: assertTrue(sheet.records.get(pos++) instanceof DimensionsRecord);
048: assertTrue(sheet.records.get(pos++) instanceof RowRecordsAggregate);
049: assertTrue(sheet.records.get(pos++) instanceof ValueRecordsAggregate);
050: assertTrue(sheet.records.get(pos++) instanceof EOFRecord);
051: }
052:
053: public void testAddMergedRegion() {
054: Sheet sheet = Sheet.createSheet();
055: int regionsToAdd = 4096;
056: int startRecords = sheet.getRecords().size();
057:
058: //simple test that adds a load of regions
059: for (int n = 0; n < regionsToAdd; n++) {
060: int index = sheet.addMergedRegion(0, (short) 0, 1,
061: (short) 1);
062: assertTrue("Merged region index expected to be " + n
063: + " got " + index, index == n);
064: }
065:
066: //test all the regions were indeed added
067: assertTrue(sheet.getNumMergedRegions() == regionsToAdd);
068:
069: //test that the regions were spread out over the appropriate number of records
070: int recordsAdded = sheet.getRecords().size() - startRecords;
071: int recordsExpected = regionsToAdd / 1027;
072: if ((regionsToAdd % 1027) != 0)
073: recordsExpected++;
074: assertTrue("The " + regionsToAdd
075: + " merged regions should have been spread out over "
076: + recordsExpected + " records, not " + recordsAdded,
077: recordsAdded == recordsExpected);
078: }
079:
080: public void testRemoveMergedRegion() {
081: Sheet sheet = Sheet.createSheet();
082: int regionsToAdd = 4096;
083:
084: for (int n = 0; n < regionsToAdd; n++)
085: sheet.addMergedRegion(0, (short) 0, 1, (short) 1);
086:
087: int records = sheet.getRecords().size();
088:
089: //remove a third from the beginning
090: for (int n = 0; n < regionsToAdd / 3; n++) {
091: sheet.removeMergedRegion(0);
092: //assert they have been deleted
093: assertTrue("Num of regions should be "
094: + (regionsToAdd - n - 1) + " not "
095: + sheet.getNumMergedRegions(), sheet
096: .getNumMergedRegions() == regionsToAdd - n - 1);
097: }
098:
099: //assert any record removing was done
100: int recordsRemoved = (regionsToAdd / 3) / 1027; //doesn't work for particular values of regionsToAdd
101: assertTrue("Expected " + recordsRemoved
102: + " record to be removed from the starting " + records
103: + ". Currently there are " + sheet.getRecords().size()
104: + " records",
105: records - sheet.getRecords().size() == recordsRemoved);
106: }
107:
108: /**
109: * Bug: 22922 (Reported by Xuemin Guan)
110: * <p>
111: * Remove mergedregion fails when a sheet loses records after an initial CreateSheet
112: * fills up the records.
113: *
114: */
115: public void testMovingMergedRegion() {
116: List records = new ArrayList();
117:
118: MergeCellsRecord merged = new MergeCellsRecord();
119: merged.addArea(0, (short) 0, 1, (short) 2);
120: records.add(new RowRecord());
121: records.add(new RowRecord());
122: records.add(new RowRecord());
123: records.add(merged);
124:
125: Sheet sheet = Sheet.createSheet(records, 0);
126: sheet.records.remove(0);
127:
128: //stub object to throw off list INDEX operations
129: sheet.removeMergedRegion(0);
130: assertEquals("Should be no more merged regions", 0, sheet
131: .getNumMergedRegions());
132: }
133:
134: public void testGetMergedRegionAt() {
135: //TODO
136: }
137:
138: public void testGetNumMergedRegions() {
139: //TODO
140: }
141:
142: /**
143: * Makes sure all rows registered for this sheet are aggregated, they were being skipped
144: *
145: */
146: public void testRowAggregation() {
147: List records = new ArrayList();
148: RowRecord row = new RowRecord();
149: row.setRowNumber(0);
150: records.add(row);
151:
152: row = new RowRecord();
153: row.setRowNumber(1);
154: records.add(row);
155:
156: records.add(new StringRecord());
157:
158: row = new RowRecord();
159: row.setRowNumber(2);
160: records.add(row);
161:
162: Sheet sheet = Sheet.createSheet(records, 0);
163: assertNotNull("Row [2] was skipped", sheet.getRow(2));
164:
165: }
166:
167: /**
168: * Make sure page break functionality works (in memory)
169: *
170: */
171: public void testRowPageBreaks() {
172: short colFrom = 0;
173: short colTo = 255;
174:
175: Sheet sheet = Sheet.createSheet();
176: sheet.setRowBreak(0, colFrom, colTo);
177:
178: assertTrue("no row break at 0", sheet.isRowBroken(0));
179: assertEquals("1 row break available", 1, sheet
180: .getNumRowBreaks());
181:
182: sheet.setRowBreak(0, colFrom, colTo);
183: sheet.setRowBreak(0, colFrom, colTo);
184:
185: assertTrue("no row break at 0", sheet.isRowBroken(0));
186: assertEquals("1 row break available", 1, sheet
187: .getNumRowBreaks());
188:
189: sheet.setRowBreak(10, colFrom, colTo);
190: sheet.setRowBreak(11, colFrom, colTo);
191:
192: assertTrue("no row break at 10", sheet.isRowBroken(10));
193: assertTrue("no row break at 11", sheet.isRowBroken(11));
194: assertEquals("3 row break available", 3, sheet
195: .getNumRowBreaks());
196:
197: boolean is10 = false;
198: boolean is0 = false;
199: boolean is11 = false;
200:
201: Iterator iterator = sheet.getRowBreaks();
202: while (iterator.hasNext()) {
203: PageBreakRecord.Break breakItem = (PageBreakRecord.Break) iterator
204: .next();
205: int main = (int) breakItem.main;
206: if (main != 0 && main != 10 && main != 11)
207: fail("Invalid page break");
208: if (main == 0)
209: is0 = true;
210: if (main == 10)
211: is10 = true;
212: if (main == 11)
213: is11 = true;
214: }
215:
216: assertTrue("one of the breaks didnt make it", is0 && is10
217: && is11);
218:
219: sheet.removeRowBreak(11);
220: assertFalse("row should be removed", sheet.isRowBroken(11));
221:
222: sheet.removeRowBreak(0);
223: assertFalse("row should be removed", sheet.isRowBroken(0));
224:
225: sheet.removeRowBreak(10);
226: assertFalse("row should be removed", sheet.isRowBroken(10));
227:
228: assertEquals("no more breaks", 0, sheet.getNumRowBreaks());
229:
230: }
231:
232: /**
233: * Make sure column pag breaks works properly (in-memory)
234: *
235: */
236: public void testColPageBreaks() {
237: short rowFrom = 0;
238: short rowTo = (short) 65535;
239:
240: Sheet sheet = Sheet.createSheet();
241: sheet.setColumnBreak((short) 0, rowFrom, rowTo);
242:
243: assertTrue("no col break at 0", sheet.isColumnBroken((short) 0));
244: assertEquals("1 col break available", 1, sheet
245: .getNumColumnBreaks());
246:
247: sheet.setColumnBreak((short) 0, rowFrom, rowTo);
248:
249: assertTrue("no col break at 0", sheet.isColumnBroken((short) 0));
250: assertEquals("1 col break available", 1, sheet
251: .getNumColumnBreaks());
252:
253: sheet.setColumnBreak((short) 1, rowFrom, rowTo);
254: sheet.setColumnBreak((short) 10, rowFrom, rowTo);
255: sheet.setColumnBreak((short) 15, rowFrom, rowTo);
256:
257: assertTrue("no col break at 1", sheet.isColumnBroken((short) 1));
258: assertTrue("no col break at 10", sheet
259: .isColumnBroken((short) 10));
260: assertTrue("no col break at 15", sheet
261: .isColumnBroken((short) 15));
262: assertEquals("4 col break available", 4, sheet
263: .getNumColumnBreaks());
264:
265: boolean is10 = false;
266: boolean is0 = false;
267: boolean is1 = false;
268: boolean is15 = false;
269:
270: Iterator iterator = sheet.getColumnBreaks();
271: while (iterator.hasNext()) {
272: PageBreakRecord.Break breakItem = (PageBreakRecord.Break) iterator
273: .next();
274: int main = (int) breakItem.main;
275: if (main != 0 && main != 1 && main != 10 && main != 15)
276: fail("Invalid page break");
277: if (main == 0)
278: is0 = true;
279: if (main == 1)
280: is1 = true;
281: if (main == 10)
282: is10 = true;
283: if (main == 15)
284: is15 = true;
285: }
286:
287: assertTrue("one of the breaks didnt make it", is0 && is1
288: && is10 && is15);
289:
290: sheet.removeColumnBreak((short) 15);
291: assertFalse("column break should not be there", sheet
292: .isColumnBroken((short) 15));
293:
294: sheet.removeColumnBreak((short) 0);
295: assertFalse("column break should not be there", sheet
296: .isColumnBroken((short) 0));
297:
298: sheet.removeColumnBreak((short) 1);
299: assertFalse("column break should not be there", sheet
300: .isColumnBroken((short) 1));
301:
302: sheet.removeColumnBreak((short) 10);
303: assertFalse("column break should not be there", sheet
304: .isColumnBroken((short) 10));
305:
306: assertEquals("no more breaks", 0, sheet.getNumColumnBreaks());
307: }
308:
309: /**
310: * test newly added method Sheet.getXFIndexForColAt(..)
311: * works as designed.
312: */
313: public void testXFIndexForColumn() {
314: try {
315: final short TEST_IDX = 10;
316: final short DEFAULT_IDX = 0xF; // 15
317: short xfindex = Short.MIN_VALUE;
318: Sheet sheet = Sheet.createSheet();
319:
320: // without ColumnInfoRecord
321: xfindex = sheet.getXFIndexForColAt((short) 0);
322: assertEquals(DEFAULT_IDX, xfindex);
323: xfindex = sheet.getXFIndexForColAt((short) 1);
324: assertEquals(DEFAULT_IDX, xfindex);
325:
326: ColumnInfoRecord nci = (ColumnInfoRecord) sheet
327: .createColInfo();
328: sheet.columns.insertColumn(nci);
329:
330: // single column ColumnInfoRecord
331: nci.setFirstColumn((short) 2);
332: nci.setLastColumn((short) 2);
333: nci.setXFIndex(TEST_IDX);
334: xfindex = sheet.getXFIndexForColAt((short) 0);
335: assertEquals(DEFAULT_IDX, xfindex);
336: xfindex = sheet.getXFIndexForColAt((short) 1);
337: assertEquals(DEFAULT_IDX, xfindex);
338: xfindex = sheet.getXFIndexForColAt((short) 2);
339: assertEquals(TEST_IDX, xfindex);
340: xfindex = sheet.getXFIndexForColAt((short) 3);
341: assertEquals(DEFAULT_IDX, xfindex);
342:
343: // ten column ColumnInfoRecord
344: nci.setFirstColumn((short) 2);
345: nci.setLastColumn((short) 11);
346: nci.setXFIndex(TEST_IDX);
347: xfindex = sheet.getXFIndexForColAt((short) 1);
348: assertEquals(DEFAULT_IDX, xfindex);
349: xfindex = sheet.getXFIndexForColAt((short) 2);
350: assertEquals(TEST_IDX, xfindex);
351: xfindex = sheet.getXFIndexForColAt((short) 6);
352: assertEquals(TEST_IDX, xfindex);
353: xfindex = sheet.getXFIndexForColAt((short) 11);
354: assertEquals(TEST_IDX, xfindex);
355: xfindex = sheet.getXFIndexForColAt((short) 12);
356: assertEquals(DEFAULT_IDX, xfindex);
357:
358: // single column ColumnInfoRecord starting at index 0
359: nci.setFirstColumn((short) 0);
360: nci.setLastColumn((short) 0);
361: nci.setXFIndex(TEST_IDX);
362: xfindex = sheet.getXFIndexForColAt((short) 0);
363: assertEquals(TEST_IDX, xfindex);
364: xfindex = sheet.getXFIndexForColAt((short) 1);
365: assertEquals(DEFAULT_IDX, xfindex);
366:
367: // ten column ColumnInfoRecord starting at index 0
368: nci.setFirstColumn((short) 0);
369: nci.setLastColumn((short) 9);
370: nci.setXFIndex(TEST_IDX);
371: xfindex = sheet.getXFIndexForColAt((short) 0);
372: assertEquals(TEST_IDX, xfindex);
373: xfindex = sheet.getXFIndexForColAt((short) 7);
374: assertEquals(TEST_IDX, xfindex);
375: xfindex = sheet.getXFIndexForColAt((short) 9);
376: assertEquals(TEST_IDX, xfindex);
377: xfindex = sheet.getXFIndexForColAt((short) 10);
378: assertEquals(DEFAULT_IDX, xfindex);
379: } catch (Exception e) {
380: e.printStackTrace();
381: fail(e.getMessage());
382: }
383: }
384:
385: }
|