KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.apache.poi.hssf.record;
21
22
23 import junit.framework.TestCase;
24 import org.apache.poi.hssf.record.formula.Area3DPtg;
25
26 import java.util.Stack JavaDoc;
27
28 /**
29  * Tests the serialization and deserialization of the LinkedDataRecord
30  * class works correctly. Test data taken directly from a real
31  * Excel file.
32  *
33  * @author Glen Stampoultzis (glens at apache.org)
34  */

35 public class TestLinkedDataRecord
36         extends TestCase
37 {
38
39 /*
40     The records below are records that would appear in a simple bar chart
41
42     The first record links to the series title (linkType = 0). It's
43     reference type is 1 which means that it links directly to data entered
44     into the forumula bar. There seems to be no reference to any data
45     however. The formulaOfLink field contains two 0 bytes. This probably
46     means that there is no particular heading set.
47
48
49 ============================================
50 Offset 0xf9c (3996)
51 rectype = 0x1051, recsize = 0x8
52 -BEGIN DUMP---------------------------------
53 00000000 00 01 00 00 00 00 00 00 ........
54 -END DUMP-----------------------------------
55 recordid = 0x1051, size =8
56 [AI]
57 .linkType = 0x00 (0 )
58 .referenceType = 0x01 (1 )
59 .options = 0x0000 (0 )
60     .customNumberFormat = false
61 .indexNumberFmtRecord = 0x0000 (0 )
62 .formulaOfLink = (org.apache.poi.hssf.record.LinkedDataFormulaField@95fd19 )
63 [/AI]
64
65
66     The second record links to the series data (linkType=1). The
67     referenceType = 2 which means it's linked to the worksheet.
68     It links using a formula. The formula value is
69     0B 00 3B 00 00 00 00 1E 00 01 00 01 00.
70
71     0B 00 11 bytes length
72     3B (tArea3d) Rectangular area
73         00 00 index to REF entry in extern sheet
74         00 00 index to first row
75         1E 00 index to last row
76         01 00 index to first column and relative flags
77         01 00 index to last column and relative flags
78
79 ============================================
80 Offset 0xfa8 (4008)
81 rectype = 0x1051, recsize = 0x13
82 -BEGIN DUMP---------------------------------
83 00000000 01 02 00 00 00 00 0B 00 3B 00 00 00 00 1E 00 01 ........;.......
84 00000010 00 01 00 ...
85 -END DUMP-----------------------------------
86 recordid = 0x1051, size =19
87 [AI]
88 .linkType = 0x01 (1 )
89 .referenceType = 0x02 (2 )
90 .options = 0x0000 (0 )
91     .customNumberFormat = false
92 .indexNumberFmtRecord = 0x0000 (0 )
93 .formulaOfLink = (org.apache.poi.hssf.record.LinkedDataFormulaField@11b9fb1 )
94 [/AI]
95
96     The third record links to the series categories (linkType=2). The
97     reference type of 2 means that it's linked to the worksheet.
98     It links using a formula. The formula value is
99     0B 00 3B 00 00 00 00 1E 00 01 00 01 00
100
101     0B 00 11 bytes in length
102         3B (tArea3d) Rectangular area
103         00 00 index to REF entry in extern sheet
104         00 00 index to first row
105         00 1F index to last row
106         00 00 index to first column and relative flags
107         00 00 index to last column and relative flags
108
109
110 ============================================
111 Offset 0xfbf (4031)
112 rectype = 0x1051, recsize = 0x13
113 -BEGIN DUMP---------------------------------
114 00000000 02 02 00 00 69 01 0B 00 3B 00 00 00 00 1F 00 00 ....i...;.......
115 00000010 00 00 00 ...
116 -END DUMP-----------------------------------
117 recordid = 0x1051, size =19
118 [AI]
119 .linkType = 0x02 (2 )
120 .referenceType = 0x02 (2 )
121 .options = 0x0000 (0 )
122     .customNumberFormat = false
123 .indexNumberFmtRecord = 0x0169 (361 )
124 .formulaOfLink = (org.apache.poi.hssf.record.LinkedDataFormulaField@913fe2 )
125 [/AI]
126
127 This third link type does not seem to be documented and does not appear to
128 contain any useful information anyway.
129
130 ============================================
131 Offset 0xfd6 (4054)
132 rectype = 0x1051, recsize = 0x8
133 -BEGIN DUMP---------------------------------
134 00000000 03 01 00 00 00 00 00 00 ........
135 -END DUMP-----------------------------------
136 recordid = 0x1051, size =8
137 [AI]
138 .linkType = 0x03 (3 )
139 .referenceType = 0x01 (1 )
140 .options = 0x0000 (0 )
141     .customNumberFormat = false
142 .indexNumberFmtRecord = 0x0000 (0 )
143 .formulaOfLink = (org.apache.poi.hssf.record.LinkedDataFormulaField@1f934ad )
144 [/AI]
145
146 */

147
148     byte[] data = new byte[]{
149         (byte)0x01, // link type
150
(byte)0x02, // reference type
151
(byte)0x00,(byte)0x00, // options
152
(byte)0x00,(byte)0x00, // index number format record
153
(byte)0x0B,(byte)0x00, // 11 bytes length
154
(byte)0x3B, // formula of link
155
(byte)0x00,(byte)0x00, // index to ref entry in extern sheet
156
(byte)0x00,(byte)0x00, // index to first row
157
(byte)0x00,(byte)0x1F, // index to last row
158
(byte)0x00,(byte)0x00, // index to first column and relative flags
159
(byte)0x00,(byte)0x00, // index to last column and relative flags
160
};
161
162     public TestLinkedDataRecord(String JavaDoc name)
163     {
164         super(name);
165     }
166
167     public void testLoad()
168             throws Exception JavaDoc
169     {
170
171         LinkedDataRecord record = new LinkedDataRecord((short)0x1051, (short)data.length, data);
172         assertEquals( LinkedDataRecord.LINK_TYPE_VALUES, record.getLinkType());
173         assertEquals( LinkedDataRecord.REFERENCE_TYPE_WORKSHEET, record.getReferenceType());
174         assertEquals( 0, record.getOptions());
175         assertEquals( false, record.isCustomNumberFormat() );
176         assertEquals( 0, record.getIndexNumberFmtRecord());
177
178         Area3DPtg ptg = new Area3DPtg();
179         ptg.setExternSheetIndex((short)0);
180         ptg.setFirstColumn((short)0);
181         ptg.setLastColumn((short)0);
182         ptg.setFirstRow((short)0);
183         ptg.setLastRow((short)7936);
184         ptg.setFirstColRelative(false);
185         ptg.setLastColRelative(false);
186         ptg.setFirstRowRelative(false);
187         ptg.setLastRowRelative(false);
188         Stack JavaDoc s = new Stack JavaDoc();
189         s.push(ptg);
190         assertEquals( s, record.getFormulaOfLink().getFormulaTokens() );
191
192         assertEquals( data.length + 4, record.getRecordSize() );
193
194         record.validateSid((short)0x1051);
195
196     }
197
198     public void testStore()
199     {
200         LinkedDataRecord record = new LinkedDataRecord();
201         record.setLinkType( LinkedDataRecord.LINK_TYPE_VALUES );
202         record.setReferenceType( LinkedDataRecord.REFERENCE_TYPE_WORKSHEET );
203         record.setOptions( (short)0 );
204         record.setCustomNumberFormat( false );
205         record.setIndexNumberFmtRecord( (short)0 );
206         Area3DPtg ptg = new Area3DPtg();
207         ptg.setExternSheetIndex((short)0);
208         ptg.setFirstColumn((short)0);
209         ptg.setLastColumn((short)0);
210         ptg.setFirstRow((short)0);
211         ptg.setLastRow((short)7936);
212         ptg.setFirstColRelative(false);
213         ptg.setLastColRelative(false);
214         ptg.setFirstRowRelative(false);
215         ptg.setLastRowRelative(false);
216         Stack JavaDoc s = new Stack JavaDoc();
217         s.push(ptg);
218         LinkedDataFormulaField formulaOfLink = new LinkedDataFormulaField();
219         formulaOfLink.setFormulaTokens(s);
220         record.setFormulaOfLink(formulaOfLink );
221
222         byte [] recordBytes = record.serialize();
223         assertEquals(recordBytes.length - 4, data.length);
224         for (int i = 0; i < data.length; i++)
225             assertEquals("At offset " + i, data[i], recordBytes[i+4]);
226     }
227 }
228
Popular Tags