eol style
[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
13 namespace MonoTests.System.IO
14 {
15
16 [TestFixture]
17 public class StreamWriterTest : Assertion
18 {
19
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";
23
24         [SetUp]
25         public void SetUp ()
26         {
27                 if (Directory.Exists (TempFolder))
28                         Directory.Delete (TempFolder, true);
29                 Directory.CreateDirectory (TempFolder);
30
31                 if (!File.Exists (_thisCodeFileName)) 
32                         File.Create (_thisCodeFileName).Close ();
33         }
34
35         [TearDown]
36         public void TearDown ()
37         {
38                 if (Directory.Exists (TempFolder))
39                         Directory.Delete (TempFolder, true);
40         }
41
42
43         // TODO - ctors
44         [Test]
45         public void TestCtor1() {
46                 {
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());
54                         }
55                         Assert("null string error not thrown", errorThrown);
56                 }
57                 {
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());
69                         }
70                         f.Close();
71                         Assert("no read error not thrown", errorThrown);
72                 }
73                 {
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();
81                 }
82         }
83
84         [Test]
85         public void TestCtor2() {
86                 {
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());
94                         }
95                         Assert("empty string error not thrown", errorThrown);
96                 }
97                 {
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());
105                         }
106                         Assert("null string error not thrown", errorThrown);
107                 }
108                 {
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());
116                         }
117                         Assert("dirNotFound error not thrown", errorThrown);
118                 }
119                 {
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());
131                         }
132                         Assert("1 invalid filename error not thrown", errorThrown);
133                 }
134                 // TODO - Security/Auth exceptions
135                 {
136                         StreamWriter r = new StreamWriter(_codeFileName);
137                         AssertNotNull("no stream writer", r);
138                         r.Close();
139                 }
140         }
141
142         [Test]
143         public void TestCtor3() {
144                 {
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());
152                         }
153                         Assert("empty string error not thrown", errorThrown);
154                 }
155                 {
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());
163                         }
164                         Assert("null string error not thrown", errorThrown);
165                 }
166                 {
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());
174                         }
175                         Assert("dirNotFound error not thrown", errorThrown);
176                 }
177                 {
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());
189                         }
190                         Assert("2 invalid filename error not thrown", errorThrown);
191                 }
192                 {
193                         StreamWriter r = new StreamWriter(_codeFileName, false);
194                         AssertNotNull("no stream writer", r);
195                         r.Close();
196                 }
197                 {
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());
205                         }
206                         Assert("empty string error not thrown", errorThrown);
207                 }
208                 {
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());
216                         }
217                         Assert("null string error not thrown", errorThrown);
218                 }
219                 {
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());
227                         }
228                         Assert("dirNotFound error not thrown", errorThrown);
229                 }
230                 {
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());
242                         }
243                         Assert("3 invalid filename error not thrown", errorThrown);
244                 }
245                 {
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());
252                         }
253                 }
254         }
255
256         // TODO - ctors with Encoding
257
258         // TODO - AutoFlush
259         [Test]
260         public void TestAutoFlush() {
261                 {
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);
274                 }               
275                 {
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);
288                 }               
289         }
290
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();
300         }
301
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();
308         }
309
310         // TODO - Close - not entirely sure how to test Close
311         //public void TestClose() {
312         //{
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         //}             
328         //}
329
330         // TODO - Flush
331         [Test]
332         public void TestFlush() {
333                 {
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());
346                         }
347                         Assert("can't flush closed error not thrown", errorThrown);
348                 }
349                 {
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);
361                 }               
362         }
363
364         [Test]
365         [ExpectedException (typeof (ObjectDisposedException))]
366         public void AutoFlush_Disposed () 
367         {
368                 StreamWriter w = new StreamWriter (new MemoryStream ());
369                 w.Close ();
370                 w.AutoFlush = true;
371         }
372
373         [Test]
374         [ExpectedException (typeof (ObjectDisposedException))]
375         public void WriteChar_Disposed () 
376         {
377                 StreamWriter w = new StreamWriter (new MemoryStream ());
378                 w.Close ();
379                 w.Write ('A');
380         }
381
382         [Test]
383         [ExpectedException (typeof (ObjectDisposedException))]
384         public void WriteCharArray_Disposed () 
385         {
386                 char[] c = new char [2] { 'a', 'b' };
387                 StreamWriter w = new StreamWriter (new MemoryStream ());
388                 w.Close ();
389                 w.Write (c, 0, 2);
390         }
391
392         [Test]
393         // accepted [ExpectedException (typeof (ArgumentNullException))]
394         public void WriteCharArray_Null () 
395         {
396                 char[] c = null;
397                 StreamWriter w = new StreamWriter (new MemoryStream ());
398                 w.Write (c);
399         }
400
401         [Test]
402         [ExpectedException (typeof (ArgumentException))]
403         public void WriteCharArray_IndexOverflow () 
404         {
405                 char[] c = new char [2] { 'a', 'b' };
406                 StreamWriter w = new StreamWriter (new MemoryStream ());
407                 w.Write (c, Int32.MaxValue, 2);
408         }
409
410         [Test]
411         [ExpectedException (typeof (ArgumentException))]
412         public void WriteCharArray_CountOverflow () 
413         {
414                 char[] c = new char [2] { 'a', 'b' };
415                 StreamWriter w = new StreamWriter (new MemoryStream ());
416                 w.Write (c, 1, Int32.MaxValue);
417         }
418
419         [Test]
420         [ExpectedException (typeof (ObjectDisposedException))]
421         public void WriteString_Disposed () 
422         {
423                 StreamWriter w = new StreamWriter (new MemoryStream ());
424                 w.Close ();
425                 w.Write ("mono");
426         }
427
428         [Test]
429         // accepted [ExpectedException (typeof (ArgumentNullException))]
430         public void WriteString_Null () 
431         {
432                 string s = null;
433                 StreamWriter w = new StreamWriter (new MemoryStream ());
434                 w.Write (s);
435         }
436
437         [Test]
438         public void NoPreambleOnAppend ()
439         {
440                 MemoryStream ms = new MemoryStream ();
441                 StreamWriter w = new StreamWriter (ms, Encoding.UTF8);
442                 w.Write ("a");
443                 w.Flush ();
444                 AssertEquals ("Incorrect size after writing 1 byte plus header", ms.Position, 4);
445
446                 // Append 1 byte, should skip the preamble now.
447                 w.Write ("a");
448                 w.Flush ();
449                 w = new StreamWriter (ms, Encoding.UTF8);
450                 AssertEquals ("Incorrect size after writing 1 byte, must have been 5", ms.Position, 5);
451                 
452         }
453         
454         // TODO - Write - test errors, functionality tested in TestFlush.
455 }
456 }