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 java.lang.reflect.Field;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import junit.framework.TestCase;
026:
027: import org.apache.poi.hssf.record.ColumnInfoRecord;
028: import org.apache.poi.hssf.record.MergeCellsRecord;
029: import org.apache.poi.hssf.record.PageBreakRecord;
030: import org.apache.poi.hssf.record.RowRecord;
031: import org.apache.poi.hssf.record.StringRecord;
032:
033: /**
034: * @author Tony Poppleton
035: */
036: public class TestSheetAdditional extends TestCase {
037: /**
038: * Constructor for SheetTest.
039: * @param arg0
040: */
041: public TestSheetAdditional(String arg0) {
042: super (arg0);
043: }
044:
045: public void testAddMergedRegion() {
046: Sheet sheet = Sheet.createSheet();
047: int regionsToAdd = 4096;
048: int startRecords = sheet.getRecords().size();
049:
050: //simple test that adds a load of regions
051: for (int n = 0; n < regionsToAdd; n++) {
052: int index = sheet.addMergedRegion(0, (short) 0, 1,
053: (short) 1);
054: assertTrue("Merged region index expected to be " + n
055: + " got " + index, index == n);
056: }
057:
058: //test all the regions were indeed added
059: assertTrue(sheet.getNumMergedRegions() == regionsToAdd);
060:
061: //test that the regions were spread out over the appropriate number of records
062: int recordsAdded = sheet.getRecords().size() - startRecords;
063: int recordsExpected = regionsToAdd / 1027;
064: if ((regionsToAdd % 1027) != 0)
065: recordsExpected++;
066: assertTrue("The " + regionsToAdd
067: + " merged regions should have been spread out over "
068: + recordsExpected + " records, not " + recordsAdded,
069: recordsAdded == recordsExpected);
070:
071: // Check we can't add one with invalud date
072: try {
073: sheet.addMergedRegion(10, (short) 10, 9, (short) 12);
074: fail();
075: } catch (IllegalArgumentException e) {
076: }
077: try {
078: sheet.addMergedRegion(10, (short) 10, 12, (short) 9);
079: fail();
080: } catch (IllegalArgumentException e) {
081: }
082: }
083:
084: public void testRemoveMergedRegion() {
085: Sheet sheet = Sheet.createSheet();
086: int regionsToAdd = 4096;
087:
088: for (int n = 0; n < regionsToAdd; n++)
089: sheet.addMergedRegion(0, (short) 0, 1, (short) 1);
090:
091: int records = sheet.getRecords().size();
092:
093: //remove a third from the beginning
094: for (int n = 0; n < regionsToAdd / 3; n++) {
095: sheet.removeMergedRegion(0);
096: //assert they have been deleted
097: assertTrue("Num of regions should be "
098: + (regionsToAdd - n - 1) + " not "
099: + sheet.getNumMergedRegions(), sheet
100: .getNumMergedRegions() == regionsToAdd - n - 1);
101: }
102:
103: //assert any record removing was done
104: int recordsRemoved = (regionsToAdd / 3) / 1027; //doesn't work for particular values of regionsToAdd
105: assertTrue("Expected " + recordsRemoved
106: + " record to be removed from the starting " + records
107: + ". Currently there are " + sheet.getRecords().size()
108: + " records",
109: records - sheet.getRecords().size() == recordsRemoved);
110: }
111:
112: /**
113: * Bug: 22922 (Reported by Xuemin Guan)
114: * <p>
115: * Remove mergedregion fails when a sheet loses records after an initial CreateSheet
116: * fills up the records.
117: *
118: */
119: public void testMovingMergedRegion() {
120: List records = new ArrayList();
121:
122: MergeCellsRecord merged = new MergeCellsRecord();
123: merged.addArea(0, (short) 0, 1, (short) 2);
124: records.add(new RowRecord());
125: records.add(new RowRecord());
126: records.add(new RowRecord());
127: records.add(merged);
128:
129: Sheet sheet = Sheet.createSheet(records, 0);
130: sheet.records.remove(0);
131:
132: //stub object to throw off list INDEX operations
133: sheet.removeMergedRegion(0);
134: assertEquals("Should be no more merged regions", 0, sheet
135: .getNumMergedRegions());
136: }
137:
138: public void testGetMergedRegionAt() {
139: //TODO
140: }
141:
142: public void testGetNumMergedRegions() {
143: //TODO
144: }
145:
146: public void DISBALEDtestGetCellWidth() throws Exception {
147: Sheet sheet = Sheet.createSheet();
148: ColumnInfoRecord nci = (ColumnInfoRecord) sheet.createColInfo();
149:
150: // Prepare test model
151: nci.setFirstColumn((short) 5);
152: nci.setLastColumn((short) 10);
153: nci.setColumnWidth((short) 100);
154:
155: Field f = null;
156: f = Sheet.class.getDeclaredField("columnSizes");
157: f.setAccessible(true);
158: List columnSizes = new ArrayList();
159: f.set(sheet, columnSizes);
160: columnSizes.add(nci);
161: sheet.records.add(1 + sheet.dimsloc, nci);
162: sheet.dimsloc++;
163:
164: assertEquals((short) 100, sheet.getColumnWidth((short) 5));
165: assertEquals((short) 100, sheet.getColumnWidth((short) 6));
166: assertEquals((short) 100, sheet.getColumnWidth((short) 7));
167: assertEquals((short) 100, sheet.getColumnWidth((short) 8));
168: assertEquals((short) 100, sheet.getColumnWidth((short) 9));
169: assertEquals((short) 100, sheet.getColumnWidth((short) 10));
170:
171: sheet.setColumnWidth((short) 6, (short) 200);
172:
173: assertEquals((short) 100, sheet.getColumnWidth((short) 5));
174: assertEquals((short) 200, sheet.getColumnWidth((short) 6));
175: assertEquals((short) 100, sheet.getColumnWidth((short) 7));
176: assertEquals((short) 100, sheet.getColumnWidth((short) 8));
177: assertEquals((short) 100, sheet.getColumnWidth((short) 9));
178: assertEquals((short) 100, sheet.getColumnWidth((short) 10));
179: }
180:
181: /**
182: * Makes sure all rows registered for this sheet are aggregated, they were being skipped
183: *
184: */
185: public void testRowAggregation() {
186: List records = new ArrayList();
187: RowRecord row = new RowRecord();
188: row.setRowNumber(0);
189: records.add(row);
190:
191: row = new RowRecord();
192: row.setRowNumber(1);
193: records.add(row);
194:
195: records.add(new StringRecord());
196:
197: row = new RowRecord();
198: row.setRowNumber(2);
199: records.add(row);
200:
201: Sheet sheet = Sheet.createSheet(records, 0);
202: assertNotNull("Row [2] was skipped", sheet.getRow(2));
203:
204: }
205:
206: /**
207: * Make sure page break functionality works (in memory)
208: *
209: */
210: public void testRowPageBreaks() {
211: short colFrom = 0;
212: short colTo = 255;
213:
214: Sheet sheet = Sheet.createSheet();
215: sheet.setRowBreak(0, colFrom, colTo);
216:
217: assertTrue("no row break at 0", sheet.isRowBroken(0));
218: assertEquals("1 row break available", 1, sheet
219: .getNumRowBreaks());
220:
221: sheet.setRowBreak(0, colFrom, colTo);
222: sheet.setRowBreak(0, colFrom, colTo);
223:
224: assertTrue("no row break at 0", sheet.isRowBroken(0));
225: assertEquals("1 row break available", 1, sheet
226: .getNumRowBreaks());
227:
228: sheet.setRowBreak(10, colFrom, colTo);
229: sheet.setRowBreak(11, colFrom, colTo);
230:
231: assertTrue("no row break at 10", sheet.isRowBroken(10));
232: assertTrue("no row break at 11", sheet.isRowBroken(11));
233: assertEquals("3 row break available", 3, sheet
234: .getNumRowBreaks());
235:
236: boolean is10 = false;
237: boolean is0 = false;
238: boolean is11 = false;
239:
240: Iterator iterator = sheet.getRowBreaks();
241: while (iterator.hasNext()) {
242: PageBreakRecord.Break breakItem = (PageBreakRecord.Break) iterator
243: .next();
244: int main = (int) breakItem.main;
245: if (main != 0 && main != 10 && main != 11)
246: fail("Invalid page break");
247: if (main == 0)
248: is0 = true;
249: if (main == 10)
250: is10 = true;
251: if (main == 11)
252: is11 = true;
253: }
254:
255: assertTrue("one of the breaks didnt make it", is0 && is10
256: && is11);
257:
258: sheet.removeRowBreak(11);
259: assertFalse("row should be removed", sheet.isRowBroken(11));
260:
261: sheet.removeRowBreak(0);
262: assertFalse("row should be removed", sheet.isRowBroken(0));
263:
264: sheet.removeRowBreak(10);
265: assertFalse("row should be removed", sheet.isRowBroken(10));
266:
267: assertEquals("no more breaks", 0, sheet.getNumRowBreaks());
268:
269: }
270:
271: /**
272: * Make sure column pag breaks works properly (in-memory)
273: *
274: */
275: public void testColPageBreaks() {
276: short rowFrom = 0;
277: short rowTo = (short) 65535;
278:
279: Sheet sheet = Sheet.createSheet();
280: sheet.setColumnBreak((short) 0, rowFrom, rowTo);
281:
282: assertTrue("no col break at 0", sheet.isColumnBroken((short) 0));
283: assertEquals("1 col break available", 1, sheet
284: .getNumColumnBreaks());
285:
286: sheet.setColumnBreak((short) 0, rowFrom, rowTo);
287:
288: assertTrue("no col break at 0", sheet.isColumnBroken((short) 0));
289: assertEquals("1 col break available", 1, sheet
290: .getNumColumnBreaks());
291:
292: sheet.setColumnBreak((short) 1, rowFrom, rowTo);
293: sheet.setColumnBreak((short) 10, rowFrom, rowTo);
294: sheet.setColumnBreak((short) 15, rowFrom, rowTo);
295:
296: assertTrue("no col break at 1", sheet.isColumnBroken((short) 1));
297: assertTrue("no col break at 10", sheet
298: .isColumnBroken((short) 10));
299: assertTrue("no col break at 15", sheet
300: .isColumnBroken((short) 15));
301: assertEquals("4 col break available", 4, sheet
302: .getNumColumnBreaks());
303:
304: boolean is10 = false;
305: boolean is0 = false;
306: boolean is1 = false;
307: boolean is15 = false;
308:
309: Iterator iterator = sheet.getColumnBreaks();
310: while (iterator.hasNext()) {
311: PageBreakRecord.Break breakItem = (PageBreakRecord.Break) iterator
312: .next();
313: int main = (int) breakItem.main;
314: if (main != 0 && main != 1 && main != 10 && main != 15)
315: fail("Invalid page break");
316: if (main == 0)
317: is0 = true;
318: if (main == 1)
319: is1 = true;
320: if (main == 10)
321: is10 = true;
322: if (main == 15)
323: is15 = true;
324: }
325:
326: assertTrue("one of the breaks didnt make it", is0 && is1
327: && is10 && is15);
328:
329: sheet.removeColumnBreak((short) 15);
330: assertFalse("column break should not be there", sheet
331: .isColumnBroken((short) 15));
332:
333: sheet.removeColumnBreak((short) 0);
334: assertFalse("column break should not be there", sheet
335: .isColumnBroken((short) 0));
336:
337: sheet.removeColumnBreak((short) 1);
338: assertFalse("column break should not be there", sheet
339: .isColumnBroken((short) 1));
340:
341: sheet.removeColumnBreak((short) 10);
342: assertFalse("column break should not be there", sheet
343: .isColumnBroken((short) 10));
344:
345: assertEquals("no more breaks", 0, sheet.getNumColumnBreaks());
346: }
347:
348: }
|