MultiRecord.cs :  » Persistence-Frameworks » FileHelpers-Library » FileHelpersTests » 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 » Persistence Frameworks » FileHelpers Library 
FileHelpers Library » FileHelpersTests » MultiRecord.cs
using System;
using System.Collections;
using FileHelpers;
using FileHelpers.MasterDetail;
using NUnit.Framework;
using System.IO;

namespace FileHelpersTests{
  [TestFixture]
  public class MultiRecords
  {
    MultiRecordEngine engine;

    [Test]
    public void MultpleRecordsFile()
    {
      engine = new MultiRecordEngine(new RecordTypeSelector(CustomSelector), typeof(OrdersVerticalBar), typeof(CustomersSemiColon), typeof(SampleType));

            object[] res = engine.ReadFile(Common.TestPath(@"Good\MultiRecord1.txt"));

            Assert.AreEqual(12, res.Length);
            Assert.AreEqual(12, engine.TotalRecords);

      Assert.AreEqual(typeof(OrdersVerticalBar), res[0].GetType());
      Assert.AreEqual(typeof(OrdersVerticalBar), res[1].GetType());
      Assert.AreEqual(typeof(CustomersSemiColon), res[2].GetType());
      Assert.AreEqual(typeof(SampleType), res[5].GetType());
    }

    [Test]
    public void MultpleRecordsFileAsync()
    {
      engine = new MultiRecordEngine(new RecordTypeSelector(CustomSelector), typeof(OrdersVerticalBar), typeof(CustomersSemiColon), typeof(SampleType));

      ArrayList res = new ArrayList();
      engine.BeginReadFile(Common.TestPath(@"Good\MultiRecord1.txt"));
      foreach (object o in engine)
      {
        res.Add(o);
      }

      Assert.AreEqual(12, res.Count);
      Assert.AreEqual(12, engine.TotalRecords);

      Assert.AreEqual(typeof(OrdersVerticalBar), res[0].GetType());
      Assert.AreEqual(typeof(OrdersVerticalBar), res[1].GetType());
      Assert.AreEqual(typeof(CustomersSemiColon), res[2].GetType());
      Assert.AreEqual(typeof(SampleType), res[5].GetType());
    }


    [Test]
    public void MultpleRecordsWriteAsync()
    {
      engine = new MultiRecordEngine(new RecordTypeSelector(CustomSelector), typeof(OrdersVerticalBar), typeof(CustomersSemiColon), typeof(SampleType));

      object[] records = engine.ReadFile(Common.TestPath(@"Good\MultiRecord1.txt"));

      engine.BeginWriteFile("tempoMulti.txt");
      foreach (object o in records)
      {
        engine.WriteNext(o);
      }
      engine.Close();
      File.Delete("tempoMulti.txt");


      object[] res = engine.ReadFile(Common.TestPath(@"Good\MultiRecord1.txt"));

      Assert.AreEqual(12, res.Length);
      Assert.AreEqual(12, engine.TotalRecords);

      Assert.AreEqual(typeof(OrdersVerticalBar), res[0].GetType());
      Assert.AreEqual(typeof(OrdersVerticalBar), res[1].GetType());
      Assert.AreEqual(typeof(CustomersSemiColon), res[2].GetType());
      Assert.AreEqual(typeof(SampleType), res[5].GetType());
    }


    [Test]
    [ExpectedException(typeof(FileHelpersException))]
    public void MultpleRecordsFileAsyncBad()
    {
      engine = new MultiRecordEngine(typeof(OrdersVerticalBar), typeof(CustomersSemiColon), typeof(SampleType));
      engine.RecordSelector = new RecordTypeSelector(CustomSelector);

      foreach (object o in engine)
      {
        o.ToString();
      }
    }
    
    [Test]
    public void MultpleRecordsFileRW()
    {
      engine = new MultiRecordEngine(typeof(OrdersVerticalBar), typeof(CustomersSemiColon), typeof(SampleType));
      engine.RecordSelector = new RecordTypeSelector(CustomSelector);

      object[] res2 = engine.ReadFile(Common.TestPath(@"Good\MultiRecord1.txt"));

      Assert.AreEqual(12, res2.Length);
      Assert.AreEqual(12, engine.TotalRecords);

      engine.WriteFile("tempMR.txt", res2);
      object[] res = engine.ReadFile("tempMR.txt");
      File.Delete("tempMR.txt");

      Assert.AreEqual(12, res.Length);
      Assert.AreEqual(12, engine.TotalRecords);

      Assert.AreEqual(typeof(OrdersVerticalBar), res[0].GetType());
      Assert.AreEqual(typeof(OrdersVerticalBar), res[1].GetType());
      Assert.AreEqual(typeof(CustomersSemiColon), res[2].GetType());
      Assert.AreEqual(typeof(SampleType), res[5].GetType());
    }

    
    [Test]
    [ExpectedException(typeof(BadUsageException))]
    public void NoTypes()
    {
      engine = new MultiRecordEngine(new Type[] {});
    }

    [Test]
    [ExpectedException(typeof(BadUsageException))]
    public void NullTypeArray()
    {
      engine = new MultiRecordEngine((Type[])null);
    }
        
    [Test]
    public void NoSelector()
    {
      engine = new MultiRecordEngine(typeof(CustomersVerticalBar), typeof(CustomersTab));
    }

    [Test]
    [ExpectedException(typeof(BadUsageException))]
    public void TwiceSameType()
    {
      engine = new MultiRecordEngine(typeof(CustomersVerticalBar), typeof(CustomersVerticalBar));
    }

    [Test]
    [ExpectedException(typeof(BadUsageException))]
    public void OneType()
    {
      engine = new MultiRecordEngine(typeof(CustomersVerticalBar));
    }

    [Test]
    [ExpectedException(typeof(BadUsageException))]
    public void NullTypes()
    {
      engine = new MultiRecordEngine(typeof(CustomersVerticalBar), null);
      engine.RecordSelector = new RecordTypeSelector(CustomSelector);
    }

        Type CustomSelector(MultiRecordEngine engine, string record)
        {
            if (Char.IsLetter(record[0]))
                return typeof(CustomersSemiColon);
      else if (record.Length == 14)
        return typeof(SampleType);
            else
                return typeof(OrdersVerticalBar);
        }

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