| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- //
- // FileTest.cs: Test cases for System.IO.File
- //
- // Author: Duncan Mak ([email protected])
- //
- // (C) 2002 Ximian, Inc. http://www.ximian.com
- //
- using NUnit.Framework;
- using System;
- using System.IO;
- namespace MonoTests.System.IO
- {
- public class FileTest : TestCase
- {
- protected override void SetUp ()
- {
- }
- protected override void TearDown ()
- {
- File.Delete ("resources" + Path.DirectorySeparatorChar + "baz");
- File.Delete ("resources" + Path.DirectorySeparatorChar + "bar");
- File.Delete ("resources" + Path.DirectorySeparatorChar + "foo");
- }
- public void TestExists ()
- {
- int i = 0;
- try {
- Assert ("null filename should not exist", !File.Exists (null));
- i++;
- Assert ("empty filename should not exist", !File.Exists (""));
- i++;
- Assert ("whitespace filename should not exist", !File.Exists (" \t\t \t \n\t\n \n"));
- i++;
- Assert ("File resources" + Path.DirectorySeparatorChar + "AFile.txt should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
- i++;
- Assert ("File resources" + Path.DirectorySeparatorChar + "doesnotexist should not exist", !File.Exists ("resources" + Path.DirectorySeparatorChar + "doesnotexist"));
- } catch (Exception e) {
- Fail ("Unexpected exception at i = " + i + ". e=" + e);
- }
- }
- public void TestCreate ()
- {
- FileStream stream;
- /* exception test: File.Create(null) */
- try {
- stream = File.Create (null);
- Fail ("File.Create(null) should throw ArgumentNullException");
- } catch (ArgumentNullException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Create(null) unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Create("") */
- try {
- stream = File.Create ("");
- Fail ("File.Create('') should throw ArgumentException");
- } catch (ArgumentException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Create('') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Create(" ") */
- try {
- stream = File.Create (" ");
- Fail ("File.Create(' ') should throw ArgumentException");
- } catch (ArgumentException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Create(' ') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Create(directory_not_found) */
- try {
- stream = File.Create ("directory_does_not_exist" + Path.DirectorySeparatorChar + "foo");
- Fail ("File.Create(directory_does_not_exist) should throw DirectoryNotFoundException");
- } catch (DirectoryNotFoundException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Create(directory_does_not_exist) unexpected exception caught: e=" + e.ToString());
- }
- /* positive test: create resources/foo */
- try {
- stream = File.Create ("resources" + Path.DirectorySeparatorChar + "foo");
- Assert ("File should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
- stream.Close ();
- } catch (Exception e) {
- Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
- }
- /* positive test: repeat test above again to test for overwriting file */
- try {
- stream = File.Create ("resources" + Path.DirectorySeparatorChar + "foo");
- Assert ("File should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
- stream.Close ();
- } catch (Exception e) {
- Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
- }
- }
- public void TestCopy ()
- {
- /* exception test: File.Copy(null, b) */
- try {
- File.Copy (null, "b");
- Fail ("File.Copy(null, 'b') should throw ArgumentNullException");
- } catch (ArgumentNullException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Copy(null, 'b') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Copy(a, null) */
- try {
- File.Copy ("a", null);
- Fail ("File.Copy('a', null) should throw ArgumentNullException");
- } catch (ArgumentNullException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Copy('a', null) unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Copy("", b) */
- try {
- File.Copy ("", "b");
- Fail ("File.Copy('', 'b') should throw ArgumentException");
- } catch (ArgumentException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Copy('', 'b') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Copy(a, "") */
- try {
- File.Copy ("a", "");
- Fail ("File.Copy('a', '') should throw ArgumentException");
- } catch (ArgumentException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Copy('a', '') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Copy(" ", b) */
- try {
- File.Copy (" ", "b");
- Fail ("File.Copy(' ', 'b') should throw ArgumentException");
- } catch (ArgumentException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Copy(' ', 'b') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Copy(a, " ") */
- try {
- File.Copy ("a", " ");
- Fail ("File.Copy('a', ' ') should throw ArgumentException");
- } catch (ArgumentException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Copy('a', ' ') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Copy(doesnotexist, b) */
- try {
- File.Copy ("doesnotexist", "b");
- Fail ("File.Copy('doesnotexist', 'b') should throw FileNotFoundException");
- } catch (FileNotFoundException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Copy('doesnotexist', 'b') unexpected exception caught: e=" + e.ToString());
- }
- /* positive test: copy resources/AFile.txt to resources/bar */
- try {
- File.Delete ("resources" + Path.DirectorySeparatorChar + "bar");
- File.Copy ("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "bar");
- Assert ("File AFile.txt should still exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
- Assert ("File bar should exist after File.Copy", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
- } catch (Exception e) {
- Fail ("#1 File.Copy('resources/AFile.txt', 'resources/bar') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Copy(resources/AFile.txt, resources/bar) (default is overwrite == false) */
- try {
- File.Copy ("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "bar");
- Fail ("File.Copy('resources/AFile.txt', 'resources/bar') should throw IOException");
- } catch (IOException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("#2 File.Copy('resources/AFile.txt', 'resources/bar') unexpected exception caught: e=" + e.ToString());
- }
- /* positive test: copy resources/AFile.txt to resources/bar, overwrite */
- try {
- Assert ("File bar should exist before File.Copy", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
- File.Copy ("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "bar", true);
- Assert ("File AFile.txt should still exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
- Assert ("File bar should exist after File.Copy", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
- } catch (Exception e) {
- Fail ("File.Copy('resources/AFile.txt', 'resources/bar', true) unexpected exception caught: e=" + e.ToString());
- }
- }
-
- public void TestDelete ()
- {
- /* exception test: File.Delete(null) */
- try {
- File.Delete (null);
- Fail ("File.Delete(null) should throw ArgumentNullException");
- } catch (ArgumentNullException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Delete(null) unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Delete("") */
- try {
- File.Delete ("");
- Fail ("File.Delete('') should throw ArgumentException");
- } catch (ArgumentException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Delete('') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Delete(" ") */
- try {
- File.Delete (" ");
- Fail ("File.Delete(' ') should throw ArgumentException");
- } catch (ArgumentException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Delete(' ') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Delete(directory_not_found) */
- try {
- File.Delete ("directory_does_not_exist" + Path.DirectorySeparatorChar + "foo");
- Fail ("File.Delete(directory_does_not_exist) should throw DirectoryNotFoundException");
- } catch (DirectoryNotFoundException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Delete(directory_does_not_exist) unexpected exception caught: e=" + e.ToString());
- }
- if (!File.Exists ("resources" + Path.DirectorySeparatorChar + "foo")) {
- FileStream f = File.Create("resources" + Path.DirectorySeparatorChar + "foo");
- f.Close();
- }
- Assert ("File resources" + Path.DirectorySeparatorChar + "foo should exist for TestDelete to succeed", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
- try {
- File.Delete ("resources" + Path.DirectorySeparatorChar + "foo");
- } catch (Exception e) {
- Fail ("Unable to delete resources" + Path.DirectorySeparatorChar + "foo: e=" + e.ToString());
- }
- Assert ("File resources" + Path.DirectorySeparatorChar + "foo should not exist after File.Delete", !File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
- }
- public void TestMove ()
- {
- /* exception test: File.Move(null, b) */
- try {
- File.Move (null, "b");
- Fail ("File.Move(null, 'b') should throw ArgumentNullException");
- } catch (ArgumentNullException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Move(null, 'b') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Move(a, null) */
- try {
- File.Move ("a", null);
- Fail ("File.Move('a', null) should throw ArgumentNullException");
- } catch (ArgumentNullException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Move('a', null) unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Move("", b) */
- try {
- File.Move ("", "b");
- Fail ("File.Move('', 'b') should throw ArgumentException");
- } catch (ArgumentException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Move('', 'b') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Move(a, "") */
- try {
- File.Move ("a", "");
- Fail ("File.Move('a', '') should throw ArgumentException");
- } catch (ArgumentException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Move('a', '') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Move(" ", b) */
- try {
- File.Move (" ", "b");
- Fail ("File.Move(' ', 'b') should throw ArgumentException");
- } catch (ArgumentException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Move(' ', 'b') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Move(a, " ") */
- try {
- File.Move ("a", " ");
- Fail ("File.Move('a', ' ') should throw ArgumentException");
- } catch (ArgumentException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Move('a', ' ') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Move(doesnotexist, b) */
- try {
- File.Move ("doesnotexist", "b");
- Fail ("File.Move('doesnotexist', 'b') should throw FileNotFoundException");
- } catch (FileNotFoundException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Move('doesnotexist', 'b') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Move(resources/foo, doesnotexist/b) */
- File.Copy("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "foo", true);
- try {
- File.Move ("resources" + Path.DirectorySeparatorChar + "foo", "doesnotexist" + Path.DirectorySeparatorChar + "b");
- Fail ("File.Move('resources/foo', 'b') should throw DirectoryNotFoundException");
- } catch (DirectoryNotFoundException) {
- // do nothing, this is what we expect
- } catch (FileNotFoundException) {
- // LAMESPEC
- // do nothing, this is (kind of) what we expect
- } catch (Exception e) {
- Fail ("File.Move('resources/foo', 'doesnotexist/b') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Move(doesnotexist/foo, b) */
- try {
- File.Move ("doesnotexist" + Path.DirectorySeparatorChar + "foo", "b");
- Fail ("File.Move('doesnotexist/foo', 'b') should throw DirectoryNotFoundException");
- } catch (DirectoryNotFoundException) {
- // do nothing, this is what we expect
- } catch (FileNotFoundException) {
- // LAMESPEC
- // do nothing, this is (kind of) what we expect
- } catch (Exception e) {
- Fail ("File.Move('doesnotexist/foo', 'b') unexpected exception caught: e=" + e.ToString());
- }
- /* exception test: File.Move(resources/foo, resources) */
- try {
- File.Move ("resources" + Path.DirectorySeparatorChar + "foo", "resources");
- Fail ("File.Move('resources/foo', 'resources') should throw IOException");
- } catch (IOException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("File.Move('resources/foo', 'resources') unexpected exception caught: e=" + e.ToString());
- }
- /* positive test: File.Move(a, a) shouldn't throw exception */
- try {
- File.Move ("resources" + Path.DirectorySeparatorChar + "foo", "resources" + Path.DirectorySeparatorChar + "foo");
- } catch (Exception e) {
- Fail ("File.Move('doesnotexist/foo', 'b') unexpected exception caught: e=" + e.ToString());
- }
- if (!File.Exists ("resources" + Path.DirectorySeparatorChar + "bar")) {
- FileStream f = File.Create("resources" + Path.DirectorySeparatorChar + "bar");
- f.Close();
- }
-
- Assert ("File resources" + Path.DirectorySeparatorChar + "bar should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
- File.Move ("resources" + Path.DirectorySeparatorChar + "bar", "resources" + Path.DirectorySeparatorChar + "baz");
- Assert ("File resources" + Path.DirectorySeparatorChar + "bar should not exist", !File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
- Assert ("File resources" + Path.DirectorySeparatorChar + "baz should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "baz"));
- }
- public void TestOpen ()
- {
- try {
- FileStream stream = File.Open ("resources" + Path.DirectorySeparatorChar + "AFile.txt", FileMode.Open);
- stream.Close ();
- } catch (Exception e) {
- Fail ("Unable to open resources" + Path.DirectorySeparatorChar + "AFile.txt: e=" + e.ToString());
- }
- /* Exception tests */
- try {
- FileStream stream = File.Open ("filedoesnotexist", FileMode.Open);
- Fail ("File 'filedoesnotexist' should not exist");
- } catch (FileNotFoundException) {
- // do nothing, this is what we expect
- } catch (Exception e) {
- Fail ("Unexpect exception caught: e=" + e.ToString());
- }
- }
- }
- }
|