TestRawDataBlock.cs :  » GUI » NPOI » TestCases » POIFS » Storage » 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 » POIFS » Storage » TestRawDataBlock.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.
==================================================================== */

/* ================================================================
 * About NPOI
 * Author: Tony Qu 
 * Author's email: tonyqus (at) gmail.com 
 * Author's Blog: tonyqus.wordpress.com.cn (wp.tonyqus.cn)
 * HomePage: http://www.codeplex.com/npoi
 * Contributors:
 * 
 * ==============================================================*/

using System;
using System.IO;
using System.Collections;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NPOI.POIFS.Storage;
using NPOI.Util;
using NPOI.POIFS.FileSystem;
using TestCases.Util;
using TestCases.POIFS.FileSystem;

namespace TestCases.POIFS.Storage{
    /**
     * Class to Test RawDataBlock functionality
     *
     * @author Marc Johnson
     */
    [TestClass]
    public class TestRawDataBlock
    {
        public TestRawDataBlock()
        {

        }

        /**
         * Test creating a normal RawDataBlock
         *
         * @exception IOException
         */
        [TestMethod]
        public void TestNormalConstructor()
        {
            byte[] data = new byte[512];

            for (int j = 0; j < 512; j++)
            {
                data[j] = (byte)j;
            }
            RawDataBlock block = new RawDataBlock(new MemoryStream(data));

            Assert.IsTrue(!block.EOF, "Should not be at EOF");
            byte[] out_data = block.Data;

            Assert.AreEqual(data.Length, out_data.Length, "Should be same Length");
            for (int j = 0; j < 512; j++)
            {
                Assert.AreEqual(data[j],
                             out_data[j], "Should be same value at offset " + j);
            }
        }

        /**
         * Test creating an empty RawDataBlock
         *
         * @exception IOException
         */
        [TestMethod]
        public void TestEmptyConstructor()
        {
            byte[] data = new byte[0];
            RawDataBlock block = new RawDataBlock(new MemoryStream(data));

            Assert.IsTrue(block.EOF, "Should be at EOF");
            try
            {
                byte[] a = block.Data;
            }
            catch (IOException )
            {

                // as expected
            }
        }

        /**
         * Test creating a short RawDataBlock
         * Will trigger a warning, but no longer an IOException,
         *  as people seem to have "valid" truncated files
         */
        [TestMethod]
        public void TestShortConstructor()
        {
            //// Get the logger to be used
            DummyPOILogger logger = (DummyPOILogger)POILogFactory.GetLogger(typeof(RawDataBlock));
            Assert.AreEqual(0, logger.logged.Count);

            // Test for various data sizes
            for (int k = 1; k <= 512; k++)
            {
                byte[] data = new byte[k];

                for (int j = 0; j < k; j++)
                {
                    data[j] = (byte)j;
                }
                RawDataBlock block = null;

                logger.Reset();
                Assert.AreEqual(0, logger.logged.Count);

                // Have it created
                block = new RawDataBlock(new MemoryStream(data));
                Assert.IsNotNull(block);

                // Check for the warning Is there for <512
                if (k < 512)
                {
                    Assert.AreEqual(
                            1, logger.logged.Count, "Warning on " + k + " byte short block"
                    );

                    // Build the expected warning message, and check
                    String bts = k + " byte";
                    if (k > 1)
                    {
                        bts += "s";
                    }

                    Assert.AreEqual(
                            (String)logger.logged[0],
                            "7 - Unable to read entire block; " + bts + " read before EOF; expected 512 bytes. Your document was either written by software that ignores the spec, or has been truncated!"
                    );
                }
                else
                {
                    Assert.AreEqual(0, logger.logged.Count);
                }
            }
        }

        /**
         * Tests that when using a slow input stream, which
         *  won't return a full block at a time, we don't
         *  incorrectly think that there's not enough data
         */
        [TestMethod]
        public void TestSlowInputStream()
        {
            // Get the logger to be used
            DummyPOILogger logger = (DummyPOILogger)POILogFactory.GetLogger(typeof(RawDataBlock));
            Assert.AreEqual(0, logger.logged.Count);

            // Test for various ok data sizes
            for (int k = 1; k < 512; k++)
            {
                byte[] data = new byte[512];
                for (int j = 0; j < data.Length; j++)
                {
                    data[j] = (byte)j;
                }

                // Shouldn't complain, as there Is enough data,
                //  even if it dribbles through
                RawDataBlock block =
                    new RawDataBlock(new SlowInputStream(data, 512));   //k is changed to 512
                Assert.IsFalse(block.EOF);
            }

            // But if there wasn't enough data available, will
            //  complain
            for (int k = 1; k < 512; k++)
            {
                byte[] data = new byte[511];
                for (int j = 0; j < data.Length; j++)
                {
                    data[j] = (byte)j;
                }

                logger.Reset();
                Assert.AreEqual(0, logger.logged.Count);

                // Should complain, as there Isn't enough data
                RawDataBlock block =
                    new RawDataBlock(new SlowInputStream(data, k));
                Assert.IsNotNull(block);
                Assert.AreEqual(
                        1, logger.logged.Count, "Warning on " + k + " byte short block"
                );
            }
        }


    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.