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