PackagePartStreamTests.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. namespace MonoTests.System.IO.Packaging {
  33. [TestFixture]
  34. public class PackagePartStreamTests : TestBase {
  35. byte [] buffer;
  36. List<PackagePart> Parts = new List<PackagePart> ();
  37. static void Main (string [] args)
  38. {
  39. PackagePartStreamTests t = new PackagePartStreamTests ();
  40. t.FixtureSetup ();
  41. t.Setup ();
  42. t.CheckFlushTest ();
  43. }
  44. public override void FixtureSetup ()
  45. {
  46. base.FixtureSetup ();
  47. Random r = new Random ();
  48. buffer = new byte [1000];
  49. r.NextBytes (buffer);
  50. }
  51. public override void Setup ()
  52. {
  53. base.Setup ();
  54. Parts.Clear ();
  55. foreach (Uri u in uris)
  56. Parts.Add (package.CreatePart (u, "mime/type"));
  57. }
  58. [Test]
  59. public void SameStreamTest ()
  60. {
  61. Assert.AreEqual (0, stream.Length, "#a");
  62. package.Flush ();
  63. Assert.IsTrue (stream.Length > 0, "#b");
  64. Stream s1 = Parts [0].GetStream ();
  65. Stream s2 = Parts [0].GetStream ();
  66. Assert.IsFalse (s1 == s2, "#1");
  67. s1.WriteByte (5);
  68. Assert.AreEqual (1, s1.Position, "#2");
  69. Assert.AreEqual (0, s2.Position, "#3");
  70. Assert.AreEqual (s1.Length, s2.Length, "#4");
  71. Assert.AreEqual (5, s2.ReadByte (), "#5");
  72. s2.SetLength (0);
  73. Assert.AreEqual (0, s1.Length);
  74. }
  75. [Test]
  76. public void NoFlushTest ()
  77. {
  78. Parts [0].GetStream ().Write (buffer, 0, buffer.Length);
  79. Assert.AreEqual (0, stream.Length);
  80. }
  81. [Test]
  82. public void FlushIndividualTest ()
  83. {
  84. Parts [0].GetStream ().Write (buffer, 0, buffer.Length);
  85. Parts [0].GetStream ().Flush ();
  86. Assert.AreEqual (buffer.Length, Parts [0].GetStream ().Length, "#1");
  87. Assert.IsTrue (stream.Length > buffer.Length, "#2");
  88. }
  89. [Test]
  90. [Category ("NotWorking")]
  91. [Ignore ("This test only works on MS.NET. Behaviour probably not easily replicatable")]
  92. public void FlushPackageTest1 ()
  93. {
  94. FlushIndividualTest ();
  95. long count = stream.Length;
  96. package.Flush ();
  97. Assert.IsTrue (stream.Length > count, "#1");
  98. }
  99. [Test]
  100. [Category ("NotWorking")]
  101. [Ignore ("This test is useless i believe")]
  102. public void FlushOnlyPackage ()
  103. {
  104. NoFlushTest ();
  105. package.Flush ();
  106. long count = stream.Length;
  107. TearDown ();
  108. Setup ();
  109. // FlushPackageTest1 ();
  110. Assert.AreEqual (count, stream.Length, "#1");
  111. }
  112. [Test]
  113. public void GetMultipleStreams ()
  114. {
  115. foreach (PackagePart p in Parts) {
  116. p.GetStream ().Write (buffer, 0, buffer.Length);
  117. p.GetStream ().Flush ();
  118. Stream ssss = p.GetStream ();
  119. bool equal = p.GetStream () == p.GetStream ();
  120. stream.Flush ();
  121. }
  122. long position = stream.Length;
  123. }
  124. [Test]
  125. public void FlushThenTruncate ()
  126. {
  127. Parts [0].GetStream ().Write (buffer, 0, buffer.Length);
  128. package.Flush ();
  129. Assert.IsTrue (stream.Length > buffer.Length, "#1");
  130. Parts [0].GetStream ().SetLength (0);
  131. package.Flush ();
  132. Assert.IsTrue (stream.Length < buffer.Length, "#2");
  133. long length = stream.Length;
  134. foreach (PackagePart p in package.GetParts ().ToArray ())
  135. package.DeletePart (p.Uri);
  136. package.Flush ();
  137. Assert.IsTrue (stream.Length < length, "#3");
  138. }
  139. [Test]
  140. // [Category ("NotWorking")]
  141. public void CheckFlushTest ()
  142. {
  143. buffer = new byte [1024 * 1024];
  144. Assert.AreEqual (0, stream.Length, "#1");
  145. Parts [0].GetStream ().Write (buffer, 0, buffer.Length);
  146. Assert.AreEqual (0, stream.Length, "#2");
  147. Assert.AreEqual (Parts[0].GetStream ().Length, buffer.Length, "#2b");
  148. Parts [1].GetStream ().Write (buffer, 0, buffer.Length);
  149. Assert.AreEqual (0, stream.Length, "#3");
  150. Assert.AreEqual (Parts[1].GetStream ().Length, buffer.Length, "#3b");
  151. Parts [0].GetStream ().Flush ();
  152. Assert.IsTrue (stream.Length > buffer.Length * 2, "#4");
  153. long count = stream.Length;
  154. package.Flush ();
  155. // FIXME: On MS.NET this works. I don't think it's worth replicating
  156. //Assert.IsTrue (count < stream.Length, "#5");
  157. }
  158. [Test]
  159. public void CheckFlushTest2 ()
  160. {
  161. buffer = new byte [1024 * 1024];
  162. Assert.AreEqual (0, stream.Length, "#1");
  163. Parts [0].GetStream ().Write (buffer, 0, buffer.Length);
  164. Assert.AreEqual (0, stream.Length, "#2");
  165. Parts [1].GetStream ().Write (buffer, 0, buffer.Length);
  166. Assert.AreEqual (0, stream.Length, "#3");
  167. package.Flush ();
  168. Assert.IsTrue (stream.Length > buffer.Length * 2, "#4");
  169. }
  170. }
  171. }