2003-05-22 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / Test / System.IO / StreamWriterTest.cs
1 // StreamWriterTest.cs - NUnit Test Cases for the SystemIO.StreamWriter class\r
2 //\r
3 // David Brandt (bucky@keystreams.com)\r
4 //\r
5 // (C) Ximian, Inc.  http://www.ximian.com\r
6 // \r
7 \r
8 using NUnit.Framework;\r
9 using System;\r
10 using System.IO;\r
11 using System.Text;\r
12 \r
13 namespace MonoTests.System.IO\r
14 {\r
15 \r
16 [TestFixture]\r
17 public class StreamWriterTest : Assertion\r
18 {\r
19 \r
20         static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");\r
21         private string _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";\r
22         private string _thisCodeFileName = TempFolder + Path.DirectorySeparatorChar + "StreamWriterTest.temp";\r
23 \r
24         [SetUp]\r
25         public void SetUp ()\r
26         {\r
27                 if (Directory.Exists (TempFolder))\r
28                         Directory.Delete (TempFolder, true);\r
29                 Directory.CreateDirectory (TempFolder);\r
30 \r
31                 if (!File.Exists (_thisCodeFileName)) \r
32                         File.Create (_thisCodeFileName).Close ();\r
33         }\r
34 \r
35         [TearDown]\r
36         public void TearDown ()\r
37         {\r
38                 if (Directory.Exists (TempFolder))\r
39                         Directory.Delete (TempFolder, true);\r
40         }\r
41 \r
42 \r
43         // TODO - ctors\r
44         [Test]\r
45         public void TestCtor1() {\r
46                 {\r
47                         bool errorThrown = false;\r
48                         try {\r
49                                 StreamWriter r = new StreamWriter((Stream)null);\r
50                         } catch (ArgumentNullException) {\r
51                                 errorThrown = true;\r
52                         } catch (Exception e) {\r
53                                 Fail ("Incorrect exception thrown at 1: " + e.ToString());\r
54                         }\r
55                         Assert("null string error not thrown", errorThrown);\r
56                 }\r
57                 {\r
58                         bool errorThrown = false;\r
59                         FileStream f = new FileStream(_thisCodeFileName, \r
60                                                       FileMode.Open, \r
61                                                       FileAccess.Read);\r
62                         try {\r
63                                 StreamWriter r = new StreamWriter(f);\r
64                                 r.Close();\r
65                         } catch (ArgumentException) {\r
66                                 errorThrown = true;\r
67                         } catch (Exception e) {\r
68                                 Fail ("Incorrect exception thrown at 2: " + e.ToString());\r
69                         }\r
70                         f.Close();\r
71                         Assert("no read error not thrown", errorThrown);\r
72                 }\r
73                 {\r
74                         FileStream f = new FileStream(_codeFileName, \r
75                                                       FileMode.Append, \r
76                                                       FileAccess.Write);\r
77                         StreamWriter r = new StreamWriter(f);\r
78                         AssertNotNull("no stream writer", r);\r
79                         r.Close();\r
80                         f.Close();\r
81                 }\r
82         }\r
83 \r
84         [Test]\r
85         public void TestCtor2() {\r
86                 {\r
87                         bool errorThrown = false;\r
88                         try {\r
89                                 StreamWriter r = new StreamWriter("");\r
90                         } catch (ArgumentException) {\r
91                                 errorThrown = true;\r
92                         } catch (Exception e) {\r
93                                 Fail ("Incorrect exception thrown at 1: " + e.ToString());\r
94                         }\r
95                         Assert("empty string error not thrown", errorThrown);\r
96                 }\r
97                 {\r
98                         bool errorThrown = false;\r
99                         try {\r
100                                 StreamWriter r = new StreamWriter((string)null);\r
101                         } catch (ArgumentNullException) {\r
102                                 errorThrown = true;\r
103                         } catch (Exception e) {\r
104                                 Fail ("Incorrect exception thrown at 2: " + e.ToString());\r
105                         }\r
106                         Assert("null string error not thrown", errorThrown);\r
107                 }\r
108                 {\r
109                         bool errorThrown = false;\r
110                         try {\r
111                                 StreamWriter r = new StreamWriter("nonexistentdir/file");\r
112                         } catch (DirectoryNotFoundException) {\r
113                                 errorThrown = true;\r
114                         } catch (Exception e) {\r
115                                 Fail ("Incorrect exception thrown at 3: " + e.ToString());\r
116                         }\r
117                         Assert("dirNotFound error not thrown", errorThrown);\r
118                 }\r
119                 {\r
120                         bool errorThrown = false;\r
121                         try {\r
122                                 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);\r
123                         } catch (IOException) {\r
124                                 errorThrown = true;\r
125                         } catch (ArgumentException) {\r
126                                 // FIXME - the spec says 'IOExc', but the\r
127                                 //   compiler says 'ArgExc'...\r
128                                 errorThrown = true;\r
129                         } catch (Exception e) {\r
130                                 Fail ("Incorrect exception thrown at 4: " + e.ToString());\r
131                         }\r
132                         Assert("1 invalid filename error not thrown", errorThrown);\r
133                 }\r
134                 // TODO - Security/Auth exceptions\r
135                 {\r
136                         StreamWriter r = new StreamWriter(_codeFileName);\r
137                         AssertNotNull("no stream writer", r);\r
138                         r.Close();\r
139                 }\r
140         }\r
141 \r
142         [Test]\r
143         public void TestCtor3() {\r
144                 {\r
145                         bool errorThrown = false;\r
146                         try {\r
147                                 StreamWriter r = new StreamWriter("", false);\r
148                         } catch (ArgumentException) {\r
149                                 errorThrown = true;\r
150                         } catch (Exception e) {\r
151                                 Fail ("Incorrect exception thrown at 1: " + e.ToString());\r
152                         }\r
153                         Assert("empty string error not thrown", errorThrown);\r
154                 }\r
155                 {\r
156                         bool errorThrown = false;\r
157                         try {\r
158                                 StreamWriter r = new StreamWriter((string)null, false);\r
159                         } catch (ArgumentNullException) {\r
160                                 errorThrown = true;\r
161                         } catch (Exception e) {\r
162                                 Fail ("Incorrect exception thrown at 2: " + e.ToString());\r
163                         }\r
164                         Assert("null string error not thrown", errorThrown);\r
165                 }\r
166                 {\r
167                         bool errorThrown = false;\r
168                         try {\r
169                                 StreamWriter r = new StreamWriter("nonexistentdir/file", false);\r
170                         } catch (DirectoryNotFoundException) {\r
171                                 errorThrown = true;\r
172                         } catch (Exception e) {\r
173                                 Fail ("Incorrect exception thrown at 3: " + e.ToString());\r
174                         }\r
175                         Assert("dirNotFound error not thrown", errorThrown);\r
176                 }\r
177                 {\r
178                         bool errorThrown = false;\r
179                         try {\r
180                                 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);\r
181                         } catch (IOException) {\r
182                                 errorThrown = true;\r
183                         } catch (ArgumentException) {\r
184                                 // FIXME - the spec says 'IOExc', but the\r
185                                 //   compiler says 'ArgExc'...\r
186                                 errorThrown = true;\r
187                         } catch (Exception e) {\r
188                                 Fail ("Incorrect exception thrown at 4: " + e.ToString());\r
189                         }\r
190                         Assert("2 invalid filename error not thrown", errorThrown);\r
191                 }\r
192                 {\r
193                         StreamWriter r = new StreamWriter(_codeFileName, false);\r
194                         AssertNotNull("no stream writer", r);\r
195                         r.Close();\r
196                 }\r
197                 {\r
198                         bool errorThrown = false;\r
199                         try {\r
200                                 StreamWriter r = new StreamWriter("", true);\r
201                         } catch (ArgumentException) {\r
202                                 errorThrown = true;\r
203                         } catch (Exception e) {\r
204                                 Fail ("Incorrect exception thrown at 5: " + e.ToString());\r
205                         }\r
206                         Assert("empty string error not thrown", errorThrown);\r
207                 }\r
208                 {\r
209                         bool errorThrown = false;\r
210                         try {\r
211                                 StreamWriter r = new StreamWriter((string)null, true);\r
212                         } catch (ArgumentNullException) {\r
213                                 errorThrown = true;\r
214                         } catch (Exception e) {\r
215                                 Fail ("Incorrect exception thrown at 6: " + e.ToString());\r
216                         }\r
217                         Assert("null string error not thrown", errorThrown);\r
218                 }\r
219                 {\r
220                         bool errorThrown = false;\r
221                         try {\r
222                                 StreamWriter r = new StreamWriter("nonexistentdir/file", true);\r
223                         } catch (DirectoryNotFoundException) {\r
224                                 errorThrown = true;\r
225                         } catch (Exception e) {\r
226                                 Fail ("Incorrect exception thrown at 7: " + e.ToString());\r
227                         }\r
228                         Assert("dirNotFound error not thrown", errorThrown);\r
229                 }\r
230                 {\r
231                         bool errorThrown = false;\r
232                         try {\r
233                                 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);\r
234                         } catch (IOException) {\r
235                                 errorThrown = true;\r
236                         } catch (ArgumentException) {\r
237                                 // FIXME - the spec says 'IOExc', but the\r
238                                 //   compiler says 'ArgExc'...\r
239                                 errorThrown = true;\r
240                         } catch (Exception e) {\r
241                                 Fail ("Incorrect exception thrown at 8: " + e.ToString());\r
242                         }\r
243                         Assert("3 invalid filename error not thrown", errorThrown);\r
244                 }\r
245                 {\r
246                         try {\r
247                                 StreamWriter r = new StreamWriter(_codeFileName, true);\r
248                                 AssertNotNull("no stream writer", r);\r
249                                 r.Close();\r
250                         } catch (Exception e) {\r
251                                 Fail ("Unxpected exception e=" + e.ToString());\r
252                         }\r
253                 }\r
254         }\r
255 \r
256         // TODO - ctors with Encoding\r
257 \r
258         // TODO - AutoFlush\r
259         [Test]\r
260         public void TestAutoFlush() {\r
261                 {\r
262                         MemoryStream m = new MemoryStream();\r
263                         StreamWriter w = new StreamWriter(m);\r
264                         w.AutoFlush = false;\r
265                         w.Write(1);\r
266                         w.Write(2);\r
267                         w.Write(3);\r
268                         w.Write(4);\r
269                         AssertEquals("Should be nothing before flush",\r
270                                      0L, m.Length);\r
271                         w.Flush();\r
272                         AssertEquals("Should be something after flush",\r
273                                      4L, m.Length);\r
274                 }               \r
275                 {\r
276                         MemoryStream m = new MemoryStream();\r
277                         StreamWriter w = new StreamWriter(m);\r
278                         w.AutoFlush = true;\r
279                         w.Write(1);\r
280                         w.Write(2);\r
281                         w.Write(3);\r
282                         w.Write(4);\r
283                         AssertEquals("Should be something before flush",\r
284                                      4L, m.Length);\r
285                         w.Flush();\r
286                         AssertEquals("Should be something after flush",\r
287                                      4L, m.Length);\r
288                 }               \r
289         }\r
290 \r
291         [Test]\r
292         public void TestBaseStream() {\r
293                 FileStream f = new FileStream(_codeFileName, \r
294                                               FileMode.Append, \r
295                                               FileAccess.Write);\r
296                 StreamWriter r = new StreamWriter(f);\r
297                 AssertEquals("wrong base stream ", f, r.BaseStream);\r
298                 r.Close();\r
299                 f.Close();\r
300         }\r
301 \r
302         [Test]\r
303         public void TestEncoding() {\r
304                 StreamWriter r = new StreamWriter(_codeFileName);\r
305                 AssertEquals("wrong encoding", \r
306                              Encoding.UTF8.GetType(), r.Encoding.GetType());\r
307                 r.Close();\r
308         }\r
309 \r
310         // TODO - Close - not entirely sure how to test Close\r
311         //public void TestClose() {\r
312         //{\r
313         //MemoryStream m = new MemoryStream();\r
314         //StreamWriter w = new StreamWriter(m);\r
315         //StreamReader r = new StreamReader(m);\r
316         //w.Write(1);\r
317         //w.Write(2);\r
318         //w.Write(3);\r
319         //w.Write(4);\r
320         //AssertEquals("Should be nothing before close",\r
321         //0, m.Length);\r
322         //AssertEquals("Should be nothing in reader",\r
323         //-1, r.Peek());\r
324         //w.Close();\r
325         //AssertEquals("Should be something after close",\r
326         //1, r.Peek());\r
327         //}             \r
328         //}\r
329 \r
330         // TODO - Flush\r
331         [Test]\r
332         public void TestFlush() {\r
333                 {\r
334                         bool errorThrown = false;\r
335                         try {\r
336                                 FileStream f = new FileStream(_codeFileName, \r
337                                                               FileMode.Append, \r
338                                                               FileAccess.Write);\r
339                                 StreamWriter r = new StreamWriter(f);\r
340                                 r.Close();\r
341                                 r.Flush();\r
342                         } catch (ObjectDisposedException) {\r
343                                 errorThrown = true;\r
344                         } catch (Exception e) {\r
345                                 Fail ("Incorrect exception thrown at 1: " + e.ToString());\r
346                         }\r
347                         Assert("can't flush closed error not thrown", errorThrown);\r
348                 }\r
349                 {\r
350                         MemoryStream m = new MemoryStream();\r
351                         StreamWriter w = new StreamWriter(m);\r
352                         w.Write(1);\r
353                         w.Write(2);\r
354                         w.Write(3);\r
355                         w.Write(4);\r
356                         AssertEquals("Should be nothing before flush",\r
357                                      0L, m.Length);\r
358                         w.Flush();\r
359                         AssertEquals("Should be something after flush",\r
360                                      4L, m.Length);\r
361                 }               \r
362         }\r
363 \r
364         // TODO - Write - test errors, functionality tested in TestFlush.\r
365 }\r
366 }\r