2002-09-26 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.IO / FileTest.cs
1 //
2 // FileTest.cs: Test cases for System.IO.File
3 //
4 // Author: Duncan Mak (duncan@ximian.com)
5 //
6 // (C) 2002 Ximian, Inc. http://www.ximian.com
7 //
8
9 using NUnit.Framework;
10 using System;
11 using System.IO;
12
13 namespace MonoTests.System.IO
14 {
15         public class FileTest : TestCase
16         {
17                 public FileTest ()
18                         : base ("System.IO.File testsuite")
19                 {
20                 }
21
22                 public FileTest (string name)
23                         : base (name)
24                 {
25                 }
26
27                 protected override void SetUp ()
28                 {
29                 }
30
31                 protected override void TearDown ()
32                 {
33                         File.Delete ("resources" + Path.DirectorySeparatorChar + "baz");
34                         File.Delete ("resources" + Path.DirectorySeparatorChar + "bar");
35                         File.Delete ("resources" + Path.DirectorySeparatorChar + "foo");
36                 }
37
38                 public static ITest Suite
39                 {
40                         get { return new TestSuite (typeof (FileTest)); }
41                 }
42
43                 public void TestExists ()
44                 {
45                         int i = 0;
46                         try {
47                                 Assert ("null filename should not exist", !File.Exists (null));
48                                 i++;
49                                 Assert ("empty filename should not exist", !File.Exists (""));
50                                 i++;
51                                 Assert ("whitespace filename should not exist", !File.Exists ("  \t\t  \t \n\t\n \n"));
52                                 i++;
53                                 Assert ("File resources" + Path.DirectorySeparatorChar + "AFile.txt should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
54                                 i++;
55                                 Assert ("File resources" + Path.DirectorySeparatorChar + "doesnotexist should not exist", !File.Exists ("resources" + Path.DirectorySeparatorChar + "doesnotexist"));
56                         } catch (Exception e) {
57                                 Fail ("Unexpected exception at i = " + i + ". e=" + e);
58                         }
59                 }
60
61                 public void TestCreate ()
62                 {
63                         FileStream stream;
64
65                         /* exception test: File.Create(null) */
66                         try {
67                                 stream = File.Create (null);
68                                 Fail ("File.Create(null) should throw ArgumentNullException");
69                         } catch (ArgumentNullException) {
70                                 // do nothing, this is what we expect
71                         } catch (Exception e) {
72                                 Fail ("File.Create(null) unexpected exception caught: e=" + e.ToString());
73                         }
74
75                         /* exception test: File.Create("") */
76                         try {
77                                 stream = File.Create ("");
78                                 Fail ("File.Create('') should throw ArgumentException");
79                         } catch (ArgumentException) {
80                                 // do nothing, this is what we expect
81                         } catch (Exception e) {
82                                 Fail ("File.Create('') unexpected exception caught: e=" + e.ToString());
83                         }
84
85                         /* exception test: File.Create(" ") */
86                         try {
87                                 stream = File.Create (" ");
88                                 Fail ("File.Create(' ') should throw ArgumentException");
89                         } catch (ArgumentException) {
90                                 // do nothing, this is what we expect
91                         } catch (Exception e) {
92                                 Fail ("File.Create(' ') unexpected exception caught: e=" + e.ToString());
93                         }
94
95                         /* exception test: File.Create(directory_not_found) */
96                         try {
97                                 stream = File.Create ("directory_does_not_exist" + Path.DirectorySeparatorChar + "foo");
98                                 Fail ("File.Create(directory_does_not_exist) should throw DirectoryNotFoundException");
99                         } catch (DirectoryNotFoundException) {
100                                 // do nothing, this is what we expect
101                         } catch (Exception e) {
102                                 Fail ("File.Create(directory_does_not_exist) unexpected exception caught: e=" + e.ToString());
103                         }
104
105
106                         /* positive test: create resources/foo */
107                         try {
108                                 stream = File.Create ("resources" + Path.DirectorySeparatorChar + "foo");
109                                 Assert ("File should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
110                                 stream.Close ();
111                         } catch (Exception e) {
112                                 Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
113                         }
114
115                         /* positive test: repeat test above again to test for overwriting file */
116                         try {
117                                 stream = File.Create ("resources" + Path.DirectorySeparatorChar + "foo");
118                                 Assert ("File should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
119                                 stream.Close ();
120                         } catch (Exception e) {
121                                 Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
122                         }
123                 }
124
125                 public void TestCopy ()
126                 {
127                         /* exception test: File.Copy(null, b) */
128                         try {
129                                 File.Copy (null, "b");
130                                 Fail ("File.Copy(null, 'b') should throw ArgumentNullException");
131                         } catch (ArgumentNullException) {
132                                 // do nothing, this is what we expect
133                         } catch (Exception e) {
134                                 Fail ("File.Copy(null, 'b') unexpected exception caught: e=" + e.ToString());
135                         }
136
137                         /* exception test: File.Copy(a, null) */
138                         try {
139                                 File.Copy ("a", null);
140                                 Fail ("File.Copy('a', null) should throw ArgumentNullException");
141                         } catch (ArgumentNullException) {
142                                 // do nothing, this is what we expect
143                         } catch (Exception e) {
144                                 Fail ("File.Copy('a', null) unexpected exception caught: e=" + e.ToString());
145                         }
146
147
148                         /* exception test: File.Copy("", b) */
149                         try {
150                                 File.Copy ("", "b");
151                                 Fail ("File.Copy('', 'b') should throw ArgumentException");
152                         } catch (ArgumentException) {
153                                 // do nothing, this is what we expect
154                         } catch (Exception e) {
155                                 Fail ("File.Copy('', 'b') unexpected exception caught: e=" + e.ToString());
156                         }
157
158                         /* exception test: File.Copy(a, "") */
159                         try {
160                                 File.Copy ("a", "");
161                                 Fail ("File.Copy('a', '') should throw ArgumentException");
162                         } catch (ArgumentException) {
163                                 // do nothing, this is what we expect
164                         } catch (Exception e) {
165                                 Fail ("File.Copy('a', '') unexpected exception caught: e=" + e.ToString());
166                         }
167
168
169                         /* exception test: File.Copy(" ", b) */
170                         try {
171                                 File.Copy (" ", "b");
172                                 Fail ("File.Copy(' ', 'b') should throw ArgumentException");
173                         } catch (ArgumentException) {
174                                 // do nothing, this is what we expect
175                         } catch (Exception e) {
176                                 Fail ("File.Copy(' ', 'b') unexpected exception caught: e=" + e.ToString());
177                         }
178
179                         /* exception test: File.Copy(a, " ") */
180                         try {
181                                 File.Copy ("a", " ");
182                                 Fail ("File.Copy('a', ' ') should throw ArgumentException");
183                         } catch (ArgumentException) {
184                                 // do nothing, this is what we expect
185                         } catch (Exception e) {
186                                 Fail ("File.Copy('a', ' ') unexpected exception caught: e=" + e.ToString());
187                         }
188
189
190                         /* exception test: File.Copy(doesnotexist, b) */
191                         try {
192                                 File.Copy ("doesnotexist", "b");
193                                 Fail ("File.Copy('doesnotexist', 'b') should throw FileNotFoundException");
194                         } catch (FileNotFoundException) {
195                                 // do nothing, this is what we expect
196                         } catch (Exception e) {
197                                 Fail ("File.Copy('doesnotexist', 'b') unexpected exception caught: e=" + e.ToString());
198                         }
199
200                         /* positive test: copy resources/AFile.txt to resources/bar */
201                         try {
202                                 File.Delete ("resources" + Path.DirectorySeparatorChar + "bar");
203                                 File.Copy ("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "bar");
204                                 Assert ("File AFile.txt should still exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
205                                 Assert ("File bar should exist after File.Copy", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
206                         } catch (Exception e) {
207                                 Fail ("#1 File.Copy('resources/AFile.txt', 'resources/bar') unexpected exception caught: e=" + e.ToString());
208                         }
209
210                         /* exception test: File.Copy(resources/AFile.txt, resources/bar) (default is overwrite == false) */
211                         try {
212                                 File.Copy ("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "bar");
213                                 Fail ("File.Copy('resources/AFile.txt', 'resources/bar') should throw IOException");
214                         } catch (IOException) {
215                                 // do nothing, this is what we expect
216                         } catch (Exception e) {
217                                 Fail ("#2 File.Copy('resources/AFile.txt', 'resources/bar') unexpected exception caught: e=" + e.ToString());
218                         }
219
220
221                         /* positive test: copy resources/AFile.txt to resources/bar, overwrite */
222                         try {
223                                 Assert ("File bar should exist before File.Copy", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
224                                 File.Copy ("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "bar", true);
225                                 Assert ("File AFile.txt should still exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
226                                 Assert ("File bar should exist after File.Copy", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
227                         } catch (Exception e) {
228                                 Fail ("File.Copy('resources/AFile.txt', 'resources/bar', true) unexpected exception caught: e=" + e.ToString());
229                         }
230
231
232                 }
233                 
234                 public void TestDelete ()
235                 {
236
237                         /* exception test: File.Delete(null) */
238                         try {
239                                 File.Delete (null);
240                                 Fail ("File.Delete(null) should throw ArgumentNullException");
241                         } catch (ArgumentNullException) {
242                                 // do nothing, this is what we expect
243                         } catch (Exception e) {
244                                 Fail ("File.Delete(null) unexpected exception caught: e=" + e.ToString());
245                         }
246
247                         /* exception test: File.Delete("") */
248                         try {
249                                 File.Delete ("");
250                                 Fail ("File.Delete('') should throw ArgumentException");
251                         } catch (ArgumentException) {
252                                 // do nothing, this is what we expect
253                         } catch (Exception e) {
254                                 Fail ("File.Delete('') unexpected exception caught: e=" + e.ToString());
255                         }
256
257                         /* exception test: File.Delete(" ") */
258                         try {
259                                 File.Delete (" ");
260                                 Fail ("File.Delete(' ') should throw ArgumentException");
261                         } catch (ArgumentException) {
262                                 // do nothing, this is what we expect
263                         } catch (Exception e) {
264                                 Fail ("File.Delete(' ') unexpected exception caught: e=" + e.ToString());
265                         }
266
267                         /* exception test: File.Delete(directory_not_found) */
268                         try {
269                                 File.Delete ("directory_does_not_exist" + Path.DirectorySeparatorChar + "foo");
270                                 Fail ("File.Delete(directory_does_not_exist) should throw DirectoryNotFoundException");
271                         } catch (DirectoryNotFoundException) {
272                                 // do nothing, this is what we expect
273                         } catch (Exception e) {
274                                 Fail ("File.Delete(directory_does_not_exist) unexpected exception caught: e=" + e.ToString());
275                         }
276
277                         if (!File.Exists ("resources" + Path.DirectorySeparatorChar + "foo")) {
278                                 FileStream f = File.Create("resources" + Path.DirectorySeparatorChar + "foo");
279                                 f.Close();
280                         }
281
282                         Assert ("File resources" + Path.DirectorySeparatorChar + "foo should exist for TestDelete to succeed", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
283                         try {
284                                 File.Delete ("resources" + Path.DirectorySeparatorChar + "foo");
285                         } catch (Exception e) {
286                                 Fail ("Unable to delete resources" + Path.DirectorySeparatorChar + "foo: e=" + e.ToString());
287                         }
288                         Assert ("File resources" + Path.DirectorySeparatorChar + "foo should not exist after File.Delete", !File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
289                 }
290
291
292                 public void TestMove ()
293                 {
294                         /* exception test: File.Move(null, b) */
295                         try {
296                                 File.Move (null, "b");
297                                 Fail ("File.Move(null, 'b') should throw ArgumentNullException");
298                         } catch (ArgumentNullException) {
299                                 // do nothing, this is what we expect
300                         } catch (Exception e) {
301                                 Fail ("File.Move(null, 'b') unexpected exception caught: e=" + e.ToString());
302                         }
303
304                         /* exception test: File.Move(a, null) */
305                         try {
306                                 File.Move ("a", null);
307                                 Fail ("File.Move('a', null) should throw ArgumentNullException");
308                         } catch (ArgumentNullException) {
309                                 // do nothing, this is what we expect
310                         } catch (Exception e) {
311                                 Fail ("File.Move('a', null) unexpected exception caught: e=" + e.ToString());
312                         }
313
314
315                         /* exception test: File.Move("", b) */
316                         try {
317                                 File.Move ("", "b");
318                                 Fail ("File.Move('', 'b') should throw ArgumentException");
319                         } catch (ArgumentException) {
320                                 // do nothing, this is what we expect
321                         } catch (Exception e) {
322                                 Fail ("File.Move('', 'b') unexpected exception caught: e=" + e.ToString());
323                         }
324
325                         /* exception test: File.Move(a, "") */
326                         try {
327                                 File.Move ("a", "");
328                                 Fail ("File.Move('a', '') should throw ArgumentException");
329                         } catch (ArgumentException) {
330                                 // do nothing, this is what we expect
331                         } catch (Exception e) {
332                                 Fail ("File.Move('a', '') unexpected exception caught: e=" + e.ToString());
333                         }
334
335
336                         /* exception test: File.Move(" ", b) */
337                         try {
338                                 File.Move (" ", "b");
339                                 Fail ("File.Move(' ', 'b') should throw ArgumentException");
340                         } catch (ArgumentException) {
341                                 // do nothing, this is what we expect
342                         } catch (Exception e) {
343                                 Fail ("File.Move(' ', 'b') unexpected exception caught: e=" + e.ToString());
344                         }
345
346                         /* exception test: File.Move(a, " ") */
347                         try {
348                                 File.Move ("a", " ");
349                                 Fail ("File.Move('a', ' ') should throw ArgumentException");
350                         } catch (ArgumentException) {
351                                 // do nothing, this is what we expect
352                         } catch (Exception e) {
353                                 Fail ("File.Move('a', ' ') unexpected exception caught: e=" + e.ToString());
354                         }
355
356
357                         /* exception test: File.Move(doesnotexist, b) */
358                         try {
359                                 File.Move ("doesnotexist", "b");
360                                 Fail ("File.Move('doesnotexist', 'b') should throw FileNotFoundException");
361                         } catch (FileNotFoundException) {
362                                 // do nothing, this is what we expect
363                         } catch (Exception e) {
364                                 Fail ("File.Move('doesnotexist', 'b') unexpected exception caught: e=" + e.ToString());
365                         }
366
367                         /* exception test: File.Move(resources/foo, doesnotexist/b) */
368                         File.Copy("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "foo", true);
369                         try {
370                                 File.Move ("resources" + Path.DirectorySeparatorChar + "foo", "doesnotexist" + Path.DirectorySeparatorChar + "b");
371                                 Fail ("File.Move('resources/foo', 'b') should throw DirectoryNotFoundException");
372                         } catch (DirectoryNotFoundException) {
373                                 // do nothing, this is what we expect
374                         } catch (FileNotFoundException) {
375                                 // LAMESPEC
376                                 // do nothing, this is (kind of) what we expect
377                         } catch (Exception e) {
378                                 Fail ("File.Move('resources/foo', 'doesnotexist/b') unexpected exception caught: e=" + e.ToString());
379                         }
380
381                         /* exception test: File.Move(doesnotexist/foo, b) */
382                         try {
383                                 File.Move ("doesnotexist" + Path.DirectorySeparatorChar + "foo", "b");
384                                 Fail ("File.Move('doesnotexist/foo', 'b') should throw DirectoryNotFoundException");
385                         } catch (DirectoryNotFoundException) {
386                                 // do nothing, this is what we expect
387                         } catch (FileNotFoundException) {
388                                 // LAMESPEC
389                                 // do nothing, this is (kind of) what we expect
390                         } catch (Exception e) {
391                                 Fail ("File.Move('doesnotexist/foo', 'b') unexpected exception caught: e=" + e.ToString());
392                         }
393
394                         /* exception test: File.Move(resources/foo, resources) */
395                         try {
396                                 File.Move ("resources" + Path.DirectorySeparatorChar + "foo", "resources");
397                                 Fail ("File.Move('resources/foo', 'resources') should throw IOException");
398                         } catch (IOException) {
399                                 // do nothing, this is what we expect
400                         } catch (Exception e) {
401                                 Fail ("File.Move('resources/foo', 'resources') unexpected exception caught: e=" + e.ToString());
402                         }
403
404                         /* positive test: File.Move(a, a) shouldn't throw exception */
405                         try {
406                                 File.Move ("resources" + Path.DirectorySeparatorChar + "foo", "resources" + Path.DirectorySeparatorChar + "foo");
407                         } catch (Exception e) {
408                                 Fail ("File.Move('doesnotexist/foo', 'b') unexpected exception caught: e=" + e.ToString());
409                         }
410
411                         if (!File.Exists ("resources" + Path.DirectorySeparatorChar + "bar")) {
412                                 FileStream f = File.Create("resources" + Path.DirectorySeparatorChar + "bar");
413                                 f.Close();
414                         }
415                         
416                         Assert ("File resources" + Path.DirectorySeparatorChar + "bar should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
417                         File.Move ("resources" + Path.DirectorySeparatorChar + "bar", "resources" + Path.DirectorySeparatorChar + "baz");
418                         Assert ("File resources" + Path.DirectorySeparatorChar + "bar should not exist", !File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
419                         Assert ("File resources" + Path.DirectorySeparatorChar + "baz should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "baz"));
420                 }
421
422                 public void TestOpen ()
423                 {
424                         try {
425                                 FileStream stream = File.Open ("resources" + Path.DirectorySeparatorChar + "AFile.txt", FileMode.Open);
426                                 stream.Close ();
427                         } catch (Exception e) {
428                                 Fail ("Unable to open resources" + Path.DirectorySeparatorChar + "AFile.txt: e=" + e.ToString());
429                         }
430
431                         /* Exception tests */
432                         try {
433                                 FileStream stream = File.Open ("filedoesnotexist", FileMode.Open);
434                                 Fail ("File 'filedoesnotexist' should not exist");
435                         } catch (FileNotFoundException) {
436                                 // do nothing, this is what we expect
437                         } catch (Exception e) {
438                                 Fail ("Unexpect exception caught: e=" + e.ToString());
439                         }
440                 }
441         }
442 }