コード例 #1
0
ファイル: Modify_FailSafe.cs プロジェクト: wpsmith/corefx
    public static void DirectoryInfoTest()
    {
        const String dirName    = "DirectoryInfoTestDir";
        const String altDirName = "DirectoryInfoTestDir2";

        // Clean up from any failed test run
        if (Directory.Exists(dirName))
        {
            Directory.Delete(dirName, true);
        }
        if (Directory.Exists(altDirName))
        {
            Directory.Delete(altDirName, true);
        }

        DirectoryInfo di = new DirectoryInfo(dirName);

        if (di.Exists)
        {
            throw new Exception("Directory exists at beginning of test!");
        }
        di.Create();
        Stream s = File.Create(Path.Combine(di.Name, "foo"));

        s.Dispose();
        di.CreateSubdirectory("bar");

        // Attributes test
        di.Refresh();  // Reload attributes information!
        FileAttributes attr = di.Attributes;

        if ((attr & FileAttributes.Directory) != FileAttributes.Directory)
        {
            throw new Exception("Unexpected attributes on the directory - the directory bit wasn't set!  Got: " + attr);
        }
        // BLORF TODO: blorf write set the system attribute?

        // Rename directory via the MoveTo method, the move it back.
        di = FailSafeDirectoryOperations.MoveDirectoryInfo(di, altDirName);
        if (Directory.Exists(dirName))
        {
            throw new Exception("Old directory still exists after MoveTo!");
        }
        if (!Directory.Exists(altDirName))
        {
            throw new Exception("New directory doesn't exists after MoveTo!");
        }
        if (!di.Exists)
        {
            throw new Exception("DirectoryInfo says the directory doesn't exist after first MoveTo!");
        }
        di = FailSafeDirectoryOperations.MoveDirectoryInfo(di, dirName);
        if (!di.Exists)
        {
            throw new Exception("DirectoryInfo says the directory doesn't exist after second MoveTo!");
        }

        // Get files and directories now.
        FileInfo[] files = di.GetFiles();
        if (files.Length != 1)
        {
            throw new Exception("GetFiles should have returned just one file!  got: " + files.Length);
        }
        if (!"foo".Equals(files[0].Name))
        {
            throw new Exception("FileInfo's Name should have been foo, but was: " + files[0].Name);
        }

        DirectoryInfo[] dirs = di.GetDirectories();
        if (dirs.Length != 1)
        {
            throw new Exception("GetDirectories should have returned just one dir!  got: " + dirs.Length);
        }
        if (!"bar".Equals(dirs[0].Name))
        {
            throw new Exception("DirectoryInfo's Name should have been bar, but was: " + dirs[0].Name);
        }

        FileSystemInfo[] infos = di.GetFileSystemInfos();
        if (infos.Length != 2)
        {
            throw new Exception("GetFileSystemInfos should have returned 2!  got: " + infos.Length);
        }
        FileInfo      tempFi = infos[0] as FileInfo;
        DirectoryInfo tempDi = null;

        if (tempFi == null)
        {
            tempFi = infos[1] as FileInfo;
            tempDi = infos[0] as DirectoryInfo;
        }
        else
        {
            tempDi = infos[1] as DirectoryInfo;
        }
        if (!tempFi.Name.Equals("foo"))
        {
            throw new Exception("GetFileSystemInfo returned FileInfo with wrong name!  got: " + tempFi.Name);
        }
        if (!tempDi.Name.Equals("bar"))
        {
            throw new Exception("GetFileSystemInfo returned DirectoryInfo with wrong name!  got: " + tempDi.Name);
        }


        // Test DirectoryInfo.Name on something like "c:\bar\"
        DirectoryInfo subDir = new DirectoryInfo(Path.Combine(di.Name, "bar") + Path.DirectorySeparatorChar);

        if (!subDir.Name.Equals("bar"))
        {
            throw new Exception("Subdirectory name was wrong.  Expected bar, Got: " + subDir.Name);
        }

        DirectoryInfo parent = subDir.Parent;

        if (!DirNameEquals(parent.FullName, di.FullName))
        {
            throw new Exception("DI.FullName != SubDir.Parent.FullName!  subdir full name: " + parent.FullName);
        }

        // Check more info about the DirectoryInfo
        String        rootName = Path.GetPathRoot(Directory.GetCurrentDirectory());
        DirectoryInfo root     = di.Root;

        if (!rootName.Equals(root.Name))
        {
            throw new Exception(String.Format("Root directory name was wrong!  rootName: {0}  DI.Root.Name: {1}", rootName, root.Name));
        }

        // Test DirectoryInfo behavior for the root
        string        rootPath = root.FullName;
        DirectoryInfo c        = new DirectoryInfo(rootPath);

        if (!rootPath.Equals(c.Name))
        {
            throw new Exception("DirectoryInfo name for root was wrong!  got: " + c.Name);
        }
        if (!rootPath.Equals(c.FullName))
        {
            throw new Exception("DirectoryInfo FullName for root was wrong!  got: " + c.FullName);
        }
        if (null != c.Parent)
        {
            throw new Exception("DirectoryInfo::Parent for root is not null!");
        }

        FailSafeDirectoryOperations.DeleteDirectoryInfo(di, true);
        di.Refresh();
        if (di.Exists)
        {
            throw new Exception("Directory still exists at end of test!");
        }

        Assert.True(s_pass);
    }
コード例 #2
0
    public static void runTest()
    {
        int    iCountErrors    = 0;
        int    iCountTestcases = 0;
        String strLoc          = "Loc_000oo";
        String strValue        = String.Empty;


        try
        {
            /////////////////////////  START TESTS ////////////////////////////
            ///////////////////////////////////////////////////////////////////

            FileInfo      fil2 = null;
            DirectoryInfo dir2 = null;

            // [] Pass in null argument

            strLoc = "Loc_099u8";
            string testDir = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());

            dir2 = Directory.CreateDirectory(testDir);
            iCountTestcases++;
            try
            {
                dir2.MoveTo(null);
                iCountErrors++;
                printerr("Error_298dy! Expected exception not thrown");
            }
            catch (ArgumentNullException)
            {
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_209xj! Incorrect exception thrown, exc==" + exc.ToString());
            }
            dir2.Delete(true);

            // [] Pass in empty String should throw ArgumentException

            strLoc = "Loc_098gt";

            dir2 = Directory.CreateDirectory(testDir);
            iCountTestcases++;
            try
            {
                dir2.MoveTo(String.Empty);
                iCountErrors++;
                printerr("Error_3987c! Expected exception not thrown");
            }
            catch (ArgumentException)
            {
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_9092c! Incorrect exception thrown, exc==" + exc.ToString());
            }
            dir2.Delete(true);


            // [] Vanilla move to new name

            strLoc = "Loc_98hvc";

            dir2 = Directory.CreateDirectory(testDir);
            iCountTestcases++;
            dir2.MoveTo(Path.Combine(TestInfo.CurrentDirectory, "Test3"));
            try
            {
                dir2 = new DirectoryInfo(Path.Combine(TestInfo.CurrentDirectory, "Test3"));
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_2881s! Directory not moved, exc==" + exc.ToString());
            }
            dir2.Delete(true);


            // [] Try to move it on top of current dir

            strLoc = "Loc_2908x";

            dir2 = Directory.CreateDirectory(testDir);
            iCountTestcases++;
            try
            {
                dir2.MoveTo(Path.Combine(TestInfo.CurrentDirectory, "."));
                iCountErrors++;
                printerr("Error_2091z! Expected exception not thrown,");
            }
            catch (IOException)
            {
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_2100s! Incorrect exception thrown, exc==" + exc.ToString());
            }
            dir2.Delete(true);

            // [] Try to move it on top of parent dir

            strLoc = "Loc_1999s";

            dir2 = Directory.CreateDirectory(testDir);
            iCountTestcases++;
            try
            {
                dir2.MoveTo(Path.Combine(TestInfo.CurrentDirectory, ".."));
                iCountErrors++;
                printerr("Error_2091b! Expected exception not thrown");
            }
            catch (IOException)
            {
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_01990! Incorrect exception thrown, exc==" + exc.ToString());
            }
            dir2.Delete(true);



            // [] Pass in string with spaces should throw ArgumentException

            strLoc = "Loc_498vy";

            dir2 = Directory.CreateDirectory(testDir);
            iCountTestcases++;
            try
            {
                dir2.MoveTo("         ");
                iCountErrors++;
                printerr("Error_209uc! Expected exception not thrown");
                fil2.Delete();
            }
            catch (ArgumentException)
            {
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_28829! Incorrect exception thrown, exc==" + exc.ToString());
            }
            dir2.Delete(true);

            // [] Mvoe to the same directory

            strLoc = "Loc_498vy";

            dir2 = Directory.CreateDirectory(testDir);
            iCountTestcases++;
            try
            {
                dir2.MoveTo(testDir);
                iCountErrors++;
                printerr("Error_209uc! Expected exception not thrown");
                fil2.Delete();
            }
            catch (IOException)
            {
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_28829! Incorrect exception thrown, exc==" + exc.ToString());
            }
            dir2.Delete(true);

#if !TEST_WINRT // Can't access other root drives
            // [] Move to different drive will throw AccessException
            //-----------------------------------------------------------------
            if (Interop.IsWindows) // drive labels

            {
                strLoc = "Loc_00025";

                dir2 = Directory.CreateDirectory(testDir);
                iCountTestcases++;
                try
                {
                    if (dir2.FullName.Substring(0, 3) == @"d:\" || dir2.FullName.Substring(0, 3) == @"D:\")
                    {
                        dir2.MoveTo("C:\\TempDirectory");
                    }
                    else
                    {
                        dir2.MoveTo("D:\\TempDirectory");
                    }
                    Console.WriteLine("Root directory..." + dir2.FullName.Substring(0, 3));
                    iCountErrors++;
                    printerr("Error_00078! Expected exception not thrown");
                }
                catch (IOException)
                {
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    printerr("Error_23r0g! Incorrect exception thrown, exc==" + exc.ToString());
                }
                dir2.Delete(true);
            }
#endif

            // [] Move non-existent directory
            dir2 = new DirectoryInfo(testDir);
            try
            {
                dir2.MoveTo(Path.Combine(TestInfo.CurrentDirectory, "Test5526"));
                iCountErrors++;
                Console.WriteLine("Err_34gs! Exception not thrown");
            }
            catch (DirectoryNotFoundException)
            {
            }
            catch (Exception ex)
            {
                iCountErrors++;
                Console.WriteLine("Err_34gs! Wrong Exception thrown: {0}", ex);
            }


            // [] Pass in string with tabs

            strLoc = "Loc_98399";

            dir2 = Directory.CreateDirectory(testDir);
            iCountTestcases++;
            try
            {
                dir2.MoveTo("\t");
                iCountErrors++;
                printerr("Error_2091c! Expected exception not thrown");
            }
            catch (ArgumentException)
            {
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_8374v! Incorrect exception thrown, exc==" + exc.ToString());
            }
            dir2.Delete(true);



            // [] Create a long filename directory


            strLoc = "Loc_2908y";

            StringBuilder sb = new StringBuilder(TestInfo.CurrentDirectory);
            while (sb.Length < IOInputs.MaxPath + 1)
            {
                sb.Append("a");
            }

            iCountTestcases++;
            dir2 = Directory.CreateDirectory(testDir);
            try
            {
                dir2.MoveTo(sb.ToString());
                iCountErrors++;
                printerr("Error_109ty! Expected exception not thrown");
            }
            catch (PathTooLongException)
            { // This should really be PathTooLongException
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_109dv! Incorrect exception thrown, exc==" + exc.ToString());
            }
            dir2.Delete(true);


            // [] Too long filename

            strLoc = "Loc_48fyf";

            sb = new StringBuilder();
            for (int i = 0; i < IOInputs.MaxPath + 1; i++)
            {
                sb.Append("a");
            }

            iCountTestcases++;
            dir2 = Directory.CreateDirectory(testDir);
            try
            {
                dir2.MoveTo(sb.ToString());
                iCountErrors++;
                printerr("Error_109ty! Expected exception not thrown");
            }
            catch (PathTooLongException)
            { // This should really be PathTooLongException
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_109dv! Incorrect exception thrown, exc==" + exc.ToString());
            }
            dir2.Delete(true);


            // [] specifying subdirectories should fail unless they exist

            strLoc = "Loc_209ud";

            dir2 = Directory.CreateDirectory(testDir);
            iCountTestcases++;
            try
            {
                dir2.MoveTo(Path.Combine(testDir, "Test", "Test", "Test"));
                iCountErrors++;
                printerr("Error_1039s! Expected exception not thrown");
                fil2.Delete();
            }
            catch (IOException)
            {
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_2019u! Incorrect exception thrown, exc==" + exc.ToString());
            }
            dir2.Delete(true);

            // [] Exception if directory already exists

            strLoc = "Loc_2498x";

            iCountTestcases++;
            Directory.CreateDirectory(testDir + "a");
            if (!Interop.IsWindows) // exception on Unix would only happen if the directory isn't empty
            {
                File.Create(Path.Combine(testDir + "a", "temp.txt")).Dispose();
            }
            dir2 = Directory.CreateDirectory(testDir);
            try
            {
                dir2.MoveTo(testDir + "a");
                iCountErrors++;
                printerr("Error_2498h! Expected exception not thrown");
            }
            catch (IOException)
            {
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_289vt! Incorrect exception thrown, exc==" + exc.ToString());
            }
            dir2.Delete(true);
            new DirectoryInfo(testDir + "a").Delete(true);



            // [] Illiegal chars in new DirectoryInfo name

            strLoc = "Loc_2798r";

            dir2 = Directory.CreateDirectory(testDir);
            iCountTestcases++;
            try
            {
                dir2.MoveTo("\0\0\0**\0.*\0\0");
                iCountErrors++;
                printerr("Error_298hv! Expected exception not thrown");
            }
            catch (ArgumentException)
            {
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_2199d! Incorrect exception thrown, exc==" + exc.ToString());
            }
            dir2.Delete(true);


            // [] Move a directory with subdirs

            strLoc = "Loc_209ux";

            dir2 = Directory.CreateDirectory(testDir);
            DirectoryInfo subdir = dir2.CreateSubdirectory("Test5525");

            //			dir2.MoveTo("NewTest5525");
            FailSafeDirectoryOperations.MoveDirectoryInfo(dir2, Path.Combine(TestInfo.CurrentDirectory, "NewTest5525"));

            iCountTestcases++;
            try
            {
                subdir = new DirectoryInfo(Path.Combine(TestInfo.CurrentDirectory, "NewTest5525", "Test5525"));
            }
            catch (Exception exc)
            {
                iCountErrors++;
                printerr("Error_290u1! Failed to move Folder, exc==" + exc.ToString());
            }
            subdir.Delete(true);
            dir2.Delete(true);


            // []


            //problems with trailing slashes and stuff - #431574

            /**
             * - We need to remove the trailing slash when we get the parent directory (we call Path.GetDirectoryName on the full path and having the slash will not work)
             * - Root drive always have the trailing slash
             * - MoveTo adds a trailing slash
             **/

            String subDir = Path.Combine(TestInfo.CurrentDirectory, "LaksTemp");
            DeleteFileDir(subDir);
            String[] values  = { "TestDir", "TestDir" + Path.DirectorySeparatorChar };
            String   moveDir = Path.Combine(TestInfo.CurrentDirectory, values[1]);
            foreach (String value in values)
            {
                dir2 = new DirectoryInfo(subDir);
                dir2.Create();
                dir2.MoveTo(Path.Combine(TestInfo.CurrentDirectory, value));
                if (!dir2.FullName.Equals(moveDir))
                {
                    Console.WriteLine("moveDir: <{0}>", moveDir);
                    iCountErrors++;
                    Console.WriteLine("Err_374g! wrong vlaue returned: {0}", dir2.FullName);
                }
                dir2.Delete();
            }

            ///////////////////////////////////////////////////////////////////
            /////////////////////////// END TESTS /////////////////////////////
        }
        catch (Exception exc_general)
        {
            ++iCountErrors;
            Console.WriteLine("Error Err_8888yyy!  strLoc==" + strLoc + ", exc_general==" + exc_general.ToString());
        }
        ////  Finish Diagnostics

        if (iCountErrors != 0)
        {
            Console.WriteLine("FAiL! " + s_strTFName + " ,iCountErrors==" + iCountErrors.ToString());
        }

        Assert.Equal(0, iCountErrors);
    }