| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
- // DirectoryInfoTest.cs - NUnit Test Cases for System.IO.DirectoryInfo class
- //
- // Ville Palo ([email protected])
- //
- // (C) 2003 Ville Palo
- //
- using NUnit.Framework;
- using System;
- using System.IO;
- namespace MonoTests.System.IO
- {
- [TestFixture]
- public class DirectoryInfoTest : Assertion
- {
- string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
- [SetUp]
- protected void SetUp() {
- if (Directory.Exists (TempFolder))
- Directory.Delete (TempFolder, true);
- Directory.CreateDirectory (TempFolder);
- }
-
- [TearDown]
- protected void TearDown() {
- if (Directory.Exists (TempFolder))
- Directory.Delete (TempFolder, true);
- }
-
- [Test]
- public void Ctr ()
- {
- string path = TempFolder + "/DIT.Ctr.Test";
- DeleteDir (path);
-
- FileInfo info = new FileInfo (path);
- AssertEquals ("test#01", true, info.DirectoryName.EndsWith (".Tests"));
- AssertEquals ("test#02", false, info.Exists);
- AssertEquals ("test#03", ".Test", info.Extension);
- AssertEquals ("test#05", "DIT.Ctr.Test", info.Name);
- }
- [Test]
- [ExpectedException(typeof(ArgumentNullException))]
- public void CtorArgumentNullException ()
- {
- DirectoryInfo info = new DirectoryInfo (null);
- }
- [Test]
- [ExpectedException(typeof(ArgumentException))]
- public void CtorArgumentException1 ()
- {
- DirectoryInfo info = new DirectoryInfo ("");
- }
- [Test]
- [ExpectedException(typeof(ArgumentException))]
- public void CtorArgumentException2 ()
- {
- DirectoryInfo info = new DirectoryInfo (" ");
- }
- [Test]
- [ExpectedException(typeof(ArgumentException))]
- public void CtorArgumentException3 ()
- {
- string path = "";
- foreach (char c in Path.InvalidPathChars) {
- path += c;
- }
- DirectoryInfo info = new DirectoryInfo (path);
- }
-
- [Test]
- public void Exists ()
- {
- string path = TempFolder + "/DIT.Exists.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = new DirectoryInfo (path);
- AssertEquals ("test#01", false, info.Exists);
-
- Directory.CreateDirectory (path);
- AssertEquals ("test#02", false, info.Exists);
- info = new DirectoryInfo (path);
- AssertEquals ("test#03", true, info.Exists);
- } finally {
- DeleteDir (path);
- }
- }
-
- [Test]
- public void Name ()
- {
- string path = TempFolder + "/DIT.Name.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = new DirectoryInfo (path);
- AssertEquals ("test#01", "DIT.Name.Test", info.Name);
-
- info = Directory.CreateDirectory (path);
- AssertEquals ("test#02", "DIT.Name.Test", info.Name);
-
-
- } finally {
- DeleteDir (path);
- }
- }
-
- [Test]
- public void Parent ()
- {
- string path = TempFolder + "/DIT.Parent.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = new DirectoryInfo (path);
- AssertEquals ("test#01", "MonoTests.System.IO.Tests", info.Parent.Name);
-
- info = Directory.CreateDirectory (path);
- AssertEquals ("test#02", "MonoTests.System.IO.Tests", info.Parent.Name);
-
- } finally {
- DeleteDir (path);
- }
- }
- [Test]
- public void Create ()
- {
- string path = TempFolder + "/DIT.Create.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = new DirectoryInfo (path);
- AssertEquals ("test#01", false, info.Exists);
- info.Create ();
- AssertEquals ("test#02", false, info.Exists);
- info = new DirectoryInfo (path);
- AssertEquals ("test#03", true, info.Exists);
- } finally {
- DeleteDir (path);
- }
- }
- [Test]
- public void CreateSubdirectory ()
- {
- string sub_path = Path.Combine ("test01", "test02");
- try {
- DirectoryInfo info = new DirectoryInfo (TempFolder);
- info.CreateSubdirectory (sub_path);
- Assert ("test#01", Directory.Exists (Path.Combine (TempFolder, sub_path)));
- } finally {
- DeleteDir (Path.Combine (TempFolder, sub_path));
- }
-
- }
- [Test]
- public void Delete1 ()
- {
- string path = TempFolder + "/DIT.Delete1.Test";
- DeleteDir (path);
-
- try {
- Directory.CreateDirectory (path);
- DirectoryInfo info = new DirectoryInfo (path);
- AssertEquals ("test#01", true, info.Exists);
-
- info.Delete ();
- AssertEquals ("test#02", true, info.Exists);
-
- info = new DirectoryInfo (path);
- AssertEquals ("test#03", false, info.Exists);
- } finally {
- DeleteDir (path);
- }
- }
- [Test]
- public void Delete2 ()
- {
- string path = TempFolder + "/DIT.Delete2.Test";
- DeleteDir (path);
-
- try {
- Directory.CreateDirectory (path);
- File.Create (path + "/test").Close ();
- DirectoryInfo info = new DirectoryInfo (path);
- AssertEquals ("test#01", true, info.Exists);
-
- info.Delete (true);
- AssertEquals ("test#02", true, info.Exists);
-
- info = new DirectoryInfo (path);
- AssertEquals ("test#03", false, info.Exists);
- } finally {
- DeleteDir (path);
- }
- }
-
- [Test]
- [ExpectedException (typeof (IOException))]
- public void DeleteIOException1 ()
- {
- string path = TempFolder + "/DIT.DeleteIOException1.Test";
- DeleteDir (path);
-
- try {
- Directory.CreateDirectory (path);
- File.Create (path + "/test").Close ();
- DirectoryInfo info = new DirectoryInfo (path);
- info.Delete ();
- } finally {
- DeleteDir (path);
- }
- }
- [Test]
- [ExpectedException (typeof (IOException))]
- public void DeleteIOException2 ()
- {
- string path = TempFolder + "/DIT.DeleteIOException2.Test";
- DeleteDir (path);
-
- try {
- Directory.CreateDirectory (path);
- File.Create (path + "/test").Close ();
- DirectoryInfo info = new DirectoryInfo (path);
- info.Delete (false);
- } finally {
- DeleteDir (path);
- }
- }
-
- [Test]
- public void GetDirectories1 ()
- {
- string path = TempFolder + "/DIT.GetDirectories1.Test";
-
- try {
- DirectoryInfo info = Directory.CreateDirectory (path);
- AssertEquals ("test#01", 0, info.GetDirectories ().Length);
-
- Directory.CreateDirectory (path + "/" + "1");
- Directory.CreateDirectory (path + "/" + "2");
- File.Create (path + "/" + "filetest").Close ();
- AssertEquals ("test#02", 2, info.GetDirectories ().Length);
-
- Directory.Delete (path + "/" + 2);
- AssertEquals ("test#02", 1, info.GetDirectories ().Length);
-
- } finally {
- DeleteDir (path);
- }
- }
-
- [Test]
- public void GetDirectories2 ()
- {
- string path = TempFolder + "/DIT.GetDirectories2.Test";
-
- try {
- DirectoryInfo info = Directory.CreateDirectory (path);
- AssertEquals ("test#01", 0, info.GetDirectories ("*").Length);
-
- Directory.CreateDirectory (path + "/" + "test120");
- Directory.CreateDirectory (path + "/" + "test210");
- Directory.CreateDirectory (path + "/" + "atest330");
- Directory.CreateDirectory (path + "/" + "test220");
- File.Create (path + "/" + "filetest").Close ();
-
- AssertEquals ("test#02", 4, info.GetDirectories ("*").Length);
- AssertEquals ("test#03", 3, info.GetDirectories ("test*").Length);
- AssertEquals ("test#04", 2, info.GetDirectories ("test?20").Length);
- AssertEquals ("test#05", 0, info.GetDirectories ("test?").Length);
- AssertEquals ("test#06", 0, info.GetDirectories ("test[12]*").Length);
- AssertEquals ("test#07", 2, info.GetDirectories ("test2*0").Length);
- AssertEquals ("test#08", 4, info.GetDirectories ("*test*").Length);
-
- } finally {
- DeleteDir (path);
- }
- }
-
- [Test]
- [ExpectedException (typeof (DirectoryNotFoundException))]
- public void GetDirectoriesDirectoryNotFoundException1 ()
- {
- string path = TempFolder + "/DIT.GetDirectoriesDirectoryNotFoundException1.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = new DirectoryInfo (path);
- info.GetDirectories ();
- } finally {
- DeleteDir (path);
- }
- }
- [Test]
- [ExpectedException (typeof (DirectoryNotFoundException))]
- public void GetDirectoriesDirectoryNotFoundException2 ()
- {
- string path = TempFolder + "/DIT.GetDirectoriesDirectoryNotFoundException2.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = new DirectoryInfo (path);
- info.GetDirectories ("*");
- } finally {
- DeleteDir (path);
- }
- }
-
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void GetDirectoriesArgumentNullException ()
- {
- string path = TempFolder + "/DIT.GetDirectoriesArgumentNullException.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = new DirectoryInfo (path);
- info.GetDirectories (null);
- } finally {
- DeleteDir (path);
- }
- }
- [Test]
- public void GetFiles1 ()
- {
- string path = TempFolder + "/DIT.GetFiles1.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = Directory.CreateDirectory (path);
- AssertEquals ("test#01", 0, info.GetFiles ().Length);
- File.Create (path + "/" + "file1").Close ();
- File.Create (path + "/" + "file2").Close ();
- Directory.CreateDirectory (path + "/" + "directory1");
- AssertEquals ("test#02", 2, info.GetFiles ().Length);
-
- } finally {
- DeleteDir (path);
- }
- }
-
- [Test]
- public void GetFiles2()
- {
- string path = TempFolder + "/DIT.GetFiles2.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = Directory.CreateDirectory (path);
- AssertEquals ("test#01", 0, info.GetFiles ("*").Length);
- File.Create (path + "/" + "file120file").Close ();
- File.Create (path + "/" + "file220file").Close ();
- File.Create (path + "/" + "afile330file").Close ();
- File.Create (path + "/" + "test.abc").Close ();
- File.Create (path + "/" + "test.abcd").Close ();
- File.Create (path + "/" + "test.abcdef").Close ();
- Directory.CreateDirectory (path + "/" + "dir");
-
- AssertEquals ("test#02", 6, info.GetFiles ("*").Length);
- AssertEquals ("test#03", 2, info.GetFiles ("file*file").Length);
- AssertEquals ("test#04", 3, info.GetFiles ("*file*").Length);
- AssertEquals ("test#05", 2, info.GetFiles ("file?20file").Length);
- AssertEquals ("test#07", 1, info.GetFiles ("*.abcd").Length);
- AssertEquals ("test#08", 2, info.GetFiles ("*.abcd*").Length);
- } finally {
- DeleteDir (path);
- }
- }
-
- [Test]
- [ExpectedException (typeof (DirectoryNotFoundException))]
- public void GetFilesDirectoryNotFoundException1 ()
- {
- string path = TempFolder + "/DIT.GetFilesDirectoryNotFoundException1.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = new DirectoryInfo (path);
- info.GetFiles ();
-
- } finally {
- DeleteDir (path);
- }
- }
- [Test]
- [ExpectedException (typeof (DirectoryNotFoundException))]
- public void GetFilesDirectoryNotFoundException2 ()
- {
- string path = TempFolder + "/DIT.GetFilesDirectoryNotFoundException2.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = new DirectoryInfo (path);
- info.GetFiles ("*");
-
- } finally {
- DeleteDir (path);
- }
- }
-
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void GetFilesArgumentNullException ()
- {
- string path = TempFolder + "/DIT.GetFilesArgumentNullException.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = new DirectoryInfo (path);
- info.GetFiles (null);
- } finally {
- DeleteDir (path);
- }
- }
-
- [Test]
- public void MoveTo ()
- {
- string path1 = TempFolder + "/DIT.MoveTo.Soucre.Test";
- string path2 = TempFolder + "/DIT.MoveTo.Dest.Test";
- DeleteDir (path1);
- DeleteDir (path2);
-
- try {
- DirectoryInfo info1 = Directory.CreateDirectory (path1);
- DirectoryInfo info2 = new DirectoryInfo (path2);
-
- AssertEquals ("test#01", true, info1.Exists);
- AssertEquals ("test#02", false, info2.Exists);
-
- info1.MoveTo (path2);
- AssertEquals ("test#03", true, info1.Exists);
- AssertEquals ("test#04", false, info2.Exists);
-
- info1 = new DirectoryInfo (path1);
- info2 = new DirectoryInfo (path2);
- AssertEquals ("test#05", false, info1.Exists);
- AssertEquals ("test#06", true, info2.Exists);
-
- } finally {
- DeleteDir (path1);
- DeleteDir (path2);
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void MoveToArgumentNullException ()
- {
- string path = TempFolder + "/DIT.MoveToArgumentNullException.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = Directory.CreateDirectory (path);
- info.MoveTo (null);
- } finally {
- DeleteDir (path);
- }
-
- }
- [Test]
- [ExpectedException (typeof (IOException))]
- public void MoveToIOException1 ()
- {
- string path = TempFolder + "/DIT.MoveToIOException1.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = Directory.CreateDirectory (path);
- info.MoveTo (path);
- } finally {
- DeleteDir (path);
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void MoveToArgumentException1 ()
- {
- string path = TempFolder + "/DIT.MoveToArgumentException1.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = Directory.CreateDirectory (path);
- info.MoveTo ("");
- } finally {
- DeleteDir (path);
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void MoveToArgumentException2 ()
- {
- string path = TempFolder + "/DIT.MoveToArgumentException2.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = Directory.CreateDirectory (path);
- info.MoveTo (" ");
- } finally {
- DeleteDir (path);
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void MoveToArgumentException3 ()
- {
- string path = TempFolder + "/DIT.MoveToArgumentException3.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = Directory.CreateDirectory (path);
- info.MoveTo (Path.InvalidPathChars [0].ToString ());
- } finally {
- DeleteDir (path);
- }
- }
- [Test]
- [ExpectedException (typeof (IOException))]
- public void MoveToIOException2 ()
- {
- string path = TempFolder + "/DIT.MoveToIOException2.Test";
- DeleteDir (path);
-
- try {
- DirectoryInfo info = new DirectoryInfo (path);
- info.MoveTo (path);
- } finally {
- DeleteDir (path);
- }
- }
- private void DeleteDir (string path)
- {
- if (Directory.Exists (path))
- Directory.Delete (path, true);
- }
-
- }
- }
|