TestEventRecordFactory.cs :  » GUI » NPOI » TestCases » HSSF » EventModel » 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 » EventModel » TestEventRecordFactory.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 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.
==================================================================== */

namespace TestCases.HSSF.EventModel{
    using System;
    using System.IO;
    using System.Collections;

    using NPOI.HSSF;
    using NPOI.HSSF.EventModel;
    using TestCases.HSSF.Record;
    using NPOI.HSSF.Record;
    using NPOI.POIFS.FileSystem;

    using Microsoft.VisualStudio.TestTools.UnitTesting;


    /**
     * enclosing_type describe the purpose here
     * 
     * @author Andrew C. Oliver acoliver@apache.org
     * @author Csaba Nagy (ncsaba at yahoo dot com)
     */
    [TestClass]
    public class TestEventRecordFactory
    {


        class ERFListener1 : ERFListener
        {
            bool[] wascalled;
            public ERFListener1(ref bool[] wascalled)
            {
                this.wascalled = wascalled;
            }

            public bool ProcessRecord(Record rec)
            {
                wascalled[0] = true;
                Assert.IsTrue(
                           (rec.Sid == BOFRecord.sid),
                           "must be BOFRecord got SID=" + rec.Sid);
                return true;
            }
        }

        /**
         * Tests that the records can be Processed and properly return 
         * values.
         */
        [TestMethod]
        public void TestProcessRecords()
        {
            bool[] wascalled = { false, }; // hack to pass boolean by ref into inner class

            ERFListener listener = new ERFListener1(ref wascalled);
            ArrayList param = new ArrayList();
            param.Add(BOFRecord.sid);
            EventRecordFactory factory = new EventRecordFactory(listener, param);

            BOFRecord bof = new BOFRecord();
            bof.Build = ((short)0);
            bof.BuildYear = ((short)1999);
            bof.RequiredVersion = (123);
            bof.Type = (BOFRecord.TYPE_WORKBOOK);
            bof.Version = ((short)0x06);
            bof.HistoryBitMask = (BOFRecord.HISTORY_MASK);

            EOFRecord eof = EOFRecord.instance;
            byte[] bytes = new byte[bof.RecordSize + eof.RecordSize];
            int offset = 0;
            offset = bof.Serialize(offset, bytes);
            offset = eof.Serialize(offset, bytes);

            factory.ProcessRecords(new MemoryStream(bytes));
            Assert.IsTrue(wascalled[0], "The record listener must be called");
        }

        /**
         * Tests that the create record function returns a properly 
         * constructed record in the simple case.
         */
        [TestMethod]
        public void TestCreateRecord()
        {
            BOFRecord bof = new BOFRecord();
            bof.Build = ((short)0);
            bof.BuildYear = ((short)1999);
            bof.RequiredVersion = (123);
            bof.Type = (BOFRecord.TYPE_WORKBOOK);
            bof.Version = ((short)0x06);
            bof.HistoryBitMask = (BOFRecord.HISTORY_MASK);

            byte[] bytes = bof.Serialize();

            Record[] records = RecordFactory.CreateRecord(TestcaseRecordInputStream.Create(bytes));

            Assert.IsTrue(records.Length == 1, "record.Length must be 1, was =" + records.Length);
            Assert.IsTrue(CompareRec(bof, records[0]), "record is the same");

        }

        /**
         * Compare the Serialized bytes of two records are equal
         * @param first the first record to Compare
         * @param second the second record to Compare
         * @return boolean whether or not the record where equal
         */
        private static bool CompareRec(Record first, Record second)
        {
            byte[] rec1 = first.Serialize();
            byte[] rec2 = second.Serialize();

            if (rec1.Length != rec2.Length)
            {
                return false;
            }
            for (int k = 0; k < rec1.Length; k++)
            {
                if (rec1[k] != rec2[k])
                {
                    return false;
                }
            }
            return true;
        }


        /**
         * Tests that the create record function returns a properly 
         * constructed record in the case of a continued record.
         * TODO - need a real world example to put in a unit Test
         */
        public void TestCreateContinuedRecord()
        {
            //  fail("not implemented");
        }



        class ERFListener2 : ERFListener
        {
            private String[] expectedRecordTypes = {
              typeof(UnknownRecord).Name,
              typeof(ContinueRecord).Name,
              typeof(ContinueRecord).Name
          };
            byte[] data;
            int[] recCnt;
            int[] offset;
            public ERFListener2(ref byte[] data, ref int[] recCnt, ref int[] offset)
            {
                this.data = data;
                this.recCnt = recCnt;
                this.offset = offset;
            }
            public bool ProcessRecord(Record rec)
            {
                // System.out.println(rec.toString());
                Assert.AreEqual(
                  expectedRecordTypes[recCnt[0]],
                  rec.GetType().Name,
                  "Record type"
                );
                CompareData(rec, "Record " + recCnt + ": ");
                recCnt[0]++;
                return true;
            }
            private void CompareData(Record record, String message)
            {
                byte[] recData = record.Serialize();
                for (int i = 0; i < recData.Length; i++)
                {
                    Assert.AreEqual(recData[i], data[offset[0]++], message + " data byte " + i);
                }
            }
        }
        /**
         * Test NAME:  Test Creating ContinueRecords After Unknown Records From An InputStream <P>
         * OBJECTIVE:  Test that the RecordFactory given an InputStream
         *             constructs the expected records.<P>
         * SUCCESS:    Record factory creates the expected records.<P>
         * FAILURE:    The wrong records are created or contain the wrong values <P>
         *
         */
        [TestMethod]
        public void TestContinuedUnknownRecord()
        {
            byte[] data = {
            0, unchecked((byte)-1), 0, 0, // an unknown record with 0 Length
            0x3C , 0, 3, 0, 1, 2, 3, // a continuation record with 3 bytes of data
            0x3C , 0, 1, 0, 4 // one more continuation record with 1 byte of data
        };

            int[] recCnt =  {0} ;
            int[] offset =  {0} ;
            ERFListener listener = new ERFListener2(ref data, ref recCnt,ref offset);
            ArrayList sids = new ArrayList(2);
            sids.Add((short)-256);
            sids.Add((short)0x3C);

            EventRecordFactory factory = new EventRecordFactory(listener, sids);

            factory.ProcessRecords(new MemoryStream(data));
            Assert.AreEqual(3, recCnt[0], "nr. of Processed records");
            Assert.AreEqual(data.Length, offset[0], "nr. of Processed bytes");
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.