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
13 namespace MonoTests
.System
.IO
17 public class StreamWriterTest
: Assertion
20 static string TempFolder
= Path
.Combine (Path
.GetTempPath (), "MonoTests.System.IO.Tests");
21 private string _codeFileName
= TempFolder
+ Path
.DirectorySeparatorChar
+ "AFile.txt";
22 private string _thisCodeFileName
= TempFolder
+ Path
.DirectorySeparatorChar
+ "StreamWriterTest.temp";
27 if (Directory
.Exists (TempFolder
))
28 Directory
.Delete (TempFolder
, true);
29 Directory
.CreateDirectory (TempFolder
);
31 if (!File
.Exists (_thisCodeFileName
))
32 File
.Create (_thisCodeFileName
).Close ();
36 public void TearDown ()
38 if (Directory
.Exists (TempFolder
))
39 Directory
.Delete (TempFolder
, true);
45 public void TestCtor1() {
47 bool errorThrown
= false;
49 StreamWriter r
= new StreamWriter((Stream
)null);
50 } catch (ArgumentNullException
) {
52 } catch (Exception e
) {
53 Fail ("Incorrect exception thrown at 1: " + e
.ToString());
55 Assert("null string error not thrown", errorThrown
);
58 bool errorThrown
= false;
59 FileStream f
= new FileStream(_thisCodeFileName
,
63 StreamWriter r
= new StreamWriter(f
);
65 } catch (ArgumentException
) {
67 } catch (Exception e
) {
68 Fail ("Incorrect exception thrown at 2: " + e
.ToString());
71 Assert("no read error not thrown", errorThrown
);
74 FileStream f
= new FileStream(_codeFileName
,
77 StreamWriter r
= new StreamWriter(f
);
78 AssertNotNull("no stream writer", r
);
85 public void TestCtor2() {
87 bool errorThrown
= false;
89 StreamWriter r
= new StreamWriter("");
90 } catch (ArgumentException
) {
92 } catch (Exception e
) {
93 Fail ("Incorrect exception thrown at 1: " + e
.ToString());
95 Assert("empty string error not thrown", errorThrown
);
98 bool errorThrown
= false;
100 StreamWriter r
= new StreamWriter((string)null);
101 } catch (ArgumentNullException
) {
103 } catch (Exception e
) {
104 Fail ("Incorrect exception thrown at 2: " + e
.ToString());
106 Assert("null string error not thrown", errorThrown
);
109 bool errorThrown
= false;
111 StreamWriter r
= new StreamWriter("nonexistentdir/file");
112 } catch (DirectoryNotFoundException
) {
114 } catch (Exception e
) {
115 Fail ("Incorrect exception thrown at 3: " + e
.ToString());
117 Assert("dirNotFound error not thrown", errorThrown
);
120 bool errorThrown
= false;
122 StreamWriter r
= new StreamWriter("!$what? what? Huh? !$*#" + Path
.InvalidPathChars
[0]);
123 } catch (IOException
) {
125 } catch (ArgumentException
) {
126 // FIXME - the spec says 'IOExc', but the
127 // compiler says 'ArgExc'...
129 } catch (Exception e
) {
130 Fail ("Incorrect exception thrown at 4: " + e
.ToString());
132 Assert("1 invalid filename error not thrown", errorThrown
);
134 // TODO - Security/Auth exceptions
136 StreamWriter r
= new StreamWriter(_codeFileName
);
137 AssertNotNull("no stream writer", r
);
143 public void TestCtor3() {
145 bool errorThrown
= false;
147 StreamWriter r
= new StreamWriter("", false);
148 } catch (ArgumentException
) {
150 } catch (Exception e
) {
151 Fail ("Incorrect exception thrown at 1: " + e
.ToString());
153 Assert("empty string error not thrown", errorThrown
);
156 bool errorThrown
= false;
158 StreamWriter r
= new StreamWriter((string)null, false);
159 } catch (ArgumentNullException
) {
161 } catch (Exception e
) {
162 Fail ("Incorrect exception thrown at 2: " + e
.ToString());
164 Assert("null string error not thrown", errorThrown
);
167 bool errorThrown
= false;
169 StreamWriter r
= new StreamWriter("nonexistentdir/file", false);
170 } catch (DirectoryNotFoundException
) {
172 } catch (Exception e
) {
173 Fail ("Incorrect exception thrown at 3: " + e
.ToString());
175 Assert("dirNotFound error not thrown", errorThrown
);
178 bool errorThrown
= false;
180 StreamWriter r
= new StreamWriter("!$what? what? Huh? !$*#" + Path
.InvalidPathChars
[0], false);
181 } catch (IOException
) {
183 } catch (ArgumentException
) {
184 // FIXME - the spec says 'IOExc', but the
185 // compiler says 'ArgExc'...
187 } catch (Exception e
) {
188 Fail ("Incorrect exception thrown at 4: " + e
.ToString());
190 Assert("2 invalid filename error not thrown", errorThrown
);
193 StreamWriter r
= new StreamWriter(_codeFileName
, false);
194 AssertNotNull("no stream writer", r
);
198 bool errorThrown
= false;
200 StreamWriter r
= new StreamWriter("", true);
201 } catch (ArgumentException
) {
203 } catch (Exception e
) {
204 Fail ("Incorrect exception thrown at 5: " + e
.ToString());
206 Assert("empty string error not thrown", errorThrown
);
209 bool errorThrown
= false;
211 StreamWriter r
= new StreamWriter((string)null, true);
212 } catch (ArgumentNullException
) {
214 } catch (Exception e
) {
215 Fail ("Incorrect exception thrown at 6: " + e
.ToString());
217 Assert("null string error not thrown", errorThrown
);
220 bool errorThrown
= false;
222 StreamWriter r
= new StreamWriter("nonexistentdir/file", true);
223 } catch (DirectoryNotFoundException
) {
225 } catch (Exception e
) {
226 Fail ("Incorrect exception thrown at 7: " + e
.ToString());
228 Assert("dirNotFound error not thrown", errorThrown
);
231 bool errorThrown
= false;
233 StreamWriter r
= new StreamWriter("!$what? what? Huh? !$*#" + Path
.InvalidPathChars
[0], true);
234 } catch (IOException
) {
236 } catch (ArgumentException
) {
237 // FIXME - the spec says 'IOExc', but the
238 // compiler says 'ArgExc'...
240 } catch (Exception e
) {
241 Fail ("Incorrect exception thrown at 8: " + e
.ToString());
243 Assert("3 invalid filename error not thrown", errorThrown
);
247 StreamWriter r
= new StreamWriter(_codeFileName
, true);
248 AssertNotNull("no stream writer", r
);
250 } catch (Exception e
) {
251 Fail ("Unxpected exception e=" + e
.ToString());
256 // TODO - ctors with Encoding
260 public void TestAutoFlush() {
262 MemoryStream m
= new MemoryStream();
263 StreamWriter w
= new StreamWriter(m
);
269 AssertEquals("Should be nothing before flush",
272 AssertEquals("Should be something after flush",
276 MemoryStream m
= new MemoryStream();
277 StreamWriter w
= new StreamWriter(m
);
283 AssertEquals("Should be something before flush",
286 AssertEquals("Should be something after flush",
292 public void TestBaseStream() {
293 FileStream f
= new FileStream(_codeFileName
,
296 StreamWriter r
= new StreamWriter(f
);
297 AssertEquals("wrong base stream ", f
, r
.BaseStream
);
303 public void TestEncoding() {
304 StreamWriter r
= new StreamWriter(_codeFileName
);
305 AssertEquals("wrong encoding",
306 Encoding
.UTF8
.GetType(), r
.Encoding
.GetType());
310 // TODO - Close - not entirely sure how to test Close
311 //public void TestClose() {
313 //MemoryStream m = new MemoryStream();
314 //StreamWriter w = new StreamWriter(m);
315 //StreamReader r = new StreamReader(m);
320 //AssertEquals("Should be nothing before close",
322 //AssertEquals("Should be nothing in reader",
325 //AssertEquals("Should be something after close",
332 public void TestFlush() {
334 bool errorThrown
= false;
336 FileStream f
= new FileStream(_codeFileName
,
339 StreamWriter r
= new StreamWriter(f
);
342 } catch (ObjectDisposedException
) {
344 } catch (Exception e
) {
345 Fail ("Incorrect exception thrown at 1: " + e
.ToString());
347 Assert("can't flush closed error not thrown", errorThrown
);
350 MemoryStream m
= new MemoryStream();
351 StreamWriter w
= new StreamWriter(m
);
356 AssertEquals("Should be nothing before flush",
359 AssertEquals("Should be something after flush",
365 [ExpectedException (typeof (ObjectDisposedException
))]
366 public void AutoFlush_Disposed ()
368 StreamWriter w
= new StreamWriter (new MemoryStream ());
374 [ExpectedException (typeof (ObjectDisposedException
))]
375 public void WriteChar_Disposed ()
377 StreamWriter w
= new StreamWriter (new MemoryStream ());
383 [ExpectedException (typeof (ObjectDisposedException
))]
384 public void WriteCharArray_Disposed ()
386 char[] c
= new char [2] { 'a', 'b' }
;
387 StreamWriter w
= new StreamWriter (new MemoryStream ());
393 // accepted [ExpectedException (typeof (ArgumentNullException))]
394 public void WriteCharArray_Null ()
397 StreamWriter w
= new StreamWriter (new MemoryStream ());
402 [ExpectedException (typeof (ArgumentException
))]
403 public void WriteCharArray_IndexOverflow ()
405 char[] c
= new char [2] { 'a', 'b' }
;
406 StreamWriter w
= new StreamWriter (new MemoryStream ());
407 w
.Write (c
, Int32
.MaxValue
, 2);
411 [ExpectedException (typeof (ArgumentException
))]
412 public void WriteCharArray_CountOverflow ()
414 char[] c
= new char [2] { 'a', 'b' }
;
415 StreamWriter w
= new StreamWriter (new MemoryStream ());
416 w
.Write (c
, 1, Int32
.MaxValue
);
420 [ExpectedException (typeof (ObjectDisposedException
))]
421 public void WriteString_Disposed ()
423 StreamWriter w
= new StreamWriter (new MemoryStream ());
429 // accepted [ExpectedException (typeof (ArgumentNullException))]
430 public void WriteString_Null ()
433 StreamWriter w
= new StreamWriter (new MemoryStream ());
437 // TODO - Write - test errors, functionality tested in TestFlush.