View | Details | Raw Unified | Return to bug 41242
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/record/ObjRecord.java (+12 lines)
Lines 81-93 Link Here
81
        subrecords = new ArrayList();
81
        subrecords = new ArrayList();
82
        //Check if this can be continued, if so then the
82
        //Check if this can be continued, if so then the
83
        //following wont work properly
83
        //following wont work properly
84
        int subSize = 0;
84
        byte[] subRecordData = in.readRemainder();
85
        byte[] subRecordData = in.readRemainder();
85
        RecordInputStream subRecStream = new RecordInputStream(new ByteArrayInputStream(subRecordData));
86
        RecordInputStream subRecStream = new RecordInputStream(new ByteArrayInputStream(subRecordData));
86
        while(subRecStream.hasNextRecord()) {
87
        while(subRecStream.hasNextRecord()) {
87
          subRecStream.nextRecord();
88
          subRecStream.nextRecord();
88
          Record subRecord = SubRecord.createSubRecord(subRecStream);
89
          Record subRecord = SubRecord.createSubRecord(subRecStream);
90
          subSize += subRecord.getRecordSize();
89
          subrecords.add(subRecord);
91
          subrecords.add(subRecord);
90
        }
92
        }
93
94
        /**
95
         * Check if the RecordInputStream skipped EndSubRecord,
96
         * if it did then append it explicitly.
97
         * See Bug 41242 for details.
98
         */
99
        if (subRecordData.length - subSize == 4){
100
            subrecords.add(new EndSubRecord());
101
        }
102
91
        /* JMH the size present/not present in the code below
103
        /* JMH the size present/not present in the code below
92
           needs to be considered in the RecordInputStream??
104
           needs to be considered in the RecordInputStream??
93
        int pos = offset;
105
        int pos = offset;
(-)src/testcases/org/apache/poi/hssf/record/TestObjRecord.java (+95 lines)
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
}

Return to bug 41242