KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > TestRecordFactory


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

17         
18
19 package org.apache.poi.hssf.record;
20
21 import java.io.ByteArrayInputStream JavaDoc;
22
23 import junit.framework.TestCase;
24
25 /**
26  * Tests the record factory
27  * @author Glen Stampoultzis (glens at apache.org)
28  * @author Andrew C. Oliver (acoliver at apache dot org)
29  * @author Csaba Nagy (ncsaba at yahoo dot com)
30  */

31
32 public class TestRecordFactory
33     extends TestCase
34 {
35
36     /**
37      * Creates new TestRecordFactory
38      * @param testCaseName
39      */

40
41     public TestRecordFactory(String JavaDoc testCaseName)
42     {
43         super(testCaseName);
44     }
45
46     /**
47      * TEST NAME: Test Basic Record Construction <P>
48      * OBJECTIVE: Test that the RecordFactory given the required parameters for know
49      * record types can construct the proper record w/values.<P>
50      * SUCCESS: Record factory creates the records with the expected values.<P>
51      * FAILURE: The wrong records are creates or contain the wrong values <P>
52      *
53      */

54
55     public void testBasicRecordConstruction()
56         throws Exception JavaDoc
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     /**
96      * TEST NAME: Test Special Record Construction <P>
97      * OBJECTIVE: Test that the RecordFactory given the required parameters for
98      * constructing a RKRecord will return a NumberRecord.<P>
99      * SUCCESS: Record factory creates the Number record with the expected values.<P>
100      * FAILURE: The wrong records are created or contain the wrong values <P>
101      *
102      */

103
104     public void testSpecial()
105         throws Exception JavaDoc
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     /**
128      * TEST NAME: Test Creating ContinueRecords After Unknown Records From An InputStream <P>
129      * OBJECTIVE: Test that the RecordFactory given an InputStream
130      * constructs the expected array of records.<P>
131      * SUCCESS: Record factory creates the expected records.<P>
132      * FAILURE: The wrong records are created or contain the wrong values <P>
133      *
134      */

135     public void testContinuedUnknownRecord()
136     {
137         byte[] data = new byte[]
138         {
139             0, -1, 0, 0, // an unknown record with 0 length
140
0x3C , 0, 3, 0, 1, 2, 3, // a continuation record with 3 bytes of data
141
0x3C , 0, 1, 0, 4 // one more continuation record with 1 byte of data
142
};
143
144         ByteArrayInputStream JavaDoc bois = new ByteArrayInputStream JavaDoc(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 JavaDoc [] 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