1 16 17 package org.apache.poi.hssf.eventmodel; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.EOFException ; 21 import java.util.Iterator ; 22 import java.util.Arrays ; 23 24 import org.apache.poi.hssf.record.BOFRecord; 25 import org.apache.poi.hssf.record.EOFRecord; 26 import org.apache.poi.hssf.record.Record; 27 import org.apache.poi.hssf.record.UnknownRecord; 28 import org.apache.poi.hssf.record.ContinueRecord; 29 30 import junit.framework.TestCase; 31 32 38 public class TestEventRecordFactory extends TestCase 39 { 40 boolean wascalled; 41 42 private EventRecordFactory factory; 43 47 public TestEventRecordFactory(String arg0) 48 { 49 super(arg0); 50 } 51 52 public static void main(String [] args) 53 { 54 junit.textui.TestRunner.run(TestEventRecordFactory.class); 55 } 56 57 protected void setUp() throws Exception 58 { 59 super.setUp(); 60 factory = new EventRecordFactory(); 61 } 62 63 protected void tearDown() throws Exception 64 { 65 super.tearDown(); 66 } 67 68 72 public void testRegisterListener() 73 { 74 factory.registerListener(new ERFListener() { 75 public boolean processRecord(Record rec) { 76 return true; 77 } 78 },null); 79 80 Iterator i = factory.listeners(); 81 assertTrue("iterator must have one",i.hasNext()); 82 83 factory.registerListener(new ERFListener() { 84 public boolean processRecord(Record rec) { 85 return true; 86 } 87 },null); 88 89 i = factory.listeners(); 90 91 i.next(); 92 assertTrue("iterator must have two",i.hasNext()); 93 factory = new EventRecordFactory(); 94 } 95 96 100 public void testProcessRecords() 101 { 102 byte[] bytes = null; 103 int offset = 0; 104 factory.registerListener(new ERFListener() { 106 public boolean processRecord(Record rec) { 107 wascalled = true; 108 assertTrue("must be BOFRecord got SID="+rec.getSid(), 109 (rec.getSid() == BOFRecord.sid)); 110 return true; 111 } 112 }, new short[] {BOFRecord.sid}); 113 114 BOFRecord bof = new BOFRecord(); 115 bof.setBuild((short)0); 116 bof.setBuildYear((short)1999); 117 bof.setRequiredVersion(123); 118 bof.setType(BOFRecord.TYPE_WORKBOOK); 119 bof.setVersion((short)0x06); 120 bof.setHistoryBitMask(BOFRecord.HISTORY_MASK); 121 122 EOFRecord eof = new EOFRecord(); 123 bytes = new byte[bof.getRecordSize() + eof.getRecordSize()]; 124 offset = bof.serialize(offset,bytes); 125 offset = eof.serialize(offset,bytes); 126 127 factory.processRecords(new ByteArrayInputStream (bytes)); 128 assertTrue("The record listener must be called",wascalled); 129 } 130 131 135 public void testCreateRecord() 136 { 137 byte[] bytes = null; 138 byte[] nbytes = null; 139 Record[] records = null; 140 BOFRecord bof = new BOFRecord(); 141 bof.setBuild((short)0); 142 bof.setBuildYear((short)1999); 143 bof.setRequiredVersion(123); 144 bof.setType(BOFRecord.TYPE_WORKBOOK); 145 bof.setVersion((short)0x06); 146 bof.setHistoryBitMask(BOFRecord.HISTORY_MASK); 147 148 bytes = bof.serialize(); 149 nbytes = new byte[bytes.length - 4]; 150 System.arraycopy(bytes,4,nbytes,0,nbytes.length); 151 152 records = factory.createRecord(bof.getSid(),(short)nbytes.length,nbytes); 153 154 assertTrue("record.length must be 1, was ="+records.length,records.length == 1); 155 assertTrue("record is the same", compareRec(bof,records[0])); 156 157 } 158 159 165 private boolean compareRec(Record first, Record second) 166 { 167 boolean retval = true; 168 byte[] rec1 = first.serialize(); 169 byte[] rec2 = second.serialize(); 170 171 if (rec1.length == rec2.length) { 172 for (int k=0; k<rec1.length; k++) { 173 if (rec1[k] != rec2[k]) { 174 retval = false; 175 break; 176 } 177 } 178 } else { 179 retval = false; 180 } 181 182 return retval; 183 } 184 185 186 191 public void testCreateContinuedRecord() 192 { 193 } 195 196 197 205 public void testContinuedUnknownRecord() 206 { 207 final byte[] data = new byte[] 208 { 209 0, -1, 0, 0, 0x3C , 0, 3, 0, 1, 2, 3, 0x3C , 0, 1, 0, 4 }; 213 214 final int[] recCnt = { 0 }; 215 final int[] offset = { 0 }; 216 factory.registerListener( 217 new ERFListener() { 218 private String [] expectedRecordTypes = { 219 UnknownRecord.class.getName(), 220 ContinueRecord.class.getName(), 221 ContinueRecord.class.getName() 222 }; 223 public boolean processRecord(Record rec) 224 { 225 assertEquals( 227 "Record type", 228 expectedRecordTypes[recCnt[0]], 229 rec.getClass().getName() 230 ); 231 compareData(rec, "Record " + recCnt[0] + ": "); 232 recCnt[0]++; 233 return true; 234 } 235 private void compareData(Record record, String message) { 236 byte[] recData = record.serialize(); 237 for (int i = 0; i < recData.length; i++) { 238 assertEquals(message + " data byte " + i, data[offset[0]++], recData[i]); 239 } 240 } 241 }, 242 new short[] {-256, 0x3C} 243 ); 244 245 factory.processRecords(new ByteArrayInputStream (data)); 246 assertEquals("nr. of processed records", 3, recCnt[0]); 247 assertEquals("nr. of processed bytes", data.length, offset[0]); 248 } 249 250 251 } 252 | Popular Tags |