FileTest.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // FileTest.cs: Test cases for System.IO.File
  3. //
  4. // Author: Duncan Mak ([email protected])
  5. //
  6. // (C) 2002 Ximian, Inc. http://www.ximian.com
  7. //
  8. using NUnit.Framework;
  9. using System;
  10. using System.IO;
  11. namespace MonoTests.System.IO
  12. {
  13. public class FileTest : TestCase
  14. {
  15. public FileTest ()
  16. : base ("System.IO.File testsuite")
  17. {
  18. }
  19. public FileTest (string name)
  20. : base (name)
  21. {
  22. }
  23. protected override void SetUp ()
  24. {
  25. }
  26. protected override void TearDown ()
  27. {
  28. File.Delete ("resources" + Path.DirectorySeparatorChar + "baz");
  29. }
  30. public static ITest Suite
  31. {
  32. get { return new TestSuite (typeof (FileTest)); }
  33. }
  34. public void TestExists ()
  35. {
  36. Assert ("File resources" + Path.DirectorySeparatorChar + "AFile.txt should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
  37. Assert ("File resources" + Path.DirectorySeparatorChar + "doesnotexist should not exist", !File.Exists ("resources" + Path.DirectorySeparatorChar + "doesnotexist"));
  38. }
  39. public void TestCreate ()
  40. {
  41. FileStream stream = File.Create ("resources" + Path.DirectorySeparatorChar + "foo");
  42. Assert ("File should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
  43. stream.Close ();
  44. }
  45. public void TestCopy ()
  46. {
  47. File.Copy ("resources" + Path.DirectorySeparatorChar + "AFile.txt", "resources" + Path.DirectorySeparatorChar + "bar", false);
  48. Assert ("File AFile.txt should still exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "AFile.txt"));
  49. Assert ("File bar should exist after File.Copy", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
  50. }
  51. public void TestDelete ()
  52. {
  53. Assert ("File resources" + Path.DirectorySeparatorChar + "foo should exist for TestDelete to succeed", File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
  54. try {
  55. File.Delete ("resources" + Path.DirectorySeparatorChar + "foo");
  56. } catch (Exception e) {
  57. Fail ("Unable to delete resources" + Path.DirectorySeparatorChar + "foo: e=" + e.ToString());
  58. }
  59. Assert ("File resources" + Path.DirectorySeparatorChar + "foo should not exist after File.Delete", !File.Exists ("resources" + Path.DirectorySeparatorChar + "foo"));
  60. }
  61. public void TestMove ()
  62. {
  63. Assert ("File resources" + Path.DirectorySeparatorChar + "bar should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
  64. File.Move ("resources" + Path.DirectorySeparatorChar + "bar", "resources" + Path.DirectorySeparatorChar + "baz");
  65. Assert ("File resources" + Path.DirectorySeparatorChar + "bar should not exist", !File.Exists ("resources" + Path.DirectorySeparatorChar + "bar"));
  66. Assert ("File resources" + Path.DirectorySeparatorChar + "baz should exist", File.Exists ("resources" + Path.DirectorySeparatorChar + "baz"));
  67. }
  68. public void TestOpen ()
  69. {
  70. try {
  71. FileStream stream = File.Open ("resources" + Path.DirectorySeparatorChar + "AFile.txt", FileMode.Open);
  72. } catch (Exception e) {
  73. Fail ("Unable to open resources" + Path.DirectorySeparatorChar + "AFile.txt: e=" + e.ToString());
  74. }
  75. /* Exception tests */
  76. try {
  77. FileStream stream = File.Open ("filedoesnotexist", FileMode.Open);
  78. Fail ("File 'filedoesnotexist' should not exist");
  79. } catch (FileNotFoundException) {
  80. // do nothing, this is what we expect
  81. } catch (Exception e) {
  82. Fail ("Unexpect exception caught: e=" + e.ToString());
  83. }
  84. }
  85. }
  86. }