KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > eventmodel > TestEventRecordFactory


1 /* ====================================================================
2    Copyright 2003-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17 package org.apache.poi.hssf.eventmodel;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.EOFException JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Arrays JavaDoc;
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 /**
33  * enclosing_type describe the purpose here
34  *
35  * @author Andrew C. Oliver acoliver@apache.org
36  * @author Csaba Nagy (ncsaba at yahoo dot com)
37  */

38 public class TestEventRecordFactory extends TestCase
39 {
40     boolean wascalled;
41
42     private EventRecordFactory factory;
43     /**
44      * Constructor for TestEventRecordFactory.
45      * @param arg0
46      */

47     public TestEventRecordFactory(String JavaDoc arg0)
48     {
49         super(arg0);
50     }
51
52     public static void main(String JavaDoc[] args)
53     {
54         junit.textui.TestRunner.run(TestEventRecordFactory.class);
55     }
56
57     protected void setUp() throws Exception JavaDoc
58     {
59         super.setUp();
60         factory = new EventRecordFactory();
61     }
62
63     protected void tearDown() throws Exception JavaDoc
64     {
65         super.tearDown();
66     }
67
68     /**
69      * tests that a listener can be registered and that once
70      * registered can be returned as expected.
71      */

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 JavaDoc 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     /**
97      * tests that the records can be processed and properly return
98      * values.
99      */

100     public void testProcessRecords()
101     {
102         byte[] bytes = null;
103         int offset = 0;
104         //boolean wascalled = false;
105
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 JavaDoc(bytes));
128         assertTrue("The record listener must be called",wascalled);
129     }
130
131     /**
132      * tests that the create record function returns a properly
133      * constructed record in the simple case.
134      */

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     /**
160      * Compare the serialized bytes of two records are equal
161      * @param first the first record to compare
162      * @param second the second record to compare
163      * @return boolean whether or not the record where equal
164      */

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     /**
187      * tests that the create record function returns a properly
188      * constructed record in the case of a continued record.
189      * @todo - need a real world example to put in a unit test
190      */

191     public void testCreateContinuedRecord()
192     {
193       // fail("not implemented");
194
}
195     
196
197     /**
198      * TEST NAME: Test Creating ContinueRecords After Unknown Records From An InputStream <P>
199      * OBJECTIVE: Test that the RecordFactory given an InputStream
200      * constructs the expected records.<P>
201      * SUCCESS: Record factory creates the expected records.<P>
202      * FAILURE: The wrong records are created or contain the wrong values <P>
203      *
204      */

205      public void testContinuedUnknownRecord()
206      {
207         final byte[] data = new byte[]
208         {
209             0, -1, 0, 0, // an unknown record with 0 length
210
0x3C , 0, 3, 0, 1, 2, 3, // a continuation record with 3 bytes of data
211
0x3C , 0, 1, 0, 4 // one more continuation record with 1 byte of data
212
};
213
214         final int[] recCnt = { 0 };
215         final int[] offset = { 0 };
216         factory.registerListener(
217           new ERFListener() {
218               private String JavaDoc[] expectedRecordTypes = {
219                   UnknownRecord.class.getName(),
220                   ContinueRecord.class.getName(),
221                   ContinueRecord.class.getName()
222               };
223               public boolean processRecord(Record rec)
224               {
225                   // System.out.println(rec.toString());
226
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 JavaDoc 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 JavaDoc(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