EncodingAdv.cs :  » Persistence-Frameworks » FileHelpers-Library » FileHelpersTests » CommonTests » 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 » CommonTests » EncodingAdv.cs
using System;
using System.Text;
using NUnit.Framework;
using FileHelpers;
using System.Net;
using System.IO;

namespace FileHelpersTests.CommonTests{

    [TestFixture]
    public class EncodingAdvanced
    {
      private const string MSWSDataUrl_Format = "http://data.msws.net/temp_precip/temp_precip{0}.txt";//http://data.msws.net/temp_precip/temp_precip7-20-06.txt
      private const string MSWSDataURL_DateFormat = "M-d-yy";

      [Test]
      public void GetMSWSReportsFromFile_20060709_28Records()
      {
        MSWSDailyReportRecord[] res = null;            
        FileHelperEngine engine = new FileHelperEngine(typeof(MSWSDailyReportRecord));
          res = (MSWSDailyReportRecord[]) Common.ReadTest(engine, @"Good\EncodingAdv1.txt");
            
        Assert.AreEqual(res.Length, 28);
      }

      [Test]
      public void GetMSWSReportsFromFile_20060720_32Records()
      {
        MSWSDailyReportRecord[] res = null;
        FileHelperEngine engine = new FileHelperEngine(typeof(MSWSDailyReportRecord));
        res = (MSWSDailyReportRecord[]) Common.ReadTest(engine, @"Good\EncodingAdv2.txt");

        Assert.AreEqual(res.Length, 32);
      }

      [Test]
      public void GetMSWSReportsFromURL_AsData_20060709_28Records()
      {
        DateTime date = new DateTime(2006, 7, 20);
        string url = string.Format(MSWSDataUrl_Format, date.ToString(MSWSDataURL_DateFormat));
        MSWSDailyReportRecord[] res = null;
        FileHelperEngine engine = new FileHelperEngine(typeof(MSWSDailyReportRecord));
          byte[] data;
        using (WebClient webClient = new WebClient())
        {
          //webClient.Encoding = System.Text.Encoding.ASCII;                
          data = webClient.DownloadData(url);
          System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
          string dataString = encoding.GetString(data);
          res = (MSWSDailyReportRecord[]) engine.ReadString(dataString);
        }
        
        Assert.AreEqual(res.Length, 32);
      }

      [Test]
      public void GetMSWSReportsFromURL_AsString_20060709_28Records()
      {
        DateTime date = new DateTime(2006, 7, 20);
        string url = string.Format(MSWSDataUrl_Format, date.ToString(MSWSDataURL_DateFormat));
        MSWSDailyReportRecord[] res = null;
        FileHelperEngine engine = new FileHelperEngine(typeof(MSWSDailyReportRecord));

          using (WebClient webClient = new WebClient())
          {
            webClient.DownloadFile(url, "tempotemp.txt");
            res = (MSWSDailyReportRecord[]) engine.ReadFile("tempotemp.txt");
          }

        Assert.AreEqual(res.Length, 32);
      }

      [Test]
      public void GetMSWSReportsFromURL_AsStream_20060709_28Records()
      {
        DateTime date = new DateTime(2006, 7, 20);
        string url = string.Format(MSWSDataUrl_Format, date.ToString(MSWSDataURL_DateFormat));
        MSWSDailyReportRecord[] res = null;
        FileHelperEngine engine = new FileHelperEngine(typeof(MSWSDailyReportRecord));

          // make request 
          HttpWebRequest webReq = null;
        HttpWebResponse webResp = null;
        StreamReader reader = null;
        try
        {
          webReq = (HttpWebRequest)HttpWebRequest.Create(url);
          webResp = (HttpWebResponse)webReq.GetResponse();
          Encoding encode = Encoding.GetEncoding("utf-8");
          reader = new StreamReader(webResp.GetResponseStream(), encode);
          res = (MSWSDailyReportRecord[]) engine.ReadStream(reader);
        }
        catch 
        {
          throw;
        }
        finally
        {
          if (webReq != null) webReq = null;
          if (webResp != null) webResp.Close();
          if (reader != null) reader.Close();
        }
                        
        Assert.AreEqual(res.Length, 32);
      }

    }

  [FixedLengthRecordAttribute()]
  [IgnoreFirst(7)]
  [IgnoreLast(5)]
  public sealed class MSWSDailyReportRecord
  {
    [FieldFixedLength(26)]
    public String Location;
    [FieldFixedLength(12)]
    public String County;
    [FieldFixedLength(5)]
    public int Elev;
    [FieldFixedLength(4)]
    public int Hi;
    [FieldFixedLength(4)]
    public int Lo;

    [FieldFixedLength(6)]
    [FieldTrim(TrimMode.Both)]
    public string PCPN;
    [FieldFixedLength(5)]
    public decimal SNOW;
    [FieldFixedLength(5)]
    public decimal SNDPT;
    [FieldFixedLength(6)]
    public decimal MONTH;

  }

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