PageRenderTime 161ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/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. using System;
  26. using System.Collections.Generic;
  27. using System.Linq;
  28. using System.Text;
  29. using NUnit.Framework;
  30. namespace System.IO.Packaging.Tests
  31. {
  32. [TestFixture]
  33. public class FakePackageTests : TestBase
  34. {
  35. //static void Main (string [] args)
  36. //{
  37. // FakePackageTests t = new FakePackageTests ();
  38. // t.FixtureSetup ();
  39. // t.Setup ();
  40. // t.RelationshipPartGetStream ();
  41. //}
  42. private new FakePackage package;
  43. public override void Setup()
  44. {
  45. package = new FakePackage(FileAccess.ReadWrite, true);
  46. }
  47. [Test]
  48. public void CheckAutomaticParts()
  49. {
  50. package.CreatePart(uris[0], contentType);
  51. Assert.AreEqual(1, package.CreatedParts.Count(), "#1");
  52. Assert.AreEqual(uris[0], package.CreatedParts[0], "#2");
  53. Assert.AreEqual(0, package.DeletedParts.Count(), "#3");
  54. Assert.AreEqual(1, package.GetParts().Count(), "#4");
  55. }
  56. [Test]
  57. public void CheckAutomaticParts2()
  58. {
  59. package.CreateRelationship(uris[0], TargetMode.External, "relationship");
  60. Assert.AreEqual(1, package.CreatedParts.Count(), "#1");
  61. Assert.AreEqual(relationshipUri, package.CreatedParts[0], "#2");
  62. Assert.AreEqual(0, package.DeletedParts.Count(), "#3");
  63. Assert.AreEqual(1, package.GetParts().Count(), "#4");
  64. PackagePart p = package.GetPart(relationshipUri);
  65. Assert.AreEqual(package, p.Package, "#5");
  66. Assert.AreEqual(CompressionOption.NotCompressed, p.CompressionOption, "#6");
  67. Assert.AreEqual("application/vnd.openxmlformats-package.relationships+xml", p.ContentType, "#7");
  68. }
  69. [Test]
  70. public void CheckProperties()
  71. {
  72. Assert.AreEqual(0, package.GotParts.Count, "#1");
  73. object o = package.PackageProperties;
  74. Assert.AreEqual(1, package.GotParts.Count, "#2");
  75. Assert.AreEqual("/_rels/.rels", package.GotParts[0].ToString(), "#3");
  76. }
  77. [Test]
  78. public void RelationshipPartGetRelationships()
  79. {
  80. CheckAutomaticParts2();
  81. PackagePart p = package.GetPart(relationshipUri);
  82. try
  83. {
  84. p.CreateRelationship(uris[0], TargetMode.Internal, "asdas");
  85. Assert.Fail("This should fail 1");
  86. }
  87. catch (InvalidOperationException)
  88. {
  89. }
  90. try
  91. {
  92. p.DeleteRelationship("aa");
  93. Assert.Fail("This should fail 2");
  94. }
  95. catch (InvalidOperationException)
  96. {
  97. }
  98. try
  99. {
  100. p.GetRelationship("id");
  101. Assert.Fail("This should fail 3");
  102. }
  103. catch (InvalidOperationException)
  104. {
  105. }
  106. try
  107. {
  108. p.GetRelationships();
  109. Assert.Fail("This should fail 4");
  110. }
  111. catch (InvalidOperationException)
  112. {
  113. }
  114. try
  115. {
  116. p.GetRelationshipsByType("type");
  117. Assert.Fail("This should fail 5");
  118. }
  119. catch (InvalidOperationException)
  120. {
  121. }
  122. try
  123. {
  124. p.RelationshipExists("id");
  125. Assert.Fail("This should fail 6");
  126. }
  127. catch (InvalidOperationException)
  128. {
  129. }
  130. }
  131. [Test]
  132. public void TestProperties()
  133. {
  134. Assert.IsNotNull(package.PackageProperties, "#1");
  135. package.PackageProperties.Title = "Title";
  136. package.Flush();
  137. // the relationship part and packageproperties part
  138. Assert.AreEqual(2, package.CreatedParts.Count, "#2");
  139. }
  140. [Test]
  141. public void TestWordDoc()
  142. {
  143. MemoryStream stream = new MemoryStream();
  144. Package package = CreateWordDoc(stream);
  145. Assert.IsTrue(package.PartExists(new Uri("/word/document.xml", UriKind.Relative)), "#1");
  146. Assert.IsTrue(package.RelationshipExists("rel1"), "#2");
  147. package.Close();
  148. package = Package.Open(new MemoryStream(stream.ToArray()), FileMode.Open);
  149. Assert.AreEqual(10, package.GetParts().Count(), "#3");
  150. Assert.AreEqual (9, package.GetRelationships ().Count (), "#4");
  151. Assert.IsTrue(package.PartExists(new Uri("/word/document.xml", UriKind.Relative)), "#5");
  152. Assert.IsTrue(package.RelationshipExists("rel1"), "#6");
  153. }
  154. Package CreateWordDoc(Stream stream)
  155. {
  156. Package pack = Package.Open(stream, FileMode.Create);
  157. // Create package parts.
  158. PackagePart wordDocument = pack.CreatePart(new Uri("/word/document.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");
  159. PackagePart wordNumbering = pack.CreatePart(new Uri("/word/numbering.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml");
  160. PackagePart wordStyles = pack.CreatePart(new Uri("/word/styles.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml");
  161. PackagePart docPropsApp = pack.CreatePart(new Uri("/docProps/app.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.extended-properties+xml");
  162. PackagePart wordSettings = pack.CreatePart(new Uri("/word/settings.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml");
  163. PackagePart wordTheme1 = pack.CreatePart(new Uri("/word/theme/theme1.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.theme+xml");
  164. PackagePart wordFontTable = pack.CreatePart(new Uri("/word/fontTable.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml");
  165. PackagePart wordWebSettings = pack.CreatePart(new Uri("/word/webSettings.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml");
  166. PackagePart docPropsCore = pack.CreatePart(new Uri("/docProps/core.xml", UriKind.Relative), "application/vnd.openxmlformats-package.core-properties+xml");
  167. // Create relationships for package.
  168. pack.CreateRelationship(new Uri("docProps/app.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties");
  169. pack.CreateRelationship(new Uri("docProps/core.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties");
  170. pack.CreateRelationship(new Uri("word/document.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument");
  171. // Create document relationships.
  172. pack.CreateRelationship(new Uri("settings.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings", "rel1");
  173. pack.CreateRelationship(new Uri("styles.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles", "rel2");
  174. pack.CreateRelationship(new Uri("numbering.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering", "rel3");
  175. pack.CreateRelationship(new Uri("theme/theme1.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme", "rel4");
  176. pack.CreateRelationship(new Uri("fontTable.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable", "rel5");
  177. pack.CreateRelationship(new Uri("webSettings.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings", "rel6");
  178. // Load some basic data into the different parts.
  179. foreach (PackagePart part in package.GetParts())
  180. using (Stream s = part.GetStream())
  181. s.Write(new byte[10], 0, 10);
  182. return pack;
  183. }
  184. }
  185. }