(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / Test / System.IO / StreamWriterTest.cs
blob307d3ef7cf44bfb8d75b12f251660a97218b9560
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 //
8 using NUnit.Framework;
9 using System;
10 using System.IO;
11 using System.Text;
13 namespace MonoTests.System.IO
16 [TestFixture]
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";
24 [SetUp]
25 public void SetUp ()
27 if (Directory.Exists (TempFolder))
28 Directory.Delete (TempFolder, true);
29 Directory.CreateDirectory (TempFolder);
31 if (!File.Exists (_thisCodeFileName))
32 File.Create (_thisCodeFileName).Close ();
35 [TearDown]
36 public void TearDown ()
38 if (Directory.Exists (TempFolder))
39 Directory.Delete (TempFolder, true);
43 // TODO - ctors
44 [Test]
45 public void TestCtor1() {
47 bool errorThrown = false;
48 try {
49 StreamWriter r = new StreamWriter((Stream)null);
50 } catch (ArgumentNullException) {
51 errorThrown = true;
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,
60 FileMode.Open,
61 FileAccess.Read);
62 try {
63 StreamWriter r = new StreamWriter(f);
64 r.Close();
65 } catch (ArgumentException) {
66 errorThrown = true;
67 } catch (Exception e) {
68 Fail ("Incorrect exception thrown at 2: " + e.ToString());
70 f.Close();
71 Assert("no read error not thrown", errorThrown);
74 FileStream f = new FileStream(_codeFileName,
75 FileMode.Append,
76 FileAccess.Write);
77 StreamWriter r = new StreamWriter(f);
78 AssertNotNull("no stream writer", r);
79 r.Close();
80 f.Close();
84 [Test]
85 public void TestCtor2() {
87 bool errorThrown = false;
88 try {
89 StreamWriter r = new StreamWriter("");
90 } catch (ArgumentException) {
91 errorThrown = true;
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;
99 try {
100 StreamWriter r = new StreamWriter((string)null);
101 } catch (ArgumentNullException) {
102 errorThrown = true;
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;
110 try {
111 StreamWriter r = new StreamWriter("nonexistentdir/file");
112 } catch (DirectoryNotFoundException) {
113 errorThrown = true;
114 } catch (Exception e) {
115 Fail ("Incorrect exception thrown at 3: " + e.ToString());
117 Assert("dirNotFound error not thrown", errorThrown);
120 bool errorThrown = false;
121 try {
122 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
123 } catch (IOException) {
124 errorThrown = true;
125 } catch (ArgumentException) {
126 // FIXME - the spec says 'IOExc', but the
127 // compiler says 'ArgExc'...
128 errorThrown = true;
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);
138 r.Close();
142 [Test]
143 public void TestCtor3() {
145 bool errorThrown = false;
146 try {
147 StreamWriter r = new StreamWriter("", false);
148 } catch (ArgumentException) {
149 errorThrown = true;
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;
157 try {
158 StreamWriter r = new StreamWriter((string)null, false);
159 } catch (ArgumentNullException) {
160 errorThrown = true;
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;
168 try {
169 StreamWriter r = new StreamWriter("nonexistentdir/file", false);
170 } catch (DirectoryNotFoundException) {
171 errorThrown = true;
172 } catch (Exception e) {
173 Fail ("Incorrect exception thrown at 3: " + e.ToString());
175 Assert("dirNotFound error not thrown", errorThrown);
178 bool errorThrown = false;
179 try {
180 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
181 } catch (IOException) {
182 errorThrown = true;
183 } catch (ArgumentException) {
184 // FIXME - the spec says 'IOExc', but the
185 // compiler says 'ArgExc'...
186 errorThrown = true;
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);
195 r.Close();
198 bool errorThrown = false;
199 try {
200 StreamWriter r = new StreamWriter("", true);
201 } catch (ArgumentException) {
202 errorThrown = true;
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;
210 try {
211 StreamWriter r = new StreamWriter((string)null, true);
212 } catch (ArgumentNullException) {
213 errorThrown = true;
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;
221 try {
222 StreamWriter r = new StreamWriter("nonexistentdir/file", true);
223 } catch (DirectoryNotFoundException) {
224 errorThrown = true;
225 } catch (Exception e) {
226 Fail ("Incorrect exception thrown at 7: " + e.ToString());
228 Assert("dirNotFound error not thrown", errorThrown);
231 bool errorThrown = false;
232 try {
233 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
234 } catch (IOException) {
235 errorThrown = true;
236 } catch (ArgumentException) {
237 // FIXME - the spec says 'IOExc', but the
238 // compiler says 'ArgExc'...
239 errorThrown = true;
240 } catch (Exception e) {
241 Fail ("Incorrect exception thrown at 8: " + e.ToString());
243 Assert("3 invalid filename error not thrown", errorThrown);
246 try {
247 StreamWriter r = new StreamWriter(_codeFileName, true);
248 AssertNotNull("no stream writer", r);
249 r.Close();
250 } catch (Exception e) {
251 Fail ("Unxpected exception e=" + e.ToString());
256 // TODO - ctors with Encoding
258 // TODO - AutoFlush
259 [Test]
260 public void TestAutoFlush() {
262 MemoryStream m = new MemoryStream();
263 StreamWriter w = new StreamWriter(m);
264 w.AutoFlush = false;
265 w.Write(1);
266 w.Write(2);
267 w.Write(3);
268 w.Write(4);
269 AssertEquals("Should be nothing before flush",
270 0L, m.Length);
271 w.Flush();
272 AssertEquals("Should be something after flush",
273 4L, m.Length);
276 MemoryStream m = new MemoryStream();
277 StreamWriter w = new StreamWriter(m);
278 w.AutoFlush = true;
279 w.Write(1);
280 w.Write(2);
281 w.Write(3);
282 w.Write(4);
283 AssertEquals("Should be something before flush",
284 4L, m.Length);
285 w.Flush();
286 AssertEquals("Should be something after flush",
287 4L, m.Length);
291 [Test]
292 public void TestBaseStream() {
293 FileStream f = new FileStream(_codeFileName,
294 FileMode.Append,
295 FileAccess.Write);
296 StreamWriter r = new StreamWriter(f);
297 AssertEquals("wrong base stream ", f, r.BaseStream);
298 r.Close();
299 f.Close();
302 [Test]
303 public void TestEncoding() {
304 StreamWriter r = new StreamWriter(_codeFileName);
305 AssertEquals("wrong encoding",
306 Encoding.UTF8.GetType(), r.Encoding.GetType());
307 r.Close();
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);
316 //w.Write(1);
317 //w.Write(2);
318 //w.Write(3);
319 //w.Write(4);
320 //AssertEquals("Should be nothing before close",
321 //0, m.Length);
322 //AssertEquals("Should be nothing in reader",
323 //-1, r.Peek());
324 //w.Close();
325 //AssertEquals("Should be something after close",
326 //1, r.Peek());
327 //}
330 // TODO - Flush
331 [Test]
332 public void TestFlush() {
334 bool errorThrown = false;
335 try {
336 FileStream f = new FileStream(_codeFileName,
337 FileMode.Append,
338 FileAccess.Write);
339 StreamWriter r = new StreamWriter(f);
340 r.Close();
341 r.Flush();
342 } catch (ObjectDisposedException) {
343 errorThrown = true;
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);
352 w.Write(1);
353 w.Write(2);
354 w.Write(3);
355 w.Write(4);
356 AssertEquals("Should be nothing before flush",
357 0L, m.Length);
358 w.Flush();
359 AssertEquals("Should be something after flush",
360 4L, m.Length);
364 [Test]
365 [ExpectedException (typeof (ObjectDisposedException))]
366 public void AutoFlush_Disposed ()
368 StreamWriter w = new StreamWriter (new MemoryStream ());
369 w.Close ();
370 w.AutoFlush = true;
373 [Test]
374 [ExpectedException (typeof (ObjectDisposedException))]
375 public void WriteChar_Disposed ()
377 StreamWriter w = new StreamWriter (new MemoryStream ());
378 w.Close ();
379 w.Write ('A');
382 [Test]
383 [ExpectedException (typeof (ObjectDisposedException))]
384 public void WriteCharArray_Disposed ()
386 char[] c = new char [2] { 'a', 'b' };
387 StreamWriter w = new StreamWriter (new MemoryStream ());
388 w.Close ();
389 w.Write (c, 0, 2);
392 [Test]
393 // accepted [ExpectedException (typeof (ArgumentNullException))]
394 public void WriteCharArray_Null ()
396 char[] c = null;
397 StreamWriter w = new StreamWriter (new MemoryStream ());
398 w.Write (c);
401 [Test]
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);
410 [Test]
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);
419 [Test]
420 [ExpectedException (typeof (ObjectDisposedException))]
421 public void WriteString_Disposed ()
423 StreamWriter w = new StreamWriter (new MemoryStream ());
424 w.Close ();
425 w.Write ("mono");
428 [Test]
429 // accepted [ExpectedException (typeof (ArgumentNullException))]
430 public void WriteString_Null ()
432 string s = null;
433 StreamWriter w = new StreamWriter (new MemoryStream ());
434 w.Write (s);
437 // TODO - Write - test errors, functionality tested in TestFlush.