X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FTest%2FSystem.IO%2FDirectoryTest.cs;h=f83dc905e97eaed99278378bc5021d1ea9f61c4b;hb=00a9a5e7cac368999afc50deab44d6b1c0aba5cf;hp=30e3edc9edfa2130bb551832a9706822761c4af6;hpb=89d0ba3968d36576553e0f483b0c69465f94e8ae;p=mono.git diff --git a/mcs/class/corlib/Test/System.IO/DirectoryTest.cs b/mcs/class/corlib/Test/System.IO/DirectoryTest.cs index 30e3edc9edf..f83dc905e97 100644 --- a/mcs/class/corlib/Test/System.IO/DirectoryTest.cs +++ b/mcs/class/corlib/Test/System.IO/DirectoryTest.cs @@ -9,19 +9,21 @@ // TODO: Find out why ArgumentOutOfRange tests does not release directories properly // -using NUnit.Framework; -using System.IO; -using System.Text; using System; +using System.Diagnostics; using System.Globalization; +using System.IO; +using System.Text; using System.Threading; -using System.Diagnostics; -namespace MonoTests.System.IO { +using NUnit.Framework; + +namespace MonoTests.System.IO +{ [TestFixture] -public class DirectoryTest : Assertion { - +public class DirectoryTest +{ string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests"); static readonly char DSC = Path.DirectorySeparatorChar; @@ -35,7 +37,8 @@ public class DirectoryTest : Assertion { } [TearDown] - public void TearDown () { + public void TearDown () + { if (Directory.Exists (TempFolder)) Directory.Delete (TempFolder, true); } @@ -47,55 +50,92 @@ public class DirectoryTest : Assertion { DeleteDirectory (path); try { DirectoryInfo info = Directory.CreateDirectory (path); - AssertEquals ("test#01", true, info.Exists); - AssertEquals ("test#02", ".1", info.Extension); - AssertEquals ("test#03", true, info.FullName.EndsWith ("DirectoryTest.Test.1")); - AssertEquals ("test#04", "DirectoryTest.Test.1", info.Name); + Assert.IsTrue (info.Exists, "#1"); + Assert.AreEqual (".1", info.Extension, "#2"); + Assert.IsTrue (info.FullName.EndsWith ("DirectoryTest.Test.1"), "#3"); + Assert.AreEqual ("DirectoryTest.Test.1", info.Name, "#4"); } finally { - DeleteDirectory (path); + DeleteDirectory (path); } } [Test] - [ExpectedException(typeof(ArgumentException))] public void CreateDirectoryNotSupportedException () { DeleteDirectory (":"); - DirectoryInfo info = Directory.CreateDirectory (":"); + try { + DirectoryInfo info = Directory.CreateDirectory (":"); + Assert.Fail ("#1"); + } catch (ArgumentException ex) { + // The path is not of a legal form + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.IsNull (ex.ParamName, "#5"); + } DeleteDirectory (":"); } [Test] - [ExpectedException(typeof(ArgumentNullException))] - public void CreateDirectoryArgumentNullException () + public void CreateDirectory_Path_Null () { - DirectoryInfo info = Directory.CreateDirectory (null as string); + try { + Directory.CreateDirectory (null as string); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.AreEqual ("path", ex.ParamName, "#5"); + } } [Test] - [ExpectedException(typeof(ArgumentException))] - public void CreateDirectoryArgumentException1 () + public void CreateDirectory_Path_Empty () { - DirectoryInfo info = Directory.CreateDirectory (""); + try { + Directory.CreateDirectory (string.Empty); + Assert.Fail ("#1"); + } catch (ArgumentException ex) { + // Path cannot be the empty string or all whitespace + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.IsNull (ex.ParamName, "#5"); + } } [Test] - [ExpectedException(typeof(ArgumentException))] - public void CreateDirectoryArgumentException2 () + public void CreateDirectory_Path_Whitespace () { - DirectoryInfo info = Directory.CreateDirectory (" "); + try { + Directory.CreateDirectory (" "); + Assert.Fail ("#1"); + } catch (ArgumentException ex) { + // The path is not of a legal form + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.IsNull (ex.ParamName, "#5"); + } } [Test] - [ExpectedException(typeof(ArgumentException))] - public void CreateDirectoryArgumentException3 () + public void CreateDirectory_Path_InvalidChars () { string path = TempFolder + DSC + "DirectoryTest.Test"; DeleteDirectory (path); try { - path += Path.InvalidPathChars [0]; + path += '\x00'; path += ".2"; - DirectoryInfo info = Directory.CreateDirectory (path); + DirectoryInfo info = Directory.CreateDirectory (path); + Assert.Fail ("#1"); + } catch (ArgumentException ex) { + // The path contains illegal characters + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.IsNull (ex.ParamName, "#5"); } finally { DeleteDirectory (path); } @@ -110,11 +150,11 @@ public class DirectoryTest : Assertion { DirectoryInfo info1 = Directory.CreateDirectory (path); DirectoryInfo info2 = Directory.CreateDirectory (path); - AssertEquals ("test#01", true, info2.Exists); - AssertEquals ("test#02", true, info2.FullName.EndsWith ("DirectoryTest.Test.Exists")); - AssertEquals ("test#03", "DirectoryTest.Test.Exists", info2.Name); + Assert.IsTrue (info2.Exists, "#1"); + Assert.IsTrue (info2.FullName.EndsWith ("DirectoryTest.Test.Exists"), "#2"); + Assert.AreEqual ("DirectoryTest.Test.Exists", info2.Name, "#3"); } finally { - DeleteDirectory (path); + DeleteDirectory (path); } } @@ -129,11 +169,20 @@ public class DirectoryTest : Assertion { fstream.Close(); DirectoryInfo dinfo = Directory.CreateDirectory (path); - AssertEquals ("test#01", false, dinfo.Exists); - AssertEquals ("test#02", true, dinfo.FullName.EndsWith ("DirectoryTest.Test.ExistsAsFile")); - AssertEquals ("test#03", "DirectoryTest.Test.ExistsAsFile", dinfo.Name); +#if NET_2_0 + Assert.Fail ("#1"); + } catch (IOException ex) { + Assert.AreEqual (typeof (IOException), ex.GetType (), "#2"); + // exception message contains the path + Assert.IsTrue (ex.Message.Contains (path), "#3"); + Assert.IsNull (ex.InnerException, "#4"); +#else + Assert.IsFalse (dinfo.Exists, "#2"); + Assert.IsTrue (dinfo.FullName.EndsWith ("DirectoryTest.Test.ExistsAsFile"), "#3"); + Assert.AreEqual ("DirectoryTest.Test.ExistsAsFile", dinfo.Name, "#4"); +#endif } finally { - DeleteDirectory (path); + DeleteDirectory (path); DeleteFile (path); } } @@ -145,27 +194,27 @@ public class DirectoryTest : Assertion { DeleteDirectory (path); try { Directory.CreateDirectory (path); - AssertEquals ("test#01", true, Directory.Exists (path)); + Assert.IsTrue (Directory.Exists (path), "#1"); Directory.CreateDirectory (path + DSC + "DirectoryTest.Test.Delete.1.2"); - AssertEquals ("test#02", true, Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2")); + Assert.IsTrue (Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"), "#2"); Directory.Delete (path + DSC + "DirectoryTest.Test.Delete.1.2"); - AssertEquals ("test#03", false, Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2")); - AssertEquals ("test#04", true, Directory.Exists (path)); + Assert.IsFalse (Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"), "#3"); + Assert.IsTrue (Directory.Exists (path), "#4"); Directory.Delete (path); - AssertEquals ("test#05", false, Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2")); - AssertEquals ("test#06", false, Directory.Exists (path)); + Assert.IsFalse (Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"), "#5"); + Assert.IsFalse (Directory.Exists (path), "#6"); Directory.CreateDirectory (path); Directory.CreateDirectory (path + DSC + "DirectoryTest.Test.Delete.1.2"); - AssertEquals ("test#07", true, Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2")); - AssertEquals ("test#08", true, Directory.Exists (path)); + Assert.IsTrue (Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"), "#7"); + Assert.IsTrue (Directory.Exists (path), "#8"); Directory.Delete (path, true); - AssertEquals ("test#09", false, Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2")); - AssertEquals ("test#10", false, Directory.Exists (path)); + Assert.IsFalse (Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"), "#9"); + Assert.IsFalse (Directory.Exists (path), "#10"); } finally { DeleteDirectory (path); } @@ -175,14 +224,14 @@ public class DirectoryTest : Assertion { [ExpectedException(typeof(ArgumentException))] public void DeleteArgumentException () { - Directory.Delete (""); + Directory.Delete (string.Empty); } [Test] [ExpectedException(typeof(ArgumentException))] public void DeleteArgumentException2 () { - Directory.Delete (" "); + Directory.Delete (" "); } [Test] @@ -200,7 +249,7 @@ public class DirectoryTest : Assertion { [ExpectedException(typeof(ArgumentNullException))] public void DeleteArgumentNullException () { - Directory.Delete (null as string); + Directory.Delete (null as string); } [Test] @@ -210,7 +259,7 @@ public class DirectoryTest : Assertion { string path = TempFolder + DSC + "DirectoryTest.Test.5"; DeleteDirectory (path); - Directory.Delete (path); + Directory.Delete (path); } [Test] @@ -227,56 +276,82 @@ public class DirectoryTest : Assertion { } finally { if (s != null) s.Close (); - DeleteDirectory (path); + DeleteDirectory (path); }; } [Test] - public void Exists () + public void DeleteDirectoryOnExistingFileName () { - AssertEquals ("test#01", false, Directory.Exists (null as string)); + string path = TempFolder + DSC + "DirectoryTest.Test.ExistsAsFile"; + DeleteDirectory (path); + DeleteFile (path); + try { + FileStream fstream = File.Create (path); + fstream.Close (); + + Directory.Delete (path); + Assert.Fail ("#1"); + } + catch (IOException ex) { + Assert.AreEqual (typeof (IOException), ex.GetType (), "#2"); + // exception message DOES NOT contains the path + Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "#3"); + Assert.IsNull (ex.InnerException, "#4"); + } + finally { + DeleteDirectory (path); + DeleteFile (path); + } } [Test] - [Category("NotDotNet")] - public void ExistsAccessDenied () + public void Exists () { - // bug #78239 + Assert.IsFalse (Directory.Exists (null as string)); + } - if (Path.DirectorySeparatorChar == '\\') +#if !TARGET_JVM // We don't support yet the Process class. + [Test] // bug #78239 + public void ExistsAccessDenied () + { + if (!RunningOnUnix) return; // this test does not work on Windows. string path = TempFolder + DSC + "ExistsAccessDenied"; - Process p; Directory.CreateDirectory (path); Process.Start ("/bin/chmod", "000 " + path).WaitForExit (); try { - AssertEquals ("#1", false, Directory.Exists(path + DSC + "b")); + Assert.IsFalse (Directory.Exists(path + DSC + "b")); } finally { Process.Start ("/bin/chmod", "755 " + path).WaitForExit (); Directory.Delete (path); } } +#endif [Test] - [ExpectedException(typeof(ArgumentNullException))] + [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM + [ExpectedException(typeof(ArgumentNullException))] public void GetCreationTimeException1 () { Directory.GetCreationTime (null as string); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM public void GetCreationTimeException2 () { - Directory.GetCreationTime (""); + Directory.GetCreationTime (string.Empty); } [Test] #if !NET_2_0 [ExpectedException(typeof(IOException))] #endif + [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM public void GetCreationTimeException_NonExistingPath () { string path = TempFolder + DSC + "DirectoryTest.GetCreationTime.1"; @@ -286,12 +361,12 @@ public class DirectoryTest : Assertion { #if NET_2_0 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime (); - Assertion.AssertEquals ("#1", expectedTime.Year, time.Year); - Assertion.AssertEquals ("#2", expectedTime.Month, time.Month); - Assertion.AssertEquals ("#3", expectedTime.Day, time.Day); - Assertion.AssertEquals ("#4", expectedTime.Hour, time.Hour); - Assertion.AssertEquals ("#5", expectedTime.Second, time.Second); - Assertion.AssertEquals ("#6", expectedTime.Millisecond, time.Millisecond); + Assert.AreEqual (expectedTime.Year, time.Year, "#1"); + Assert.AreEqual (expectedTime.Month, time.Month, "#2"); + Assert.AreEqual (expectedTime.Day, time.Day, "#3"); + Assert.AreEqual (expectedTime.Hour, time.Hour, "#4"); + Assert.AreEqual (expectedTime.Second, time.Second, "#5"); + Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6"); #endif } finally { DeleteDirectory (path); @@ -299,37 +374,42 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM public void GetCreationTimeException4 () { Directory.GetCreationTime (" "); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM public void GetCreationTimeException5 () { Directory.GetCreationTime (Path.InvalidPathChars [0].ToString ()); } [Test] - [ExpectedException(typeof(ArgumentNullException))] + [ExpectedException(typeof(ArgumentNullException))] + [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM public void GetCreationTimeUtcException1 () { Directory.GetCreationTimeUtc (null as string); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM public void GetCreationTimeUtcException2 () { - Directory.GetCreationTimeUtc (""); + Directory.GetCreationTimeUtc (string.Empty); } [Test] #if !NET_2_0 [ExpectedException (typeof (IOException))] #endif + [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM public void GetCreationTimeUtc_NonExistingPath () { string path = TempFolder + DSC + "DirectoryTest.GetCreationTimeUtc.1"; @@ -339,12 +419,12 @@ public class DirectoryTest : Assertion { DateTime time = Directory.GetCreationTimeUtc (path); #if NET_2_0 - Assertion.AssertEquals ("#1", 1601, time.Year); - Assertion.AssertEquals ("#2", 1, time.Month); - Assertion.AssertEquals ("#3", 1, time.Day); - Assertion.AssertEquals ("#4", 0, time.Hour); - Assertion.AssertEquals ("#5", 0, time.Second); - Assertion.AssertEquals ("#6", 0, time.Millisecond); + Assert.AreEqual (1601, time.Year, "#1"); + Assert.AreEqual (1, time.Month, "#2"); + Assert.AreEqual (1, time.Day, "#3"); + Assert.AreEqual (0, time.Hour, "#4"); + Assert.AreEqual (0, time.Second, "#5"); + Assert.AreEqual (0, time.Millisecond, "#6"); #endif } finally { DeleteDirectory (path); @@ -352,37 +432,42 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM public void GetCreationTimeUtcException4 () { Directory.GetCreationTimeUtc (" "); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM public void GetCreationTimeUtcException5 () { Directory.GetCreationTime (Path.InvalidPathChars [0].ToString ()); } [Test] - [ExpectedException(typeof(ArgumentNullException))] - public void GetLastAccessTimeException1 () + [ExpectedException(typeof(ArgumentNullException))] + [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM + public void GetLastAccessTime_Null () { Directory.GetLastAccessTime (null as string); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM public void GetLastAccessTimeException2 () { - Directory.GetLastAccessTime (""); + Directory.GetLastAccessTime (string.Empty); } [Test] #if !NET_2_0 [ExpectedException (typeof (IOException))] #endif + [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM public void GetLastAccessTime_NonExistingPath () { string path = TempFolder + DSC + "DirectoryTest.GetLastAccessTime.1"; @@ -393,12 +478,12 @@ public class DirectoryTest : Assertion { #if NET_2_0 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime (); - Assertion.AssertEquals ("#1", expectedTime.Year, time.Year); - Assertion.AssertEquals ("#2", expectedTime.Month, time.Month); - Assertion.AssertEquals ("#3", expectedTime.Day, time.Day); - Assertion.AssertEquals ("#4", expectedTime.Hour, time.Hour); - Assertion.AssertEquals ("#5", expectedTime.Second, time.Second); - Assertion.AssertEquals ("#6", expectedTime.Millisecond, time.Millisecond); + Assert.AreEqual (expectedTime.Year, time.Year, "#1"); + Assert.AreEqual (expectedTime.Month, time.Month, "#2"); + Assert.AreEqual (expectedTime.Day, time.Day, "#3"); + Assert.AreEqual (expectedTime.Hour, time.Hour, "#4"); + Assert.AreEqual (expectedTime.Second, time.Second, "#5"); + Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6"); #endif } finally { DeleteDirectory (path); @@ -406,37 +491,42 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM public void GetLastAccessTimeException4 () { Directory.GetLastAccessTime (" "); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM public void GetLastAccessTimeException5 () { Directory.GetLastAccessTime (Path.InvalidPathChars [0].ToString ()); } [Test] - [ExpectedException(typeof(ArgumentNullException))] - public void GetLastAccessTimeUtcException1 () + [ExpectedException(typeof(ArgumentNullException))] + [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM + public void GetLastAccessTimeUtc_Null () { Directory.GetLastAccessTimeUtc (null as string); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM public void GetLastAccessTimeUtcException2 () { - Directory.GetLastAccessTimeUtc (""); + Directory.GetLastAccessTimeUtc (string.Empty); } [Test] #if !NET_2_0 [ExpectedException (typeof (IOException))] #endif + [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM public void GetLastAccessTimeUtc_NonExistingPath () { string path = TempFolder + DSC + "DirectoryTest.GetLastAccessTimeUtc.1"; @@ -445,12 +535,12 @@ public class DirectoryTest : Assertion { DateTime time = Directory.GetLastAccessTimeUtc (path); #if NET_2_0 - Assertion.AssertEquals ("#1", 1601, time.Year); - Assertion.AssertEquals ("#2", 1, time.Month); - Assertion.AssertEquals ("#3", 1, time.Day); - Assertion.AssertEquals ("#4", 0, time.Hour); - Assertion.AssertEquals ("#5", 0, time.Second); - Assertion.AssertEquals ("#6", 0, time.Millisecond); + Assert.AreEqual (1601, time.Year, "#1"); + Assert.AreEqual (1, time.Month, "#2"); + Assert.AreEqual (1, time.Day, "#3"); + Assert.AreEqual (0, time.Hour, "#4"); + Assert.AreEqual (0, time.Second, "#5"); + Assert.AreEqual (0, time.Millisecond, "#6"); #endif } finally { DeleteDirectory (path); @@ -458,31 +548,33 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM public void GetLastAccessTimeUtcException4 () { Directory.GetLastAccessTimeUtc (" "); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM public void GetLastAccessTimeUtcException5 () { Directory.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ()); } [Test] - [ExpectedException(typeof(ArgumentNullException))] + [ExpectedException(typeof(ArgumentNullException))] public void GetLastWriteTimeException1 () { Directory.GetLastWriteTime (null as string); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] public void GetLastWriteTimeException2 () { - Directory.GetLastWriteTime (""); + Directory.GetLastWriteTime (string.Empty); } [Test] @@ -498,12 +590,12 @@ public class DirectoryTest : Assertion { #if NET_2_0 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime (); - Assertion.AssertEquals ("#1", expectedTime.Year, time.Year); - Assertion.AssertEquals ("#2", expectedTime.Month, time.Month); - Assertion.AssertEquals ("#3", expectedTime.Day, time.Day); - Assertion.AssertEquals ("#4", expectedTime.Hour, time.Hour); - Assertion.AssertEquals ("#5", expectedTime.Second, time.Second); - Assertion.AssertEquals ("#6", expectedTime.Millisecond, time.Millisecond); + Assert.AreEqual (expectedTime.Year, time.Year, "#1"); + Assert.AreEqual (expectedTime.Month, time.Month, "#2"); + Assert.AreEqual (expectedTime.Day, time.Day, "#3"); + Assert.AreEqual (expectedTime.Hour, time.Hour, "#4"); + Assert.AreEqual (expectedTime.Second, time.Second, "#5"); + Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6"); #endif } finally { DeleteDirectory (path); @@ -511,31 +603,31 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] public void GetLastWriteTimeException4 () { Directory.GetLastWriteTime (" "); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] public void GetLastWriteTimeException5 () { Directory.GetLastWriteTime (Path.InvalidPathChars [0].ToString ()); } [Test] - [ExpectedException(typeof(ArgumentNullException))] + [ExpectedException(typeof(ArgumentNullException))] public void GetLastWriteTimeUtcException1 () { Directory.GetLastWriteTimeUtc (null as string); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] public void GetLastWriteTimeUtcException2 () { - Directory.GetLastWriteTimeUtc (""); + Directory.GetLastWriteTimeUtc (string.Empty); } [Test] @@ -550,12 +642,12 @@ public class DirectoryTest : Assertion { DateTime time = Directory.GetLastWriteTimeUtc (path); #if NET_2_0 - Assertion.AssertEquals ("#1", 1601, time.Year); - Assertion.AssertEquals ("#2", 1, time.Month); - Assertion.AssertEquals ("#3", 1, time.Day); - Assertion.AssertEquals ("#4", 0, time.Hour); - Assertion.AssertEquals ("#5", 0, time.Second); - Assertion.AssertEquals ("#6", 0, time.Millisecond); + Assert.AreEqual (1601, time.Year, "#1"); + Assert.AreEqual (1, time.Month, "#2"); + Assert.AreEqual (1, time.Day, "#3"); + Assert.AreEqual (0, time.Hour, "#4"); + Assert.AreEqual (0, time.Second, "#5"); + Assert.AreEqual (0, time.Millisecond, "#6"); #endif } finally { DeleteDirectory (path); @@ -564,21 +656,103 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] public void GetLastWriteTimeUtcException4 () { Directory.GetLastWriteTimeUtc (" "); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] public void GetLastWriteTimeUtcException5 () { Directory.GetLastWriteTimeUtc (Path.InvalidPathChars[0].ToString ()); } [Test] - public void Move () + public void Move_DestDirName_Empty () + { + try { + Directory.Move (TempFolder, string.Empty); + Assert.Fail ("#A1"); + } catch (ArgumentException ex) { + // Empty file name is not legal + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2"); + Assert.IsNull (ex.InnerException, "#A3"); + Assert.IsNotNull (ex.Message, "#A4"); + Assert.IsNotNull (ex.ParamName, "#A5"); + Assert.AreEqual ("destDirName", ex.ParamName, "#A6"); + } + + try { + Directory.Move (TempFolder, " "); + Assert.Fail ("#B1"); + } catch (ArgumentException ex) { + // The path is not of a legal form + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2"); + Assert.IsNull (ex.InnerException, "#B3"); + Assert.IsNotNull (ex.Message, "#B4"); + } + } + + [Test] + public void Move_DestDirName_Null () + { + try { + Directory.Move (TempFolder, (string) null); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.IsNotNull (ex.ParamName, "#5"); + Assert.AreEqual ("destDirName", ex.ParamName, "#6"); + } + } + + [Test] + public void Move_SourceDirName_Empty () + { + try { + Directory.Move (string.Empty, TempFolder); + Assert.Fail ("#A1"); + } catch (ArgumentException ex) { + // Empty file name is not legal + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2"); + Assert.IsNull (ex.InnerException, "#A3"); + Assert.IsNotNull (ex.Message, "#A4"); + Assert.IsNotNull (ex.ParamName, "#A5"); + Assert.AreEqual ("sourceDirName", ex.ParamName, "#A6"); + } + + try { + Directory.Move (" ", TempFolder); + Assert.Fail ("#B1"); + } catch (ArgumentException ex) { + // The path is not of a legal form + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2"); + Assert.IsNull (ex.InnerException, "#B3"); + Assert.IsNotNull (ex.Message, "#B4"); + } + } + + [Test] + public void Move_SourceDirName_Null () + { + try { + Directory.Move ((string) null, TempFolder); + Assert.Fail ("#1"); + } catch (ArgumentNullException ex) { + Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.IsNotNull (ex.ParamName, "#5"); + Assert.AreEqual ("sourceDirName", ex.ParamName, "#6"); + } + } + + [Test] + public void MoveDirectory () { string path = TempFolder + DSC + "DirectoryTest.Test.9"; string path2 = TempFolder + DSC + "DirectoryTest.Test.10"; @@ -587,56 +761,123 @@ public class DirectoryTest : Assertion { try { Directory.CreateDirectory (path); Directory.CreateDirectory (path + DSC + "dir"); - AssertEquals ("test#01", true, Directory.Exists (path + DSC + "dir")); + Assert.IsTrue (Directory.Exists (path + DSC + "dir"), "#1"); Directory.Move (path, path2); - AssertEquals ("test#02", false, Directory.Exists (path + DSC + "dir")); - AssertEquals ("test#03", true, Directory.Exists (path2 + DSC + "dir")); - + Assert.IsFalse (Directory.Exists (path + DSC + "dir"), "#2"); + Assert.IsTrue (Directory.Exists (path2 + DSC + "dir"), "#3"); } finally { DeleteDirectory (path); DeleteDirectory (path2); if (Directory.Exists (path2 + DSC + "dir")) - Directory.Delete (path2 + DSC + "dir", true); + Directory.Delete (path2 + DSC + "dir", true); } } - + [Test] - [ExpectedException(typeof(IOException))] - public void MoveException1 () + [ExpectedException (typeof (IOException))] + public void MoveDirectory_Same () { string path = TempFolder + DSC + "DirectoryTest.Test.8"; - DeleteDirectory (path); + DeleteDirectory (path); try { - Directory.Move (path, path); + Directory.Move (path, path); } finally { DeleteDirectory (path); } } [Test] - [ExpectedException(typeof(ArgumentException))] - public void MoveException2 () + public void MoveFile () { - string path = TempFolder + DSC + "DirectoryTest.Test.11"; - DeleteDirectory (path); + string tempFile1 = Path.Combine (TempFolder, "temp1.txt"); + string tempFile2 = Path.Combine (TempFolder, "temp2.txt"); + + using (StreamWriter sw = File.CreateText (tempFile1)) { + sw.Write ("temp1"); + } + Assert.IsFalse (File.Exists (tempFile2), "#1"); + Directory.Move (tempFile1, tempFile2); + Assert.IsFalse (File.Exists (tempFile1), "#2"); + Assert.IsTrue (File.Exists (tempFile2), "#3"); + using (StreamReader sr = File.OpenText (tempFile2)) { + Assert.AreEqual ("temp1", sr.ReadToEnd (), "#4"); + } + } + + [Test] + public void MoveFile_DestDir_Exists () + { + string tempFile = Path.Combine (TempFolder, "temp1.txt"); + string tempDir = Path.Combine (TempFolder, "temp2"); + + using (StreamWriter sw = File.CreateText (tempFile)) { + sw.Write ("temp1"); + } + Directory.CreateDirectory (tempDir); + try { - Directory.Move ("", path); - } finally { - DeleteDirectory (path); + Directory.Move (tempFile, tempDir); + Assert.Fail ("#A1"); + } catch (IOException ex) { + // Cannot create a file when that file already exists + Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2"); + Assert.IsNull (ex.InnerException, "#A3"); + Assert.IsNotNull (ex.Message, "#A4"); } + + Assert.IsTrue (File.Exists (tempFile), "#B1"); + Assert.IsFalse (File.Exists (tempDir), "#B2"); + Assert.IsTrue (Directory.Exists (tempDir), "#B3"); } [Test] - [ExpectedException(typeof(ArgumentException))] - public void MoveException3 () + public void MoveFile_DestFile_Exists () { - string path = TempFolder + DSC + "DirectoryTest.Test.12"; - DeleteDirectory (path); + string tempFile1 = Path.Combine (TempFolder, "temp1.txt"); + string tempFile2 = Path.Combine (TempFolder, "temp2.txt"); + + using (StreamWriter sw = File.CreateText (tempFile1)) { + sw.Write ("temp1"); + } + using (StreamWriter sw = File.CreateText (tempFile2)) { + sw.Write ("temp2"); + } + try { - Directory.Move (" ", path); - } finally { - DeleteDirectory (path); + Directory.Move (tempFile1, tempFile2); + Assert.Fail ("#A1"); + } catch (IOException ex) { + // Cannot create a file when that file already exists + Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2"); + Assert.IsNull (ex.InnerException, "#A3"); + Assert.IsNotNull (ex.Message, "#A4"); + } + + Assert.IsTrue (File.Exists (tempFile1), "#B1"); + using (StreamReader sr = File.OpenText (tempFile1)) { + Assert.AreEqual ("temp1", sr.ReadToEnd (), "#B2"); + } + + Assert.IsTrue (File.Exists (tempFile2), "#C1"); + using (StreamReader sr = File.OpenText (tempFile2)) { + Assert.AreEqual ("temp2", sr.ReadToEnd (), "#C2"); + } + } + + [Test] + public void MoveFile_Same () + { + string tempFile = Path.Combine (TempFolder, "temp.txt"); + + try { + Directory.Move (tempFile, tempFile); + Assert.Fail ("#1"); + } catch (IOException ex) { + // Source and destination path must be different + Assert.AreEqual (typeof (IOException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); } } @@ -647,11 +888,11 @@ public class DirectoryTest : Assertion { { string path = TempFolder + DSC + "DirectoryTest.Test.13"; path += Path.InvalidPathChars [0]; - string path2 = TempFolder + DSC + "DirectoryTest.Test.13"; + string path2 = TempFolder + DSC + "DirectoryTest.Test.13"; DeleteDirectory (path); DeleteDirectory (path2); try { - Directory.CreateDirectory (path2); + Directory.CreateDirectory (path2); Directory.Move (path2, path); } finally { DeleteDirectory (path); @@ -666,7 +907,7 @@ public class DirectoryTest : Assertion { string path = TempFolder + DSC + "DirectoryTest.Test.14"; DeleteDirectory (path); try { - Directory.Move (path, path + "Test.Test"); + Directory.Move (path, path + "Test.Test"); } finally { DeleteDirectory (path); DeleteDirectory (path + "Test.Test"); @@ -675,42 +916,45 @@ public class DirectoryTest : Assertion { [Test] [ExpectedException(typeof(IOException))] - public void MoveException6 () + public void MoveDirectory_Dest_SubDir () { string path = TempFolder + DSC + "DirectoryTest.Test.15"; DeleteDirectory (path); try { - Directory.CreateDirectory (path); + Directory.CreateDirectory (path); Directory.Move (path, path + DSC + "dir"); } finally { DeleteDirectory (path); - DeleteDirectory (path + DSC + "dir"); + DeleteDirectory (path + DSC + "dir"); } } [Test] - [ExpectedException(typeof(IOException))] - public void MoveException7 () + [ExpectedException (typeof (IOException))] + public void MoveDirectory_Dest_Exists () { string path = TempFolder + DSC + "DirectoryTest.Test.16"; string path2 = TempFolder + DSC + "DirectoryTest.Test.17"; DeleteDirectory (path); - DeleteDirectory (path2); + DeleteDirectory (path2); try { Directory.CreateDirectory (path); Directory.CreateDirectory (path2); Directory.Move (path, path2); } finally { DeleteDirectory (path); - DeleteDirectory (path2); + DeleteDirectory (path2); } } [Test] - [Ignore("Unix doesnt support CreationTime")] + [Category("TargetJvmNotSupported")] // CreationTime not supported for TARGET_JVM public void CreationTime () { + if (RunningOnUnix) + Assert.Ignore ("Unix doesn't support CreationTime"); + string path = TempFolder + DSC + "DirectoryTest.CreationTime.1"; DeleteDirectory (path); @@ -718,44 +962,45 @@ public class DirectoryTest : Assertion { Directory.CreateDirectory (path); Directory.SetCreationTime (path, new DateTime (2003, 6, 4, 6, 4, 0)); - DateTime time = Directory.GetCreationTime (path); - AssertEquals ("test#01", 2003, time.Year); - AssertEquals ("test#02", 6, time.Month); - AssertEquals ("test#03", 4, time.Day); - AssertEquals ("test#04", 6, time.Hour); - AssertEquals ("test#05", 4, time.Minute); - AssertEquals ("test#06", 0, time.Second); + DateTime time = Directory.GetCreationTime (path); + Assert.AreEqual (2003, time.Year, "#A1"); + Assert.AreEqual (6, time.Month, "#A2"); + Assert.AreEqual (4, time.Day, "#A3"); + Assert.AreEqual (6, time.Hour, "#A4"); + Assert.AreEqual (4, time.Minute, "#A5"); + Assert.AreEqual (0, time.Second, "#A6"); time = TimeZone.CurrentTimeZone.ToLocalTime (Directory.GetCreationTimeUtc (path)); - AssertEquals ("test#07", 2003, time.Year); - AssertEquals ("test#08", 6, time.Month); - AssertEquals ("test#09", 4, time.Day); - AssertEquals ("test#10", 6, time.Hour); - AssertEquals ("test#11", 4, time.Minute); - AssertEquals ("test#12", 0, time.Second); + Assert.AreEqual (2003, time.Year, "#B1"); + Assert.AreEqual (6, time.Month, "#B2"); + Assert.AreEqual (4, time.Day, "#B3"); + Assert.AreEqual (6, time.Hour, "#B4"); + Assert.AreEqual (4, time.Minute, "#B5"); + Assert.AreEqual (0, time.Second, "#B6"); Directory.SetCreationTimeUtc (path, new DateTime (2003, 6, 4, 6, 4, 0)); time = TimeZone.CurrentTimeZone.ToUniversalTime (Directory.GetCreationTime (path)); - AssertEquals ("test#13", 2003, time.Year); - AssertEquals ("test#14", 6, time.Month); - AssertEquals ("test#15", 4, time.Day); - AssertEquals ("test#16", 6, time.Hour); - AssertEquals ("test#17", 4, time.Minute); - AssertEquals ("test#18", 0, time.Second); - - time = Directory.GetCreationTimeUtc (path); - AssertEquals ("test#19", 2003, time.Year); - AssertEquals ("test#20", 6, time.Month); - AssertEquals ("test#21", 4, time.Day); - AssertEquals ("test#22", 6, time.Hour); - AssertEquals ("test#23", 4, time.Minute); - AssertEquals ("test#24", 0, time.Second); + Assert.AreEqual (2003, time.Year, "#C1"); + Assert.AreEqual (6, time.Month, "#C2"); + Assert.AreEqual (4, time.Day, "#C3"); + Assert.AreEqual (6, time.Hour, "#C4"); + Assert.AreEqual (4, time.Minute, "#C5"); + Assert.AreEqual (0, time.Second, "#C6"); + + time = Directory.GetCreationTimeUtc (path); + Assert.AreEqual (2003, time.Year, "#D1"); + Assert.AreEqual (6, time.Month, "#D2"); + Assert.AreEqual (4, time.Day, "#D3"); + Assert.AreEqual (6, time.Hour, "#D4"); + Assert.AreEqual (4, time.Minute, "#D5"); + Assert.AreEqual (0, time.Second, "#D6"); } finally { - DeleteDirectory (path); + DeleteDirectory (path); } } [Test] + [Category("TargetJvmNotSupported")] // LastAccessTime not supported for TARGET_JVM public void LastAccessTime () { string path = TempFolder + DSC + "DirectoryTest.AccessTime.1"; @@ -765,38 +1010,38 @@ public class DirectoryTest : Assertion { Directory.CreateDirectory (path); Directory.SetLastAccessTime (path, new DateTime (2003, 6, 4, 6, 4, 0)); - DateTime time = Directory.GetLastAccessTime (path); - AssertEquals ("test#01", 2003, time.Year); - AssertEquals ("test#02", 6, time.Month); - AssertEquals ("test#03", 4, time.Day); - AssertEquals ("test#04", 6, time.Hour); - AssertEquals ("test#05", 4, time.Minute); - AssertEquals ("test#06", 0, time.Second); + DateTime time = Directory.GetLastAccessTime (path); + Assert.AreEqual (2003, time.Year, "#A1"); + Assert.AreEqual (6, time.Month, "#A2"); + Assert.AreEqual (4, time.Day, "#A3"); + Assert.AreEqual (6, time.Hour, "#A4"); + Assert.AreEqual (4, time.Minute, "#A5"); + Assert.AreEqual (0, time.Second, "#A6"); time = TimeZone.CurrentTimeZone.ToLocalTime (Directory.GetLastAccessTimeUtc (path)); - AssertEquals ("test#07", 2003, time.Year); - AssertEquals ("test#08", 6, time.Month); - AssertEquals ("test#09", 4, time.Day); - AssertEquals ("test#10", 6, time.Hour); - AssertEquals ("test#11", 4, time.Minute); - AssertEquals ("test#12", 0, time.Second); + Assert.AreEqual (2003, time.Year, "#B1"); + Assert.AreEqual (6, time.Month, "#B2"); + Assert.AreEqual (4, time.Day, "#B3"); + Assert.AreEqual (6, time.Hour, "#B4"); + Assert.AreEqual (4, time.Minute, "#B5"); + Assert.AreEqual (0, time.Second, "#B6"); Directory.SetLastAccessTimeUtc (path, new DateTime (2003, 6, 4, 6, 4, 0)); time = TimeZone.CurrentTimeZone.ToUniversalTime (Directory.GetLastAccessTime (path)); - AssertEquals ("test#13", 2003, time.Year); - AssertEquals ("test#14", 6, time.Month); - AssertEquals ("test#15", 4, time.Day); - AssertEquals ("test#16", 6, time.Hour); - AssertEquals ("test#17", 4, time.Minute); - AssertEquals ("test#18", 0, time.Second); - - time = Directory.GetLastAccessTimeUtc (path); - AssertEquals ("test#19", 2003, time.Year); - AssertEquals ("test#20", 6, time.Month); - AssertEquals ("test#21", 4, time.Day); - AssertEquals ("test#22", 6, time.Hour); - AssertEquals ("test#23", 4, time.Minute); - AssertEquals ("test#24", 0, time.Second); + Assert.AreEqual (2003, time.Year, "#C1"); + Assert.AreEqual (6, time.Month, "#C2"); + Assert.AreEqual (4, time.Day, "#C3"); + Assert.AreEqual (6, time.Hour, "#C4"); + Assert.AreEqual (4, time.Minute, "#C5"); + Assert.AreEqual (0, time.Second, "#C6"); + + time = Directory.GetLastAccessTimeUtc (path); + Assert.AreEqual (2003, time.Year, "#D1"); + Assert.AreEqual (6, time.Month, "#D2"); + Assert.AreEqual (4, time.Day, "#D3"); + Assert.AreEqual (6, time.Hour, "#D4"); + Assert.AreEqual (4, time.Minute, "#D5"); + Assert.AreEqual (0, time.Second, "#D6"); } finally { DeleteDirectory (path); } @@ -806,51 +1051,51 @@ public class DirectoryTest : Assertion { public void LastWriteTime () { string path = TempFolder + DSC + "DirectoryTest.WriteTime.1"; - DeleteDirectory (path); + DeleteDirectory (path); try { Directory.CreateDirectory (path); Directory.SetLastWriteTime (path, new DateTime (2003, 6, 4, 6, 4, 0)); - DateTime time = Directory.GetLastWriteTime (path); - AssertEquals ("test#01", 2003, time.Year); - AssertEquals ("test#02", 6, time.Month); - AssertEquals ("test#03", 4, time.Day); - AssertEquals ("test#04", 6, time.Hour); - AssertEquals ("test#05", 4, time.Minute); - AssertEquals ("test#06", 0, time.Second); + DateTime time = Directory.GetLastWriteTime (path); + Assert.AreEqual (2003, time.Year, "#A1"); + Assert.AreEqual (6, time.Month, "#A2"); + Assert.AreEqual (4, time.Day, "#A3"); + Assert.AreEqual (6, time.Hour, "#A4"); + Assert.AreEqual (4, time.Minute, "#A5"); + Assert.AreEqual (0, time.Second, "#A6"); time = TimeZone.CurrentTimeZone.ToLocalTime (Directory.GetLastWriteTimeUtc (path)); - AssertEquals ("test#07", 2003, time.Year); - AssertEquals ("test#08", 6, time.Month); - AssertEquals ("test#09", 4, time.Day); - AssertEquals ("test#10", 6, time.Hour); - AssertEquals ("test#11", 4, time.Minute); - AssertEquals ("test#12", 0, time.Second); + Assert.AreEqual (2003, time.Year, "#B1"); + Assert.AreEqual (6, time.Month, "#B2"); + Assert.AreEqual (4, time.Day, "#B3"); + Assert.AreEqual (6, time.Hour, "#B4"); + Assert.AreEqual (4, time.Minute, "#B5"); + Assert.AreEqual (0, time.Second, "#B6"); Directory.SetLastWriteTimeUtc (path, new DateTime (2003, 6, 4, 6, 4, 0)); time = TimeZone.CurrentTimeZone.ToUniversalTime (Directory.GetLastWriteTime (path)); - AssertEquals ("test#13", 2003, time.Year); - AssertEquals ("test#14", 6, time.Month); - AssertEquals ("test#15", 4, time.Day); - AssertEquals ("test#16", 6, time.Hour); - AssertEquals ("test#17", 4, time.Minute); - AssertEquals ("test#18", 0, time.Second); - - time = Directory.GetLastWriteTimeUtc (path); - AssertEquals ("test#19", 2003, time.Year); - AssertEquals ("test#20", 6, time.Month); - AssertEquals ("test#21", 4, time.Day); - AssertEquals ("test#22", 6, time.Hour); - AssertEquals ("test#23", 4, time.Minute); - AssertEquals ("test#24", 0, time.Second); + Assert.AreEqual (2003, time.Year, "#C1"); + Assert.AreEqual (6, time.Month, "#C2"); + Assert.AreEqual (4, time.Day, "#C3"); + Assert.AreEqual (6, time.Hour, "#C4"); + Assert.AreEqual (4, time.Minute, "#C5"); + Assert.AreEqual (0, time.Second, "#C6"); + + time = Directory.GetLastWriteTimeUtc (path); + Assert.AreEqual (2003, time.Year, "#D1"); + Assert.AreEqual (6, time.Month, "#D2"); + Assert.AreEqual (4, time.Day, "#D3"); + Assert.AreEqual (6, time.Hour, "#D4"); + Assert.AreEqual (4, time.Minute, "#D5"); + Assert.AreEqual (0, time.Second, "#D6"); } finally { - DeleteDirectory (path); + DeleteDirectory (path); } } [Test] - [ExpectedException(typeof(ArgumentNullException))] + [ExpectedException(typeof(ArgumentNullException))] public void SetLastWriteTimeException1 () { DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); @@ -861,15 +1106,15 @@ public class DirectoryTest : Assertion { [ExpectedException(typeof(ArgumentException))] public void SetLastWriteTimeException2 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); - Directory.SetLastWriteTime ("", time); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + Directory.SetLastWriteTime (string.Empty, time); } [Test] [ExpectedException(typeof(FileNotFoundException))] public void SetLastWriteTimeException3 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); string path = TempFolder + DSC + "DirectoryTest.SetLastWriteTime.2"; DeleteDirectory (path); try { @@ -880,30 +1125,30 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] public void SetLastWriteTimeException4 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetLastWriteTime (" ", time); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] public void SetLastWriteTimeException5 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetLastWriteTime (Path.InvalidPathChars [0].ToString (), time); } // [Test] -// [ExpectedException(typeof(ArgumentOutOfRangeException))] +// [ExpectedException(typeof(ArgumentOutOfRangeException))] // public void SetLastWriteTimeException6 () // { // DateTime time = new DateTime (1003, 4, 6, 6, 4, 2); // string path = TempFolder + Path.DirectorySeparatorChar + "DirectoryTest.SetLastWriteTime.1"; // // try { -// if (!Directory.Exists (path)) +// if (!Directory.Exists (path)) // Directory.CreateDirectory (path); // // Directory.SetLastWriteTime (path, time); @@ -914,26 +1159,26 @@ public class DirectoryTest : Assertion { // } [Test] - [ExpectedException(typeof(ArgumentNullException))] + [ExpectedException(typeof(ArgumentNullException))] public void SetLastWriteTimeUtcException1 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetLastWriteTimeUtc (null as string, time); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] public void SetLastWriteTimeUtcException2 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); - Directory.SetLastWriteTimeUtc ("", time); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + Directory.SetLastWriteTimeUtc (string.Empty, time); } [Test] [ExpectedException(typeof(FileNotFoundException))] public void SetLastWriteTimeUtcException3 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); string path = TempFolder + DSC + "DirectoryTest.SetLastWriteTimeUtc.2"; DeleteDirectory (path); try { @@ -944,23 +1189,23 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] public void SetLastWriteTimeUtcException4 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetLastWriteTimeUtc (" ", time); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] public void SetLastWriteTimeUtcException5 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString (), time); } // [Test] -// [ExpectedException(typeof(ArgumentOutOfRangeException))] +// [ExpectedException(typeof(ArgumentOutOfRangeException))] // public void SetLastWriteTimeUtcException6 () // { // DateTime time = new DateTime (1000, 4, 6, 6, 4, 2); @@ -976,7 +1221,8 @@ public class DirectoryTest : Assertion { // } [Test] - [ExpectedException(typeof(ArgumentNullException))] + [ExpectedException(typeof(ArgumentNullException))] + [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM public void SetLastAccessTimeException1 () { DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); @@ -984,18 +1230,20 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM public void SetLastAccessTimeException2 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); - Directory.SetLastAccessTime ("", time); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + Directory.SetLastAccessTime (string.Empty, time); } [Test] [ExpectedException(typeof(FileNotFoundException))] + [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM public void SetLastAccessTimeException3 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTime.2"; DeleteDirectory (path); try { @@ -1006,29 +1254,31 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM public void SetLastAccessTimeException4 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetLastAccessTime (" ", time); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM public void SetLastAccessTimeException5 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetLastAccessTime (Path.InvalidPathChars [0].ToString (), time); } // [Test] -// [ExpectedException(typeof(ArgumentOutOfRangeException))] +// [ExpectedException(typeof(ArgumentOutOfRangeException))] // public void SetLastAccessTimeException6 () // { // DateTime time = new DateTime (1003, 4, 6, 6, 4, 2); // string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTime.1"; // -// if (!Directory.Exists (path)) +// if (!Directory.Exists (path)) // Directory.CreateDirectory (path); // try { // Directory.SetLastAccessTime (path, time); @@ -1039,26 +1289,29 @@ public class DirectoryTest : Assertion { // } [Test] - [ExpectedException(typeof(ArgumentNullException))] + [ExpectedException(typeof(ArgumentNullException))] + [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM public void SetLastAccessTimeUtcException1 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetLastAccessTimeUtc (null as string, time); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM public void SetLastAccessTimeUtcException2 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); - Directory.SetLastAccessTimeUtc ("", time); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + Directory.SetLastAccessTimeUtc (string.Empty, time); } [Test] [ExpectedException(typeof(FileNotFoundException))] + [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM public void SetLastAccessTimeUtcException3 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTimeUtc.2"; DeleteDirectory (path); try { @@ -1069,23 +1322,25 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM public void SetLastAccessTimeUtcException4 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetLastAccessTimeUtc (" ", time); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM public void SetLastAccessTimeUtcException5 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString (), time); } // [Test] -// [ExpectedException(typeof(ArgumentOutOfRangeException))] +// [ExpectedException(typeof(ArgumentOutOfRangeException))] // public void SetLastAccessTimeUtcException6 () // { // DateTime time = new DateTime (1000, 4, 6, 6, 4, 2); @@ -1102,6 +1357,7 @@ public class DirectoryTest : Assertion { [Test] [ExpectedException(typeof(ArgumentNullException))] + [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM public void SetCreationTimeException1 () { DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); @@ -1109,18 +1365,20 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM public void SetCreationTimeException2 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); - Directory.SetCreationTime ("", time); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + Directory.SetCreationTime (string.Empty, time); } [Test] [ExpectedException(typeof(FileNotFoundException))] + [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM public void SetCreationTimeException3 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); string path = TempFolder + DSC + "DirectoryTest.SetCreationTime.2"; DeleteDirectory (path); @@ -1132,33 +1390,35 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM public void SetCreationTimeException4 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetCreationTime (" ", time); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM public void SetCreationTimeException5 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetCreationTime (Path.InvalidPathChars [0].ToString (), time); } // [Test] -// [ExpectedException(typeof(ArgumentOutOfRangeException))] +// [ExpectedException(typeof(ArgumentOutOfRangeException))] // public void SetCreationTimeException6 () // { // DateTime time = new DateTime (1003, 4, 6, 6, 4, 2); // string path = TempFolder + DSC + "DirectoryTest.SetCreationTime.1"; // -// if (!Directory.Exists (path)) +// if (!Directory.Exists (path)) // Directory.CreateDirectory (path); // try { // Directory.SetCreationTime (path, time); -// DeleteDirectory (path); +// DeleteDirectory (path); // } finally { // DeleteDirectory (path); // } @@ -1166,26 +1426,29 @@ public class DirectoryTest : Assertion { // } [Test] - [ExpectedException(typeof(ArgumentNullException))] + [ExpectedException(typeof(ArgumentNullException))] + [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM public void SetCreationTimeUtcException1 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetCreationTimeUtc (null as string, time); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM public void SetCreationTimeUtcException2 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); - Directory.SetCreationTimeUtc ("", time); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + Directory.SetCreationTimeUtc (string.Empty, time); } [Test] [ExpectedException(typeof(FileNotFoundException))] + [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM public void SetCreationTimeUtcException3 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTimeUtc.2"; DeleteDirectory (path); @@ -1198,23 +1461,25 @@ public class DirectoryTest : Assertion { } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM public void SetCreationTimeUtcException4 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetCreationTimeUtc (" ", time); } [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof(ArgumentException))] + [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM public void SetCreationTimeUtcException5 () { - DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); + DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); Directory.SetCreationTimeUtc (Path.InvalidPathChars [0].ToString (), time); } // [Test] -// [ExpectedException(typeof(ArgumentOutOfRangeException))] +// [ExpectedException(typeof(ArgumentOutOfRangeException))] // public void SetCreationTimeUtcException6 () // { // DateTime time = new DateTime (1000, 4, 6, 6, 4, 2); @@ -1248,19 +1513,37 @@ public class DirectoryTest : Assertion { return; } - Assert ("Directory Not Found", false); + Assert.Fail ("Directory Not Found"); } finally { DeleteDirectory (DirPath); } } + [Test] // bug #346123 + public void GetDirectories_Backslash () + { + if (!RunningOnUnix) + // on Windows, backslash is used as directory separator + return; + + string dir = Path.Combine (TempFolder, @"sub\dir"); + Directory.CreateDirectory (dir); + + Assert.IsTrue (Directory.Exists (dir), "#A1"); + Assert.IsFalse (Directory.Exists (Path.Combine (TempFolder, "dir")), "#A2"); + + string [] dirs = Directory.GetDirectories (TempFolder); + Assert.AreEqual (1, dirs.Length, "#B1"); + Assert.AreEqual (dir, dirs [0], "#B2"); + } + [Test] public void GetParentOfRootDirectory () { DirectoryInfo info; info = Directory.GetParent (Path.GetPathRoot (Path.GetTempPath ())); - AssertEquals (null, info); + Assert.IsNull (info); } [Test] @@ -1280,12 +1563,113 @@ public class DirectoryTest : Assertion { return; } - Assert ("File Not Found", false); + Assert.Fail ("File Not Found"); } finally { if (File.Exists (DirPath)) File.Delete (DirPath); - - } + } + } + + [Test] // bug #346123 + public void GetFiles_Backslash () + { + if (!RunningOnUnix) + // on Windows, backslash is used as directory separator + return; + + string file = Path.Combine (TempFolder, @"doc\temp1.file"); + File.Create (file).Close (); + + Assert.IsTrue (File.Exists (file), "#A1"); + Assert.IsFalse (File.Exists (Path.Combine (TempFolder, "temp1.file")), "#A2"); + + string [] files = Directory.GetFiles (TempFolder); + Assert.AreEqual (1, files.Length, "#B1"); + Assert.AreEqual (file, files [0], "#B2"); + } + + [Test] // bug #82212 and bug #325107 + public void GetFiles_Pattern () + { + string [] files = Directory.GetFiles (TempFolder, "*.*"); + Assert.IsNotNull (files, "#A1"); + Assert.AreEqual (0, files.Length, "#A2"); + + string tempFile1 = Path.Combine (TempFolder, "tempFile1"); + File.Create (tempFile1).Close (); + + files = Directory.GetFiles (TempFolder, "*.*"); + Assert.IsNotNull (files, "#B1"); + Assert.AreEqual (1, files.Length, "#B2"); + Assert.AreEqual (tempFile1, files [0], "#B3"); + + string tempFile2 = Path.Combine (TempFolder, "FileTemp2.tmp"); + File.Create (tempFile2).Close (); + + files = Directory.GetFiles (TempFolder, "*.*"); + Assert.IsNotNull (files, "#C1"); + Assert.AreEqual (2, files.Length, "#C2"); + + files = Directory.GetFiles (TempFolder, "temp*.*"); + Assert.IsNotNull (files, "#D1"); + Assert.AreEqual (1, files.Length, "#D2"); + Assert.AreEqual (tempFile1, files [0], "#D3"); + + string tempFile3 = Path.Combine (TempFolder, "tempFile3.txt"); + File.Create (tempFile3).Close (); + + files = Directory.GetFiles (TempFolder, "*File*.*"); + Assert.IsNotNull (files, "#E1"); + Assert.AreEqual (3, files.Length, "#E2"); + + files = Directory.GetFiles (TempFolder, "*File*.tmp"); + Assert.IsNotNull (files, "#F1"); + Assert.AreEqual (1, files.Length, "#F2"); + Assert.AreEqual (tempFile2, files [0], "#F3"); + + files = Directory.GetFiles (TempFolder, "*tempFile*"); + Assert.IsNotNull (files, "#G1"); + Assert.AreEqual (2, files.Length, "#G2"); + + files = Directory.GetFiles (TempFolder, "*tempFile1"); + Assert.IsNotNull (files, "#H1"); + Assert.AreEqual (1, files.Length, "#H2"); + Assert.AreEqual (tempFile1, files [0], "#H3"); + + files = Directory.GetFiles (TempFolder, "*.txt"); + Assert.IsNotNull (files, "#I1"); + Assert.AreEqual (1, files.Length, "#I2"); + Assert.AreEqual (tempFile3, files [0], "#I3"); + + files = Directory.GetFiles (TempFolder, "*.t*"); + Assert.IsNotNull (files, "#J1"); + Assert.AreEqual (2, files.Length, "#J2"); + + files = Directory.GetFiles (TempFolder, "temp*.*"); + Assert.IsNotNull (files, "#K1"); + Assert.AreEqual (2, files.Length, "#K2"); + + File.Delete (tempFile1); + + files = Directory.GetFiles (TempFolder, "temp*.*"); + Assert.IsNotNull (files, "#L1"); + Assert.AreEqual (1, files.Length, "#L2"); + Assert.AreEqual (tempFile3, files [0], "#L3"); + + files = Directory.GetFiles (TempFolder, ".*"); + Assert.IsNotNull (files, "#M1"); + Assert.AreEqual (0, files.Length, "#M2"); + + string tempFile4 = Path.Combine (TempFolder, "tempFile4."); + File.Create (tempFile4).Close (); + + files = Directory.GetFiles (TempFolder, "temp*."); + Assert.IsNotNull (files, "#N1"); + Assert.AreEqual (1, files.Length, "#N2"); + if (RunningOnUnix) + Assert.AreEqual (tempFile4, files [0], "#N3"); + else // on Windows, the trailing dot is automatically trimmed + Assert.AreEqual (Path.Combine (TempFolder, "tempFile4"), files [0], "#N3"); } [Test] @@ -1317,17 +1701,25 @@ public class DirectoryTest : Assertion { dir.GetFiles ("*.nonext"); } - [Test] public void FilenameOnly () // bug 78209 { Directory.GetParent ("somefile"); } + private static bool RunningOnUnix { + get { + // check for Unix platforms - see FAQ for more details + // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F + int platform = (int) Environment.OSVersion.Platform; + return ((platform == 4) || (platform == 128) || (platform == 6)); + } + } + private void DeleteDirectory (string path) { if (Directory.Exists (path)) - Directory.Delete (path, true); + Directory.Delete (path, true); } private void DeleteFile (string path)