PackagePartFileTests.cs :  » 2.6.4-mono-.net-core » System.IO » System » IO » Packaging » Tests » 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 » 2.6.4 mono .net core » System.IO 
System.IO » System » IO » Packaging » Tests » PackagePartFileTests.cs
using System;
using System.IO;
using System.IO.Packaging;
using NUnit.Framework;

namespace System.IO.Packaging.Tests{
  [TestFixture]
  public class PackagePartFileTests
  {
    string path;

    [SetUp]
    public void Setup()
    {
      path = Path.GetTempFileName();
      Package.Open(path, FileMode.Create).Close();
    }

    [TearDown]
    public void Teardown()
    {
      File.Delete(path);
    }

    [Test]
    public void TestFileMode ()
    {
      Uri uri =new Uri("/somepart.xml", UriKind.Relative);
      FileMode[] modes =  { FileMode.Open, FileMode.OpenOrCreate, FileMode.Create };
      using (Package package = Package.Open(path))
      {
        PackagePart part;
        foreach (FileMode mode in modes)
        {
          part = package.CreatePart(uri, "application/xml");
          part.GetStream(mode, FileAccess.Write);
          package.DeletePart(uri);
        }

        part = package.CreatePart(uri, "application/xml");
        foreach (FileMode mode in modes)
          part.GetStream(mode, FileAccess.Write);
      }
    }

    [Test]
    public void TestFileMode2()
    {
      Uri uri = new Uri("/somepart.xml", UriKind.Relative);
      FileMode[] modes = { FileMode.Create, FileMode.CreateNew, FileMode.Truncate, FileMode.Append };
      FileMode[] otherModes = { FileMode.Open, FileMode.OpenOrCreate };

      using (Package package = Package.Open(path))
      {
        PackagePart part = package.CreatePart(uri, "application/xml");
        foreach (FileMode mode in modes)
        {
          try
          {
            part.GetStream(mode, FileAccess.Read);
            throw new Exception (string.Format ("Should not be able to open with: {0}", mode));
          }
          catch (IOException)
          {
            // This should be thrown
          }
        }

        foreach (FileMode mode in otherModes)
        {
          Stream s = part.GetStream(mode, FileAccess.Read);
          Assert.IsTrue(s.CanRead);
          Assert.IsFalse(s.CanWrite);
        }
      }
    }

    [Test]
    [ExpectedException(typeof(ArgumentException))]
    public void CreateNewPackagePart()
    {
      using (Package package = Package.Open(path))
      {
        PackagePart part = package.CreatePart(new Uri("/somepart.xml", UriKind.Relative), "application/xml");
        // CreateNew is not supported
        part.GetStream(FileMode.CreateNew, FileAccess.Write);
      }
    }

    [Test]
    [ExpectedException(typeof(ArgumentException))]
    public void TruncatePackagePart()
    {
      using (Package package = Package.Open(path))
      {
        PackagePart part = package.CreatePart(new Uri("/somepart.xml", UriKind.Relative), "application/xml");
        // CreateNew is not supported
        part.GetStream(FileMode.Truncate, FileAccess.Write);
      }
    }

    [Test]
    [ExpectedException(typeof(ArgumentException))]
    public void AppendPackagePart()
    {
      using (Package package = Package.Open(path))
      {
        PackagePart part = package.CreatePart(new Uri("/somepart.xml", UriKind.Relative), "application/xml");
        // CreateNew is not supported
        part.GetStream(FileMode.Append, FileAccess.Write);
      }
    }

    [Test]
    public void TestOverwrite()
    {
      using (Package package = Package.Open(path))
      {
        PackagePart part = package.CreatePart(new Uri("/Uri.xml", UriKind.Relative), "content/type");
        Stream s = part.GetStream(FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter sw = new StreamWriter(s);
        sw.Write("<test>aaaaaaa</test>");
        sw.Flush();

        Stream s5 = part.GetStream(FileMode.Create, FileAccess.ReadWrite);
        StreamWriter sw2 = new StreamWriter(s5);
        sw2.Write("<test>bbb</test>");
        sw2.Flush();

        // Verify that the part got overwritten correctly.
        Stream s6 = part.GetStream();
        StreamReader sr = new StreamReader(s6);
        Assert.AreEqual("<test>bbb</test>", sr.ReadToEnd());
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.