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