Line 0
Link Here
|
|
|
1 |
|
2 |
/* ==================================================================== |
3 |
Licensed to the Apache Software Foundation (ASF) under one or more |
4 |
contributor license agreements. See the NOTICE file distributed with |
5 |
this work for additional information regarding copyright ownership. |
6 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
7 |
(the "License"); you may not use this file except in compliance with |
8 |
the License. You may obtain a copy of the License at |
9 |
|
10 |
http://www.apache.org/licenses/LICENSE-2.0 |
11 |
|
12 |
Unless required by applicable law or agreed to in writing, software |
13 |
distributed under the License is distributed on an "AS IS" BASIS, |
14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 |
See the License for the specific language governing permissions and |
16 |
limitations under the License. |
17 |
==================================================================== */ |
18 |
|
19 |
package org.apache.poi.hssf.record; |
20 |
|
21 |
import junit.framework.*; |
22 |
|
23 |
import java.util.Arrays; |
24 |
import java.util.List; |
25 |
|
26 |
/** |
27 |
* Tests the serialization and deserialization of the ObjRecord class works correctly. |
28 |
* Test data taken directly from a real Excel file. |
29 |
* |
30 |
* @author Yegor Kozlov |
31 |
*/ |
32 |
public class TestObjRecord extends TestCase { |
33 |
/** |
34 |
* OBJ record data containing two sub-records. |
35 |
* The data taken directly from a real Excel file. |
36 |
* |
37 |
* [OBJ] |
38 |
* [ftCmo] |
39 |
* [ftEnd] |
40 |
*/ |
41 |
public static byte[] recdata = { |
42 |
0x15, 0x00, 0x12, 0x00, 0x06, 0x00, 0x01, 0x00, 0x11, 0x60, |
43 |
(byte)0xF4, 0x02, 0x41, 0x01, 0x14, 0x10, 0x1F, 0x02, 0x00, 0x00, |
44 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
45 |
}; |
46 |
|
47 |
|
48 |
public void testLoad() throws Exception { |
49 |
ObjRecord record = new ObjRecord(new TestcaseRecordInputStream(ObjRecord.sid, (short)recdata.length, recdata)); |
50 |
|
51 |
assertEquals( recdata.length, record.getRecordSize() - 4); |
52 |
|
53 |
List subrecords = record.getSubRecords(); |
54 |
assertEquals( 2, subrecords.size() ); |
55 |
assertTrue( subrecords.get(0) instanceof CommonObjectDataSubRecord); |
56 |
assertTrue( subrecords.get(1) instanceof EndSubRecord ); |
57 |
|
58 |
} |
59 |
|
60 |
public void testStore() { |
61 |
ObjRecord record = new ObjRecord(new TestcaseRecordInputStream(ObjRecord.sid, (short)recdata.length, recdata)); |
62 |
|
63 |
byte [] recordBytes = record.serialize(); |
64 |
assertEquals(recdata.length, recordBytes.length - 4); |
65 |
byte[] subData = new byte[recordBytes.length - 4]; |
66 |
System.arraycopy(recordBytes, 4, subData, 0, subData.length); |
67 |
assertTrue(Arrays.equals(recdata, subData)); |
68 |
} |
69 |
|
70 |
public void testConstruct() { |
71 |
ObjRecord record = new ObjRecord(); |
72 |
CommonObjectDataSubRecord ftCmo = new CommonObjectDataSubRecord(); |
73 |
ftCmo.setObjectType( CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT); |
74 |
ftCmo.setObjectId( (short) 1024 ); |
75 |
ftCmo.setLocked( true ); |
76 |
ftCmo.setPrintable( true ); |
77 |
ftCmo.setAutofill( true ); |
78 |
ftCmo.setAutoline( true ); |
79 |
record.addSubRecord(ftCmo); |
80 |
EndSubRecord ftEnd = new EndSubRecord(); |
81 |
record.addSubRecord(ftEnd); |
82 |
|
83 |
//serialize and read again |
84 |
byte [] recordBytes = record.serialize(); |
85 |
//cut off the record header |
86 |
byte [] bytes = new byte[recordBytes.length-4]; |
87 |
System.arraycopy(recordBytes, 4, bytes, 0, bytes.length); |
88 |
|
89 |
record = new ObjRecord(new TestcaseRecordInputStream(ObjRecord.sid, (short)bytes.length, bytes)); |
90 |
List subrecords = record.getSubRecords(); |
91 |
assertEquals( 2, subrecords.size() ); |
92 |
assertTrue( subrecords.get(0) instanceof CommonObjectDataSubRecord); |
93 |
assertTrue( subrecords.get(1) instanceof EndSubRecord ); |
94 |
} |
95 |
} |