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.record.aggregates;
019:
020: import junit.framework.TestCase;
021: import org.apache.poi.hssf.record.*;
022:
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: public class TestValueRecordsAggregate extends TestCase {
028: ValueRecordsAggregate valueRecord = new ValueRecordsAggregate();
029:
030: /**
031: * Make sure the shared formula DOESNT makes it to the FormulaRecordAggregate when being parsed
032: * as part of the value records
033: */
034: public void testSharedFormula() {
035: List records = new ArrayList();
036: records.add(new FormulaRecord());
037: records.add(new SharedFormulaRecord());
038:
039: valueRecord.construct(0, records);
040: Iterator iterator = valueRecord.getIterator();
041: Record record = (Record) iterator.next();
042: assertNotNull("Row contains a value", record);
043: assertTrue("First record is a FormulaRecordsAggregate",
044: (record instanceof FormulaRecordAggregate));
045: //Ensure that the SharedFormulaRecord has been converted
046: assertFalse("SharedFormulaRecord is null", iterator.hasNext());
047:
048: }
049:
050: public void testUnknownRecordsIgnored() {
051: List records = testData();
052: valueRecord.construct(0, records);
053: Iterator iterator = valueRecord.getIterator();
054: Record record1 = (Record) iterator.next();
055: Record record2 = (Record) iterator.next();
056: assertNotNull("No record found", record1);
057: assertNotNull("No record found", record2);
058: assertFalse(iterator.hasNext());
059:
060: }
061:
062: private List testData() {
063: List records = new ArrayList();
064: FormulaRecord formulaRecord = new FormulaRecord();
065: UnknownRecord unknownRecord = new UnknownRecord();
066: BlankRecord blankRecord = new BlankRecord();
067: WindowOneRecord windowOneRecord = new WindowOneRecord();
068: formulaRecord.setRow(1);
069: formulaRecord.setColumn((short) 1);
070: blankRecord.setRow(2);
071: blankRecord.setColumn((short) 2);
072: records.add(formulaRecord);
073: records.add(unknownRecord);
074: records.add(blankRecord);
075: records.add(windowOneRecord);
076: return records;
077: }
078:
079: public void testInsertCell() throws Exception {
080: Iterator iterator = valueRecord.getIterator();
081: assertFalse(iterator.hasNext());
082:
083: BlankRecord blankRecord = newBlankRecord();
084: valueRecord.insertCell(blankRecord);
085: iterator = valueRecord.getIterator();
086: assertTrue(iterator.hasNext());
087: }
088:
089: public void testRemoveCell() throws Exception {
090: BlankRecord blankRecord1 = newBlankRecord();
091: valueRecord.insertCell(blankRecord1);
092: BlankRecord blankRecord2 = newBlankRecord();
093: valueRecord.removeCell(blankRecord2);
094: Iterator iterator = valueRecord.getIterator();
095: assertFalse(iterator.hasNext());
096:
097: // removing an already empty cell just falls through
098: valueRecord.removeCell(blankRecord2);
099:
100: // even trying to remove null just falls through silently.
101: valueRecord.removeCell(null);
102:
103: }
104:
105: public void testGetPhysicalNumberOfCells() throws Exception {
106: assertEquals(0, valueRecord.getPhysicalNumberOfCells());
107: BlankRecord blankRecord1 = newBlankRecord();
108: valueRecord.insertCell(blankRecord1);
109: assertEquals(1, valueRecord.getPhysicalNumberOfCells());
110: valueRecord.removeCell(blankRecord1);
111: assertEquals(0, valueRecord.getPhysicalNumberOfCells());
112: }
113:
114: public void testGetFirstCellNum() throws Exception {
115: assertEquals(-1, valueRecord.getFirstCellNum());
116: valueRecord.insertCell(newBlankRecord(2, 2));
117: assertEquals(2, valueRecord.getFirstCellNum());
118: valueRecord.insertCell(newBlankRecord(3, 3));
119: assertEquals(2, valueRecord.getFirstCellNum());
120:
121: // Note: Removal doesn't currently reset the first column. It probably should but it doesn't.
122: valueRecord.removeCell(newBlankRecord(2, 2));
123: assertEquals(2, valueRecord.getFirstCellNum());
124: }
125:
126: public void testGetLastCellNum() throws Exception {
127: assertEquals(-1, valueRecord.getLastCellNum());
128: valueRecord.insertCell(newBlankRecord(2, 2));
129: assertEquals(2, valueRecord.getLastCellNum());
130: valueRecord.insertCell(newBlankRecord(3, 3));
131: assertEquals(3, valueRecord.getLastCellNum());
132:
133: // Note: Removal doesn't currently reset the last column. It probably should but it doesn't.
134: valueRecord.removeCell(newBlankRecord(3, 3));
135: assertEquals(3, valueRecord.getLastCellNum());
136:
137: }
138:
139: public void testSerialize() throws Exception {
140: byte[] actualArray = new byte[36];
141: byte[] expectedArray = new byte[] { (byte) 0x06, (byte) 0x00,
142: (byte) 0x16, (byte) 0x00, (byte) 0x01, (byte) 0x00,
143: (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00,
144: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
145: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
146: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
147: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
148: (byte) 0x01, (byte) 0x02, (byte) 0x06, (byte) 0x00,
149: (byte) 0x02, (byte) 0x00, (byte) 0x02, (byte) 0x00,
150: (byte) 0x00, (byte) 0x00, };
151: List records = testData();
152: valueRecord.construct(0, records);
153: int bytesWritten = valueRecord.serializeCellRow(1, 0,
154: actualArray);
155: bytesWritten += valueRecord.serializeCellRow(2, bytesWritten,
156: actualArray);
157: assertEquals(36, bytesWritten);
158: for (int i = 0; i < 36; i++)
159: assertEquals(expectedArray[i], actualArray[i]);
160: }
161:
162: public static void main(String[] args) {
163: System.out
164: .println("Testing org.apache.poi.hssf.record.aggregates.TestValueRecordAggregate");
165: junit.textui.TestRunner.run(TestValueRecordsAggregate.class);
166: }
167:
168: private BlankRecord newBlankRecord() {
169: return newBlankRecord(2, 2);
170: }
171:
172: private BlankRecord newBlankRecord(int col, int row) {
173: BlankRecord blankRecord = new BlankRecord();
174: blankRecord.setRow(row);
175: blankRecord.setColumn((short) col);
176: return blankRecord;
177: }
178:
179: public void testGetRecordSize() throws Exception {
180: List records = testData();
181: valueRecord.construct(0, records);
182: assertEquals(36, valueRecord.getRecordSize());
183: }
184:
185: public void testClone() throws Exception {
186: List records = testData();
187: valueRecord.construct(0, records);
188: valueRecord = (ValueRecordsAggregate) valueRecord.clone();
189: assertEquals(36, valueRecord.getRecordSize());
190: }
191:
192: }
|