More fixes for running unit tests under NET_2_1 profile
[mono.git] / mcs / class / corlib / Test / System.IO / StreamWriterTest.cs
1 // StreamWriterTest.cs - NUnit Test Cases for the SystemIO.StreamWriter class
2 //
3 // David Brandt (bucky@keystreams.com)
4 //
5 // (C) Ximian, Inc.  http://www.ximian.com
6 // 
7
8 using NUnit.Framework;
9 using System;
10 using System.IO;
11 using System.Text;
12
13 namespace MonoTests.System.IO
14 {
15
16 [TestFixture]
17 public class StreamWriterTest
18 {
19         static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
20         private string _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
21         private string _thisCodeFileName = TempFolder + Path.DirectorySeparatorChar + "StreamWriterTest.temp";
22
23         [SetUp]
24         public void SetUp ()
25         {
26                 if (Directory.Exists (TempFolder))
27                         Directory.Delete (TempFolder, true);
28                 Directory.CreateDirectory (TempFolder);
29
30                 if (!File.Exists (_thisCodeFileName)) 
31                         File.Create (_thisCodeFileName).Close ();
32         }
33
34         [TearDown]
35         public void TearDown ()
36         {
37                 if (Directory.Exists (TempFolder))
38                         Directory.Delete (TempFolder, true);
39         }
40
41         [Test] // .ctor (Stream)
42         public void Constructor1 ()
43         {
44                 FileStream f = new FileStream(_codeFileName, 
45                                               FileMode.Append, 
46                                               FileAccess.Write);
47                 StreamWriter r = new StreamWriter (f);
48                 Assert.IsFalse (r.AutoFlush, "#1");
49                 Assert.AreSame (f, r.BaseStream, "#2");
50                 Assert.IsNotNull (r.Encoding, "#3");
51                 Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#4");
52                 r.Close();
53                 f.Close();
54         }
55
56         [Test] // .ctor (Stream)
57         public void Constructor1_Stream_NotWritable ()
58         {
59                 FileStream f = new FileStream (_thisCodeFileName, FileMode.Open,
60                         FileAccess.Read);
61                 try {
62                         new StreamWriter (f);
63                         Assert.Fail ("#B1");
64                 } catch (ArgumentException ex) {
65                         // Stream was not writable
66                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
67                         Assert.IsNull (ex.InnerException, "#3");
68                         Assert.IsNotNull (ex.Message, "#4");
69                         Assert.IsNull (ex.ParamName, "#5");
70                 } finally {
71                         f.Close ();
72                 }
73         }
74
75         [Test] // .ctor (Stream)
76         public void Constructor1_Stream_Null ()
77         {
78                 try {
79                         new StreamWriter((Stream) null);
80                         Assert.Fail ("#1");
81                 } catch (ArgumentNullException ex) {
82                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
83                         Assert.IsNull (ex.InnerException, "#3");
84                         Assert.IsNotNull (ex.Message, "#4");
85                         Assert.AreEqual ("stream", ex.ParamName, "#5");
86                 }
87         }
88
89         [Test] // .ctor (String)
90         public void Constructor2 ()
91         {
92                 // TODO - Security/Auth exceptions
93                 using (StreamWriter r = new StreamWriter (_codeFileName)) {
94                         Assert.IsFalse (r.AutoFlush, "#1");
95                         Assert.IsNotNull (r.BaseStream, "#2");
96                         Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#3");
97                         Assert.IsNotNull (r.Encoding, "#4");
98                         Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#5");
99                         r.Close ();
100                 }
101         }
102
103         [Test] // .ctor (String)
104         public void Constructor2_Path_DirectoryNotFound ()
105         {
106                 Directory.Delete (TempFolder, true);
107
108                 try {
109                         new StreamWriter (_codeFileName);
110                         Assert.Fail ("#1");
111                 } catch (DirectoryNotFoundException ex) {
112                         // Could not find a part of the path '...'
113                         Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
114                         Assert.IsNull (ex.InnerException, "#3");
115                         Assert.IsNotNull (ex.Message, "#4");
116                         Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#5");
117                 }
118         }
119
120         [Test] // .ctor (String)
121         public void Constructor2_Path_Empty ()
122         {
123                 try {
124                         new StreamWriter (string.Empty);
125                         Assert.Fail ("#1");
126                 } catch (ArgumentException ex) {
127                         // Empty path name is not legal
128                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
129                         Assert.IsNull (ex.InnerException, "#3");
130                         Assert.IsNotNull (ex.Message, "#4");
131                         Assert.IsNull (ex.ParamName, "#5");
132                 }
133         }
134
135         [Test] // .ctor (String)
136         public void Constructor2_Path_IllegalChars ()
137         {
138                 try {
139                         new StreamWriter ("!$what? what? Huh? !$*#" + Path.InvalidPathChars [0]);
140                         Assert.Fail ("#1");
141                 } catch (ArgumentException ex) {
142                         // Illegal characters in path
143                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
144                         Assert.IsNull (ex.InnerException, "#3");
145                         Assert.IsNotNull (ex.Message, "#4");
146                         Assert.IsNull (ex.ParamName, "#5");
147                 }
148         }
149
150         [Test] // .ctor (String)
151         public void Constructor2_Path_Null ()
152         {
153                 try {
154                         new StreamWriter ((string) null);
155                         Assert.Fail ("#1");
156                 } catch (ArgumentNullException ex) {
157                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
158                         Assert.IsNull (ex.InnerException, "#3");
159                         Assert.IsNotNull (ex.Message, "#4");
160                         Assert.AreEqual ("path", ex.ParamName, "#5");
161                 }
162         }
163
164         [Test] // .ctor (Stream, Encoding)
165         public void Constructor3 ()
166         {
167                 FileStream f = new FileStream (_codeFileName,
168                                               FileMode.Append,
169                                               FileAccess.Write);
170                 StreamWriter r = new StreamWriter (f, Encoding.ASCII);
171                 Assert.IsFalse (r.AutoFlush, "#1");
172                 Assert.AreSame (f, r.BaseStream, "#2");
173                 Assert.IsNotNull (r.Encoding, "#3");
174                 Assert.AreEqual (typeof (ASCIIEncoding), r.Encoding.GetType (), "#4");
175                 r.Close ();
176                 f.Close ();
177         }
178
179         [Test] // .ctor (Stream, Encoding)
180         public void Constructor3_Encoding_Null ()
181         {
182                 MemoryStream m = new MemoryStream ();
183                 try {
184                         new StreamWriter (m, (Encoding) null);
185                         Assert.Fail ("#1");
186                 } catch (ArgumentNullException ex) {
187                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
188                         Assert.IsNull (ex.InnerException, "#3");
189                         Assert.IsNotNull (ex.Message, "#4");
190                         Assert.AreEqual ("encoding", ex.ParamName, "#5");
191                 }
192         }
193
194         [Test] // .ctor (Stream, Encoding)
195         public void Constructor3_Stream_NotWritable ()
196         {
197                 FileStream f = new FileStream (_thisCodeFileName, FileMode.Open,
198                         FileAccess.Read);
199                 try {
200                         new StreamWriter (f, Encoding.UTF8);
201                         Assert.Fail ("#B1");
202                 } catch (ArgumentException ex) {
203                         // Stream was not writable
204                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
205                         Assert.IsNull (ex.InnerException, "#3");
206                         Assert.IsNotNull (ex.Message, "#4");
207                         Assert.IsNull (ex.ParamName, "#5");
208                 } finally {
209                         f.Close ();
210                 }
211         }
212
213         [Test] // .ctor (Stream, Encoding)
214         public void Constructor3_Stream_Null ()
215         {
216                 try {
217                         new StreamWriter ((Stream) null, Encoding.UTF8);
218                         Assert.Fail ("#1");
219                 } catch (ArgumentNullException ex) {
220                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
221                         Assert.IsNull (ex.InnerException, "#3");
222                         Assert.IsNotNull (ex.Message, "#4");
223                         Assert.AreEqual ("stream", ex.ParamName, "#5");
224                 }
225         }
226
227         [Test] // .ctor (String, Boolean)
228         public void Constructor4 ()
229         {
230                 using (StreamWriter r = new StreamWriter (_codeFileName, false)) {
231                         Assert.IsFalse (r.AutoFlush, "#A1");
232                         Assert.IsNotNull (r.BaseStream, "#A2");
233                         Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#A3");
234                         Assert.IsNotNull (r.Encoding, "#A4");
235                         Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#A5");
236                         r.Close();
237                 }
238
239                 using (StreamWriter r = new StreamWriter(_codeFileName, true)) {
240                         Assert.IsFalse (r.AutoFlush, "#B1");
241                         Assert.IsNotNull (r.BaseStream, "#B2");
242                         Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#B3");
243                         Assert.IsNotNull (r.Encoding, "#B4");
244                         Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#B5");
245                         r.Close();
246                 }
247         }
248
249         [Test] // .ctor (String, Boolean)
250         public void Constructor4_Path_DirectoryNotFound ()
251         {
252                 Directory.Delete (TempFolder, true);
253
254                 try {
255                         new StreamWriter (_codeFileName, false);
256                         Assert.Fail ("#A1");
257                 } catch (DirectoryNotFoundException ex) {
258                         // Could not find a part of the path '...'
259                         Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
260                         Assert.IsNull (ex.InnerException, "#A3");
261                         Assert.IsNotNull (ex.Message, "#A4");
262                         Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#A5");
263                 }
264
265                 try {
266                         new StreamWriter (_codeFileName, true);
267                         Assert.Fail ("#B1");
268                 } catch (DirectoryNotFoundException ex) {
269                         // Could not find a part of the path '...'
270                         Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#B2");
271                         Assert.IsNull (ex.InnerException, "#B3");
272                         Assert.IsNotNull (ex.Message, "#B4");
273                         Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#B5");
274                 }
275         }
276
277         [Test] // .ctor (String, Boolean)
278         public void Constructor4_Path_Empty ()
279         {
280                 try {
281                         new StreamWriter (string.Empty, false);
282                         Assert.Fail ("#A1");
283                 } catch (ArgumentException ex) {
284                         // Empty path name is not legal
285                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
286                         Assert.IsNull (ex.InnerException, "#A3");
287                         Assert.IsNotNull (ex.Message, "#A4");
288                         Assert.IsNull (ex.ParamName, "#A5");
289                 }
290
291                 try {
292                         new StreamWriter (string.Empty, true);
293                         Assert.Fail ("#B1");
294                 } catch (ArgumentException ex) {
295                         // Empty path name is not legal
296                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
297                         Assert.IsNull (ex.InnerException, "#B3");
298                         Assert.IsNotNull (ex.Message, "#B4");
299                         Assert.IsNull (ex.ParamName, "#B5");
300                 }
301         }
302
303         [Test] // .ctor (String, Boolean)
304         public void Constructor4_Path_InvalidChars ()
305         {
306                 try {
307                         new StreamWriter ("!$what? what? Huh? !$*#" + Path.InvalidPathChars [0], false);
308                         Assert.Fail ("#A1");
309                 } catch (ArgumentException ex) {
310                         // Illegal characters in path
311                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
312                         Assert.IsNull (ex.InnerException, "#A3");
313                         Assert.IsNotNull (ex.Message, "#A4");
314                         Assert.IsNull (ex.ParamName, "#A5");
315                 }
316
317                 try {
318                         new StreamWriter ("!$what? what? Huh? !$*#" + Path.InvalidPathChars [0], true);
319                         Assert.Fail ("#B1");
320                 } catch (ArgumentException ex) {
321                         // Illegal characters in path
322                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
323                         Assert.IsNull (ex.InnerException, "#B3");
324                         Assert.IsNotNull (ex.Message, "#B4");
325                         Assert.IsNull (ex.ParamName, "#B5");
326                 }
327         }
328
329         [Test] // .ctor (String, Boolean)
330         public void Constructor4_Path_Null ()
331         {
332                 try {
333                         new StreamWriter ((string) null, false);
334                         Assert.Fail ("#A1");
335                 } catch (ArgumentNullException ex) {
336                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
337                         Assert.IsNull (ex.InnerException, "#A3");
338                         Assert.IsNotNull (ex.Message, "#A4");
339                         Assert.AreEqual ("path", ex.ParamName, "#A5");
340                 }
341
342                 try {
343                         new StreamWriter ((string) null, true);
344                         Assert.Fail ("#B1");
345                 } catch (ArgumentNullException ex) {
346                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
347                         Assert.IsNull (ex.InnerException, "#B3");
348                         Assert.IsNotNull (ex.Message, "#B4");
349                         Assert.AreEqual ("path", ex.ParamName, "#B5");
350                 }
351         }
352
353         [Test] // .ctor (Stream, Encoding, Int32)
354         public void Constructor5 ()
355         {
356                 MemoryStream m;
357                 StreamWriter r;
358
359                 m = new MemoryStream ();
360                 r = new StreamWriter (m, Encoding.ASCII, 10);
361                 Assert.IsFalse (r.AutoFlush, "#A1");
362                 Assert.AreSame (m, r.BaseStream, "#A2");
363                 Assert.IsNotNull (r.Encoding, "#A3");
364                 Assert.AreEqual (typeof (ASCIIEncoding), r.Encoding.GetType (), "#A4");
365                 r.Close ();
366                 m.Close ();
367
368                 m = new MemoryStream ();
369                 r = new StreamWriter (m, Encoding.UTF8, 1);
370                 Assert.IsFalse (r.AutoFlush, "#B1");
371                 Assert.AreSame (m, r.BaseStream, "#B2");
372                 Assert.IsNotNull (r.Encoding, "#B3");
373                 Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#B4");
374                 r.Close ();
375                 m.Close ();
376         }
377
378         [Test] // .ctor (Stream, Encoding, Int32)
379         public void Constructor5_BufferSize_NotPositive ()
380         {
381                 MemoryStream m = new MemoryStream ();
382
383                 try {
384                         new StreamWriter (m, Encoding.UTF8, 0);
385                         Assert.Fail ("#A1");
386                 } catch (ArgumentOutOfRangeException ex) {
387                         // Positive number required
388                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
389                         Assert.IsNull (ex.InnerException, "#A3");
390                         Assert.IsNotNull (ex.Message, "#A4");
391 #if NET_2_0
392                         Assert.AreEqual ("bufferSize", ex.ParamName, "#A5");
393 #endif
394                 }
395
396                 try {
397                         new StreamWriter (m, Encoding.UTF8, -1);
398                         Assert.Fail ("#B1");
399                 } catch (ArgumentOutOfRangeException ex) {
400                         // Positive number required
401                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
402                         Assert.IsNull (ex.InnerException, "#B3");
403                         Assert.IsNotNull (ex.Message, "#B4");
404 #if NET_2_0
405                         Assert.AreEqual ("bufferSize", ex.ParamName, "#B5");
406 #endif
407                 }
408         }
409
410         [Test] // .ctor (Stream, Encoding, Int32)
411         public void Constructor5_Encoding_Null ()
412         {
413                 MemoryStream m = new MemoryStream ();
414                 try {
415                         new StreamWriter (m, (Encoding) null, 10);
416                         Assert.Fail ("#1");
417                 } catch (ArgumentNullException ex) {
418                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
419                         Assert.IsNull (ex.InnerException, "#3");
420                         Assert.IsNotNull (ex.Message, "#4");
421                         Assert.AreEqual ("encoding", ex.ParamName, "#5");
422                 }
423         }
424
425         [Test] // .ctor (Stream, Encoding, Int32)
426         public void Constructor5_Stream_NotWritable ()
427         {
428                 FileStream f = new FileStream (_thisCodeFileName, FileMode.Open,
429                         FileAccess.Read);
430                 try {
431                         new StreamWriter (f, Encoding.UTF8, 10);
432                         Assert.Fail ("#B1");
433                 } catch (ArgumentException ex) {
434                         // Stream was not writable
435                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
436                         Assert.IsNull (ex.InnerException, "#3");
437                         Assert.IsNotNull (ex.Message, "#4");
438                         Assert.IsNull (ex.ParamName, "#5");
439                 } finally {
440                         f.Close ();
441                 }
442         }
443
444         [Test] // .ctor (Stream, Encoding, Int32)
445         public void Constructor5_Stream_Null ()
446         {
447                 try {
448                         new StreamWriter ((Stream) null, Encoding.UTF8, 10);
449                         Assert.Fail ("#1");
450                 } catch (ArgumentNullException ex) {
451                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
452                         Assert.IsNull (ex.InnerException, "#3");
453                         Assert.IsNotNull (ex.Message, "#4");
454                         Assert.AreEqual ("stream", ex.ParamName, "#5");
455                 }
456         }
457
458         [Test] // .ctor (String, Boolean, Encoding)
459         public void Constructor6 ()
460         {
461                 using (StreamWriter r = new StreamWriter (_codeFileName, false, Encoding.ASCII)) {
462                         Assert.IsFalse (r.AutoFlush, "#A1");
463                         Assert.IsNotNull (r.BaseStream, "#A2");
464                         Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#A3");
465                         Assert.IsNotNull (r.Encoding, "#A4");
466                         Assert.AreEqual (typeof (ASCIIEncoding), r.Encoding.GetType (), "#A5");
467                         r.Close ();
468                 }
469
470                 using (StreamWriter r = new StreamWriter (_codeFileName, true, Encoding.UTF8)) {
471                         Assert.IsFalse (r.AutoFlush, "#B1");
472                         Assert.IsNotNull (r.BaseStream, "#B2");
473                         Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#B3");
474                         Assert.IsNotNull (r.Encoding, "#B4");
475                         Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#B5");
476                         r.Close ();
477                 }
478         }
479
480         [Test] // .ctor (String, Boolean, Encoding)
481         public void Constructor6_Encoding_Null ()
482         {
483                 try {
484                         new StreamWriter (_codeFileName, false, (Encoding) null);
485                         Assert.Fail ("#A1");
486                 } catch (ArgumentNullException ex) {
487                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
488                         Assert.IsNull (ex.InnerException, "#A3");
489                         Assert.IsNotNull (ex.Message, "#A4");
490                         Assert.AreEqual ("encoding", ex.ParamName, "#A5");
491                 }
492
493                 try {
494                         new StreamWriter (_codeFileName, true, (Encoding) null);
495                         Assert.Fail ("#B1");
496                 } catch (ArgumentNullException ex) {
497                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
498                         Assert.IsNull (ex.InnerException, "#B3");
499                         Assert.IsNotNull (ex.Message, "#B4");
500                         Assert.AreEqual ("encoding", ex.ParamName, "#B5");
501                 }
502         }
503
504         [Test] // .ctor (String, Boolean, Encoding)
505         public void Constructor6_Path_DirectoryNotFound ()
506         {
507                 Directory.Delete (TempFolder, true);
508
509                 try {
510                         new StreamWriter (_codeFileName, false, Encoding.UTF8);
511                         Assert.Fail ("#A1");
512                 } catch (DirectoryNotFoundException ex) {
513                         // Could not find a part of the path '...'
514                         Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
515                         Assert.IsNull (ex.InnerException, "#A3");
516                         Assert.IsNotNull (ex.Message, "#A4");
517                         Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#A5");
518                 }
519
520                 try {
521                         new StreamWriter (_codeFileName, true, Encoding.UTF8);
522                         Assert.Fail ("#B1");
523                 } catch (DirectoryNotFoundException ex) {
524                         // Could not find a part of the path '...'
525                         Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#B2");
526                         Assert.IsNull (ex.InnerException, "#B3");
527                         Assert.IsNotNull (ex.Message, "#B4");
528                         Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#B5");
529                 }
530         }
531
532         [Test] // .ctor (String, Boolean, Encoding)
533         public void Constructor6_Path_Empty ()
534         {
535                 try {
536                         new StreamWriter (string.Empty, false, Encoding.UTF8);
537                         Assert.Fail ("#A1");
538                 } catch (ArgumentException ex) {
539                         // Empty path name is not legal
540                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
541                         Assert.IsNull (ex.InnerException, "#A3");
542                         Assert.IsNotNull (ex.Message, "#A4");
543                         Assert.IsNull (ex.ParamName, "#A5");
544                 }
545
546                 try {
547                         new StreamWriter (string.Empty, true, Encoding.UTF8);
548                         Assert.Fail ("#B1");
549                 } catch (ArgumentException ex) {
550                         // Empty path name is not legal
551                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
552                         Assert.IsNull (ex.InnerException, "#B3");
553                         Assert.IsNotNull (ex.Message, "#B4");
554                         Assert.IsNull (ex.ParamName, "#B5");
555                 }
556         }
557
558         [Test] // .ctor (String, Boolean, Encoding)
559         public void Constructor6_Path_InvalidChars ()
560         {
561                 try {
562                         new StreamWriter ("!$what? what? Huh? !$*#" +
563                                 Path.InvalidPathChars [0], false,
564                                 Encoding.UTF8);
565                         Assert.Fail ("#A1");
566                 } catch (ArgumentException ex) {
567                         // Illegal characters in path
568                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
569                         Assert.IsNull (ex.InnerException, "#A3");
570                         Assert.IsNotNull (ex.Message, "#A4");
571                         Assert.IsNull (ex.ParamName, "#A5");
572                 }
573
574                 try {
575                         new StreamWriter ("!$what? what? Huh? !$*#" +
576                                 Path.InvalidPathChars [0], true,
577                                 Encoding.UTF8);
578                         Assert.Fail ("#B1");
579                 } catch (ArgumentException ex) {
580                         // Illegal characters in path
581                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
582                         Assert.IsNull (ex.InnerException, "#B3");
583                         Assert.IsNotNull (ex.Message, "#B4");
584                         Assert.IsNull (ex.ParamName, "#B5");
585                 }
586         }
587
588         [Test] // .ctor (String, Boolean, Encoding)
589         public void Constructor6_Path_Null ()
590         {
591                 try {
592                         new StreamWriter ((string) null, false, Encoding.UTF8);
593                         Assert.Fail ("#A1");
594                 } catch (ArgumentNullException ex) {
595                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
596                         Assert.IsNull (ex.InnerException, "#A3");
597                         Assert.IsNotNull (ex.Message, "#A4");
598                         Assert.AreEqual ("path", ex.ParamName, "#A5");
599                 }
600
601                 try {
602                         new StreamWriter ((string) null, true, Encoding.UTF8);
603                         Assert.Fail ("#B1");
604                 } catch (ArgumentNullException ex) {
605                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
606                         Assert.IsNull (ex.InnerException, "#B3");
607                         Assert.IsNotNull (ex.Message, "#B4");
608                         Assert.AreEqual ("path", ex.ParamName, "#B5");
609                 }
610         }
611
612         [Test] // .ctor (String, Boolean, Encoding, Int32)
613         public void Constructor7 ()
614         {
615                 using (StreamWriter r = new StreamWriter (_codeFileName, false, Encoding.ASCII, 10)) {
616                         Assert.IsFalse (r.AutoFlush, "#A1");
617                         Assert.IsNotNull (r.BaseStream, "#A2");
618                         Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#A3");
619                         Assert.IsNotNull (r.Encoding, "#A4");
620                         Assert.AreEqual (typeof (ASCIIEncoding), r.Encoding.GetType (), "#A5");
621                         r.Close ();
622                 }
623
624                 using (StreamWriter r = new StreamWriter (_codeFileName, true, Encoding.UTF8, 1)) {
625                         Assert.IsFalse (r.AutoFlush, "#B1");
626                         Assert.IsNotNull (r.BaseStream, "#B2");
627                         Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#B3");
628                         Assert.IsNotNull (r.Encoding, "#B4");
629                         Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#B5");
630                         r.Close ();
631                 }
632         }
633
634         [Test] // .ctor (String, Boolean, Encoding, Int32)
635         public void Constructor7_BufferSize_NotPositive ()
636         {
637                 try {
638                         new StreamWriter (_codeFileName, false, Encoding.UTF8, 0);
639                         Assert.Fail ("#A1");
640                 } catch (ArgumentOutOfRangeException ex) {
641                         // Positive number required
642                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
643                         Assert.IsNull (ex.InnerException, "#A3");
644                         Assert.IsNotNull (ex.Message, "#A4");
645 #if NET_2_0
646                         Assert.AreEqual ("bufferSize", ex.ParamName, "#A5");
647 #endif
648                 }
649
650                 try {
651                         new StreamWriter (_codeFileName, false, Encoding.UTF8, -1);
652                         Assert.Fail ("#B1");
653                 } catch (ArgumentOutOfRangeException ex) {
654                         // Positive number required
655                         Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
656                         Assert.IsNull (ex.InnerException, "#B3");
657                         Assert.IsNotNull (ex.Message, "#B4");
658 #if NET_2_0
659                         Assert.AreEqual ("bufferSize", ex.ParamName, "#B5");
660 #endif
661                 }
662         }
663
664         [Test] // .ctor (String, Boolean, Encoding, Int32)
665         public void Constructor7_Encoding_Null ()
666         {
667                 try {
668                         new StreamWriter (_codeFileName, false, (Encoding) null, 10);
669                         Assert.Fail ("#A1");
670                 } catch (ArgumentNullException ex) {
671                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
672                         Assert.IsNull (ex.InnerException, "#A3");
673                         Assert.IsNotNull (ex.Message, "#A4");
674                         Assert.AreEqual ("encoding", ex.ParamName, "#A5");
675                 }
676
677                 try {
678                         new StreamWriter (_codeFileName, true, (Encoding) null, 10);
679                         Assert.Fail ("#B1");
680                 } catch (ArgumentNullException ex) {
681                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
682                         Assert.IsNull (ex.InnerException, "#B3");
683                         Assert.IsNotNull (ex.Message, "#B4");
684                         Assert.AreEqual ("encoding", ex.ParamName, "#B5");
685                 }
686         }
687
688         [Test] // .ctor (String, Boolean, Encoding, Int32)
689         public void Constructor7_Path_DirectoryNotFound ()
690         {
691                 Directory.Delete (TempFolder, true);
692
693                 try {
694                         new StreamWriter (_codeFileName, false, Encoding.UTF8, 10);
695                         Assert.Fail ("#A1");
696                 } catch (DirectoryNotFoundException ex) {
697                         // Could not find a part of the path '...'
698                         Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
699                         Assert.IsNull (ex.InnerException, "#A3");
700                         Assert.IsNotNull (ex.Message, "#A4");
701                         Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#A5");
702                 }
703
704                 try {
705                         new StreamWriter (_codeFileName, true, Encoding.UTF8, 10);
706                         Assert.Fail ("#B1");
707                 } catch (DirectoryNotFoundException ex) {
708                         // Could not find a part of the path '...'
709                         Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#B2");
710                         Assert.IsNull (ex.InnerException, "#B3");
711                         Assert.IsNotNull (ex.Message, "#B4");
712                         Assert.IsTrue (ex.Message.IndexOf (TempFolder) != -1, "#B5");
713                 }
714         }
715
716         [Test] // .ctor (String, Boolean, Encoding, Int32)
717         public void Constructor7_Path_Empty ()
718         {
719                 try {
720                         new StreamWriter (string.Empty, false, Encoding.UTF8, 10);
721                         Assert.Fail ("#A1");
722                 } catch (ArgumentException ex) {
723                         // Empty path name is not legal
724                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
725                         Assert.IsNull (ex.InnerException, "#A3");
726                         Assert.IsNotNull (ex.Message, "#A4");
727                         Assert.IsNull (ex.ParamName, "#A5");
728                 }
729
730                 try {
731                         new StreamWriter (string.Empty, true, Encoding.UTF8, 10);
732                         Assert.Fail ("#B1");
733                 } catch (ArgumentException ex) {
734                         // Empty path name is not legal
735                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
736                         Assert.IsNull (ex.InnerException, "#B3");
737                         Assert.IsNotNull (ex.Message, "#B4");
738                         Assert.IsNull (ex.ParamName, "#B5");
739                 }
740         }
741
742         [Test] // .ctor (String, Boolean, Encoding, Int32)
743         public void Constructor7_Path_InvalidChars ()
744         {
745                 try {
746                         new StreamWriter ("!$what? what? Huh? !$*#" +
747                                 Path.InvalidPathChars [0], false,
748                                 Encoding.UTF8, 10);
749                         Assert.Fail ("#A1");
750                 } catch (ArgumentException ex) {
751                         // Illegal characters in path
752                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
753                         Assert.IsNull (ex.InnerException, "#A3");
754                         Assert.IsNotNull (ex.Message, "#A4");
755                         Assert.IsNull (ex.ParamName, "#A5");
756                 }
757
758                 try {
759                         new StreamWriter ("!$what? what? Huh? !$*#" +
760                                 Path.InvalidPathChars [0], true,
761                                 Encoding.UTF8, 10);
762                         Assert.Fail ("#B1");
763                 } catch (ArgumentException ex) {
764                         // Illegal characters in path
765                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
766                         Assert.IsNull (ex.InnerException, "#B3");
767                         Assert.IsNotNull (ex.Message, "#B4");
768                         Assert.IsNull (ex.ParamName, "#B5");
769                 }
770         }
771
772         [Test] // .ctor (String, Boolean, Encoding, Int32)
773         public void Constructor7_Path_Null ()
774         {
775                 try {
776                         new StreamWriter ((string) null, false, Encoding.UTF8, 10);
777                         Assert.Fail ("#A1");
778                 } catch (ArgumentNullException ex) {
779                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
780                         Assert.IsNull (ex.InnerException, "#A3");
781                         Assert.IsNotNull (ex.Message, "#A4");
782                         Assert.AreEqual ("path", ex.ParamName, "#A5");
783                 }
784
785                 try {
786                         new StreamWriter ((string) null, true, Encoding.UTF8, 10);
787                         Assert.Fail ("#B1");
788                 } catch (ArgumentNullException ex) {
789                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
790                         Assert.IsNull (ex.InnerException, "#B3");
791                         Assert.IsNotNull (ex.Message, "#B4");
792                         Assert.AreEqual ("path", ex.ParamName, "#B5");
793                 }
794         }
795
796         [Test]
797         public void AutoFlush ()
798         {
799                 MemoryStream m;
800                 StreamWriter w;
801
802                 m = new MemoryStream ();
803                 w = new StreamWriter (m);
804                 w.Write (1);
805                 w.Write (2);
806                 w.Write (3);
807                 w.Write (4);
808                 Assert.AreEqual (0, m.Length, "#A1");
809                 w.AutoFlush = true;
810                 Assert.IsTrue (w.AutoFlush, "#A2");
811                 Assert.AreEqual (4, m.Length, "#A3");
812                 w.Flush ();
813                 Assert.AreEqual (4, m.Length, "#A4");
814
815                 m = new MemoryStream ();
816                 w = new StreamWriter(m);
817                 w.AutoFlush = true;
818                 Assert.IsTrue (w.AutoFlush, "#B1");
819                 w.Write (1);
820                 w.Write (2);
821                 w.Write (3);
822                 w.Write (4);
823                 Assert.AreEqual (4, m.Length, "#B2");
824                 w.Flush ();
825                 Assert.AreEqual (4, m.Length, "#B3");
826                 w.AutoFlush = false;
827                 Assert.IsFalse (w.AutoFlush, "#B4");
828                 w.Write (4);
829                 Assert.AreEqual (4, m.Length, "#B5");
830                 w.Flush ();
831                 Assert.AreEqual (5, m.Length, "#B6");
832         }
833
834         [Test]
835         public void AutoFlush_Disposed ()
836         {
837                 StreamWriter w;
838                 
839                 w = new StreamWriter (new MemoryStream ());
840                 w.Close ();
841                 w.AutoFlush = false;
842                 Assert.IsFalse (w.AutoFlush, "#A1");
843                 try {
844                         w.AutoFlush = true;
845                         Assert.Fail ("#A2");
846                 } catch (ObjectDisposedException) {
847                 }
848                 Assert.IsTrue (w.AutoFlush, "#A3");
849
850                 w = new StreamWriter (new MemoryStream ());
851                 w.AutoFlush = true;
852                 w.Close ();
853                 Assert.IsTrue (w.AutoFlush, "#B1");
854                 try {
855                         w.AutoFlush = true;
856                         Assert.Fail ("#B2");
857                 } catch (ObjectDisposedException) {
858                 }
859                 Assert.IsTrue (w.AutoFlush, "#B3");
860                 w.AutoFlush = false;
861                 Assert.IsFalse (w.AutoFlush, "#B4");
862         }
863
864         [Test]
865         public void Close ()
866         {
867                 Encoding encoding = Encoding.ASCII;
868                 MemoryStream m = new MemoryStream ();
869                 StreamWriter w = new StreamWriter (m, encoding);
870                 w.Write (2);
871                 Assert.AreEqual (0, m.Length, "#1");
872                 w.Close ();
873                 Assert.IsFalse (m.CanWrite, "#2");
874                 Assert.AreEqual (50, m.GetBuffer () [0], "#3");
875                 Assert.IsNull (w.BaseStream, "#4");
876                 Assert.IsNull (w.Encoding, "#5");
877         }
878
879         [Test]
880         public void Flush ()
881         {
882                 MemoryStream m = new MemoryStream();
883                 StreamWriter w = new StreamWriter(m);
884                 w.Write(1);
885                 w.Write(2);
886                 w.Write(3);
887                 w.Write(4);
888                 Assert.AreEqual (0L, m.Length, "#1");
889                 w.Flush();
890                 Assert.AreEqual (4L, m.Length, "#2");
891         }
892
893         [Test]
894         public void Flush_Disposed ()
895         {
896                 StreamWriter w = new StreamWriter(new MemoryStream ());
897                 w.Close();
898                 try {
899                         w.Flush ();
900                         Assert.Fail ("#1");
901                 } catch (ObjectDisposedException) {
902                 }
903         }
904
905         [Test]
906         [ExpectedException (typeof (ObjectDisposedException))]
907         public void WriteChar_Disposed () 
908         {
909                 StreamWriter w = new StreamWriter (new MemoryStream ());
910                 w.Close ();
911                 w.Write ('A');
912         }
913
914         [Test]
915         [ExpectedException (typeof (ObjectDisposedException))]
916         public void WriteCharArray_Disposed () 
917         {
918                 char[] c = new char [2] { 'a', 'b' };
919                 StreamWriter w = new StreamWriter (new MemoryStream ());
920                 w.Close ();
921                 w.Write (c, 0, 2);
922         }
923
924         [Test]
925         public void WriteCharArray_Null () 
926         {
927                 char[] c = null;
928                 StreamWriter w = new StreamWriter (new MemoryStream ());
929                 w.Write (c);
930         }
931
932         [Test]
933         [ExpectedException (typeof (ArgumentException))]
934         public void WriteCharArray_IndexOverflow () 
935         {
936                 char[] c = new char [2] { 'a', 'b' };
937                 StreamWriter w = new StreamWriter (new MemoryStream ());
938                 w.Write (c, Int32.MaxValue, 2);
939         }
940
941         [Test]
942         [ExpectedException (typeof (ArgumentException))]
943         public void WriteCharArray_CountOverflow () 
944         {
945                 char[] c = new char [2] { 'a', 'b' };
946                 StreamWriter w = new StreamWriter (new MemoryStream ());
947                 w.Write (c, 1, Int32.MaxValue);
948         }
949
950         [Test]
951         [ExpectedException (typeof (ObjectDisposedException))]
952         public void WriteString_Disposed () 
953         {
954                 StreamWriter w = new StreamWriter (new MemoryStream ());
955                 w.Close ();
956                 w.Write ("mono");
957         }
958
959         [Test]
960         public void WriteString_Null () 
961         {
962                 string s = null;
963                 StreamWriter w = new StreamWriter (new MemoryStream ());
964                 w.Write (s);
965         }
966
967         [Test]
968         public void NoPreambleOnAppend ()
969         {
970                 MemoryStream ms = new MemoryStream ();
971                 StreamWriter w = new StreamWriter (ms, Encoding.UTF8);
972                 w.Write ("a");
973                 w.Flush ();
974                 Assert.AreEqual (4, ms.Position, "#1");
975
976                 // Append 1 byte, should skip the preamble now.
977                 w.Write ("a");
978                 w.Flush ();
979                 w = new StreamWriter (ms, Encoding.UTF8);
980                 Assert.AreEqual (5, ms.Position, "#2");
981         }
982         
983         // TODO - Write - test errors, functionality tested in TestFlush.
984 }
985 }