]> source.dussan.org Git - poi.git/commitdiff
Yegor's patch from bug #41242
authorNick Burch <nick@apache.org>
Fri, 5 Jan 2007 17:38:54 +0000 (17:38 +0000)
committerNick Burch <nick@apache.org>
Fri, 5 Jan 2007 17:38:54 +0000 (17:38 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@493098 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/ObjRecord.java
src/testcases/org/apache/poi/hssf/record/TestObjRecord.java [new file with mode: 0644]

index 34ccef755101d530572a1d77659104c055c815a4..0dc48836fbad3c5ca786a3e7a266bc1cdf19a46c 100644 (file)
@@ -81,13 +81,25 @@ public class ObjRecord
         subrecords = new ArrayList();
         //Check if this can be continued, if so then the
         //following wont work properly
+        int subSize = 0;
         byte[] subRecordData = in.readRemainder();
         RecordInputStream subRecStream = new RecordInputStream(new ByteArrayInputStream(subRecordData));
         while(subRecStream.hasNextRecord()) {
           subRecStream.nextRecord();
           Record subRecord = SubRecord.createSubRecord(subRecStream);
+          subSize += subRecord.getRecordSize();
           subrecords.add(subRecord);
         }
+
+        /**
+         * Check if the RecordInputStream skipped EndSubRecord,
+         * if it did then append it explicitly.
+         * See Bug 41242 for details.
+         */
+        if (subRecordData.length - subSize == 4){
+            subrecords.add(new EndSubRecord());
+        }
+
         /* JMH the size present/not present in the code below
            needs to be considered in the RecordInputStream??
         int pos = offset;
diff --git a/src/testcases/org/apache/poi/hssf/record/TestObjRecord.java b/src/testcases/org/apache/poi/hssf/record/TestObjRecord.java
new file mode 100644 (file)
index 0000000..5a792ba
--- /dev/null
@@ -0,0 +1,95 @@
+
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hssf.record;
+
+import junit.framework.*;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Tests the serialization and deserialization of the ObjRecord class works correctly.
+ * Test data taken directly from a real Excel file.
+ *
+ * @author Yegor Kozlov
+ */
+public class TestObjRecord extends TestCase {
+    /**
+     * OBJ record data containing two sub-records.
+     * The data taken directly from a real Excel file.
+     *
+     * [OBJ]
+     *     [ftCmo]
+     *     [ftEnd]
+     */
+    public static byte[] recdata = {
+        0x15, 0x00, 0x12, 0x00, 0x06, 0x00, 0x01, 0x00, 0x11, 0x60,
+        (byte)0xF4, 0x02, 0x41, 0x01, 0x14, 0x10, 0x1F, 0x02, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    };
+
+
+    public void testLoad() throws Exception {
+        ObjRecord record = new ObjRecord(new TestcaseRecordInputStream(ObjRecord.sid, (short)recdata.length, recdata));
+
+        assertEquals( recdata.length, record.getRecordSize() - 4);
+
+        List subrecords = record.getSubRecords();
+        assertEquals( 2, subrecords.size() );
+        assertTrue( subrecords.get(0) instanceof CommonObjectDataSubRecord);
+        assertTrue( subrecords.get(1) instanceof EndSubRecord );
+
+    }
+
+    public void testStore() {
+        ObjRecord record = new ObjRecord(new TestcaseRecordInputStream(ObjRecord.sid, (short)recdata.length, recdata));
+
+        byte [] recordBytes = record.serialize();
+        assertEquals(recdata.length, recordBytes.length - 4);
+        byte[] subData = new byte[recordBytes.length - 4];
+        System.arraycopy(recordBytes, 4, subData, 0, subData.length);
+        assertTrue(Arrays.equals(recdata, subData));
+    }
+
+    public void testConstruct() {
+        ObjRecord record = new ObjRecord();
+        CommonObjectDataSubRecord ftCmo = new CommonObjectDataSubRecord();
+        ftCmo.setObjectType( CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT);
+        ftCmo.setObjectId( (short) 1024 );
+        ftCmo.setLocked( true );
+        ftCmo.setPrintable( true );
+        ftCmo.setAutofill( true );
+        ftCmo.setAutoline( true );
+        record.addSubRecord(ftCmo);
+        EndSubRecord ftEnd = new EndSubRecord();
+        record.addSubRecord(ftEnd);
+
+        //serialize and read again
+        byte [] recordBytes = record.serialize();
+        //cut off the record header
+        byte [] bytes = new byte[recordBytes.length-4];
+        System.arraycopy(recordBytes, 4, bytes, 0, bytes.length);
+
+        record = new ObjRecord(new TestcaseRecordInputStream(ObjRecord.sid, (short)bytes.length, bytes));
+        List subrecords = record.getSubRecords();
+        assertEquals( 2, subrecords.size() );
+        assertTrue( subrecords.get(0) instanceof CommonObjectDataSubRecord);
+        assertTrue( subrecords.get(1) instanceof EndSubRecord );
+    }
+}