Merge pull request #2024 from BogdanovKirill/webrequesttest
[mono.git] / mcs / class / corlib / Test / System.IO / StreamWriterTest.cs
index 7dc9285482ee880c7b48e208aca3813accc38607..661cffc18c61370723ed9b5137ce483534128833 100644 (file)
@@ -9,13 +9,92 @@ using NUnit.Framework;
 using System;
 using System.IO;
 using System.Text;
+using System.Threading;
 
 namespace MonoTests.System.IO
 {
+       [TestFixture]
+       public class StreamWriterTest
+       {
+               class MockStream : Stream
+               {
+                       bool canRead, canSeek, canWrite;
+                       public event Action OnFlush;
+                       public event Func<byte[], int, int, int> OnRead;
+                       public event Action<byte[], int, int> OnWrite;
+                       long length;
+
+                       public MockStream (bool canRead, bool canSeek, bool canWrite)
+                       {
+                               this.canRead = canRead;
+                               this.canSeek = canSeek;
+                               this.canWrite = canWrite;
+                       }
+
+                       public override bool CanRead {
+                               get {
+                                       return canRead;
+                               }
+                       }
+
+                       public override bool CanSeek {
+                               get {
+                                       return canSeek;
+                               }
+                       }
+
+                       public override bool CanWrite {
+                               get {
+                                       return canWrite;
+                               }
+                       }
+
+                       public override void Flush ()
+                       {
+                               if (OnFlush != null)
+                                       OnFlush ();
+                       }
+
+                       public override long Length {
+                               get {
+                                       return length;
+                               }
+                       }
+
+                       public override long Position {
+                               get {
+                                       throw new NotImplementedException ();
+                               }
+                               set {
+                                       throw new NotImplementedException ();
+                               }
+                       }
+
+                       public override int Read (byte[] buffer, int offset, int count)
+                       {
+                               if (OnRead != null)
+                                       return OnRead (buffer, offset, count);
+
+                               return -1;
+                       }
+
+                       public override long Seek (long offset, SeekOrigin origin)
+                       {
+                               throw new NotImplementedException ();
+                       }
+
+                       public override void SetLength (long value)
+                       {
+                               this.length = value;
+                       }
+
+                       public override void Write (byte[] buffer, int offset, int count)
+                       {
+                               if (OnWrite != null)
+                                       OnWrite (buffer, offset, count);
+                       }
+               }
 
-[TestFixture]
-public class StreamWriterTest
-{
        static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
        private string _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
        private string _thisCodeFileName = TempFolder + Path.DirectorySeparatorChar + "StreamWriterTest.temp";
@@ -388,9 +467,7 @@ public class StreamWriterTest
                        Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
                        Assert.IsNull (ex.InnerException, "#A3");
                        Assert.IsNotNull (ex.Message, "#A4");
-#if NET_2_0
                        Assert.AreEqual ("bufferSize", ex.ParamName, "#A5");
-#endif
                }
 
                try {
@@ -401,9 +478,7 @@ public class StreamWriterTest
                        Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
                        Assert.IsNull (ex.InnerException, "#B3");
                        Assert.IsNotNull (ex.Message, "#B4");
-#if NET_2_0
                        Assert.AreEqual ("bufferSize", ex.ParamName, "#B5");
-#endif
                }
        }
 
@@ -975,7 +1050,56 @@ public class StreamWriterTest
                w = new StreamWriter (ms, Encoding.UTF8);
                Assert.AreEqual (5, ms.Position, "#2");
        }
-       
+
+#if NET_4_5
+       [Test]
+       public void FlushAsync ()
+       {
+               ManualResetEvent mre = new ManualResetEvent (false);
+               var m = new MockStream(true, false, true);
+               var w = new StreamWriter (m);
+               w.Write(1);
+               Assert.AreEqual (0L, m.Length, "#1");
+               var t = w.WriteLineAsync ();
+               Assert.IsTrue (t.Wait (1000), "#2");
+               Assert.IsTrue (w.FlushAsync ().Wait (1000), "#3");
+       }
+
+       [Test]
+       public void KeepOpenWithDispose ()
+       {
+               var ms = new MemoryStream ();
+               using (StreamWriter writer = new StreamWriter (ms, new UTF8Encoding (false), 4096, true)) {
+                       writer.Write ('X');
+               }
+
+               Assert.AreEqual (1, ms.Length);
+       }
+
+       [Test]
+       public void WriteAsync ()
+       {
+               var m = new MockStream(true, false, true);
+               var w = new StreamWriter (m);
+
+               var t = w.WriteAsync ("v");
+               Assert.IsTrue (t.Wait (1000), "#1");
+
+               t = w.WriteAsync ((string) null);
+               Assert.IsTrue (t.Wait (1000), "#2");
+
+               t = w.WriteLineAsync ("line");
+               Assert.IsTrue (t.Wait (1000), "#3");
+
+               t = w.WriteLineAsync ((string) null);
+               Assert.IsTrue (t.Wait (1000), "#4");
+
+               t = w.WriteLineAsync ('c');
+               Assert.IsTrue (t.Wait (1000), "#5");
+       }
+
+#endif
+
        // TODO - Write - test errors, functionality tested in TestFlush.
 }
 }