Add test
[mono.git] / mcs / class / corlib / Test / System.IO / StreamWriterTest.cs
index 4cd047a8ed993dca393c5b1e201f5951a336341a..2d200f4e38e1f627f8f3e3918edbb101003e7d66 100644 (file)
-// StreamWriterTest.cs - NUnit Test Cases for the SystemIO.StreamWriter class\r
-//\r
-// David Brandt (bucky@keystreams.com)\r
-//\r
-// (C) Ximian, Inc.  http://www.ximian.com\r
-// \r
-\r
-using NUnit.Framework;\r
-using System;\r
-using System.IO;\r
-using System.Text;\r
-\r
-namespace MonoTests.System.IO\r
-{\r
-\r
-public class StreamWriterTest : TestCase\r
-{\r
-       public StreamWriterTest() : \r
-               base ("MonoTests.System.StreamWriterTest testsuite") {}\r
-       public StreamWriterTest(string name) : base(name) {}\r
-\r
-       protected override void SetUp() \r
-       {\r
-       }\r
-\r
-       protected override void TearDown() \r
-       {\r
-       }\r
-\r
-       public static ITest Suite {\r
-               get { \r
-                       return new TestSuite(typeof(StreamWriterTest)); \r
-               }\r
-       }\r
-\r
-       private string _codeFileName = "StreamWriterTest.temp";\r
-       private string _thisCodeFileName = "System.IO/StreamReaderTest.cs";\r
-\r
-       // TODO - ctors\r
-       public void TestCtor1() {\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               StreamWriter r = new StreamWriter((Stream)null);\r
-                       } catch (ArgumentNullException) {\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 1: " + e.ToString());\r
-                       }\r
-                       Assert("null string error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       bool errorThrown = false;\r
-                       FileStream f = new FileStream(_thisCodeFileName, \r
-                                                     FileMode.Open, \r
-                                                     FileAccess.Read);\r
-                       try {\r
-                               StreamWriter r = new StreamWriter(f);\r
-                               r.Close();\r
-                       } catch (ArgumentException) {\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 2: " + e.ToString());\r
-                       }\r
-                       f.Close();\r
-                       Assert("no read error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       FileStream f = new FileStream(_codeFileName, \r
-                                                     FileMode.Append, \r
-                                                     FileAccess.Write);\r
-                       StreamWriter r = new StreamWriter(f);\r
-                       AssertNotNull("no stream writer", r);\r
-                       r.Close();\r
-                       f.Close();\r
-               }\r
-       }\r
-       public void TestCtor2() {\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               StreamWriter r = new StreamWriter("");\r
-                       } catch (ArgumentException) {\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 1: " + e.ToString());\r
-                       }\r
-                       Assert("empty string error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               StreamWriter r = new StreamWriter((string)null);\r
-                       } catch (ArgumentNullException) {\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 2: " + e.ToString());\r
-                       }\r
-                       Assert("null string error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               StreamWriter r = new StreamWriter("nonexistentdir/file");\r
-                       } catch (DirectoryNotFoundException) {\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 3: " + e.ToString());\r
-                       }\r
-                       Assert("dirNotFound error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#");\r
-                       } catch (IOException) {\r
-                               errorThrown = true;\r
-                       } catch (ArgumentException) {\r
-                               // FIXME - the spec says 'IOExc', but the\r
-                               //   compiler says 'ArgExc'...\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 4: " + e.ToString());\r
-                       }\r
-                       Assert("invalid filename error not thrown", errorThrown);\r
-               }\r
-               // TODO - Security/Auth exceptions\r
-               {\r
-                       StreamWriter r = new StreamWriter(_codeFileName);\r
-                       AssertNotNull("no stream writer", r);\r
-                       r.Close();\r
-               }\r
-       }\r
-       public void TestCtor3() {\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               StreamWriter r = new StreamWriter("", false);\r
-                       } catch (ArgumentException) {\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 1: " + e.ToString());\r
-                       }\r
-                       Assert("empty string error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               StreamWriter r = new StreamWriter((string)null, false);\r
-                       } catch (ArgumentNullException) {\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 2: " + e.ToString());\r
-                       }\r
-                       Assert("null string error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               StreamWriter r = new StreamWriter("nonexistentdir/file", false);\r
-                       } catch (DirectoryNotFoundException) {\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 3: " + e.ToString());\r
-                       }\r
-                       Assert("dirNotFound error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#", false);\r
-                       } catch (IOException) {\r
-                               errorThrown = true;\r
-                       } catch (ArgumentException) {\r
-                               // FIXME - the spec says 'IOExc', but the\r
-                               //   compiler says 'ArgExc'...\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 4: " + e.ToString());\r
-                       }\r
-                       Assert("invalid filename error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       StreamWriter r = new StreamWriter(_codeFileName, false);\r
-                       AssertNotNull("no stream writer", r);\r
-                       r.Close();\r
-               }\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               StreamWriter r = new StreamWriter("", true);\r
-                       } catch (ArgumentException) {\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 5: " + e.ToString());\r
-                       }\r
-                       Assert("empty string error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               StreamWriter r = new StreamWriter((string)null, true);\r
-                       } catch (ArgumentNullException) {\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 6: " + e.ToString());\r
-                       }\r
-                       Assert("null string error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               StreamWriter r = new StreamWriter("nonexistentdir/file", true);\r
-                       } catch (DirectoryNotFoundException) {\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 7: " + e.ToString());\r
-                       }\r
-                       Assert("dirNotFound error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#", true);\r
-                       } catch (IOException) {\r
-                               errorThrown = true;\r
-                       } catch (ArgumentException) {\r
-                               // FIXME - the spec says 'IOExc', but the\r
-                               //   compiler says 'ArgExc'...\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 8: " + e.ToString());\r
-                       }\r
-                       Assert("invalid filename error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       StreamWriter r = new StreamWriter(_codeFileName, true);\r
-                       AssertNotNull("no stream writer", r);\r
-                       r.Close();\r
-               }\r
-       }\r
-\r
-       // TODO - ctors with Encoding\r
-\r
-       // TODO - AutoFlush\r
-       public void TestAutoFlush() {\r
-               {\r
-                       MemoryStream m = new MemoryStream();\r
-                       StreamWriter w = new StreamWriter(m);\r
-                       w.AutoFlush = false;\r
-                       w.Write(1);\r
-                       w.Write(2);\r
-                       w.Write(3);\r
-                       w.Write(4);\r
-                       AssertEquals("Should be nothing before flush",\r
-                                    0L, m.Length);\r
-                       w.Flush();\r
-                       AssertEquals("Should be something after flush",\r
-                                    4L, m.Length);\r
-               }               \r
-               {\r
-                       MemoryStream m = new MemoryStream();\r
-                       StreamWriter w = new StreamWriter(m);\r
-                       w.AutoFlush = true;\r
-                       w.Write(1);\r
-                       w.Write(2);\r
-                       w.Write(3);\r
-                       w.Write(4);\r
-                       AssertEquals("Should be something before flush",\r
-                                    4L, m.Length);\r
-                       w.Flush();\r
-                       AssertEquals("Should be something after flush",\r
-                                    4L, m.Length);\r
-               }               \r
-       }\r
-\r
-       public void TestBaseStream() {\r
-               FileStream f = new FileStream(_codeFileName, \r
-                                             FileMode.Append, \r
-                                             FileAccess.Write);\r
-               StreamWriter r = new StreamWriter(f);\r
-               AssertEquals("wrong base stream ", f, r.BaseStream);\r
-               r.Close();\r
-               f.Close();\r
-       }\r
-\r
-       public void TestEncoding() {\r
-               StreamWriter r = new StreamWriter(_codeFileName);\r
-               AssertEquals("wrong encoding", \r
-                            Encoding.UTF8.GetType(), r.Encoding.GetType());\r
-               r.Close();\r
-       }\r
-\r
-       // TODO - Close - not entirely sure how to test Close\r
-       //public void TestClose() {\r
-       //{\r
-       //MemoryStream m = new MemoryStream();\r
-       //StreamWriter w = new StreamWriter(m);\r
-       //StreamReader r = new StreamReader(m);\r
-       //w.Write(1);\r
-       //w.Write(2);\r
-       //w.Write(3);\r
-       //w.Write(4);\r
-       //AssertEquals("Should be nothing before close",\r
-       //0, m.Length);\r
-       //AssertEquals("Should be nothing in reader",\r
-       //-1, r.Peek());\r
-       //w.Close();\r
-       //AssertEquals("Should be something after close",\r
-       //1, r.Peek());\r
-        //}            \r
-       //}\r
-\r
-       // TODO - Flush\r
-       public void TestFlush() {\r
-               {\r
-                       bool errorThrown = false;\r
-                       try {\r
-                               FileStream f = new FileStream(_codeFileName, \r
-                                                             FileMode.Append, \r
-                                                             FileAccess.Write);\r
-                               StreamWriter r = new StreamWriter(f);\r
-                               r.Close();\r
-                               r.Flush();\r
-                       } catch (ObjectDisposedException) {\r
-                               errorThrown = true;\r
-                       } catch (Exception e) {\r
-                               Fail ("Incorrect exception thrown at 1: " + e.ToString());\r
-                       }\r
-                       Assert("can't flush closed error not thrown", errorThrown);\r
-               }\r
-               {\r
-                       MemoryStream m = new MemoryStream();\r
-                       StreamWriter w = new StreamWriter(m);\r
-                       w.Write(1);\r
-                       w.Write(2);\r
-                       w.Write(3);\r
-                       w.Write(4);\r
-                       AssertEquals("Should be nothing before flush",\r
-                                    0L, m.Length);\r
-                       w.Flush();\r
-                       AssertEquals("Should be something after flush",\r
-                                    4L, m.Length);\r
-               }               \r
-       }\r
-\r
-       // TODO - Write - test errors, functionality tested in TestFlush.\r
-}\r
-}\r
+// StreamWriterTest.cs - NUnit Test Cases for the SystemIO.StreamWriter class
+//
+// David Brandt (bucky@keystreams.com)
+//
+// (C) Ximian, Inc.  http://www.ximian.com
+// 
+
+using NUnit.Framework;
+using System;
+using System.IO;
+using System.Text;
+
+namespace MonoTests.System.IO
+{
+
+[TestFixture]
+public class StreamWriterTest : Assertion
+{
+
+       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";
+
+       [SetUp]
+       public void SetUp ()
+       {
+               if (Directory.Exists (TempFolder))
+                       Directory.Delete (TempFolder, true);
+               Directory.CreateDirectory (TempFolder);
+
+               if (!File.Exists (_thisCodeFileName)) 
+                       File.Create (_thisCodeFileName).Close ();
+       }
+
+       [TearDown]
+       public void TearDown ()
+       {
+               if (Directory.Exists (TempFolder))
+                       Directory.Delete (TempFolder, true);
+       }
+
+
+       // TODO - ctors
+       [Test]
+       public void TestCtor1() {
+               {
+                       bool errorThrown = false;
+                       try {
+                               StreamWriter r = new StreamWriter((Stream)null);
+                       } catch (ArgumentNullException) {
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 1: " + e.ToString());
+                       }
+                       Assert("null string error not thrown", errorThrown);
+               }
+               {
+                       bool errorThrown = false;
+                       FileStream f = new FileStream(_thisCodeFileName, 
+                                                     FileMode.Open, 
+                                                     FileAccess.Read);
+                       try {
+                               StreamWriter r = new StreamWriter(f);
+                               r.Close();
+                       } catch (ArgumentException) {
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 2: " + e.ToString());
+                       }
+                       f.Close();
+                       Assert("no read error not thrown", errorThrown);
+               }
+               {
+                       FileStream f = new FileStream(_codeFileName, 
+                                                     FileMode.Append, 
+                                                     FileAccess.Write);
+                       StreamWriter r = new StreamWriter(f);
+                       AssertNotNull("no stream writer", r);
+                       r.Close();
+                       f.Close();
+               }
+       }
+
+       [Test]
+       public void TestCtor2() {
+               {
+                       bool errorThrown = false;
+                       try {
+                               StreamWriter r = new StreamWriter("");
+                       } catch (ArgumentException) {
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 1: " + e.ToString());
+                       }
+                       Assert("empty string error not thrown", errorThrown);
+               }
+               {
+                       bool errorThrown = false;
+                       try {
+                               StreamWriter r = new StreamWriter((string)null);
+                       } catch (ArgumentNullException) {
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 2: " + e.ToString());
+                       }
+                       Assert("null string error not thrown", errorThrown);
+               }
+               {
+                       bool errorThrown = false;
+                       try {
+                               StreamWriter r = new StreamWriter("nonexistentdir/file");
+                       } catch (DirectoryNotFoundException) {
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 3: " + e.ToString());
+                       }
+                       Assert("dirNotFound error not thrown", errorThrown);
+               }
+               {
+                       bool errorThrown = false;
+                       try {
+                               StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
+                       } catch (IOException) {
+                               errorThrown = true;
+                       } catch (ArgumentException) {
+                               // FIXME - the spec says 'IOExc', but the
+                               //   compiler says 'ArgExc'...
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 4: " + e.ToString());
+                       }
+                       Assert("1 invalid filename error not thrown", errorThrown);
+               }
+               // TODO - Security/Auth exceptions
+               {
+                       StreamWriter r = new StreamWriter(_codeFileName);
+                       AssertNotNull("no stream writer", r);
+                       r.Close();
+               }
+       }
+
+       [Test]
+       public void TestCtor3() {
+               {
+                       bool errorThrown = false;
+                       try {
+                               StreamWriter r = new StreamWriter("", false);
+                       } catch (ArgumentException) {
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 1: " + e.ToString());
+                       }
+                       Assert("empty string error not thrown", errorThrown);
+               }
+               {
+                       bool errorThrown = false;
+                       try {
+                               StreamWriter r = new StreamWriter((string)null, false);
+                       } catch (ArgumentNullException) {
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 2: " + e.ToString());
+                       }
+                       Assert("null string error not thrown", errorThrown);
+               }
+               {
+                       bool errorThrown = false;
+                       try {
+                               StreamWriter r = new StreamWriter("nonexistentdir/file", false);
+                       } catch (DirectoryNotFoundException) {
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 3: " + e.ToString());
+                       }
+                       Assert("dirNotFound error not thrown", errorThrown);
+               }
+               {
+                       bool errorThrown = false;
+                       try {
+                               StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
+                       } catch (IOException) {
+                               errorThrown = true;
+                       } catch (ArgumentException) {
+                               // FIXME - the spec says 'IOExc', but the
+                               //   compiler says 'ArgExc'...
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 4: " + e.ToString());
+                       }
+                       Assert("2 invalid filename error not thrown", errorThrown);
+               }
+               {
+                       StreamWriter r = new StreamWriter(_codeFileName, false);
+                       AssertNotNull("no stream writer", r);
+                       r.Close();
+               }
+               {
+                       bool errorThrown = false;
+                       try {
+                               StreamWriter r = new StreamWriter("", true);
+                       } catch (ArgumentException) {
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 5: " + e.ToString());
+                       }
+                       Assert("empty string error not thrown", errorThrown);
+               }
+               {
+                       bool errorThrown = false;
+                       try {
+                               StreamWriter r = new StreamWriter((string)null, true);
+                       } catch (ArgumentNullException) {
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 6: " + e.ToString());
+                       }
+                       Assert("null string error not thrown", errorThrown);
+               }
+               {
+                       bool errorThrown = false;
+                       try {
+                               StreamWriter r = new StreamWriter("nonexistentdir/file", true);
+                       } catch (DirectoryNotFoundException) {
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 7: " + e.ToString());
+                       }
+                       Assert("dirNotFound error not thrown", errorThrown);
+               }
+               {
+                       bool errorThrown = false;
+                       try {
+                               StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
+                       } catch (IOException) {
+                               errorThrown = true;
+                       } catch (ArgumentException) {
+                               // FIXME - the spec says 'IOExc', but the
+                               //   compiler says 'ArgExc'...
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 8: " + e.ToString());
+                       }
+                       Assert("3 invalid filename error not thrown", errorThrown);
+               }
+               {
+                       try {
+                               StreamWriter r = new StreamWriter(_codeFileName, true);
+                               AssertNotNull("no stream writer", r);
+                               r.Close();
+                       } catch (Exception e) {
+                               Fail ("Unxpected exception e=" + e.ToString());
+                       }
+               }
+       }
+
+       // TODO - ctors with Encoding
+
+       // TODO - AutoFlush
+       [Test]
+       public void TestAutoFlush() {
+               {
+                       MemoryStream m = new MemoryStream();
+                       StreamWriter w = new StreamWriter(m);
+                       w.AutoFlush = false;
+                       w.Write(1);
+                       w.Write(2);
+                       w.Write(3);
+                       w.Write(4);
+                       AssertEquals("Should be nothing before flush",
+                                    0L, m.Length);
+                       w.Flush();
+                       AssertEquals("Should be something after flush",
+                                    4L, m.Length);
+               }               
+               {
+                       MemoryStream m = new MemoryStream();
+                       StreamWriter w = new StreamWriter(m);
+                       w.AutoFlush = true;
+                       w.Write(1);
+                       w.Write(2);
+                       w.Write(3);
+                       w.Write(4);
+                       AssertEquals("Should be something before flush",
+                                    4L, m.Length);
+                       w.Flush();
+                       AssertEquals("Should be something after flush",
+                                    4L, m.Length);
+               }               
+       }
+
+       [Test]
+       public void TestBaseStream() {
+               FileStream f = new FileStream(_codeFileName, 
+                                             FileMode.Append, 
+                                             FileAccess.Write);
+               StreamWriter r = new StreamWriter(f);
+               AssertEquals("wrong base stream ", f, r.BaseStream);
+               r.Close();
+               f.Close();
+       }
+
+       [Test]
+       public void TestEncoding() {
+               StreamWriter r = new StreamWriter(_codeFileName);
+               AssertEquals("wrong encoding", 
+                            Encoding.UTF8.GetType(), r.Encoding.GetType());
+               r.Close();
+       }
+
+       // TODO - Close - not entirely sure how to test Close
+       //public void TestClose() {
+       //{
+       //MemoryStream m = new MemoryStream();
+       //StreamWriter w = new StreamWriter(m);
+       //StreamReader r = new StreamReader(m);
+       //w.Write(1);
+       //w.Write(2);
+       //w.Write(3);
+       //w.Write(4);
+       //AssertEquals("Should be nothing before close",
+       //0, m.Length);
+       //AssertEquals("Should be nothing in reader",
+       //-1, r.Peek());
+       //w.Close();
+       //AssertEquals("Should be something after close",
+       //1, r.Peek());
+        //}            
+       //}
+
+       // TODO - Flush
+       [Test]
+       public void TestFlush() {
+               {
+                       bool errorThrown = false;
+                       try {
+                               FileStream f = new FileStream(_codeFileName, 
+                                                             FileMode.Append, 
+                                                             FileAccess.Write);
+                               StreamWriter r = new StreamWriter(f);
+                               r.Close();
+                               r.Flush();
+                       } catch (ObjectDisposedException) {
+                               errorThrown = true;
+                       } catch (Exception e) {
+                               Fail ("Incorrect exception thrown at 1: " + e.ToString());
+                       }
+                       Assert("can't flush closed error not thrown", errorThrown);
+               }
+               {
+                       MemoryStream m = new MemoryStream();
+                       StreamWriter w = new StreamWriter(m);
+                       w.Write(1);
+                       w.Write(2);
+                       w.Write(3);
+                       w.Write(4);
+                       AssertEquals("Should be nothing before flush",
+                                    0L, m.Length);
+                       w.Flush();
+                       AssertEquals("Should be something after flush",
+                                    4L, m.Length);
+               }               
+       }
+
+       [Test]
+       [ExpectedException (typeof (ObjectDisposedException))]
+       public void AutoFlush_Disposed () 
+       {
+               StreamWriter w = new StreamWriter (new MemoryStream ());
+               w.Close ();
+               w.AutoFlush = true;
+       }
+
+       [Test]
+       [ExpectedException (typeof (ObjectDisposedException))]
+       public void WriteChar_Disposed () 
+       {
+               StreamWriter w = new StreamWriter (new MemoryStream ());
+               w.Close ();
+               w.Write ('A');
+       }
+
+       [Test]
+       [ExpectedException (typeof (ObjectDisposedException))]
+       public void WriteCharArray_Disposed () 
+       {
+               char[] c = new char [2] { 'a', 'b' };
+               StreamWriter w = new StreamWriter (new MemoryStream ());
+               w.Close ();
+               w.Write (c, 0, 2);
+       }
+
+       [Test]
+       // accepted [ExpectedException (typeof (ArgumentNullException))]
+       public void WriteCharArray_Null () 
+       {
+               char[] c = null;
+               StreamWriter w = new StreamWriter (new MemoryStream ());
+               w.Write (c);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentException))]
+       public void WriteCharArray_IndexOverflow () 
+       {
+               char[] c = new char [2] { 'a', 'b' };
+               StreamWriter w = new StreamWriter (new MemoryStream ());
+               w.Write (c, Int32.MaxValue, 2);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentException))]
+       public void WriteCharArray_CountOverflow () 
+       {
+               char[] c = new char [2] { 'a', 'b' };
+               StreamWriter w = new StreamWriter (new MemoryStream ());
+               w.Write (c, 1, Int32.MaxValue);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ObjectDisposedException))]
+       public void WriteString_Disposed () 
+       {
+               StreamWriter w = new StreamWriter (new MemoryStream ());
+               w.Close ();
+               w.Write ("mono");
+       }
+
+       [Test]
+       // accepted [ExpectedException (typeof (ArgumentNullException))]
+       public void WriteString_Null () 
+       {
+               string s = null;
+               StreamWriter w = new StreamWriter (new MemoryStream ());
+               w.Write (s);
+       }
+
+       [Test]
+       public void NoPreambleOnAppend ()
+       {
+               MemoryStream ms = new MemoryStream ();
+               StreamWriter w = new StreamWriter (ms, Encoding.UTF8);
+               w.Write ("a");
+               w.Flush ();
+               AssertEquals ("Incorrect size after writing 1 byte plus header", ms.Position, 4);
+
+               // Append 1 byte, should skip the preamble now.
+               w.Write ("a");
+               w.Flush ();
+               w = new StreamWriter (ms, Encoding.UTF8);
+               AssertEquals ("Incorrect size after writing 1 byte, must have been 5", ms.Position, 5);
+               
+       }
+       
+       // TODO - Write - test errors, functionality tested in TestFlush.
+}
+}