1 2 17 18 19 package org.apache.poi.hssf.record; 20 21 import java.io.ByteArrayInputStream ; 22 23 import junit.framework.TestCase; 24 25 31 32 public class TestRecordFactory 33 extends TestCase 34 { 35 36 40 41 public TestRecordFactory(String testCaseName) 42 { 43 super(testCaseName); 44 } 45 46 54 55 public void testBasicRecordConstruction() 56 throws Exception 57 { 58 short recType = BOFRecord.sid; 59 byte[] data = new byte[] 60 { 61 0, 6, 5, 0, -2, 28, -51, 7, -55, 64, 0, 0, 6, 1, 0, 0 62 }; 63 short size = 16; 64 Record[] record = RecordFactory.createRecord(recType, size, data); 65 66 assertEquals(BOFRecord.class.getName(), 67 record[ 0 ].getClass().getName()); 68 BOFRecord bofRecord = ( BOFRecord ) record[ 0 ]; 69 70 assertEquals(7422, bofRecord.getBuild()); 71 assertEquals(1997, bofRecord.getBuildYear()); 72 assertEquals(16585, bofRecord.getHistoryBitMask()); 73 assertEquals(20, bofRecord.getRecordSize()); 74 assertEquals(262, bofRecord.getRequiredVersion()); 75 assertEquals(2057, bofRecord.getSid()); 76 assertEquals(5, bofRecord.getType()); 77 assertEquals(1536, bofRecord.getVersion()); 78 recType = MMSRecord.sid; 79 size = 2; 80 data = new byte[] 81 { 82 0, 0 83 }; 84 record = RecordFactory.createRecord(recType, size, data); 85 assertEquals(MMSRecord.class.getName(), 86 record[ 0 ].getClass().getName()); 87 MMSRecord mmsRecord = ( MMSRecord ) record[ 0 ]; 88 89 assertEquals(0, mmsRecord.getAddMenuCount()); 90 assertEquals(0, mmsRecord.getDelMenuCount()); 91 assertEquals(6, mmsRecord.getRecordSize()); 92 assertEquals(193, mmsRecord.getSid()); 93 } 94 95 103 104 public void testSpecial() 105 throws Exception 106 { 107 short recType = RKRecord.sid; 108 byte[] data = new byte[] 109 { 110 0, 0, 0, 0, 21, 0, 0, 0, 0, 0 111 }; 112 short size = 10; 113 Record[] record = RecordFactory.createRecord(recType, size, data); 114 115 assertEquals(NumberRecord.class.getName(), 116 record[ 0 ].getClass().getName()); 117 NumberRecord numberRecord = ( NumberRecord ) record[ 0 ]; 118 119 assertEquals(0, numberRecord.getColumn()); 120 assertEquals(18, numberRecord.getRecordSize()); 121 assertEquals(0, numberRecord.getRow()); 122 assertEquals(515, numberRecord.getSid()); 123 assertEquals(0.0, numberRecord.getValue(), 0.001); 124 assertEquals(21, numberRecord.getXFIndex()); 125 } 126 127 135 public void testContinuedUnknownRecord() 136 { 137 byte[] data = new byte[] 138 { 139 0, -1, 0, 0, 0x3C , 0, 3, 0, 1, 2, 3, 0x3C , 0, 1, 0, 4 }; 143 144 ByteArrayInputStream bois = new ByteArrayInputStream (data); 145 Record[] records = (Record[]) 146 RecordFactory.createRecords(bois).toArray(new Record[0]); 147 assertEquals("Created record count", 3, records.length); 148 assertEquals("1st record's type", 149 UnknownRecord.class.getName(), 150 records[ 0 ].getClass().getName()); 151 assertEquals("1st record's sid", (short)-256, records[0].getSid()); 152 assertEquals("2nd record's type", 153 ContinueRecord.class.getName(), 154 records[ 1 ].getClass().getName()); 155 ContinueRecord record = (ContinueRecord) records[1]; 156 assertEquals("2nd record's sid", 0x3C, record.getSid()); 157 assertEquals("1st data byte", 1, record.getData()[ 0 ]); 158 assertEquals("2nd data byte", 2, record.getData()[ 1 ]); 159 assertEquals("3rd data byte", 3, record.getData()[ 2 ]); 160 assertEquals("3rd record's type", 161 ContinueRecord.class.getName(), 162 records[ 2 ].getClass().getName()); 163 record = (ContinueRecord) records[2]; 164 assertEquals("3nd record's sid", 0x3C, record.getSid()); 165 assertEquals("4th data byte", 4, record.getData()[ 0 ]); 166 } 167 168 public static void main(String [] ignored_args) 169 { 170 System.out 171 .println("Testing org.apache.poi.hssf.record.TestRecordFactory"); 172 junit.textui.TestRunner.run(TestRecordFactory.class); 173 } 174 } 175 | Popular Tags |