1 // Permission is hereby granted, free of charge, to any person obtaining
\r
2 // a copy of this software and associated documentation files (the
\r
3 // "Software"), to deal in the Software without restriction, including
\r
4 // without limitation the rights to use, copy, modify, merge, publish,
\r
5 // distribute, sublicense, and/or sell copies of the Software, and to
\r
6 // permit persons to whom the Software is furnished to do so, subject to
\r
7 // the following conditions:
\r
9 // The above copyright notice and this permission notice shall be
\r
10 // included in all copies or substantial portions of the Software.
\r
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
20 // Copyright (c) 2008 Novell, Inc. (http://www.novell.com)
\r
23 // Alan McGovern (amcgovern@novell.com)
\r
28 using System.Collections.Generic;
\r
30 using System.IO.Packaging;
\r
33 using NUnit.Framework;
\r
36 namespace MonoTests.System.IO.Packaging {
\r
39 public class PackagePartTest : TestBase {
\r
41 const string PackagePropertiesType = "application/vnd.openxmlformats-package.core-properties+xml";
\r
43 //static void Main (string [] args)
\r
45 // PackagePartTest t = new PackagePartTest ();
\r
46 // t.FixtureSetup ();
\r
49 // t.AddThreeParts ();
\r
52 [ExpectedException (typeof (ArgumentException))]
\r
53 public void AddAbsoluteUri ()
\r
55 package.CreatePart (new Uri ("file://lo/asd.asm", UriKind.Absolute), "aa/aa");
\r
59 [Category ("NotWorking")]
\r
60 [Ignore ("This is a bug in the MS implementation. I don't think i can/should replicate it")]
\r
61 public void AddInvalidPartTwice ()
\r
64 package.CreatePart (new Uri ("/file1.bmp", UriKind.Relative), "bmp");
\r
65 } catch (ArgumentException) {
\r
67 package.CreatePart (new Uri ("/file1.bmp", UriKind.Relative), "bmp");
\r
68 } catch (InvalidOperationException) {
\r
69 Assert.AreEqual (1, package.GetParts ().Count (), "Need to be buggy and return null");
\r
70 Assert.AreEqual (null, package.GetParts ().ToArray () [0], "Be buggy and add null to the internal list");
\r
76 Assert.Fail ("Should have thrown an ArgumentException then InvalidOperationException");
\r
80 public void AddThreeParts ()
\r
82 foreach (Uri u in uris)
\r
83 package.CreatePart (u, "mime/type");
\r
85 Assert.AreEqual (3, package.GetParts ().Count (), "Should be three parts");
\r
86 PackagePartCollection c1 = package.GetParts ();
\r
87 package.CreatePart (new Uri ("/asdasdas", UriKind.Relative), "asa/s");
\r
88 PackagePartCollection c2 = package.GetParts ();
\r
91 Assert.IsNotNull (package.GetPart (new Uri (uris[0].ToString ().ToUpper (), UriKind.Relative)));
\r
95 public void CheckPartProperties ()
\r
97 package.CreatePart (new Uri ("/first", UriKind.Relative), "my/a");
\r
98 package.CreatePart (new Uri ("/second", UriKind.Relative), "my/b", CompressionOption.Maximum);
\r
99 package.CreatePart (new Uri ("/third", UriKind.Relative), "test/c", CompressionOption.SuperFast);
\r
101 package = Package.Open (new MemoryStream (stream.ToArray ()), FileMode.Open, FileAccess.Read);
\r
103 PackagePart[] parts = package.GetParts ().ToArray ();
\r
104 Assert.AreEqual (3, parts.Length);
\r
106 PackagePart part = parts[0];
\r
107 Assert.AreEqual (CompressionOption.NotCompressed, part.CompressionOption, "Compress option wrong1");
\r
108 Assert.AreEqual ("my/a", part.ContentType, "Content type wrong1");
\r
109 Assert.AreEqual (package, part.Package, "Wrong package1");
\r
110 Assert.AreEqual ("/first", part.Uri.ToString (), "Wrong package selected1");
\r
113 Assert.AreEqual (CompressionOption.Maximum, part.CompressionOption, "Compress option wrong2");
\r
114 Assert.AreEqual ("my/b", part.ContentType, "Content type wrong2");
\r
115 Assert.AreEqual (package, part.Package, "Wrong package2");
\r
116 Assert.AreEqual ("/second", part.Uri.OriginalString, "Wrong package selected2");
\r
119 Assert.AreEqual (CompressionOption.SuperFast, part.CompressionOption, "Compress option wrong3");
\r
120 Assert.AreEqual ("test/c", part.ContentType, "Content type wrong3");
\r
121 Assert.AreEqual (package, part.Package, "Wrong package3");
\r
122 Assert.AreEqual ("/third", part.Uri.ToString (), "Wrong package selected3");
\r
126 public void SameExtensionDifferentContentTypeTest ()
\r
128 // FIXME: Ideally we should be opening the zip and checking
\r
129 // exactly what was written to verify it's correct
\r
130 using (var stream = new MemoryStream ()) {
\r
131 var package = Package.Open (stream, FileMode.OpenOrCreate);
\r
132 package.CreatePart (uris [0], contentType + "1");
\r
133 package.CreatePart (uris [1], contentType + "2");
\r
134 package.CreatePart (uris [2], contentType + "2");
\r
137 package = Package.Open (new MemoryStream (stream.ToArray ()));
\r
138 Assert.AreEqual (contentType + "1", package.GetPart (uris [0]).ContentType, "#1");
\r
139 Assert.AreEqual (contentType + "2", package.GetPart (uris [1]).ContentType, "#2");
\r
140 Assert.AreEqual (contentType + "2", package.GetPart (uris [2]).ContentType, "#3");
\r
145 public void SameExtensionSameContentTypeTest ()
\r
147 // FIXME: Ideally we should be opening the zip and checking
\r
148 // exactly what was written to verify it's correct
\r
149 using (var stream = new MemoryStream ()) {
\r
150 var package = Package.Open (stream, FileMode.OpenOrCreate);
\r
151 package.CreatePart (uris [0], contentType);
\r
152 package.CreatePart (uris [1], contentType);
\r
153 package.CreatePart (uris [2], contentType);
\r
156 package = Package.Open (new MemoryStream (stream.ToArray ()));
\r
157 Assert.AreEqual (contentType, package.GetPart (uris [0]).ContentType, "#1");
\r
158 Assert.AreEqual (contentType, package.GetPart (uris [1]).ContentType, "#2");
\r
159 Assert.AreEqual (contentType, package.GetPart (uris [2]).ContentType, "#3");
\r
164 public void CheckPartRelationships ()
\r
167 Assert.AreEqual (4, package.GetParts ().Count (), "#a");
\r
168 PackagePart part = package.GetPart (uris [0]);
\r
169 PackageRelationship r1 = part.CreateRelationship (part.Uri, TargetMode.Internal, "self");
\r
170 PackageRelationship r2 = package.CreateRelationship (part.Uri, TargetMode.Internal, "fake");
\r
171 PackageRelationship r3 = package.CreateRelationship (new Uri ("/fake/uri", UriKind.Relative), TargetMode.Internal, "self");
\r
173 Assert.AreEqual (6, package.GetParts ().Count (), "#b");
\r
174 Assert.AreEqual (1, part.GetRelationships ().Count (), "#1");
\r
175 Assert.AreEqual (1, part.GetRelationshipsByType ("self").Count (), "#2");
\r
176 Assert.AreEqual (r1, part.GetRelationship (r1.Id), "#3");
\r
177 Assert.AreEqual (2, package.GetRelationships ().Count (), "#4");
\r
178 Assert.AreEqual (1, package.GetRelationshipsByType ("self").Count (), "#5");
\r
179 Assert.AreEqual (r3, package.GetRelationship (r3.Id), "#6");
\r
181 Assert.AreEqual (6, package.GetParts ().Count (), "#c");
\r
182 Assert.AreEqual (part.Uri, r1.SourceUri, "#7");
\r
183 Assert.AreEqual (new Uri ("/", UriKind.Relative), r3.SourceUri, "#8");
\r
185 PackageRelationship r4 = part.CreateRelationship (uris [2], TargetMode.Internal, "other");
\r
186 Assert.AreEqual (part.Uri, r4.SourceUri);
\r
188 PackageRelationshipCollection relations = package.GetPart (uris [2]).GetRelationships ();
\r
189 Assert.AreEqual (0, relations.Count ());
\r
190 Assert.AreEqual (6, package.GetParts ().Count (), "#d");
\r
194 public void CheckIndividualRelationships ()
\r
196 PackagePart part = package.CreatePart (uris [0], contentType);
\r
197 part.CreateRelationship (uris [1], TargetMode.Internal, "relType");
\r
198 part.CreateRelationship (uris [2], TargetMode.External, "relType");
\r
201 Assert.AreEqual (2, package.GetParts ().Count(), "#1");
\r
202 part = package.GetPart (new Uri ("/_rels" + uris [0].ToString () + ".rels", UriKind.Relative));
\r
203 Assert.IsNotNull (part);
\r
207 [ExpectedException (typeof (InvalidOperationException))]
\r
208 public void DeletePartsAfterAddingRelationships ()
\r
210 CheckPartRelationships ();
\r
211 foreach (PackagePart p in new List<PackagePart> (package.GetParts ()))
\r
212 package.DeletePart (p.Uri);
\r
216 [ExpectedException (typeof (InvalidOperationException))]
\r
217 public void DeleteRelsThenParts ()
\r
219 CheckPartRelationships ();
\r
220 foreach (PackageRelationship r in new List<PackageRelationship> (package.GetRelationships ()))
\r
221 package.DeleteRelationship (r.Id);
\r
222 foreach (PackagePart p in new List<PackagePart> (package.GetParts ()))
\r
223 package.DeletePart (p.Uri);
\r
227 public void CreateValidPart ()
\r
229 PackagePart part = package.CreatePart (uris [0], "img/bmp");
\r
233 [ExpectedException (typeof (InvalidOperationException))]
\r
234 public void CreateDuplicatePart ()
\r
236 CreateValidPart ();
\r
237 CreateValidPart ();
\r
241 public void CreateValidPartTwice ()
\r
243 CreateValidPart ();
\r
244 package.DeletePart (uris [0]);
\r
245 CreateValidPart ();
\r
247 Assert.AreEqual (1, package.GetParts ().Count (), "#1");
\r
248 Assert.AreEqual (uris [0], package.GetParts ().ToArray () [0].Uri, "#2");
\r
249 package.DeletePart (uris [0]);
\r
250 package.DeletePart (uris [0]);
\r
251 package.DeletePart (uris [0]);
\r
255 public void IterateParts ()
\r
257 List<PackagePart> parts = new List<PackagePart> ();
\r
258 parts.Add (package.CreatePart (new Uri ("/a", UriKind.Relative), "mime/type"));
\r
259 parts.Add (package.CreatePart (new Uri ("/b", UriKind.Relative), "mime/type"));
\r
260 List<PackagePart> found = new List<PackagePart> (package.GetParts ());
\r
261 Assert.AreEqual (parts.Count, found.Count, "Invalid number of parts");
\r
262 Assert.IsTrue (found.Contains (parts [0]), "Doesn't contain first part");
\r
263 Assert.IsTrue (found.Contains (parts [1]), "Doesn't contain second part");
\r
265 Assert.IsTrue (found [0] == parts [0] || found [0] == parts [1], "Same object reference should be used");
\r
266 Assert.IsTrue (found [1] == parts [0] || found [1] == parts [1], "Same object reference should be used");
\r
270 [ExpectedException (typeof (ArgumentException))]
\r
271 public void NoStartingSlashPartialUri ()
\r
273 PackagePart part = package.CreatePart (new Uri ("file1.bmp", UriKind.Relative), "bmp");
\r
277 public void PackagePropertiesTest ()
\r
279 package.PackageProperties.Category = "category";
\r
280 package.PackageProperties.ContentStatus = "status";
\r
281 package.PackageProperties.ContentType = "contentttype";
\r
282 package.PackageProperties.Created = new DateTime (2008, 12, 12, 2, 3, 4);
\r
283 package.PackageProperties.Creator = "mono";
\r
284 package.PackageProperties.Description = "description";
\r
285 package.PackageProperties.Identifier = "id";
\r
286 package.PackageProperties.Keywords = "key words";
\r
287 package.PackageProperties.Language = "english";
\r
288 package.PackageProperties.LastModifiedBy = "modified";
\r
289 package.PackageProperties.LastPrinted = new DateTime (2007, 12, 12, 2, 3, 4);
\r
290 package.PackageProperties.Modified = new DateTime (2008, 12, 12, 3, 4, 5);
\r
291 package.PackageProperties.Revision = "reviison";
\r
292 package.PackageProperties.Subject = "subject";
\r
293 package.PackageProperties.Title = "title";
\r
294 package.PackageProperties.Version = "version";
\r
295 Assert.AreEqual (0, package.GetParts ().Count (), "#1");
\r
297 Assert.AreEqual (2, package.GetParts ().Count (), "#2");
\r
298 var part = package.GetParts ().Where (p => p.ContentType == PackagePropertiesType).ToList ().First ();
\r
299 Assert.IsNotNull (part);
\r
300 Assert.IsTrue (part.Uri.OriginalString.StartsWith ("/package/services/metadata/core-properties/"), "#3");
\r
301 Assert.IsTrue (part.Uri.OriginalString.EndsWith (".psmdcp"), "#4");
\r
303 package = Package.Open (new MemoryStream (stream.ToArray ()));
\r
305 Assert.AreEqual (package.PackageProperties.Category, "category", "#5");
\r
306 Assert.AreEqual (package.PackageProperties.ContentStatus, "status", "#6");
\r
307 Assert.AreEqual (package.PackageProperties.ContentType, "contentttype", "#7");
\r
308 Assert.AreEqual (package.PackageProperties.Created, new DateTime (2008, 12, 12, 2, 3, 4), "#8");
\r
309 Assert.AreEqual (package.PackageProperties.Creator, "mono", "#9");
\r
310 Assert.AreEqual (package.PackageProperties.Description, "description", "#10");
\r
311 Assert.AreEqual (package.PackageProperties.Identifier, "id", "#11");
\r
312 Assert.AreEqual (package.PackageProperties.Keywords, "key words", "#12");
\r
313 Assert.AreEqual (package.PackageProperties.Language, "english", "#13");
\r
314 Assert.AreEqual (package.PackageProperties.LastModifiedBy, "modified", "#14");
\r
315 Assert.AreEqual (package.PackageProperties.LastPrinted, new DateTime (2007, 12, 12, 2, 3, 4), "#15");
\r
316 Assert.AreEqual (package.PackageProperties.Modified, new DateTime (2008, 12, 12, 3, 4, 5), "#16");
\r
317 Assert.AreEqual (package.PackageProperties.Revision, "reviison", "#17");
\r
318 Assert.AreEqual (package.PackageProperties.Subject, "subject", "#18");
\r
319 Assert.AreEqual (package.PackageProperties.Title, "title", "#19");
\r
320 Assert.AreEqual (package.PackageProperties.Version, "version", "#20");
\r
324 public void PackagePropertiestest2 ()
\r
326 package.PackageProperties.Title = "TITLE";
\r
328 package = Package.Open (new MemoryStream (stream.ToArray ()));
\r
329 Assert.AreEqual (null, package.PackageProperties.Category, "#1");
\r
333 [ExpectedException (typeof (IOException))]
\r
334 public void PackagePropertiesReadonly ()
\r
336 PackagePropertiesTest ();
\r
337 package.PackageProperties.Title = "Title";
\r
341 public void PartExists ()
\r
343 CreateValidPart ();
\r
344 Assert.IsNotNull (package.GetPart (uris [0]), "Part could not be found");
\r
345 Assert.IsTrue (package.PartExists (uris [0]), "Part didn't exist");
\r
346 Assert.AreEqual (1, package.GetParts ().Count (), "Only one part");
\r
350 public void RemoveThreeParts ()
\r
353 foreach (PackagePart p in new List<PackagePart> (package.GetParts ()))
\r
354 package.DeletePart (p.Uri);
\r
355 Assert.AreEqual (0, package.GetParts ().Count (), "Should contain no parts");
\r
359 [ExpectedException (typeof (InvalidOperationException))]
\r
360 public void RemoveThreePartsBreak ()
\r
363 foreach (PackagePart p in package.GetParts ())
\r
364 package.DeletePart (p.Uri);
\r
368 public void CheckContentTypes ()
\r
370 Assert.IsFalse (package.PartExists(new Uri ("[Content_Types].xml", UriKind.Relative)));
\r