1 // StreamWriterTest.cs - NUnit Test Cases for the SystemIO.StreamWriter class
3 // David Brandt (bucky@keystreams.com)
5 // (C) Ximian, Inc. http://www.ximian.com
12 using System.Threading;
14 namespace MonoTests.System.IO
17 public class StreamWriterTest
19 class MockStream : Stream
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;
27 public MockStream (bool canRead, bool canSeek, bool canWrite)
29 this.canRead = canRead;
30 this.canSeek = canSeek;
31 this.canWrite = canWrite;
34 public override bool CanRead {
40 public override bool CanSeek {
46 public override bool CanWrite {
52 public override void Flush ()
58 public override long Length {
64 public override long Position {
66 throw new NotImplementedException ();
69 throw new NotImplementedException ();
73 public override int Read (byte[] buffer, int offset, int count)
76 return OnRead (buffer, offset, count);
81 public override long Seek (long offset, SeekOrigin origin)
83 throw new NotImplementedException ();
86 public override void SetLength (long value)
91 public override void Write (byte[] buffer, int offset, int count)
94 OnWrite (buffer, offset, count);
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";
105 if (Directory.Exists (TempFolder))
106 Directory.Delete (TempFolder, true);
107 Directory.CreateDirectory (TempFolder);
109 if (!File.Exists (_thisCodeFileName))
110 File.Create (_thisCodeFileName).Close ();
114 public void TearDown ()
116 if (Directory.Exists (TempFolder))
117 Directory.Delete (TempFolder, true);
120 [Test] // .ctor (Stream)
121 public void Constructor1 ()
123 FileStream f = new FileStream(_codeFileName,
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");
135 [Test] // .ctor (Stream)
136 public void Constructor1_Stream_NotWritable ()
138 FileStream f = new FileStream (_thisCodeFileName, FileMode.Open,
141 new StreamWriter (f);
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");
154 [Test] // .ctor (Stream)
155 public void Constructor1_Stream_Null ()
158 new StreamWriter((Stream) null);
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");
168 [Test] // .ctor (String)
169 public void Constructor2 ()
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");
182 [Test] // .ctor (String)
183 public void Constructor2_Path_DirectoryNotFound ()
185 Directory.Delete (TempFolder, true);
188 new StreamWriter (_codeFileName);
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");
199 [Test] // .ctor (String)
200 public void Constructor2_Path_Empty ()
203 new StreamWriter (string.Empty);
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");
214 [Test] // .ctor (String)
215 public void Constructor2_Path_IllegalChars ()
218 new StreamWriter ("!$what? what? Huh? !$*#" + Path.InvalidPathChars [0]);
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");
229 [Test] // .ctor (String)
230 public void Constructor2_Path_Null ()
233 new StreamWriter ((string) null);
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");
243 [Test] // .ctor (Stream, Encoding)
244 public void Constructor3 ()
246 FileStream f = new FileStream (_codeFileName,
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");
258 [Test] // .ctor (Stream, Encoding)
259 public void Constructor3_Encoding_Null ()
261 MemoryStream m = new MemoryStream ();
263 new StreamWriter (m, (Encoding) null);
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");
273 [Test] // .ctor (Stream, Encoding)
274 public void Constructor3_Stream_NotWritable ()
276 FileStream f = new FileStream (_thisCodeFileName, FileMode.Open,
279 new StreamWriter (f, Encoding.UTF8);
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");
292 [Test] // .ctor (Stream, Encoding)
293 public void Constructor3_Stream_Null ()
296 new StreamWriter ((Stream) null, Encoding.UTF8);
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");
306 [Test] // .ctor (String, Boolean)
307 public void Constructor4 ()
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");
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");
328 [Test] // .ctor (String, Boolean)
329 public void Constructor4_Path_DirectoryNotFound ()
331 Directory.Delete (TempFolder, true);
334 new StreamWriter (_codeFileName, false);
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");
345 new StreamWriter (_codeFileName, true);
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");
356 [Test] // .ctor (String, Boolean)
357 public void Constructor4_Path_Empty ()
360 new StreamWriter (string.Empty, false);
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");
371 new StreamWriter (string.Empty, true);
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");
382 [Test] // .ctor (String, Boolean)
383 public void Constructor4_Path_InvalidChars ()
386 new StreamWriter ("!$what? what? Huh? !$*#" + Path.InvalidPathChars [0], false);
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");
397 new StreamWriter ("!$what? what? Huh? !$*#" + Path.InvalidPathChars [0], true);
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");
408 [Test] // .ctor (String, Boolean)
409 public void Constructor4_Path_Null ()
412 new StreamWriter ((string) null, false);
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");
422 new StreamWriter ((string) null, true);
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");
432 [Test] // .ctor (Stream, Encoding, Int32)
433 public void Constructor5 ()
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");
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");
457 [Test] // .ctor (Stream, Encoding, Int32)
458 public void Constructor5_BufferSize_NotPositive ()
460 MemoryStream m = new MemoryStream ();
463 new StreamWriter (m, Encoding.UTF8, 0);
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");
474 new StreamWriter (m, Encoding.UTF8, -1);
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");
485 [Test] // .ctor (Stream, Encoding, Int32)
486 public void Constructor5_Encoding_Null ()
488 MemoryStream m = new MemoryStream ();
490 new StreamWriter (m, (Encoding) null, 10);
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");
500 [Test] // .ctor (Stream, Encoding, Int32)
501 public void Constructor5_Stream_NotWritable ()
503 FileStream f = new FileStream (_thisCodeFileName, FileMode.Open,
506 new StreamWriter (f, Encoding.UTF8, 10);
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");
519 [Test] // .ctor (Stream, Encoding, Int32)
520 public void Constructor5_Stream_Null ()
523 new StreamWriter ((Stream) null, Encoding.UTF8, 10);
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");
533 [Test] // .ctor (String, Boolean, Encoding)
534 public void Constructor6 ()
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");
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");
555 [Test] // .ctor (String, Boolean, Encoding)
556 public void Constructor6_Encoding_Null ()
559 new StreamWriter (_codeFileName, false, (Encoding) null);
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");
569 new StreamWriter (_codeFileName, true, (Encoding) null);
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");
579 [Test] // .ctor (String, Boolean, Encoding)
580 public void Constructor6_Path_DirectoryNotFound ()
582 Directory.Delete (TempFolder, true);
585 new StreamWriter (_codeFileName, false, Encoding.UTF8);
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");
596 new StreamWriter (_codeFileName, true, Encoding.UTF8);
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");
607 [Test] // .ctor (String, Boolean, Encoding)
608 public void Constructor6_Path_Empty ()
611 new StreamWriter (string.Empty, false, Encoding.UTF8);
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");
622 new StreamWriter (string.Empty, true, Encoding.UTF8);
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");
633 [Test] // .ctor (String, Boolean, Encoding)
634 public void Constructor6_Path_InvalidChars ()
637 new StreamWriter ("!$what? what? Huh? !$*#" +
638 Path.InvalidPathChars [0], false,
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");
650 new StreamWriter ("!$what? what? Huh? !$*#" +
651 Path.InvalidPathChars [0], true,
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");
663 [Test] // .ctor (String, Boolean, Encoding)
664 public void Constructor6_Path_Null ()
667 new StreamWriter ((string) null, false, Encoding.UTF8);
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");
677 new StreamWriter ((string) null, true, Encoding.UTF8);
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");
687 [Test] // .ctor (String, Boolean, Encoding, Int32)
688 public void Constructor7 ()
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");
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");
709 [Test] // .ctor (String, Boolean, Encoding, Int32)
710 public void Constructor7_BufferSize_NotPositive ()
713 new StreamWriter (_codeFileName, false, Encoding.UTF8, 0);
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");
724 new StreamWriter (_codeFileName, false, Encoding.UTF8, -1);
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");
735 [Test] // .ctor (String, Boolean, Encoding, Int32)
736 public void Constructor7_Encoding_Null ()
739 new StreamWriter (_codeFileName, false, (Encoding) null, 10);
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");
749 new StreamWriter (_codeFileName, true, (Encoding) null, 10);
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");
759 [Test] // .ctor (String, Boolean, Encoding, Int32)
760 public void Constructor7_Path_DirectoryNotFound ()
762 Directory.Delete (TempFolder, true);
765 new StreamWriter (_codeFileName, false, Encoding.UTF8, 10);
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");
776 new StreamWriter (_codeFileName, true, Encoding.UTF8, 10);
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");
787 [Test] // .ctor (String, Boolean, Encoding, Int32)
788 public void Constructor7_Path_Empty ()
791 new StreamWriter (string.Empty, false, Encoding.UTF8, 10);
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");
802 new StreamWriter (string.Empty, true, Encoding.UTF8, 10);
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");
813 [Test] // .ctor (String, Boolean, Encoding, Int32)
814 public void Constructor7_Path_InvalidChars ()
817 new StreamWriter ("!$what? what? Huh? !$*#" +
818 Path.InvalidPathChars [0], false,
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");
830 new StreamWriter ("!$what? what? Huh? !$*#" +
831 Path.InvalidPathChars [0], true,
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");
843 [Test] // .ctor (String, Boolean, Encoding, Int32)
844 public void Constructor7_Path_Null ()
847 new StreamWriter ((string) null, false, Encoding.UTF8, 10);
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");
857 new StreamWriter ((string) null, true, Encoding.UTF8, 10);
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");
868 public void AutoFlush ()
873 m = new MemoryStream ();
874 w = new StreamWriter (m);
879 Assert.AreEqual (0, m.Length, "#A1");
881 Assert.IsTrue (w.AutoFlush, "#A2");
882 Assert.AreEqual (4, m.Length, "#A3");
884 Assert.AreEqual (4, m.Length, "#A4");
886 m = new MemoryStream ();
887 w = new StreamWriter(m);
889 Assert.IsTrue (w.AutoFlush, "#B1");
894 Assert.AreEqual (4, m.Length, "#B2");
896 Assert.AreEqual (4, m.Length, "#B3");
898 Assert.IsFalse (w.AutoFlush, "#B4");
900 Assert.AreEqual (4, m.Length, "#B5");
902 Assert.AreEqual (5, m.Length, "#B6");
906 public void AutoFlush_Disposed ()
910 w = new StreamWriter (new MemoryStream ());
913 Assert.IsFalse (w.AutoFlush, "#A1");
917 } catch (ObjectDisposedException) {
919 Assert.IsTrue (w.AutoFlush, "#A3");
921 w = new StreamWriter (new MemoryStream ());
924 Assert.IsTrue (w.AutoFlush, "#B1");
928 } catch (ObjectDisposedException) {
930 Assert.IsTrue (w.AutoFlush, "#B3");
932 Assert.IsFalse (w.AutoFlush, "#B4");
938 Encoding encoding = Encoding.ASCII;
939 MemoryStream m = new MemoryStream ();
940 StreamWriter w = new StreamWriter (m, encoding);
942 Assert.AreEqual (0, m.Length, "#1");
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");
953 MemoryStream m = new MemoryStream();
954 StreamWriter w = new StreamWriter(m);
959 Assert.AreEqual (0L, m.Length, "#1");
961 Assert.AreEqual (4L, m.Length, "#2");
965 public void Flush_Disposed ()
967 StreamWriter w = new StreamWriter(new MemoryStream ());
972 } catch (ObjectDisposedException) {
977 [ExpectedException (typeof (ObjectDisposedException))]
978 public void WriteChar_Disposed ()
980 StreamWriter w = new StreamWriter (new MemoryStream ());
986 [ExpectedException (typeof (ObjectDisposedException))]
987 public void WriteCharArray_Disposed ()
989 char[] c = new char [2] { 'a', 'b' };
990 StreamWriter w = new StreamWriter (new MemoryStream ());
996 public void WriteCharArray_Null ()
999 StreamWriter w = new StreamWriter (new MemoryStream ());
1004 [ExpectedException (typeof (ArgumentException))]
1005 public void WriteCharArray_IndexOverflow ()
1007 char[] c = new char [2] { 'a', 'b' };
1008 StreamWriter w = new StreamWriter (new MemoryStream ());
1009 w.Write (c, Int32.MaxValue, 2);
1013 [ExpectedException (typeof (ArgumentException))]
1014 public void WriteCharArray_CountOverflow ()
1016 char[] c = new char [2] { 'a', 'b' };
1017 StreamWriter w = new StreamWriter (new MemoryStream ());
1018 w.Write (c, 1, Int32.MaxValue);
1022 [ExpectedException (typeof (ObjectDisposedException))]
1023 public void WriteString_Disposed ()
1025 StreamWriter w = new StreamWriter (new MemoryStream ());
1031 public void WriteString_Null ()
1034 StreamWriter w = new StreamWriter (new MemoryStream ());
1039 public void NoPreambleOnAppend ()
1041 MemoryStream ms = new MemoryStream ();
1042 StreamWriter w = new StreamWriter (ms, Encoding.UTF8);
1045 Assert.AreEqual (4, ms.Position, "#1");
1047 // Append 1 byte, should skip the preamble now.
1050 w = new StreamWriter (ms, Encoding.UTF8);
1051 Assert.AreEqual (5, ms.Position, "#2");
1056 public void FlushAsync ()
1058 ManualResetEvent mre = new ManualResetEvent (false);
1059 var m = new MockStream(true, false, true);
1060 var w = new StreamWriter (m);
1062 Assert.AreEqual (0L, m.Length, "#1");
1063 var t = w.WriteLineAsync ();
1064 Assert.IsTrue (t.Wait (1000), "#2");
1065 Assert.IsTrue (w.FlushAsync ().Wait (1000), "#3");
1069 public void KeepOpenWithDispose ()
1071 var ms = new MemoryStream ();
1072 using (StreamWriter writer = new StreamWriter (ms, new UTF8Encoding (false), 4096, true)) {
1076 Assert.AreEqual (1, ms.Length);
1081 // TODO - Write - test errors, functionality tested in TestFlush.