* Directory.cs: Renamed Move arguments to match MS. Allow Move to be
authorGert Driesen <drieseng@users.sourceforge.net>
Sun, 8 Jul 2007 15:56:55 +0000 (15:56 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Sun, 8 Jul 2007 15:56:55 +0000 (15:56 -0000)
used to move files, patch by Robert Jordan. Fixes bug #81912. Spaces
to tabs.
* DirectoryCas.cs: Changed Move to MoveDirectory to match the
method in DirectoryTest. Fixed line endings.
* DirectoryTest.cs: Added test for bug #81912. Added tests for Move
argument checks.

svn path=/trunk/mcs/; revision=81599

mcs/class/corlib/System.IO/ChangeLog
mcs/class/corlib/System.IO/Directory.cs
mcs/class/corlib/Test/System.IO/ChangeLog
mcs/class/corlib/Test/System.IO/DirectoryCas.cs
mcs/class/corlib/Test/System.IO/DirectoryTest.cs

index 93aee54bbbc0731530b051cf91ad4ac77646243b..0493b21b05dc37f05509ad078209b6f4c93e5e92 100644 (file)
@@ -1,3 +1,9 @@
+2007-07-08  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * Directory.cs: Renamed Move arguments to match MS. Allow Move to be
+       used to move files, patch by Robert Jordan. Fixes bug #81912. Spaces
+       to tabs.
+
 2007-06-21  Dick Porter  <dick@ximian.com>
 
        * FileStream.cs: Fix FileShare test, fixing better bug 79250.
index 2c9d1b461b31f2fc68b7962637cea906cfd8d574..dfc6049f1bc8f2bfcfce0e246f80464f95a30630 100644 (file)
@@ -155,8 +155,7 @@ namespace System.IO
                                 */
                                success = MonoIO.DeleteFile (path, out error);
                        } else {
-                               success = MonoIO.RemoveDirectory (path,
-                                                                 out error);
+                               success = MonoIO.RemoveDirectory (path, out error);
                        }
                        
                        if (!success) {
@@ -230,12 +229,12 @@ namespace System.IO
                {
                        return File.GetLastAccessTime (path);
                }
-               
+
                public static DateTime GetLastAccessTimeUtc (string path)
                {
                        return GetLastAccessTime (path).ToUniversalTime ();
                }
-                     
+
                public static DateTime GetLastWriteTime (string path)
                {
                        return File.GetLastWriteTime (path);
@@ -349,15 +348,15 @@ namespace System.IO
                static bool IsRootDirectory (string path)
                {
                        // Unix
-                      if (Path.DirectorySeparatorChar == '/' && path == "/")
-                              return true;
+                       if (Path.DirectorySeparatorChar == '/' && path == "/")
+                               return true;
 
-                      // Windows
-                      if (Path.DirectorySeparatorChar == '\\')
-                              if (path.Length == 3 && path.EndsWith (":\\"))
-                                      return true;
+                       // Windows
+                       if (Path.DirectorySeparatorChar == '\\')
+                               if (path.Length == 3 && path.EndsWith (":\\"))
+                                       return true;
 
-                      return false;
+                       return false;
                }
 
                public static DirectoryInfo GetParent (string path)
@@ -380,31 +379,31 @@ namespace System.IO
                        return new DirectoryInfo (parent_name);
                }
 
-               public static void Move (string src, string dest)
+               public static void Move (string sourceDirName, string destDirName)
                {
-                       if (src == null)
-                               throw new ArgumentNullException ("src");
+                       if (sourceDirName == null)
+                               throw new ArgumentNullException ("sourceDirName");
 
-                       if (dest == null)
-                               throw new ArgumentNullException ("dest");
+                       if (destDirName == null)
+                               throw new ArgumentNullException ("destDirName");
 
-                       if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
-                               throw new ArgumentException ("Invalid source directory name: " + src, "src");
+                       if (sourceDirName.Trim () == "" || sourceDirName.IndexOfAny (Path.InvalidPathChars) != -1)
+                               throw new ArgumentException ("Invalid source directory name: " + sourceDirName, "sourceDirName");
 
-                       if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
-                               throw new ArgumentException ("Invalid target directory name: " + dest, "dest");
+                       if (destDirName.Trim () == "" || destDirName.IndexOfAny (Path.InvalidPathChars) != -1)
+                               throw new ArgumentException ("Invalid target directory name: " + destDirName, "destDirName");
 
-                       if (src == dest)
-                               throw new IOException ("Source directory cannot be same as a target directory.");
+                       if (sourceDirName == destDirName)
+                               throw new IOException ("Source and destination path must be different.");
 
-                       if (Exists (dest))
-                               throw new IOException (dest + " already exists.");
+                       if (Exists (destDirName))
+                               throw new IOException (destDirName + " already exists.");
 
-                       if (!Exists (src))
-                               throw new DirectoryNotFoundException (src + " does not exist");
+                       if (!Exists (sourceDirName) && !File.Exists (sourceDirName))
+                               throw new DirectoryNotFoundException (sourceDirName + " does not exist");
 
                        MonoIOError error;
-                       if (!MonoIO.MoveFile (src, dest, out error))
+                       if (!MonoIO.MoveFile (sourceDirName, destDirName, out error))
                                throw MonoIO.GetException (error);
                }
 
@@ -545,4 +544,3 @@ namespace System.IO
 #endif
        }
 }
-
index f9d24c0da122ee1bce6ab211aef1999f4cab8208..7a355f221162551111b6b3da84b32189c349e678 100644 (file)
@@ -1,3 +1,10 @@
+2007-07-08  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * DirectoryCas.cs: Changed Move to MoveDirectory to match the
+       method in DirectoryTest. Fixed line endings.
+       * DirectoryTest.cs: Added test for bug #81912. Added tests for Move
+       argument checks.
+
 2007-06-24  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * DirectoryTest.cs: Restore original CurrentCulture on teardown.
index de42f26365e02e279974e510efba59c7a7fb76e4..f793f45ebd3bd3cbc2ad1fb12e416a6efa66bf89 100644 (file)
@@ -48,7 +48,7 @@ namespace MonoCasTests.System.IO {
                {
                        // this occurs with a "clean" stack (full trust)
                        dt = new MonoTests.System.IO.DirectoryTest ();
-                       dir = Path.Combine (Path.GetTempPath (), "MonoCasTests.System.IO");\r
+                       dir = Path.Combine (Path.GetTempPath (), "MonoCasTests.System.IO");
                }
 
                [SetUp]
@@ -59,17 +59,17 @@ namespace MonoCasTests.System.IO {
                        dt.SetUp ();
                }
 
-               [TearDown]\r
+               [TearDown]
                public void TearDown () 
                {
                        dt.TearDown ();
-               }\r
+               }
 
                [TestFixtureTearDown]
                public void FixtureTearDown ()
                {
-                       if (Directory.Exists (dir))\r
-                               Directory.Delete (dir, true);\r
+                       if (Directory.Exists (dir))
+                               Directory.Delete (dir, true);
                }
 
                private bool RunningOnWindows {
@@ -91,7 +91,7 @@ namespace MonoCasTests.System.IO {
                        dt.CreateDirectory ();
                        dt.Delete ();
                        dt.Exists ();
-                       dt.Move ();
+                       dt.MoveDirectory ();
                        dt.LastAccessTime ();
                        dt.LastWriteTime ();
                        dt.GetDirectories ();
index 2e8e434acd4ac97ba1831c154f76d2f2a57892ee..deb135a9d36462c6a16bc559347530fd2e0e622b 100644 (file)
@@ -24,7 +24,6 @@ namespace MonoTests.System.IO
 [TestFixture]\r
 public class DirectoryTest\r
 {\r
-       CultureInfo old_culture;\r
        string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");\r
        static readonly char DSC = Path.DirectorySeparatorChar;\r
 \r
@@ -33,15 +32,14 @@ public class DirectoryTest
        {\r
                if (!Directory.Exists (TempFolder))\r
                        Directory.CreateDirectory (TempFolder);\r
-               old_culture = Thread.CurrentThread.CurrentCulture;\r
-               Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);\r
+\r
+               Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");\r
        }\r
        \r
        [TearDown]\r
        public void TearDown () {\r
                if (Directory.Exists (TempFolder))\r
                        Directory.Delete (TempFolder, true);\r
-               Thread.CurrentThread.CurrentCulture = old_culture;\r
        }\r
 \r
        [Test]\r
@@ -614,7 +612,89 @@ public class DirectoryTest
        }\r
 \r
        [Test]\r
-       public void Move ()\r
+       public void Move_DestDirName_Empty ()\r
+       {\r
+               try {\r
+                       Directory.Move (TempFolder, string.Empty);\r
+                       Assert.Fail ("#A1");\r
+               } catch (ArgumentException ex) {\r
+                       // Empty file name is not legal\r
+                       Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");\r
+                       Assert.IsNull (ex.InnerException, "#A3");\r
+                       Assert.IsNotNull (ex.Message, "#A4");\r
+                       Assert.IsNotNull (ex.ParamName, "#A5");\r
+                       Assert.AreEqual ("destDirName", ex.ParamName, "#A6");\r
+               }\r
+\r
+               try {\r
+                       Directory.Move (TempFolder, "             ");\r
+                       Assert.Fail ("#B1");\r
+               } catch (ArgumentException ex) {\r
+                       // The path is not of a legal form\r
+                       Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");\r
+                       Assert.IsNull (ex.InnerException, "#B3");\r
+                       Assert.IsNotNull (ex.Message, "#B4");\r
+               }\r
+       }\r
+\r
+       [Test]\r
+       public void Move_DestDirName_Null ()\r
+       {\r
+               try {\r
+                       Directory.Move (TempFolder, (string) null);\r
+                       Assert.Fail ("#1");\r
+               } catch (ArgumentNullException ex) {\r
+                       Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
+                       Assert.IsNull (ex.InnerException, "#3");\r
+                       Assert.IsNotNull (ex.Message, "#4");\r
+                       Assert.IsNotNull (ex.ParamName, "#5");\r
+                       Assert.AreEqual ("destDirName", ex.ParamName, "#6");\r
+               }\r
+       }\r
+\r
+       [Test]\r
+       public void Move_SourceDirName_Empty ()\r
+       {\r
+               try {\r
+                       Directory.Move (string.Empty, TempFolder);\r
+                       Assert.Fail ("#A1");\r
+               } catch (ArgumentException ex) {\r
+                       // Empty file name is not legal\r
+                       Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");\r
+                       Assert.IsNull (ex.InnerException, "#A3");\r
+                       Assert.IsNotNull (ex.Message, "#A4");\r
+                       Assert.IsNotNull (ex.ParamName, "#A5");\r
+                       Assert.AreEqual ("sourceDirName", ex.ParamName, "#A6");\r
+               }\r
+\r
+               try {\r
+                       Directory.Move ("             ", TempFolder);\r
+                       Assert.Fail ("#B1");\r
+               } catch (ArgumentException ex) {\r
+                       // The path is not of a legal form\r
+                       Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");\r
+                       Assert.IsNull (ex.InnerException, "#B3");\r
+                       Assert.IsNotNull (ex.Message, "#B4");\r
+               }\r
+       }\r
+\r
+       [Test]\r
+       public void Move_SourceDirName_Null ()\r
+       {\r
+               try {\r
+                       Directory.Move ((string) null, TempFolder);\r
+                       Assert.Fail ("#1");\r
+               } catch (ArgumentNullException ex) {\r
+                       Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
+                       Assert.IsNull (ex.InnerException, "#3");\r
+                       Assert.IsNotNull (ex.Message, "#4");\r
+                       Assert.IsNotNull (ex.ParamName, "#5");\r
+                       Assert.AreEqual ("sourceDirName", ex.ParamName, "#6");\r
+               }\r
+       }\r
+\r
+       [Test]\r
+       public void MoveDirectory ()\r
        {\r
                string path = TempFolder + DSC + "DirectoryTest.Test.9";\r
                string path2 = TempFolder + DSC + "DirectoryTest.Test.10";\r
@@ -635,10 +715,10 @@ public class DirectoryTest
                                Directory.Delete (path2 + DSC + "dir", true);\r
                }\r
        }\r
-       \r
+\r
        [Test]\r
-       [ExpectedException(typeof(IOException))]\r
-       public void MoveException1 ()\r
+       [ExpectedException (typeof (IOException))]\r
+       public void MoveDirectory_Same ()\r
        {\r
                string path = TempFolder + DSC + "DirectoryTest.Test.8";\r
                DeleteDirectory (path);\r
@@ -650,28 +730,96 @@ public class DirectoryTest
        }\r
 \r
        [Test]\r
-       [ExpectedException(typeof(ArgumentException))]\r
-       public void MoveException2 ()\r
+       public void MoveFile ()\r
        {\r
-               string path = TempFolder + DSC + "DirectoryTest.Test.11";\r
-               DeleteDirectory (path);\r
+               string tempFile1 = Path.Combine (TempFolder, "temp1.txt");\r
+               string tempFile2 = Path.Combine (TempFolder, "temp2.txt");\r
+\r
+               using (StreamWriter sw = File.CreateText (tempFile1)) {\r
+                       sw.Write ("temp1");\r
+               }\r
+               Assert.IsFalse (File.Exists (tempFile2), "#1");\r
+               Directory.Move (tempFile1, tempFile2);\r
+               Assert.IsFalse (File.Exists (tempFile1), "#2");\r
+               Assert.IsTrue (File.Exists (tempFile2), "#3");\r
+               using (StreamReader sr = File.OpenText (tempFile2)) {\r
+                       Assert.AreEqual ("temp1", sr.ReadToEnd (), "#4");\r
+               }\r
+       }\r
+\r
+       [Test]\r
+       public void MoveFile_DestDir_Exists ()\r
+       {\r
+               string tempFile = Path.Combine (TempFolder, "temp1.txt");\r
+               string tempDir = Path.Combine (TempFolder, "temp2");\r
+\r
+               using (StreamWriter sw = File.CreateText (tempFile)) {\r
+                       sw.Write ("temp1");\r
+               }\r
+               Directory.CreateDirectory (tempDir);\r
+\r
                try {\r
-                       Directory.Move ("", path);\r
-               } finally {\r
-                       DeleteDirectory (path);\r
+                       Directory.Move (tempFile, tempDir);\r
+                       Assert.Fail ("#A1");\r
+               } catch (IOException ex) {\r
+                       // Cannot create a file when that file already exists\r
+                       Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");\r
+                       Assert.IsNull (ex.InnerException, "#A3");\r
+                       Assert.IsNotNull (ex.Message, "#A4");\r
                }\r
+\r
+               Assert.IsTrue (File.Exists (tempFile), "#B1");\r
+               Assert.IsFalse (File.Exists (tempDir), "#B2");\r
+               Assert.IsTrue (Directory.Exists (tempDir), "#B3");\r
        }\r
 \r
        [Test]\r
-       [ExpectedException(typeof(ArgumentException))]\r
-       public void MoveException3 ()\r
+       public void MoveFile_DestFile_Exists ()\r
        {\r
-               string path = TempFolder + DSC + "DirectoryTest.Test.12";\r
-               DeleteDirectory (path);\r
+               string tempFile1 = Path.Combine (TempFolder, "temp1.txt");\r
+               string tempFile2 = Path.Combine (TempFolder, "temp2.txt");\r
+\r
+               using (StreamWriter sw = File.CreateText (tempFile1)) {\r
+                       sw.Write ("temp1");\r
+               }\r
+               using (StreamWriter sw = File.CreateText (tempFile2)) {\r
+                       sw.Write ("temp2");\r
+               }\r
+\r
                try {\r
-                       Directory.Move ("             ", path);\r
-               } finally {\r
-                       DeleteDirectory (path);\r
+                       Directory.Move (tempFile1, tempFile2);\r
+                       Assert.Fail ("#A1");\r
+               } catch (IOException ex) {\r
+                       // Cannot create a file when that file already exists\r
+                       Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");\r
+                       Assert.IsNull (ex.InnerException, "#A3");\r
+                       Assert.IsNotNull (ex.Message, "#A4");\r
+               }\r
+\r
+               Assert.IsTrue (File.Exists (tempFile1), "#B1");\r
+               using (StreamReader sr = File.OpenText (tempFile1)) {\r
+                       Assert.AreEqual ("temp1", sr.ReadToEnd (), "#B2");\r
+               }\r
+\r
+               Assert.IsTrue (File.Exists (tempFile2), "#C1");\r
+               using (StreamReader sr = File.OpenText (tempFile2)) {\r
+                       Assert.AreEqual ("temp2", sr.ReadToEnd (), "#C2");\r
+               }\r
+       }\r
+\r
+       [Test]\r
+       public void MoveFile_Same ()\r
+       {\r
+               string tempFile = Path.Combine (TempFolder, "temp.txt");\r
+\r
+               try {\r
+                       Directory.Move (tempFile, tempFile);\r
+                       Assert.Fail ("#1");\r
+               } catch (IOException ex) {\r
+                       // Source and destination path must be different\r
+                       Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");\r
+                       Assert.IsNull (ex.InnerException, "#3");\r
+                       Assert.IsNotNull (ex.Message, "#4");\r
                }\r
        }\r
 \r
@@ -710,7 +858,7 @@ public class DirectoryTest
 \r
        [Test]\r
        [ExpectedException(typeof(IOException))]\r
-       public void MoveException6 ()\r
+       public void MoveDirectory_Dest_SubDir ()\r
        {\r
                string path = TempFolder + DSC + "DirectoryTest.Test.15";\r
                DeleteDirectory (path);\r
@@ -724,8 +872,8 @@ public class DirectoryTest
        }\r
 \r
        [Test]\r
-       [ExpectedException(typeof(IOException))]\r
-       public void MoveException7 ()\r
+       [ExpectedException (typeof (IOException))]\r
+       public void MoveDirectory_Dest_Exists ()\r
        {\r
                string path = TempFolder + DSC + "DirectoryTest.Test.16";\r
                string path2 = TempFolder + DSC + "DirectoryTest.Test.17";\r