PageRenderTime 60ms CodeModel.GetById 19ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 1ms

/mcs/class/WindowsBase/Test/System.IO.Packaging/FakePackageTests.cs

https://github.com/iainlane/mono
C# | 217 lines | 149 code | 32 blank | 36 comment | 0 complexity | d6f68498cbf52bf14b27408bf513f061 MD5 | raw file
  1// Permission is hereby granted, free of charge, to any person obtaining
  2// a copy of this software and associated documentation files (the
  3// "Software"), to deal in the Software without restriction, including
  4// without limitation the rights to use, copy, modify, merge, publish,
  5// distribute, sublicense, and/or sell copies of the Software, and to
  6// permit persons to whom the Software is furnished to do so, subject to
  7// the following conditions:
  8// 
  9// The above copyright notice and this permission notice shall be
 10// included in all copies or substantial portions of the Software.
 11// 
 12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 13// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 14// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 15// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 16// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 17// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 18// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 19//
 20// Copyright (c) 2008 Novell, Inc. (http://www.novell.com)
 21//
 22// Authors:
 23// Alan McGovern (amcgovern@novell.com)
 24//
 25
 26
 27using System;
 28using System.Collections.Generic;
 29using System.Linq;
 30using System.Text;
 31using NUnit.Framework;
 32
 33namespace System.IO.Packaging.Tests
 34{
 35
 36    [TestFixture]
 37    public class FakePackageTests : TestBase
 38    {
 39
 40        //static void Main (string [] args)
 41        //{
 42        //    FakePackageTests t = new FakePackageTests ();
 43        //    t.FixtureSetup ();
 44        //    t.Setup ();
 45        //    t.RelationshipPartGetStream ();
 46        //}
 47
 48        private new FakePackage package;
 49        public override void Setup()
 50        {
 51            package = new FakePackage(FileAccess.ReadWrite, true);
 52        }
 53
 54        [Test]
 55        public void CheckAutomaticParts()
 56        {
 57            package.CreatePart(uris[0], contentType);
 58            Assert.AreEqual(1, package.CreatedParts.Count(), "#1");
 59            Assert.AreEqual(uris[0], package.CreatedParts[0], "#2");
 60            Assert.AreEqual(0, package.DeletedParts.Count(), "#3");
 61            Assert.AreEqual(1, package.GetParts().Count(), "#4");
 62        }
 63
 64        [Test]
 65        public void CheckAutomaticParts2()
 66        {
 67            package.CreateRelationship(uris[0], TargetMode.External, "relationship");
 68            Assert.AreEqual(1, package.CreatedParts.Count(), "#1");
 69            Assert.AreEqual(relationshipUri, package.CreatedParts[0], "#2");
 70            Assert.AreEqual(0, package.DeletedParts.Count(), "#3");
 71            Assert.AreEqual(1, package.GetParts().Count(), "#4");
 72
 73            PackagePart p = package.GetPart(relationshipUri);
 74            Assert.AreEqual(package, p.Package, "#5");
 75            Assert.AreEqual(CompressionOption.NotCompressed, p.CompressionOption, "#6");
 76            Assert.AreEqual("application/vnd.openxmlformats-package.relationships+xml", p.ContentType, "#7");
 77        }
 78
 79        [Test]
 80        public void CheckProperties()
 81        {
 82            Assert.AreEqual(0, package.GotParts.Count, "#1");
 83            object o = package.PackageProperties;
 84            Assert.AreEqual(1, package.GotParts.Count, "#2");
 85            Assert.AreEqual("/_rels/.rels", package.GotParts[0].ToString(), "#3");
 86        }
 87
 88        [Test]
 89        public void RelationshipPartGetRelationships()
 90        {
 91            CheckAutomaticParts2();
 92            PackagePart p = package.GetPart(relationshipUri);
 93
 94            try
 95            {
 96                p.CreateRelationship(uris[0], TargetMode.Internal, "asdas");
 97                Assert.Fail("This should fail 1");
 98            }
 99            catch (InvalidOperationException)
100            {
101
102            }
103
104            try
105            {
106                p.DeleteRelationship("aa");
107                Assert.Fail("This should fail 2");
108            }
109            catch (InvalidOperationException)
110            {
111
112            }
113
114            try
115            {
116                p.GetRelationship("id");
117                Assert.Fail("This should fail 3");
118            }
119            catch (InvalidOperationException)
120            {
121
122            }
123
124            try
125            {
126                p.GetRelationships();
127                Assert.Fail("This should fail 4");
128            }
129            catch (InvalidOperationException)
130            {
131
132            }
133
134            try
135            {
136                p.GetRelationshipsByType("type");
137                Assert.Fail("This should fail 5");
138            }
139            catch (InvalidOperationException)
140            {
141
142            }
143
144            try
145            {
146                p.RelationshipExists("id");
147                Assert.Fail("This should fail 6");
148            }
149            catch (InvalidOperationException)
150            {
151
152            }
153        }
154
155        [Test]
156        public void TestProperties()
157        {
158            Assert.IsNotNull(package.PackageProperties, "#1");
159            package.PackageProperties.Title = "Title";
160            package.Flush();
161
162            // the relationship part and packageproperties part
163            Assert.AreEqual(2, package.CreatedParts.Count, "#2");
164        }
165
166        [Test]
167        public void TestWordDoc()
168        {
169            MemoryStream stream = new MemoryStream();
170            Package package = CreateWordDoc(stream);
171            Assert.IsTrue(package.PartExists(new Uri("/word/document.xml", UriKind.Relative)), "#1");
172            Assert.IsTrue(package.RelationshipExists("rel1"), "#2");
173            package.Close();
174            package = Package.Open(new MemoryStream(stream.ToArray()), FileMode.Open);
175            Assert.AreEqual(10, package.GetParts().Count(), "#3");
176            Assert.AreEqual (9, package.GetRelationships ().Count (), "#4");
177            Assert.IsTrue(package.PartExists(new Uri("/word/document.xml", UriKind.Relative)), "#5");
178            Assert.IsTrue(package.RelationshipExists("rel1"), "#6");
179        }
180
181        Package CreateWordDoc(Stream stream)
182        {
183            Package pack = Package.Open(stream, FileMode.Create);
184
185            // Create package parts.
186            PackagePart wordDocument = pack.CreatePart(new Uri("/word/document.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");
187            PackagePart wordNumbering = pack.CreatePart(new Uri("/word/numbering.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml");
188            PackagePart wordStyles = pack.CreatePart(new Uri("/word/styles.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml");
189            PackagePart docPropsApp = pack.CreatePart(new Uri("/docProps/app.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.extended-properties+xml");
190            PackagePart wordSettings = pack.CreatePart(new Uri("/word/settings.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml");
191            PackagePart wordTheme1 = pack.CreatePart(new Uri("/word/theme/theme1.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.theme+xml");
192            PackagePart wordFontTable = pack.CreatePart(new Uri("/word/fontTable.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml");
193            PackagePart wordWebSettings = pack.CreatePart(new Uri("/word/webSettings.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml");
194            PackagePart docPropsCore = pack.CreatePart(new Uri("/docProps/core.xml", UriKind.Relative), "application/vnd.openxmlformats-package.core-properties+xml");
195
196            // Create relationships for package.
197            pack.CreateRelationship(new Uri("docProps/app.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties");
198            pack.CreateRelationship(new Uri("docProps/core.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties");
199            pack.CreateRelationship(new Uri("word/document.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument");
200
201            // Create document relationships.
202            pack.CreateRelationship(new Uri("settings.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings", "rel1");
203            pack.CreateRelationship(new Uri("styles.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles", "rel2");
204            pack.CreateRelationship(new Uri("numbering.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering", "rel3");
205            pack.CreateRelationship(new Uri("theme/theme1.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme", "rel4");
206            pack.CreateRelationship(new Uri("fontTable.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable", "rel5");
207            pack.CreateRelationship(new Uri("webSettings.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings", "rel6");
208
209            // Load some basic data into the different parts.
210            foreach (PackagePart part in package.GetParts())
211                using (Stream s = part.GetStream())
212                    s.Write(new byte[10], 0, 10);
213            
214            return pack;
215        }
216    }
217}