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
31 using NUnit.Framework;
\r
33 namespace System.IO.Packaging.Tests {
\r
36 public class PackagePartStreamTests : TestBase {
\r
39 List<PackagePart> Parts = new List<PackagePart> ();
\r
40 static void Main (string [] args)
\r
42 PackagePartStreamTests t = new PackagePartStreamTests ();
\r
45 t.CheckFlushTest ();
\r
48 public override void FixtureSetup ()
\r
50 base.FixtureSetup ();
\r
52 Random r = new Random ();
\r
53 buffer = new byte [1000];
\r
54 r.NextBytes (buffer);
\r
57 public override void Setup ()
\r
62 foreach (Uri u in uris)
\r
63 Parts.Add (package.CreatePart (u, "mime/type"));
\r
67 public void SameStreamTest ()
\r
69 Assert.AreEqual (0, stream.Length, "#a");
\r
71 Assert.IsTrue (stream.Length > 0, "#b");
\r
73 Stream s1 = Parts [0].GetStream ();
\r
74 Stream s2 = Parts [0].GetStream ();
\r
75 Assert.IsFalse (s1 == s2, "#1");
\r
77 Assert.AreEqual (1, s1.Position, "#2");
\r
78 Assert.AreEqual (0, s2.Position, "#3");
\r
79 Assert.AreEqual (s1.Length, s2.Length, "#4");
\r
80 Assert.AreEqual (5, s2.ReadByte (), "#5");
\r
82 Assert.AreEqual (0, s1.Length);
\r
86 public void NoFlushTest ()
\r
88 Parts [0].GetStream ().Write (buffer, 0, buffer.Length);
\r
89 Assert.AreEqual (0, stream.Length);
\r
93 public void FlushIndividualTest ()
\r
95 Parts [0].GetStream ().Write (buffer, 0, buffer.Length);
\r
96 Parts [0].GetStream ().Flush ();
\r
97 Assert.AreEqual (buffer.Length, Parts [0].GetStream ().Length, "#1");
\r
98 Assert.IsTrue (stream.Length > buffer.Length, "#2");
\r
102 [Category ("NotWorking")]
\r
103 [Ignore ("This test only works on MS.NET. Behaviour probably not easily replicatable")]
\r
104 public void FlushPackageTest1 ()
\r
106 FlushIndividualTest ();
\r
108 long count = stream.Length;
\r
110 Assert.IsTrue (stream.Length > count, "#1");
\r
114 [Category ("NotWorking")]
\r
115 [Ignore ("This test is useless i believe")]
\r
116 public void FlushOnlyPackage ()
\r
120 long count = stream.Length;
\r
123 // FlushPackageTest1 ();
\r
124 Assert.AreEqual (count, stream.Length, "#1");
\r
128 public void GetMultipleStreams ()
\r
130 foreach (PackagePart p in Parts) {
\r
131 p.GetStream ().Write (buffer, 0, buffer.Length);
\r
132 p.GetStream ().Flush ();
\r
133 Stream ssss = p.GetStream ();
\r
134 bool equal = p.GetStream () == p.GetStream ();
\r
137 long position = stream.Length;
\r
141 public void FlushThenTruncate ()
\r
143 Parts [0].GetStream ().Write (buffer, 0, buffer.Length);
\r
145 Assert.IsTrue (stream.Length > buffer.Length, "#1");
\r
147 Parts [0].GetStream ().SetLength (0);
\r
149 Assert.IsTrue (stream.Length < buffer.Length, "#2");
\r
151 long length = stream.Length;
\r
152 foreach (PackagePart p in package.GetParts ().ToArray ())
\r
153 package.DeletePart (p.Uri);
\r
156 Assert.IsTrue (stream.Length < length, "#3");
\r
160 // [Category ("NotWorking")]
\r
161 public void CheckFlushTest ()
\r
163 buffer = new byte [1024 * 1024];
\r
164 Assert.AreEqual (0, stream.Length, "#1");
\r
165 Parts [0].GetStream ().Write (buffer, 0, buffer.Length);
\r
166 Assert.AreEqual (0, stream.Length, "#2");
\r
167 Assert.AreEqual (Parts[0].GetStream ().Length, buffer.Length, "#2b");
\r
168 Parts [1].GetStream ().Write (buffer, 0, buffer.Length);
\r
169 Assert.AreEqual (0, stream.Length, "#3");
\r
170 Assert.AreEqual (Parts[1].GetStream ().Length, buffer.Length, "#3b");
\r
171 Parts [0].GetStream ().Flush ();
\r
172 Assert.IsTrue (stream.Length > buffer.Length * 2, "#4");
\r
173 long count = stream.Length;
\r
176 // FIXME: On MS.NET this works. I don't think it's worth replicating
\r
177 //Assert.IsTrue (count < stream.Length, "#5");
\r
181 public void CheckFlushTest2 ()
\r
183 buffer = new byte [1024 * 1024];
\r
184 Assert.AreEqual (0, stream.Length, "#1");
\r
185 Parts [0].GetStream ().Write (buffer, 0, buffer.Length);
\r
186 Assert.AreEqual (0, stream.Length, "#2");
\r
187 Parts [1].GetStream ().Write (buffer, 0, buffer.Length);
\r
188 Assert.AreEqual (0, stream.Length, "#3");
\r
190 Assert.IsTrue (stream.Length > buffer.Length * 2, "#4");
\r