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