2 // FileTest.cs: Test cases for System.IO.File
4 // Author: Duncan Mak (duncan@ximian.com)
6 // (C) 2002 Ximian, Inc. http://www.ximian.com
13 namespace MonoTests.System.IO
15 public class FileTest : TestCase
18 : base ("System.IO.File testsuite")
22 public FileTest (string name)
27 protected override void SetUp ()
31 protected override void TearDown ()
33 File.Delete ("resources" + Path.DirectorySeparatorChar + "baz");
34 File.Delete ("resources" + Path.DirectorySeparatorChar + "bar");
35 File.Delete ("resources" + Path.DirectorySeparatorChar + "foo");
38 public static ITest Suite
40 get { return new TestSuite (typeof (FileTest)); }
43 public void TestExists ()
47 Assert ("null filename should not exist", !File.Exists (null));
49 Assert ("empty filename should not exist", !File.Exists (""));
51 Assert ("whitespace filename should not exist", !File.Exists (" \t\t \t \n\t\n \n"));
53 Assert ("File resources" + Path.DirectorySeparatorChar + "AFile.txt should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
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);
61 public void TestCreate ()
65 /* exception test: File.Create(null) */
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());
75 /* exception test: File.Create("") */
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());
85 /* exception test: File.Create(" ") */
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());
95 /* exception test: File.Create(directory_not_found) */
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());
106 /* positive test: create resources/foo */
108 stream = File.Create ("resources" + Path.DirectorySeparatorChar + "foo");
109 Assert ("File should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
111 } catch (Exception e) {
112 Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
115 /* positive test: repeat test above again to test for overwriting file */
117 stream = File.Create ("resources" + Path.DirectorySeparatorChar + "foo");
118 Assert ("File should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
120 } catch (Exception e) {
121 Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
125 public void TestCopy ()
127 /* exception test: File.Copy(null, b) */
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());
137 /* exception test: File.Copy(a, null) */
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());
148 /* exception test: 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());
158 /* exception test: 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());
169 /* exception test: File.Copy(" ", b) */
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());
179 /* exception test: File.Copy(a, " ") */
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());
190 /* exception test: File.Copy(doesnotexist, b) */
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());
200 /* positive test: copy resources/AFile.txt to resources/bar */
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());
210 /* exception test: File.Copy(resources/AFile.txt, resources/bar) (default is overwrite == false) */
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());
221 /* positive test: copy resources/AFile.txt to resources/bar, overwrite */
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());
234 public void TestDelete ()
237 /* exception test: 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());
247 /* exception test: 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());
257 /* exception test: 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());
267 /* exception test: File.Delete(directory_not_found) */
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());
277 if (!File.Exists ("resources" + Path.DirectorySeparatorChar + "foo")) {
278 FileStream f = File.Create("resources" + Path.DirectorySeparatorChar + "foo");
282 Assert ("File resources" + Path.DirectorySeparatorChar + "foo should exist for TestDelete to succeed", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
284 File.Delete ("resources" + Path.DirectorySeparatorChar + "foo");
285 } catch (Exception e) {
286 Fail ("Unable to delete resources" + Path.DirectorySeparatorChar + "foo: e=" + e.ToString());
288 Assert ("File resources" + Path.DirectorySeparatorChar + "foo should not exist after File.Delete", !File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
292 public void TestMove ()
294 /* exception test: File.Move(null, b) */
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());
304 /* exception test: File.Move(a, null) */
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());
315 /* exception test: 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());
325 /* exception test: 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());
336 /* exception test: File.Move(" ", b) */
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());
346 /* exception test: File.Move(a, " ") */
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());
357 /* exception test: File.Move(doesnotexist, b) */
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());
367 /* exception test: File.Move(resources/foo, doesnotexist/b) */
368 File.Copy("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "foo", true);
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) {
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());
381 /* exception test: File.Move(doesnotexist/foo, b) */
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) {
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());
394 /* exception test: File.Move(resources/foo, resources) */
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());
404 /* positive test: File.Move(a, a) shouldn't throw exception */
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());
411 if (!File.Exists ("resources" + Path.DirectorySeparatorChar + "bar")) {
412 FileStream f = File.Create("resources" + Path.DirectorySeparatorChar + "bar");
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"));
422 public void TestOpen ()
425 FileStream stream = File.Open ("resources" + Path.DirectorySeparatorChar + "AFile.txt", FileMode.Open);
427 } catch (Exception e) {
428 Fail ("Unable to open resources" + Path.DirectorySeparatorChar + "AFile.txt: e=" + e.ToString());
431 /* Exception tests */
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());