2
0

PackagePartTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 ([email protected])
  24. //
  25. using System;
  26. using System.Collections.Generic;
  27. using System.IO;
  28. using System.IO.Packaging;
  29. using System.Linq;
  30. using System.Text;
  31. using NUnit.Framework;
  32. using System.Xml;
  33. namespace MonoTests.System.IO.Packaging {
  34. [TestFixture]
  35. public class PackagePartTest : TestBase {
  36. const string PackagePropertiesType = "application/vnd.openxmlformats-package.core-properties+xml";
  37. //static void Main (string [] args)
  38. //{
  39. // PackagePartTest t = new PackagePartTest ();
  40. // t.FixtureSetup ();
  41. // t.Setup ();
  42. // t.AddThreeParts ();
  43. //}
  44. [Test]
  45. [ExpectedException (typeof (ArgumentException))]
  46. public void AddAbsoluteUri ()
  47. {
  48. package.CreatePart (new Uri ("file://lo/asd.asm", UriKind.Absolute), "aa/aa");
  49. }
  50. [Test]
  51. [Category ("NotWorking")]
  52. [Ignore ("This is a bug in the MS implementation. I don't think i can/should replicate it")]
  53. public void AddInvalidPartTwice ()
  54. {
  55. try {
  56. package.CreatePart (new Uri ("/file1.bmp", UriKind.Relative), "bmp");
  57. } catch (ArgumentException) {
  58. try {
  59. package.CreatePart (new Uri ("/file1.bmp", UriKind.Relative), "bmp");
  60. } catch (InvalidOperationException) {
  61. Assert.AreEqual (1, package.GetParts ().Count (), "Need to be buggy and return null");
  62. Assert.AreEqual (null, package.GetParts ().ToArray () [0], "Be buggy and add null to the internal list");
  63. return; // Success
  64. }
  65. }
  66. Assert.Fail ("Should have thrown an ArgumentException then InvalidOperationException");
  67. }
  68. [Test]
  69. public void AddThreeParts ()
  70. {
  71. foreach (Uri u in uris)
  72. package.CreatePart (u, "mime/type");
  73. Assert.AreEqual (3, package.GetParts ().Count (), "Should be three parts");
  74. PackagePartCollection c1 = package.GetParts ();
  75. package.CreatePart (new Uri ("/asdasdas", UriKind.Relative), "asa/s");
  76. PackagePartCollection c2 = package.GetParts ();
  77. bool eq = c1 == c2;
  78. Assert.IsNotNull (package.GetPart (new Uri (uris[0].ToString ().ToUpper (), UriKind.Relative)));
  79. }
  80. [Test]
  81. public void CheckPartProperties ()
  82. {
  83. package.CreatePart (new Uri ("/first", UriKind.Relative), "my/a");
  84. package.CreatePart (new Uri ("/second", UriKind.Relative), "my/b", CompressionOption.Maximum);
  85. package.CreatePart (new Uri ("/third", UriKind.Relative), "test/c", CompressionOption.SuperFast);
  86. package.Close ();
  87. package = Package.Open (new MemoryStream (stream.ToArray ()), FileMode.Open, FileAccess.Read);
  88. PackagePart[] parts = package.GetParts ().ToArray ();
  89. Assert.AreEqual (3, parts.Length);
  90. PackagePart part = parts[0];
  91. Assert.AreEqual (CompressionOption.NotCompressed, part.CompressionOption, "Compress option wrong1");
  92. Assert.AreEqual ("my/a", part.ContentType, "Content type wrong1");
  93. Assert.AreEqual (package, part.Package, "Wrong package1");
  94. Assert.AreEqual ("/first", part.Uri.ToString (), "Wrong package selected1");
  95. part = parts[1];
  96. Assert.AreEqual (CompressionOption.Maximum, part.CompressionOption, "Compress option wrong2");
  97. Assert.AreEqual ("my/b", part.ContentType, "Content type wrong2");
  98. Assert.AreEqual (package, part.Package, "Wrong package2");
  99. Assert.AreEqual ("/second", part.Uri.OriginalString, "Wrong package selected2");
  100. part = parts[2];
  101. Assert.AreEqual (CompressionOption.SuperFast, part.CompressionOption, "Compress option wrong3");
  102. Assert.AreEqual ("test/c", part.ContentType, "Content type wrong3");
  103. Assert.AreEqual (package, part.Package, "Wrong package3");
  104. Assert.AreEqual ("/third", part.Uri.ToString (), "Wrong package selected3");
  105. }
  106. [Test]
  107. public void SameExtensionDifferentContentTypeTest ()
  108. {
  109. // FIXME: Ideally we should be opening the zip and checking
  110. // exactly what was written to verify it's correct
  111. using (var stream = new MemoryStream ()) {
  112. var package = Package.Open (stream, FileMode.OpenOrCreate);
  113. package.CreatePart (uris [0], contentType + "1");
  114. package.CreatePart (uris [1], contentType + "2");
  115. package.CreatePart (uris [2], contentType + "2");
  116. package.Close ();
  117. package = Package.Open (new MemoryStream (stream.ToArray ()));
  118. Assert.AreEqual (contentType + "1", package.GetPart (uris [0]).ContentType, "#1");
  119. Assert.AreEqual (contentType + "2", package.GetPart (uris [1]).ContentType, "#2");
  120. Assert.AreEqual (contentType + "2", package.GetPart (uris [2]).ContentType, "#3");
  121. }
  122. }
  123. [Test]
  124. public void SameExtensionSameContentTypeTest ()
  125. {
  126. // FIXME: Ideally we should be opening the zip and checking
  127. // exactly what was written to verify it's correct
  128. using (var stream = new MemoryStream ()) {
  129. var package = Package.Open (stream, FileMode.OpenOrCreate);
  130. package.CreatePart (uris [0], contentType);
  131. package.CreatePart (uris [1], contentType);
  132. package.CreatePart (uris [2], contentType);
  133. package.Close ();
  134. package = Package.Open (new MemoryStream (stream.ToArray ()));
  135. Assert.AreEqual (contentType, package.GetPart (uris [0]).ContentType, "#1");
  136. Assert.AreEqual (contentType, package.GetPart (uris [1]).ContentType, "#2");
  137. Assert.AreEqual (contentType, package.GetPart (uris [2]).ContentType, "#3");
  138. }
  139. }
  140. [Test]
  141. public void CheckPartRelationships ()
  142. {
  143. AddThreeParts ();
  144. Assert.AreEqual (4, package.GetParts ().Count (), "#a");
  145. PackagePart part = package.GetPart (uris [0]);
  146. PackageRelationship r1 = part.CreateRelationship (part.Uri, TargetMode.Internal, "self");
  147. PackageRelationship r2 = package.CreateRelationship (part.Uri, TargetMode.Internal, "fake");
  148. PackageRelationship r3 = package.CreateRelationship (new Uri ("/fake/uri", UriKind.Relative), TargetMode.Internal, "self");
  149. Assert.AreEqual (6, package.GetParts ().Count (), "#b");
  150. Assert.AreEqual (1, part.GetRelationships ().Count (), "#1");
  151. Assert.AreEqual (1, part.GetRelationshipsByType ("self").Count (), "#2");
  152. Assert.AreEqual (r1, part.GetRelationship (r1.Id), "#3");
  153. Assert.AreEqual (2, package.GetRelationships ().Count (), "#4");
  154. Assert.AreEqual (1, package.GetRelationshipsByType ("self").Count (), "#5");
  155. Assert.AreEqual (r3, package.GetRelationship (r3.Id), "#6");
  156. Assert.AreEqual (6, package.GetParts ().Count (), "#c");
  157. Assert.AreEqual (part.Uri, r1.SourceUri, "#7");
  158. Assert.AreEqual (new Uri ("/", UriKind.Relative), r3.SourceUri, "#8");
  159. PackageRelationship r4 = part.CreateRelationship (uris [2], TargetMode.Internal, "other");
  160. Assert.AreEqual (part.Uri, r4.SourceUri);
  161. PackageRelationshipCollection relations = package.GetPart (uris [2]).GetRelationships ();
  162. Assert.AreEqual (0, relations.Count ());
  163. Assert.AreEqual (6, package.GetParts ().Count (), "#d");
  164. }
  165. [Test]
  166. public void CheckIndividualRelationships ()
  167. {
  168. PackagePart part = package.CreatePart (uris [0], contentType);
  169. part.CreateRelationship (uris [1], TargetMode.Internal, "relType");
  170. part.CreateRelationship (uris [2], TargetMode.External, "relType");
  171. package.Flush ();
  172. Assert.AreEqual (2, package.GetParts ().Count(), "#1");
  173. part = package.GetPart (new Uri ("/_rels" + uris [0].ToString () + ".rels", UriKind.Relative));
  174. Assert.IsNotNull (part);
  175. }
  176. [Test]
  177. [ExpectedException (typeof (InvalidOperationException))]
  178. public void DeletePartsAfterAddingRelationships ()
  179. {
  180. CheckPartRelationships ();
  181. foreach (PackagePart p in new List<PackagePart> (package.GetParts ()))
  182. package.DeletePart (p.Uri);
  183. }
  184. [Test]
  185. [ExpectedException (typeof (InvalidOperationException))]
  186. public void DeleteRelsThenParts ()
  187. {
  188. CheckPartRelationships ();
  189. foreach (PackageRelationship r in new List<PackageRelationship> (package.GetRelationships ()))
  190. package.DeleteRelationship (r.Id);
  191. foreach (PackagePart p in new List<PackagePart> (package.GetParts ()))
  192. package.DeletePart (p.Uri);
  193. }
  194. [Test]
  195. public void CreateValidPart ()
  196. {
  197. PackagePart part = package.CreatePart (uris [0], "img/bmp");
  198. }
  199. [Test]
  200. [ExpectedException (typeof (InvalidOperationException))]
  201. public void CreateDuplicatePart ()
  202. {
  203. CreateValidPart ();
  204. CreateValidPart ();
  205. }
  206. [Test]
  207. public void CreateValidPartTwice ()
  208. {
  209. CreateValidPart ();
  210. package.DeletePart (uris [0]);
  211. CreateValidPart ();
  212. Assert.AreEqual (1, package.GetParts ().Count (), "#1");
  213. Assert.AreEqual (uris [0], package.GetParts ().ToArray () [0].Uri, "#2");
  214. package.DeletePart (uris [0]);
  215. package.DeletePart (uris [0]);
  216. package.DeletePart (uris [0]);
  217. }
  218. [Test]
  219. public void IterateParts ()
  220. {
  221. List<PackagePart> parts = new List<PackagePart> ();
  222. parts.Add (package.CreatePart (new Uri ("/a", UriKind.Relative), "mime/type"));
  223. parts.Add (package.CreatePart (new Uri ("/b", UriKind.Relative), "mime/type"));
  224. List<PackagePart> found = new List<PackagePart> (package.GetParts ());
  225. Assert.AreEqual (parts.Count, found.Count, "Invalid number of parts");
  226. Assert.IsTrue (found.Contains (parts [0]), "Doesn't contain first part");
  227. Assert.IsTrue (found.Contains (parts [1]), "Doesn't contain second part");
  228. Assert.IsTrue (found [0] == parts [0] || found [0] == parts [1], "Same object reference should be used");
  229. Assert.IsTrue (found [1] == parts [0] || found [1] == parts [1], "Same object reference should be used");
  230. }
  231. [Test]
  232. [ExpectedException (typeof (ArgumentException))]
  233. public void NoStartingSlashPartialUri ()
  234. {
  235. PackagePart part = package.CreatePart (new Uri ("file1.bmp", UriKind.Relative), "bmp");
  236. }
  237. [Test]
  238. public void PackagePropertiesTest ()
  239. {
  240. package.PackageProperties.Category = "category";
  241. package.PackageProperties.ContentStatus = "status";
  242. package.PackageProperties.ContentType = "contentttype";
  243. package.PackageProperties.Created = new DateTime (2008, 12, 12, 2, 3, 4);
  244. package.PackageProperties.Creator = "mono";
  245. package.PackageProperties.Description = "description";
  246. package.PackageProperties.Identifier = "id";
  247. package.PackageProperties.Keywords = "key words";
  248. package.PackageProperties.Language = "english";
  249. package.PackageProperties.LastModifiedBy = "modified";
  250. package.PackageProperties.LastPrinted = new DateTime (2007, 12, 12, 2, 3, 4);
  251. package.PackageProperties.Modified = new DateTime (2008, 12, 12, 3, 4, 5);
  252. package.PackageProperties.Revision = "reviison";
  253. package.PackageProperties.Subject = "subject";
  254. package.PackageProperties.Title = "title";
  255. package.PackageProperties.Version = "version";
  256. Assert.AreEqual (0, package.GetParts ().Count (), "#1");
  257. package.Flush ();
  258. Assert.AreEqual (2, package.GetParts ().Count (), "#2");
  259. var part = package.GetParts ().Where (p => p.ContentType == PackagePropertiesType).ToList ().First ();
  260. Assert.IsNotNull (part);
  261. Assert.IsTrue (part.Uri.OriginalString.StartsWith ("/package/services/metadata/core-properties/"), "#3");
  262. Assert.IsTrue (part.Uri.OriginalString.EndsWith (".psmdcp"), "#4");
  263. package.Close ();
  264. package = Package.Open (new MemoryStream (stream.ToArray ()));
  265. Assert.AreEqual (package.PackageProperties.Category, "category", "#5");
  266. Assert.AreEqual (package.PackageProperties.ContentStatus, "status", "#6");
  267. Assert.AreEqual (package.PackageProperties.ContentType, "contentttype", "#7");
  268. Assert.AreEqual (package.PackageProperties.Created, new DateTime (2008, 12, 12, 2, 3, 4), "#8");
  269. Assert.AreEqual (package.PackageProperties.Creator, "mono", "#9");
  270. Assert.AreEqual (package.PackageProperties.Description, "description", "#10");
  271. Assert.AreEqual (package.PackageProperties.Identifier, "id", "#11");
  272. Assert.AreEqual (package.PackageProperties.Keywords, "key words", "#12");
  273. Assert.AreEqual (package.PackageProperties.Language, "english", "#13");
  274. Assert.AreEqual (package.PackageProperties.LastModifiedBy, "modified", "#14");
  275. Assert.AreEqual (package.PackageProperties.LastPrinted, new DateTime (2007, 12, 12, 2, 3, 4), "#15");
  276. Assert.AreEqual (package.PackageProperties.Modified, new DateTime (2008, 12, 12, 3, 4, 5), "#16");
  277. Assert.AreEqual (package.PackageProperties.Revision, "reviison", "#17");
  278. Assert.AreEqual (package.PackageProperties.Subject, "subject", "#18");
  279. Assert.AreEqual (package.PackageProperties.Title, "title", "#19");
  280. Assert.AreEqual (package.PackageProperties.Version, "version", "#20");
  281. }
  282. [Test]
  283. public void PackagePropertiestest2 ()
  284. {
  285. package.PackageProperties.Title = "TITLE";
  286. package.Close ();
  287. package = Package.Open (new MemoryStream (stream.ToArray ()));
  288. Assert.AreEqual (null, package.PackageProperties.Category, "#1");
  289. }
  290. [Test]
  291. [ExpectedException (typeof (IOException))]
  292. public void PackagePropertiesReadonly ()
  293. {
  294. PackagePropertiesTest ();
  295. package.PackageProperties.Title = "Title";
  296. }
  297. [Test]
  298. public void PartExists ()
  299. {
  300. CreateValidPart ();
  301. Assert.IsNotNull (package.GetPart (uris [0]), "Part could not be found");
  302. Assert.IsTrue (package.PartExists (uris [0]), "Part didn't exist");
  303. Assert.AreEqual (1, package.GetParts ().Count (), "Only one part");
  304. }
  305. [Test]
  306. public void RemoveThreeParts ()
  307. {
  308. AddThreeParts ();
  309. foreach (PackagePart p in new List<PackagePart> (package.GetParts ()))
  310. package.DeletePart (p.Uri);
  311. Assert.AreEqual (0, package.GetParts ().Count (), "Should contain no parts");
  312. }
  313. [Test]
  314. [ExpectedException (typeof (InvalidOperationException))]
  315. public void RemoveThreePartsBreak ()
  316. {
  317. AddThreeParts ();
  318. foreach (PackagePart p in package.GetParts ())
  319. package.DeletePart (p.Uri);
  320. }
  321. [Test]
  322. public void CheckContentTypes ()
  323. {
  324. Assert.IsFalse (package.PartExists(new Uri ("[Content_Types].xml", UriKind.Relative)));
  325. }
  326. }
  327. }