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