TestObjRecord.cs :  » GUI » NPOI » TestCases » HSSF » Record » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » NPOI 
NPOI » TestCases » HSSF » Record » TestObjRecord.cs
/* ====================================================================
   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 is1 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.
==================================================================== */

namespace TestCases.HSSF.Record{
    using System;
    using System.Collections;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using NPOI.HSSF.Record;
    using NPOI.Util;

    /**
     * Tests the serialization and deserialization of the ObjRecord class works correctly.
     * Test data taken directly from a real Excel file.
     *
     * @author Yegor Kozlov
     */
    [TestClass]
    public class TestObjRecord
    {
        /**
         * OBJ record data containing two sub-records.
         * The data taken directly from a real Excel file.
         *
         * [OBJ]
         *     [ftCmo]
         *     [ftEnd]
         */
        private 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
        // TODO - this data seems to require two extra bytes padding. not sure where original file is.
        // it's not bug 38607 attachment 17639
    };

        private static byte[] recdataNeedingPadding = {
      21, 0, 18, 0, 0, 0, 1, 0, 17, 96, 0, 0, 0, 0, 56, 111, unchecked((byte)-52), 3, 0, 0, 0, 0, 6, 0, 2, 0, 0, 0, 0, 0, 0, 0
    };
        [TestInitialize]
        public void TestLoad()
        {
            ObjRecord record = new ObjRecord(TestcaseRecordInputStream.Create(ObjRecord.sid, recdata));

            Assert.AreEqual(26, record.RecordSize - 4);

            IList subrecords = record.GetSubRecords();
            Assert.AreEqual(2, subrecords.Count);
            Assert.IsTrue(subrecords[0] is CommonObjectDataSubRecord);
            Assert.IsTrue(subrecords[1] is EndSubRecord);

        }
        [TestMethod]
        public void TestStore()
        {
            ObjRecord record = new ObjRecord(TestcaseRecordInputStream.Create(ObjRecord.sid, recdata));

            byte[] recordBytes = record.Serialize();
            Assert.AreEqual(26, recordBytes.Length - 4);
            byte[] subData = new byte[recdata.Length];
            System.Array.Copy(recordBytes, 4, subData, 0, subData.Length);
            Assert.IsTrue(NPOI.Util.Arrays.Equals(recdata, subData));
        }
        [TestMethod]
        public void TestConstruct()
        {
            ObjRecord record = new ObjRecord();
            CommonObjectDataSubRecord ftCmo = new CommonObjectDataSubRecord();
            ftCmo.ObjectType = (CommonObjectType.COMMENT);
            ftCmo.ObjectId = ((short)1024);
            ftCmo.IsLocked = (true);
            ftCmo.IsPrintable = (true);
            ftCmo.IsAutoFill = (true);
            ftCmo.IsAutoline = (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.Array.Copy(recordBytes, 4, bytes, 0, bytes.Length);

            record = new ObjRecord(TestcaseRecordInputStream.Create(ObjRecord.sid, bytes));
            IList subrecords = record.GetSubRecords();
            Assert.AreEqual(2, subrecords.Count);
            Assert.IsTrue(subrecords[0] is CommonObjectDataSubRecord);
            Assert.IsTrue(subrecords[1] is EndSubRecord);
        }
        [TestMethod]
        public void TestReadWriteWithPadding_bug45133()
        {
            ObjRecord record = new ObjRecord(TestcaseRecordInputStream.Create(ObjRecord.sid, recdataNeedingPadding));

            if (record.RecordSize == 34)
            {
                throw new AssertFailedException("Identified bug 45133");
            }

            Assert.AreEqual(36, record.RecordSize);

            IList subrecords = record.GetSubRecords();
            Assert.AreEqual(3, subrecords.Count);
            Assert.AreEqual(typeof(CommonObjectDataSubRecord), subrecords[0].GetType());
            Assert.AreEqual(typeof(GroupMarkerSubRecord), subrecords[1].GetType());
            Assert.AreEqual(typeof(EndSubRecord), subrecords[2].GetType());
        }
        /**
 * Check that ObjRecord tolerates and preserves padding to a 4-byte boundary
 * (normally padding is to a 2-byte boundary).
 */
        [TestMethod]
        public void Test4BytePadding()
        {
            // actual data from file saved by Excel 2007
            byte[] data = HexRead.ReadFromString(""
                    + "15 00 12 00  1E 00 01 00  11 60 B4 6D  3C 01 C4 06 "
                    + "49 06 00 00  00 00 00 00  00 00 00 00");
            // this data seems to have 2 extra bytes of padding more than usual
            // the total may have been padded to the nearest quad-byte length
            RecordInputStream in1 = TestcaseRecordInputStream.Create(ObjRecord.sid, data);
            // check read OK
            ObjRecord record = new ObjRecord(in1);
            // check that it re-serializes to the same data
            byte[] ser = record.Serialize();
            TestcaseRecordInputStream.ConfirmRecordEncoding(ObjRecord.sid, data, ser);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.