1 16 17 package org.apache.poi.hssf.record.aggregates; 18 19 import junit.framework.TestCase; 20 import org.apache.poi.hssf.record.*; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 public class TestValueRecordsAggregate extends TestCase 27 { 28 ValueRecordsAggregate valueRecord = new ValueRecordsAggregate(); 29 30 34 public void testSharedFormula() 35 { 36 List records = new ArrayList (); 37 records.add( new FormulaRecord() ); 38 records.add( new SharedFormulaRecord() ); 39 40 valueRecord.construct( 0, records ); 41 Iterator iterator = valueRecord.getIterator(); 42 Record record = (Record) iterator.next(); 43 assertNotNull( "Row contains a value", record ); 44 assertTrue( "First record is a FormulaRecordsAggregate", ( record instanceof FormulaRecordAggregate ) ); 45 FormulaRecordAggregate aggregate = (FormulaRecordAggregate) record; 46 assertNotNull( "SharedFormulaRecord is null", aggregate.getSharedFormulaRecord() ); 47 48 } 49 50 public void testUnknownRecordsIgnored() 51 { 52 List records = testData(); 53 valueRecord.construct( 0, records ); 54 Iterator iterator = valueRecord.getIterator(); 55 Record record1 = (Record) iterator.next(); 56 Record record2 = (Record) iterator.next(); 57 assertNotNull( "No record found", record1 ); 58 assertNotNull( "No record found", record2 ); 59 assertFalse( iterator.hasNext() ); 60 61 } 62 63 private List testData(){ 64 List records = new ArrayList (); 65 FormulaRecord formulaRecord = new FormulaRecord(); 66 UnknownRecord unknownRecord = new UnknownRecord(); 67 BlankRecord blankRecord = new BlankRecord(); 68 WindowOneRecord windowOneRecord = new WindowOneRecord(); 69 formulaRecord.setRow( 1 ); 70 formulaRecord.setColumn( (short) 1 ); 71 blankRecord.setRow( 2 ); 72 blankRecord.setColumn( (short) 2 ); 73 records.add( formulaRecord ); 74 records.add( unknownRecord ); 75 records.add( blankRecord ); 76 records.add( windowOneRecord ); 77 return records; 78 } 79 80 public void testInsertCell() 81 throws Exception 82 { 83 Iterator iterator = valueRecord.getIterator(); 84 assertFalse( iterator.hasNext() ); 85 86 BlankRecord blankRecord = newBlankRecord(); 87 valueRecord.insertCell( blankRecord ); 88 iterator = valueRecord.getIterator(); 89 assertTrue( iterator.hasNext() ); 90 } 91 92 public void testRemoveCell() 93 throws Exception 94 { 95 BlankRecord blankRecord1 = newBlankRecord(); 96 valueRecord.insertCell( blankRecord1 ); 97 BlankRecord blankRecord2 = newBlankRecord(); 98 valueRecord.removeCell( blankRecord2 ); 99 Iterator iterator = valueRecord.getIterator(); 100 assertFalse( iterator.hasNext() ); 101 102 valueRecord.removeCell( blankRecord2 ); 104 105 valueRecord.removeCell( null ); 107 108 } 109 110 public void testGetPhysicalNumberOfCells() throws Exception 111 { 112 assertEquals(0, valueRecord.getPhysicalNumberOfCells()); 113 BlankRecord blankRecord1 = newBlankRecord(); 114 valueRecord.insertCell( blankRecord1 ); 115 assertEquals(1, valueRecord.getPhysicalNumberOfCells()); 116 valueRecord.removeCell( blankRecord1 ); 117 assertEquals(0, valueRecord.getPhysicalNumberOfCells()); 118 } 119 120 public void testGetFirstCellNum() throws Exception 121 { 122 assertEquals( -1, valueRecord.getFirstCellNum() ); 123 valueRecord.insertCell( newBlankRecord( 2, 2 ) ); 124 assertEquals( 2, valueRecord.getFirstCellNum() ); 125 valueRecord.insertCell( newBlankRecord( 3, 3 ) ); 126 assertEquals( 2, valueRecord.getFirstCellNum() ); 127 128 valueRecord.removeCell( newBlankRecord( 2, 2 ) ); 130 assertEquals( 2, valueRecord.getFirstCellNum() ); 131 } 132 133 public void testGetLastCellNum() throws Exception 134 { 135 assertEquals( -1, valueRecord.getLastCellNum() ); 136 valueRecord.insertCell( newBlankRecord( 2, 2 ) ); 137 assertEquals( 2, valueRecord.getLastCellNum() ); 138 valueRecord.insertCell( newBlankRecord( 3, 3 ) ); 139 assertEquals( 3, valueRecord.getLastCellNum() ); 140 141 valueRecord.removeCell( newBlankRecord( 3, 3 ) ); 143 assertEquals( 3, valueRecord.getLastCellNum() ); 144 145 } 146 147 public void testSerialize() throws Exception 148 { 149 byte[] actualArray = new byte[36]; 150 byte[] expectedArray = new byte[] 151 { 152 (byte)0x06, (byte)0x00, (byte)0x16, (byte)0x00, 153 (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x00, 154 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, 155 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, 156 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, 157 (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, 158 (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x02, 159 (byte)0x06, (byte)0x00, (byte)0x02, (byte)0x00, 160 (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00, 161 }; 162 List records = testData(); 163 valueRecord.construct( 0, records ); 164 int bytesWritten = valueRecord.serializeCellRow(1, 0, actualArray ); 165 bytesWritten += valueRecord.serializeCellRow(2, bytesWritten, actualArray ); 166 assertEquals( 36, bytesWritten ); 167 for (int i = 0; i < 36; i++) 168 assertEquals( expectedArray[i], actualArray[i] ); 169 } 170 171 public static void main( String [] args ) 172 { 173 System.out.println( "Testing org.apache.poi.hssf.record.aggregates.TestValueRecordAggregate" ); 174 junit.textui.TestRunner.run( TestValueRecordsAggregate.class ); 175 } 176 177 private BlankRecord newBlankRecord() 178 { 179 return newBlankRecord( 2, 2 ); 180 } 181 182 private BlankRecord newBlankRecord( int col, int row) 183 { 184 BlankRecord blankRecord = new BlankRecord(); 185 blankRecord.setRow( row ); 186 blankRecord.setColumn( (short) col ); 187 return blankRecord; 188 } 189 190 public void testGetRecordSize() throws Exception 191 { 192 List records = testData(); 193 valueRecord.construct( 0, records ); 194 assertEquals( 36, valueRecord.getRecordSize() ); 195 } 196 197 public void testClone() throws Exception 198 { 199 List records = testData(); 200 valueRecord.construct( 0, records ); 201 valueRecord = (ValueRecordsAggregate) valueRecord.clone(); 202 assertEquals( 36, valueRecord.getRecordSize() ); 203 } 204 205 } 206 | Popular Tags |