public void DeleteTestDirs() { bool deleted = false; int attemptsRemaining = 5; while (!deleted && attemptsRemaining > 0) { try { if (Directory.Exists(testDir)) { FailSafeDirectoryOperations.DeleteDirectory(testDir, true); } deleted = true; break; } catch (IOException) { if (-attemptsRemaining == 0) { throw; } else { Task.Delay(200).Wait(); } } } }
protected virtual void Dispose(bool disposing) { if (disposing) { // free other state (managed objects) // set interesting (large) fields to null } // free your own state (unmanaged objects) FailSafeDirectoryOperations.DeleteDirectory(_startDir, true); }
public void GetEntriesThenDelete() { string testDirPath = GetTestFilePath(); DirectoryInfo testDirInfo = new DirectoryInfo(testDirPath); testDirInfo.Create(); string testDir1 = GetTestFileName(); string testDir2 = GetTestFileName(); string testFile1 = GetTestFileName(); string testFile2 = GetTestFileName(); string testFile3 = GetTestFileName(); string testFile4 = GetTestFileName(); string testFile5 = GetTestFileName(); testDirInfo.CreateSubdirectory(testDir1); testDirInfo.CreateSubdirectory(testDir2); using (File.Create(Path.Combine(testDirPath, testFile1))) using (File.Create(Path.Combine(testDirPath, testFile2))) using (File.Create(Path.Combine(testDirPath, testFile3))) { string[] results; using (File.Create(Path.Combine(testDirPath, testFile4))) using (File.Create(Path.Combine(testDirPath, testFile5))) { results = GetEntries(testDirPath); Assert.NotNull(results); Assert.NotEmpty(results); if (TestFiles) { Assert.Contains(Path.Combine(testDirPath, testFile1), results); Assert.Contains(Path.Combine(testDirPath, testFile2), results); Assert.Contains(Path.Combine(testDirPath, testFile3), results); Assert.Contains(Path.Combine(testDirPath, testFile4), results); Assert.Contains(Path.Combine(testDirPath, testFile5), results); } if (TestDirectories) { Assert.Contains(Path.Combine(testDirPath, testDir1), results); Assert.Contains(Path.Combine(testDirPath, testDir2), results); } } File.Delete(Path.Combine(testDirPath, testFile4)); File.Delete(Path.Combine(testDirPath, testFile5)); FailSafeDirectoryOperations.DeleteDirectory(testDir1, true); results = GetEntries(testDirPath); Assert.NotNull(results); Assert.NotEmpty(results); if (TestFiles) { Assert.Contains(Path.Combine(testDirPath, testFile1), results); Assert.Contains(Path.Combine(testDirPath, testFile2), results); Assert.Contains(Path.Combine(testDirPath, testFile3), results); } if (TestDirectories) { Assert.Contains(Path.Combine(testDirPath, testDir2), results); } } }
public static void runTest() { int iCountErrors = 0; int iCountTestcases = 0; String strLoc = "Loc_000oo"; String strValue = String.Empty; try { ///////////////////////// START TESTS //////////////////////////// /////////////////////////////////////////////////////////////////// String tempDirName = Path.Combine(TestInfo.CurrentDirectory, "TempDirectory"); String dirName = Path.Combine(TestInfo.CurrentDirectory, "Move_str_str_test_Dir"); DirectoryInfo dir2 = null; if (Directory.Exists(tempDirName)) { Directory.Delete(tempDirName, true); } if (Directory.Exists(dirName)) { Directory.Delete(dirName, true); } // [] Argumentnull exception for null arguments //----------------------------------------------------------------- strLoc = "Loc_00001"; iCountTestcases++; try { Directory.Move(null, dirName); iCountErrors++; printerr("Error_00002! Expected exception not thrown"); } catch (ArgumentNullException) { } catch (Exception exc) { iCountErrors++; printerr("Error_00004! Incorrect exception thrown, exc==" + exc.ToString()); } iCountTestcases++; try { Directory.Move(dirName, null); iCountErrors++; printerr("Error_00005! Expected exception not thrown"); } catch (ArgumentNullException) { } catch (Exception exc) { iCountErrors++; printerr("Error_00007! Incorrect exception thrown, exc==" + exc.ToString()); } //----------------------------------------------------------------- // [] ArgumentException for zero length arguments //----------------------------------------------------------------- strLoc = "Loc_00008"; iCountTestcases++; try { Directory.Move(String.Empty, dirName); iCountErrors++; printerr("Error_00008! Expected exception not thrown"); } catch (ArgumentException) { } catch (Exception exc) { iCountErrors++; printerr("Error_00010! Incorrect exception thrown, exc==" + exc.ToString()); } iCountTestcases++; try { Directory.Move(dirName, String.Empty); iCountErrors++; printerr("Error_00011! Expected exception not thrown"); } catch (ArgumentException) { } catch (Exception exc) { iCountErrors++; printerr("Error_00013! Incorrect exception thrown, exc==" + exc.ToString()); } //----------------------------------------------------------------- // [] Try to move a directory that does not exist //----------------------------------------------------------------- strLoc = "Loc_00014"; iCountTestcases++; try { Directory.Move(Path.Combine(TestInfo.CurrentDirectory, "NonExistentDirectory"), dirName); iCountErrors++; printerr("Error_00015! Expected exception not thrown"); } catch (DirectoryNotFoundException) { } catch (Exception exc) { iCountErrors++; printerr("Error_00017! Incorrect exception thrown, exc==" + exc.ToString()); } //----------------------------------------------------------------- // [] AccessException when moving onto existing directory //----------------------------------------------------------------- strLoc = "Loc_00018"; iCountTestcases++; try { Directory.Move(TestInfo.CurrentDirectory, TestInfo.CurrentDirectory); iCountErrors++; printerr("Error_00019! Expected exception not thrown"); } catch (IOException) { } catch (Exception exc) { iCountErrors++; printerr("Error_00021! Incorrect exception thrown, exc==" + exc.ToString()); } //----------------------------------------------------------------- // [] Move a directory and check that it is moved //----------------------------------------------------------------- strLoc = "Loc_00022"; Directory.CreateDirectory(dirName); Directory.Move(dirName, tempDirName); iCountTestcases++; if (Directory.Exists(dirName)) { iCountErrors++; printerr("Error_00023! Source directory still there"); } if (!Directory.Exists(tempDirName)) { iCountErrors++; printerr("Error_00024! destination directory missing"); } Directory.Delete(tempDirName); //[]Move directories that end with directory separator //----------------------------------------------------------------- Directory.CreateDirectory(dirName); Directory.Move(dirName + Path.DirectorySeparatorChar, tempDirName + Path.DirectorySeparatorChar); iCountTestcases++; if (Directory.Exists(dirName)) { iCountErrors++; printerr("Error_00023! Source directory still there"); } if (!Directory.Exists(tempDirName)) { iCountErrors++; printerr("Error_00024! destination directory missing"); } Directory.Delete(tempDirName); #if !TEST_WINRT if (Interop.IsWindows) // moving between drive labels { // [] Move to different drive will throw AccessException //----------------------------------------------------------------- strLoc = "Loc_00025"; string fullDirName = Path.GetFullPath(dirName); Directory.CreateDirectory(dirName); iCountTestcases++; try { if (fullDirName.Substring(0, 3) == @"d:\" || fullDirName.Substring(0, 3) == @"D:\") { Directory.Move(fullDirName, "C:\\TempDirectory"); } else { Directory.Move(fullDirName, "D:\\TempDirectory"); } Console.WriteLine("Root directory..." + fullDirName.Substring(0, 3)); iCountErrors++; printerr("Error_00026! Expected exception not thrown"); } catch (IOException) { } catch (Exception exc) { iCountErrors++; printerr("Error_00000! Incorrect exception thrown, exc==" + exc.ToString()); } if (Directory.Exists(fullDirName)) { Directory.Delete(fullDirName, true); } //----------------------------------------------------------------- } #endif // [] Moving Directory with subdirectories //----------------------------------------------------------------- strLoc = "Loc_00028"; dir2 = Directory.CreateDirectory(dirName); dir2.CreateSubdirectory(Path.Combine("SubDir", "SubSubDir")); FailSafeDirectoryOperations.MoveDirectory(dirName, tempDirName); // Directory.Move(dirName, tempDirName); iCountTestcases++; if (Directory.Exists(dirName)) { iCountErrors++; printerr("Error_00029! Source directory still there"); } dir2 = new DirectoryInfo(tempDirName); iCountTestcases++; if (!Directory.Exists(dir2.FullName)) { iCountErrors++; printerr("Error_00030! Destination directory missing"); } iCountTestcases++; if (!Directory.Exists(Path.Combine(dir2.FullName, "SubDir", "SubSubDir"))) { iCountErrors++; printerr("Error_00031! Subdirectories not moved"); } dir2.Delete(true); //----------------------------------------------------------------- // [] wildchars in src directory //----------------------------------------------------------------- strLoc = "Loc_00032"; iCountTestcases++; try { Directory.Move("*", tempDirName); iCountErrors++; printerr("Error_00033! Expected exception not thrown"); } catch (ArgumentException) { } catch (Exception exc) { iCountErrors++; printerr("Error_00035! Incorrect exception thrown, exc==" + exc.ToString()); } //----------------------------------------------------------------- // [] wildchars in dest directory //----------------------------------------------------------------- strLoc = "Loc_00036"; iCountTestcases++; try { Directory.Move(TestInfo.CurrentDirectory, "Temp*"); iCountErrors++; printerr("Error_00037! Expected exception not thrown"); } catch (ArgumentException) { } catch (Exception exc) { iCountErrors++; printerr("Error_00039! Incorrect exception thrown, exc==" + exc.ToString()); } //----------------------------------------------------------------- // [] InvalidPathChars //----------------------------------------------------------------- strLoc = "Loc_00040"; iCountTestcases++; try { Directory.Move(TestInfo.CurrentDirectory, "<MyDirectory\0"); iCountErrors++; printerr("Error_00041! Expected exception not thrown"); } catch (ArgumentException) { } catch (Exception exc) { iCountErrors++; printerr("Error_00043 Incorret exception thrown, exc==" + exc.ToString()); } //----------------------------------------------------------------- // [] PathTooLongException if destination name is too long //----------------------------------------------------------------- strLoc = "Loc_00044"; String str = new string('a', IOInputs.MaxPath); iCountTestcases++; try { Directory.Move(TestInfo.CurrentDirectory, str); iCountErrors++; printerr("Error_00045! Expected exception not thrown"); } catch (PathTooLongException) { } catch (Exception exc) { iCountErrors++; printerr("Error_00047! Incorrect exception thrown, exc==" + exc.ToString()); } //----------------------------------------------------------------- // [] Non-existent drive specified for destination //----------------------------------------------------------------- strLoc = "Loc_00048"; if (Interop.IsWindows) // drive labels { iCountTestcases++; Directory.CreateDirectory(dirName); try { Directory.Move(dirName, "X:\\Temp"); iCountErrors++; printerr("Error_00049! Expected exception not thrown"); } catch (IOException) { } catch (Exception exc) { iCountErrors++; printerr("Error_00051! Incorrect exception thrown, exc==" + exc.ToString()); } Directory.Delete(dirName); } //----------------------------------------------------------------- // [] Use directory names with spaces //----------------------------------------------------------------- strLoc = "Loc_00052"; string destDirName = Path.Combine(TestInfo.CurrentDirectory, "This is my directory"); Directory.CreateDirectory(dirName); Directory.Move(dirName, destDirName); iCountTestcases++; if (!Directory.Exists(destDirName)) { iCountErrors++; printerr("Error_00053! Destination directory missing"); } Directory.Delete(destDirName); //----------------------------------------------------------------- // ][ Directory names in different cultures //----------------------------------------------------------------- //----------------------------------------------------------------- if (Directory.Exists(tempDirName)) { Directory.Delete(tempDirName, true); } if (Directory.Exists(dirName)) { Directory.Delete(dirName, true); } /////////////////////////////////////////////////////////////////// /////////////////////////// END TESTS ///////////////////////////// } catch (Exception exc_general) { ++iCountErrors; printerr("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); }
public static void runTest() { int iCountErrors = 0; int iCountTestcases = 0; String strLoc = "Loc_000oo"; String strValue = String.Empty; try { DirectoryInfo dir2; String dirName = "GetFileSystemEntries_str_str_TestDir"; String[] strArr; FailSafeDirectoryOperations.DeleteDirectory(dirName, true); strLoc = "Loc_4y982"; // [] With null file name iCountTestcases++; try { Directory.GetFileSystemEntries(null, "*"); iCountErrors++; printerr("Error_0002! Expected exception not thrown"); } catch (ArgumentNullException) { } catch (Exception exc) { iCountErrors++; printerr("Error_0003! Unexpected exceptiont thrown: " + exc.ToString()); } // [] With empty file name iCountTestcases++; try { Directory.GetFileSystemEntries("", "*"); iCountErrors++; printerr("Error_0004! Expected exception not thrown"); } catch (ArgumentException) { } catch (Exception exc) { iCountErrors++; printerr("Error_0005! Unexpected exceptiont thrown: " + exc.ToString()); } //Directory that doesn't exist iCountTestcases++; try { Directory.GetFileSystemEntries(dirName, "*"); iCountErrors++; printerr("Error_1001! Expected exception not thrown"); } catch (DirectoryNotFoundException) { } catch (Exception exc) { iCountErrors++; printerr("Error_1002! Unexpected exceptiont thrown: " + exc.ToString()); } //With wild character's as file name iCountTestcases++; try { String strTempDir = Path.Combine("dls;d", "442349-0", "v443094(*)(+*$#$*") + new string(Path.DirectorySeparatorChar, 3); Directory.GetFileSystemEntries(strTempDir, "*"); iCountErrors++; printerr("Error_1003! Expected exception not thrown"); } catch (ArgumentException) { } catch (Exception exc) { iCountErrors++; printerr("Error_1004! Unexpected exceptiont thrown: " + exc.ToString()); } //With spaces as file name iCountTestcases++; try { String strTempDir = " "; Directory.GetFileSystemEntries(strTempDir, "*"); iCountErrors++; printerr("Error_1044! Expected exception not thrown"); } catch (ArgumentException) { } catch (Exception exc) { iCountErrors++; printerr("Error_1023! Unexpected exceptiont thrown: " + exc.ToString()); } dir2 = new DirectoryInfo(dirName); dir2.Create(); // [] With null search pattern iCountTestcases++; try { Directory.GetFileSystemEntries(dirName, null); iCountErrors++; printerr("Error_02002! Expected exception not thrown"); } catch (ArgumentNullException) { } catch (Exception exc) { iCountErrors++; printerr("Error_00023! Unexpected exceptiont thrown: " + exc.ToString()); } // [] With empty search pattern iCountTestcases++; try { String[] strInfos = Directory.GetFileSystemEntries(dirName, ""); // To avoid OS differences we have decided not to throw an argument exception when empty // string passed. But we should return 0 items. if (strInfos.Length != 0) { iCountErrors++; printerr("Error_9004! Invalid number of directories returned"); } } catch (Exception exc) { iCountErrors++; printerr("Error_9005! Unexpected exceptiont thrown: " + exc.ToString()); } //With wild character's as search pattern iCountTestcases++; try { String strTempDir = Path.Combine("dls;d", "442349-0", "v443094(*)(+*$#$*") + new string(Path.DirectorySeparatorChar, 3); Directory.GetFileSystemEntries(dirName, strTempDir); iCountErrors++; printerr("Error_3003! Expected exception not thrown"); } catch (ArgumentException) { } catch (Exception exc) { iCountErrors++; printerr("Error_3004! Unexpected exceptiont thrown: " + exc.ToString()); } //Valid characters for search pattern iCountTestcases++; try { String strTempDir = "a..b abc..d"; Directory.GetFileSystemEntries(dirName, strTempDir); } catch (Exception exc) { iCountErrors++; printerr("Error_4004! Unexpected exceptiont thrown: " + exc.ToString()); } //Invalid characters for search pattern iCountTestcases++; try { String strTempDir = Path.Combine("..ab ab.. .. abc..d", "abc.."); Directory.GetFileSystemEntries(dirName, strTempDir); iCountErrors++; printerr("Error_144003! Expected exception not thrown"); } catch (ArgumentException) { } catch (Exception exc) { iCountErrors++; printerr("Error_10404! Unexpected exceptiont thrown: " + exc.ToString()); } strLoc = "Loc_0001"; //Path that doesn't exist iCountTestcases++; try { strArr = Directory.GetFileSystemEntries("ThisDirectoryShouldNotExist", "*"); iCountErrors++; printerr("Error_14443! Expected exception not thrown"); } catch (IOException) { } catch (Exception exc) { iCountErrors++; printerr("Error_10433! Unexpected exceptiont thrown: " + exc.ToString()); } //With lot's of \'s at the end of file name iCountTestcases++; try { String strTempDir = Directory.GetCurrentDirectory() + new string(Path.DirectorySeparatorChar, 5); strArr = Directory.GetFileSystemEntries(strTempDir, "*"); if (strArr == null || strArr.Length == 0) { printerr("Error_1234!!! INvalid number of file system entries count :: " + strArr.Length); iCountErrors++; } } catch (Exception exc) { iCountErrors++; printerr("Error_10406! Unexpected exceptiont thrown: " + exc.ToString()); } //With the current directory iCountTestcases++; try { strArr = Directory.GetFileSystemEntries(s_strTFPath, "*"); if (strArr == null || strArr.Length == 0) { printerr("Error_2434!!! INvalid number of file system entries count :: " + strArr.Length); iCountErrors++; } } catch (Exception exc) { iCountErrors++; printerr("Error_12321!!! Unexpected exceptiont thrown: " + exc.ToString()); } //With valid directory strArr = Directory.GetFileSystemEntries(dirName, "*"); iCountTestcases++; if (strArr.Length != 0) { iCountErrors++; printerr("Error_207v7! Incorrect number of directories returned"); } //----------------------------------------------------------------- // [] Create a directorystructure get all the filesystementries //----------------------------------------------------------------- strLoc = "Loc_2398c"; dir2.CreateSubdirectory("TestDir1"); dir2.CreateSubdirectory("TestDir2"); dir2.CreateSubdirectory("TestDir3"); FileStream fs1 = new FileInfo(Path.Combine(dir2.ToString(), "TestFile1")).Create(); FileStream fs2 = new FileInfo(Path.Combine(dir2.ToString(), "TestFile2")).Create(); FileStream fs3 = new FileInfo(Path.Combine(dir2.ToString(), "Test1File2")).Create(); FileStream fs4 = new FileInfo(Path.Combine(dir2.ToString(), "Test1Dir2")).Create(); //white spaces for search pattern strArr = Directory.GetFileSystemEntries(dirName, " "); iCountTestcases++; if (strArr.Length != 0) { Console.WriteLine(dir2.ToString()); iCountErrors++; printerr("Error_24324! Incorrect number of directories returned" + strArr.Length); } //Search pattern with '?'. strArr = Directory.GetFileSystemEntries(dirName, "Test1*"); iCountTestcases++; if (strArr.Length != 2) { iCountErrors++; printerr("Error_24343! Incorrect number of directories returned" + strArr.Length); } strArr = Directory.GetFileSystemEntries(dir2.Name, "*"); iCountTestcases++; if (strArr.Length != 7) { iCountErrors++; printerr("Error_1yt75! Incorrect number of directories returned" + strArr.Length); } for (int iLoop = 0; iLoop < strArr.Length; iLoop++) { strArr[iLoop] = Path.GetFileName(strArr[iLoop]); } iCountTestcases++; if (Array.IndexOf(strArr, "TestDir1") < 0) { iCountErrors++; printerr("Error_4yg76! Incorrect name==" + strArr[0]); } iCountTestcases++; if (Array.IndexOf(strArr, "TestDir2") < 0) { iCountErrors++; printerr("Error_1987y! Incorrect name==" + strArr[1]); } iCountTestcases++; if (Array.IndexOf(strArr, "TestDir3") < 0) { iCountErrors++; printerr("Error_4yt76! Incorrect name==" + strArr[2]); } iCountTestcases++; if (Array.IndexOf(strArr, "Test1File2") < 0) { iCountErrors++; printerr("Error_3y775! Incorrect name==" + strArr[3]); } iCountTestcases++; if (Array.IndexOf(strArr, "Test1Dir2") < 0) { iCountErrors++; printerr("Error_90885! Incorrect name==" + strArr[4]); } iCountTestcases++; if (Array.IndexOf(strArr, "TestFile1") < 0) { iCountErrors++; printerr("Error_879by! Incorrect name==" + strArr[5]); } iCountTestcases++; if (Array.IndexOf(strArr, "TestFile2") < 0) { iCountErrors++; printerr("Error_29894! Incorrect name==" + strArr[6]); } fs1.Dispose(); fs2.Dispose(); fs3.Dispose(); fs4.Dispose(); // [] Search criteria beginning with '*' strArr = Directory.GetFileSystemEntries(dir2.Name, "*2"); iCountTestcases++; if (strArr.Length != 4) { iCountErrors++; printerr("Error_8019x! Incorrect number of files==" + strArr.Length); } for (int iLoop = 0; iLoop < strArr.Length; iLoop++) { strArr[iLoop] = Path.GetFileName(strArr[iLoop]); } iCountTestcases++; if (Array.IndexOf(strArr, "Test1Dir2") < 0) { iCountErrors++; printerr("Error_247yg! Incorrect name==" + strArr[0]); } iCountTestcases++; if (Array.IndexOf(strArr, "TestDir2") < 0) { iCountErrors++; printerr("Error_24gy7! Incorrect name==" + strArr[1]); } iCountTestcases++; if (Array.IndexOf(strArr, "Test1File2") < 0) { iCountErrors++; printerr("Error_167yb! Incorrect name==" + strArr[2]); } iCountTestcases++; if (Array.IndexOf(strArr, "TestFile2") < 0) { iCountErrors++; printerr("Error_49yb7! Incorrect name==" + strArr[3]); } strArr = Directory.GetFileSystemEntries(dir2.Name, "*Dir2"); iCountTestcases++; if (strArr.Length != 2) { iCountErrors++; printerr("Error_948yv! Incorrect number of files==" + strArr.Length); } for (int iLoop = 0; iLoop < strArr.Length; iLoop++) { strArr[iLoop] = Path.GetFileName(strArr[iLoop]); } iCountTestcases++; if (Array.IndexOf(strArr, "Test1Dir2") < 0) { iCountErrors++; printerr("Error_247yg! Incorrect name==" + strArr[0]); } iCountTestcases++; if (Array.IndexOf(strArr, "TestDir2") < 0) { iCountErrors++; printerr("Error_24gy7! Incorrect name==" + strArr[1]); } // [] Search criteria Beginning and ending with '*' new FileInfo(Path.Combine(dir2.FullName, "AAABB")).Create(); Directory.CreateDirectory(Path.Combine(dir2.FullName, "aaabbcc")); strArr = Directory.GetFileSystemEntries(dir2.Name, "*BB*"); iCountTestcases++; if (strArr.Length != (Interop.IsWindows ? 2 : 1)) { iCountErrors++; printerr("Error_4y190! Incorrect number of files==" + strArr.Length); } for (int iLoop = 0; iLoop < strArr.Length; iLoop++) { strArr[iLoop] = Path.GetFileName(strArr[iLoop]); } if (Interop.IsWindows) { iCountTestcases++; if (Array.IndexOf(strArr, "aaabbcc") < 0) { iCountErrors++; printerr("Error_956yb! Incorrect name==" + strArr[0]); } } iCountTestcases++; if (Array.IndexOf(strArr, "AAABB") < 0) { iCountErrors++; printerr("Error_48yg7! Incorrect name==" + strArr[1]); } // [] Should not search on fullpath // [] Search Criteria without match should return empty array strLoc = "Loc_2301"; FileInfo f = new FileInfo(Path.Combine(dir2.Name, "TestDir1", "Test.tmp")); FileStream fs6 = f.Create(); strArr = Directory.GetFileSystemEntries(dir2.Name, Path.Combine("TestDir1", "*")); iCountTestcases++; if (strArr.Length != 1) { iCountErrors++; printerr("Error_28gyb! Incorrect number of files"); } fs6.Dispose(); //if(Directory.Exists(dirName)) // Directory.Delete(dirName, true); } catch (Exception exc_general) { ++iCountErrors; printerr("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); }
public static void runTest() { String strLoc = "Loc_0001"; String strValue = String.Empty; int iCountErrors = 0; int iCountTestcases = 0; try { String fileName = Path.GetRandomFileName(); // [] Valid file name and datetime(Today) strLoc = "Loc_0006"; DirectoryInfo dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { dir2.CreationTime = DateTime.Today; if ((dir2.CreationTime - DateTime.Now).Seconds > 0) { iCountErrors++; printerr("Error_0007! Creation time cannot be correct"); } } catch (Exception exc) { iCountErrors++; printerr("Error_0008! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //Add one year from DateTime.today. strLoc = "Loc_0009"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { dir2.CreationTime = DateTime.Now.AddYears(1); if ((dir2.CreationTime - DateTime.Now.AddYears(1)).Days > 1) { iCountErrors++; printerr("Error_0010! Creation time cannot be correct"); } } catch (Exception exc) { iCountErrors++; printerr("Error_0011! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //Subtract one year from DateTime.today. strLoc = "Loc_0012"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { dir2.CreationTime = DateTime.Now.AddYears(-1); if ((dir2.CreationTime - DateTime.Now.AddYears(-1)).Seconds > 0) { iCountErrors++; printerr("Error_0013! Creation time cannot be correct"); } } catch (Exception exc) { iCountErrors++; printerr("Error_0014! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //Add one month from DateTime.today. strLoc = "Loc_0015"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { dir2.CreationTime = DateTime.Now.AddMonths(1); if ((dir2.CreationTime - DateTime.Now.AddMonths(1)).Seconds > 2) { iCountErrors++; printerr("Error_0016! Creation time cannot be correct"); } } catch (Exception exc) { iCountErrors++; printerr("Error_0017! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //Subtract one month from DateTime.today. strLoc = "Loc_0018"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { dir2.CreationTime = DateTime.Now.AddMonths(-1); if ((dir2.CreationTime - DateTime.Now.AddMonths(-1)).Seconds > 0) { iCountErrors++; printerr("Error_0019! Creation time cannot be correct"); } } catch (Exception exc) { iCountErrors++; printerr("Error_0020! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //Add one day from DateTime.today. strLoc = "Loc_0021"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { dir2.CreationTime = DateTime.Now.AddDays(1); if ((dir2.CreationTime - DateTime.Now.AddDays(1)).Seconds > 2) { iCountErrors++; printerr("Error_0022! Creation time cannot be correct"); } } catch (Exception exc) { iCountErrors++; printerr("Error_0023! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //Subtract one day from DateTime.today. strLoc = "Loc_0024"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { dir2.CreationTime = DateTime.Now.AddDays(-1); if ((dir2.CreationTime - DateTime.Now.AddDays(-1)).Seconds > 0) { iCountErrors++; printerr("Error_0025! Creation time cannot be correct"); } } catch (Exception exc) { iCountErrors++; printerr("Error_0026! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //With invalid datetime object. strLoc = "Loc_0025"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { dir2.CreationTime = new DateTime(2001, 332, 20, 50, 50, 50); iCountErrors++; printerr("Error_0026! Creation time cannot be correct"); } catch (ArgumentOutOfRangeException) { } catch (Exception exc) { iCountErrors++; printerr("Error_0027! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //With valid date and time. strLoc = "Loc_0028"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { DateTime dt = new DateTime(2001, 2, 2, 20, 20, 20); dir2.CreationTime = dt; if ((dir2.CreationTime - dt).Seconds > 0) { iCountErrors++; printerr("Error_0029! Creation time cannot be correct"); } } catch (Exception exc) { iCountErrors++; printerr("Error_0030! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); } 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); }
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); }
public static void runTest() { int iCountErrors = 0; int iCountTestcases = 0; String strLoc = "Loc_000oo"; String strValue = String.Empty; string dirName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName()); Directory.CreateDirectory(dirName); try { DirectoryInfo dir = null; FileAttributes diratt; new DirectoryInfo(dirName).Attributes = new FileAttributes(); // [] Invalid value strLoc = "Loc_09t77"; dir = new DirectoryInfo(dirName); iCountTestcases++; try { dir.Attributes = ~FileAttributes.ReadOnly; iCountErrors++; printerr("Error_78t59! Expected exception not thrown"); } catch (ArgumentException) { } catch (Exception exc) { iCountErrors++; printerr("Error_97678! Incorrect exception thrown, exc==" + exc.ToString()); } // [] Set the ReadOnly attribute strLoc = "Loc_039ux"; dir = new DirectoryInfo(dirName); dir.Attributes = FileAttributes.ReadOnly; diratt = dir.Attributes; iCountTestcases++; if ((diratt & (FileAttributes.ReadOnly | FileAttributes.Directory)) != (FileAttributes.ReadOnly | FileAttributes.Directory)) { iCountErrors++; printerr("Error_2989d! Incorrect directory attribute set"); } dir.Attributes = new FileAttributes(); // [] Set hidden only strLoc = "Loc_209cu"; dir = new DirectoryInfo(dirName); dir.Attributes = FileAttributes.Hidden; diratt = dir.Attributes; iCountTestcases++; #if TEST_WINRT // WinRT doesn't support hidden if ((diratt & (FileAttributes.Hidden | FileAttributes.Directory)) != (FileAttributes.Directory)) { #else if ((diratt & (FileAttributes.Hidden | FileAttributes.Directory)) != (FileAttributes.Hidden | FileAttributes.Directory) && Interop.IsWindows) // setting Hidden not supported on Unix { #endif iCountErrors++; printerr("Error_3091h! Incorrect direcotory attribute set"); } dir.Attributes = new FileAttributes(); // [] Set hidden and readonly strLoc = "Loc_390uv"; dir = new DirectoryInfo(dirName); dir.Attributes = FileAttributes.Hidden | FileAttributes.ReadOnly; diratt = dir.Attributes; iCountTestcases++; #if !TEST_WINRT // WinRT doesn't support hidden diratt = dir.Attributes; iCountTestcases++; if ((int)(diratt & FileAttributes.Hidden) == 0 && Interop.IsWindows) // setting Hidden not supported on Unix { iCountErrors++; printerr("Error_20x97! Hidden attribute not set"); } #endif iCountTestcases++; if ((int)(diratt & FileAttributes.ReadOnly) == 0) { iCountErrors++; printerr("Error_1990c! ReadOnly attribute not set"); } dir.Attributes = new FileAttributes(); // [] Set system strLoc = "Loc_8gy0b"; dir = new DirectoryInfo(dirName); dir.Attributes = FileAttributes.System; diratt = dir.Attributes; iCountTestcases++; #if TEST_WINRT // WinRT doesn't support system if ((diratt & (FileAttributes.System | FileAttributes.Directory)) != (FileAttributes.Directory)) { #else if ((diratt & (FileAttributes.System | FileAttributes.Directory)) != (FileAttributes.System | FileAttributes.Directory) && Interop.IsWindows) // setting System not supported on Unix { #endif iCountErrors++; printerr("Error_7t87y! System attribute not set"); } dir.Attributes = new FileAttributes(); // [] set archive strLoc = "Loc_29d88"; dir = new DirectoryInfo(dirName); dir.Attributes = FileAttributes.Archive; diratt = dir.Attributes; iCountTestcases++; #if TEST_WINRT // WinRT doesn't support archive if ((diratt & (FileAttributes.Archive | FileAttributes.Directory)) != (FileAttributes.Directory)) { #else if ((diratt & (FileAttributes.Archive | FileAttributes.Directory)) != (FileAttributes.Archive | FileAttributes.Directory) && Interop.IsWindows) // setting Archive not supported on Unix { #endif iCountErrors++; printerr("Error_f487b! System attribute not set"); } dir.Attributes = new FileAttributes(); // [] Encrypted can't be set on directory strLoc = "Loc_t78yg"; /* dir = new DirectoryInfo("."); * dir.Attributes = FileAttributes.Encrypted; * diratt = dir.Attributes; * iCountTestcases++; * if(diratt != FileAttributes.Directory) { * iCountErrors++; * printerr( "Error_09828! System attribute not set"); * } * dir.Attributes = new FileAttributes(); */ // [] Normal can't be set on directory strLoc = "Loc_90v77"; dir = new DirectoryInfo(dirName); dir.Attributes = FileAttributes.Normal; diratt = dir.Attributes; iCountTestcases++; if ((diratt & FileAttributes.Directory) == 0) { iCountErrors++; printerr("Error_2t09b! System attribute not set"); } dir.Attributes = new FileAttributes(); /* // [] Temporary can't be set on directory * * strLoc = "Loc_99g94"; * * dir = new DirectoryInfo("."); * iCountTestcases++; * try { * dir.Attributes = FileAttributes.Temporary; * iCountErrors++; * printerr( "Error_247tb! Expected exception not thrown"); * } catch (ArgumentException aexc) { * } catch (Exception exc) { * iCountErrors++; * printerr( "Error_758bh! Incorrect exception thrown, exc=="+exc.ToString()); * } * dir.Attributes = new FileAttributes(); */ // [] Sparsefile can't be set on directory strLoc = "Loc_8gy0b"; dir = new DirectoryInfo(dirName); dir.Attributes = FileAttributes.SparseFile; diratt = dir.Attributes; iCountTestcases++; if ((diratt & (FileAttributes.Directory)) == 0) { iCountErrors++; printerr("Error_1300g! SparseFile attribute not set"); } dir.Attributes = new FileAttributes(); // [] ReparsePoint can't be set on directory strLoc = "Loc_9180c"; dir = new DirectoryInfo(dirName); dir.Attributes = FileAttributes.ReparsePoint; diratt = dir.Attributes; iCountTestcases++; if ((diratt & (FileAttributes.Directory)) == 0) { iCountErrors++; printerr("Error_0199c! ReparsePoint attribute not set"); } dir.Attributes = new FileAttributes(); // [] Compressed can't be set on directory strLoc = "Loc_0200d"; dir = new DirectoryInfo(dirName); dir.Attributes = FileAttributes.Compressed; diratt = dir.Attributes; iCountTestcases++; if ((diratt & (FileAttributes.Directory)) == 0) { iCountErrors++; printerr("Error_9010c! Compressed attribute not set"); } dir.Attributes = new FileAttributes(); // [] Offline iCountTestcases++; /* Does not work on FAT filesystems * strLoc = "Loc_109tg"; * * dir = new DirectoryInfo("."); * dir.Attributes = FileAttributes.Offline; * diratt = dir.Attributes; * iCountTestcases++; * if(diratt != (FileAttributes.Offline|FileAttributes.Directory)) { * iCountErrors++; * printerr( "Error_010vy! Offline attribute not set"); * } * dir.Attributes = new FileAttributes(); * * * // [] NotContectIndexed * * strLoc = "Loc_t0698"; * * dir = new DirectoryInfo("."); * dir.Attributes = FileAttributes.NotContentIndexed; * diratt = dir.Attributes; * iCountTestcases++; * if(diratt != (FileAttributes.NotContentIndexed|FileAttributes.Directory)) { * iCountErrors++; * printerr( "Error_23047! NotContentIndexed attribute not set"); * } * dir.Attributes = new FileAttributes(); */ } 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()); } FailSafeDirectoryOperations.DeleteDirectory(dirName, true); Assert.Equal(0, iCountErrors); }
public static void runTest() { String strLoc = "Loc_0001"; String strValue = String.Empty; int iCountErrors = 0; int iCountTestcases = 0; try { String fileName = "SetLastWriteTime_str_dt_test_TestDirectory"; // [] With null string iCountTestcases++; try { Directory.SetLastWriteTime(null, DateTime.Today); iCountErrors++; printerr("Error_0002! Expected exception not thrown"); } catch (ArgumentNullException) { } catch (Exception exc) { iCountErrors++; printerr("Error_0003! Unexpected exceptiont thrown: " + exc.ToString()); } // [] With an empty string. iCountTestcases++; try { Directory.SetLastWriteTime("", DateTime.Today); iCountErrors++; printerr("Error_0004! Expected exception not thrown"); } catch (ArgumentException) { } catch (Exception exc) { iCountErrors++; printerr("Error_0005! Unexpected exceptiont thrown: " + exc.ToString()); } // [] Valid file name and datetime(Today) strLoc = "Loc_0006"; DirectoryInfo dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { Directory.SetLastWriteTime(fileName, DateTime.Today); if ((Directory.GetLastWriteTime(fileName) - DateTime.Now).Seconds > 0) { iCountErrors++; printerr("Error_0007! Creation time cannot be correct"); } } catch (Exception exc) { iCountErrors++; printerr("Error_0008! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //Add one year from DateTime.today. strLoc = "Loc_0009"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { Directory.SetLastWriteTime(fileName, DateTime.Now.AddYears(1)); int iSeconds = (Directory.GetLastWriteTime(fileName) - DateTime.Now.AddYears(1)).Seconds; if (iSeconds > 2) { iCountErrors++; Console.WriteLine("Error_0010! Creation time cannot be correct, Expected....(<2), Actual....{0}", iSeconds); } } catch (Exception exc) { iCountErrors++; printerr("Error_0011! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //Subtract one year from DateTime.today. strLoc = "Loc_0012"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { Directory.SetLastWriteTime(fileName, DateTime.Now.AddYears(-1)); int iSeconds = (Directory.GetLastAccessTime(fileName) - DateTime.Now.AddMonths(-1)).Seconds; if (iSeconds > 60) { iCountErrors++; printerr("Error_0013! Creation time cannot be correct" + iSeconds); } } catch (Exception exc) { iCountErrors++; printerr("Error_0014! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //Add one month from DateTime.today. strLoc = "Loc_0015"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { Directory.SetLastWriteTime(fileName, DateTime.Now.AddMonths(1)); int iSeconds = (Directory.GetLastWriteTime(fileName) - DateTime.Now.AddMonths(1)).Seconds; if (iSeconds > 2) { iCountErrors++; Console.WriteLine("Error_0016! Creation time cannot be correct, Expected....(<2), Actual....{0}", iSeconds); } } catch (Exception exc) { iCountErrors++; printerr("Error_0017! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //Subtract one month from DateTime.today. strLoc = "Loc_0018"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { Directory.SetLastWriteTime(fileName, DateTime.Now.AddMonths(-1)); int iSeconds = (Directory.GetLastAccessTime(fileName) - DateTime.Now.AddMonths(-1)).Seconds; if (iSeconds > 60) { iCountErrors++; printerr("Error_0019! Creation time cannot be correct" + iSeconds); } } catch (Exception exc) { iCountErrors++; printerr("Error_0020! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //Add one day from DateTime.today. strLoc = "Loc_0021"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { Directory.SetLastWriteTime(fileName, DateTime.Now.AddDays(1)); int iSeconds = (Directory.GetLastWriteTime(fileName) - DateTime.Now.AddDays(1)).Seconds; if (iSeconds > 2) { iCountErrors++; Console.WriteLine("Error_0022! Creation time cannot be correct, Expected....(<2), Actual....{0}", iSeconds); } } catch (Exception exc) { iCountErrors++; printerr("Error_0023! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //Subtract one day from DateTime.today. strLoc = "Loc_0024"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { Directory.SetLastWriteTime(fileName, DateTime.Now.AddDays(-1)); int iSeconds = (Directory.GetLastAccessTime(fileName) - DateTime.Now.AddMonths(-1)).Seconds; if (iSeconds > 60) { iCountErrors++; printerr("Error_0025! Creation time cannot be correct" + iSeconds); } } catch (Exception exc) { iCountErrors++; printerr("Error_0026! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //With invalid datetime object. strLoc = "Loc_0025"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { Directory.SetLastWriteTime(fileName, new DateTime(2001, 332, 20, 50, 50, 50)); iCountErrors++; printerr("Error_0026! Creation time cannot be correct"); } catch (ArgumentOutOfRangeException) { } catch (Exception exc) { iCountErrors++; printerr("Error_0027! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); //With valid date and time. strLoc = "Loc_0028"; dir2 = new DirectoryInfo(fileName); dir2.Create(); iCountTestcases++; try { DateTime dt = new DateTime(2001, 2, 2, 20, 20, 20); Directory.SetLastWriteTime(fileName, dt); if ((Directory.GetLastWriteTime(fileName) - dt).Seconds > 60) { iCountErrors++; printerr("Error_0029! Creation time cannot be correct"); } } catch (Exception exc) { iCountErrors++; printerr("Error_0030! Unexpected exceptiont thrown: " + exc.ToString()); } FailSafeDirectoryOperations.DeleteDirectoryInfo(dir2, false); } catch (Exception exc_general) { ++iCountErrors; printerr("Error Err_0100! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString()); } //// Finish Diagnostics if (iCountErrors != 0) { Console.WriteLine("FAiL! " + s_strTFName + " ,iCountErrors==" + iCountErrors.ToString()); } Assert.Equal(0, iCountErrors); }
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); }
public static void runTest() { int iCountErrors = 0; int iCountTestcases = 0; String strLoc = "Loc_000oo"; String strValue = String.Empty; string dirName = Path.Combine(Directory.GetCurrentDirectory(), Path.GetRandomFileName()); Directory.CreateDirectory(dirName); try { DirectoryInfo dir = null; // [] Do the current dir strLoc = "Loc_909d9"; dir = new DirectoryInfo(dirName); iCountTestcases++; if (!dir.ToString().Equals(Path.GetFileName(dirName))) { iCountErrors++; printerr("Error_209xu! Incorrect Directory returned , dir==" + dir.ToString()); Console.WriteLine(dirName); Console.WriteLine(dir == null); Console.WriteLine(dir); } // [] Root drive strLoc = "Loc_20u9x"; dir = new DirectoryInfo(Path.GetPathRoot(Directory.GetCurrentDirectory())); iCountTestcases++; if (!dir.ToString().Equals(Path.GetPathRoot(Directory.GetCurrentDirectory()))) { iCountErrors++; printerr("Error_2098x! Incorrect dir returned==" + dir.ToString()); } string testDir = Path.Combine(dirName, "TestDir"); Directory.CreateDirectory(testDir); dir = new DirectoryInfo(testDir); iCountTestcases++; if (!dir.FullName.Equals(testDir)) { iCountErrors++; printerr("Error_298yx! Incorrect dir constructed, dir==" + dir.ToString()); } dir.Delete(); // [] Whitespace string strLoc = "Loc_298yb"; if (Interop.IsWindows) // whitespace-only names are valid on Unix { iCountTestcases++; try { dir = new DirectoryInfo(" "); dir.Create(); iCountErrors++; printerr("Error_09rux! Expected exception not thrown, dir==" + dir.ToString()); } catch (ArgumentException) { } catch (Exception exc) { iCountErrors++; printerr("Bug 14866 Error_4577c! Incorrect exception thrown, exc==" + exc.ToString()); } } // [] Go for same dir strLoc = "Loc_949gg"; dir = new DirectoryInfo("."); iCountTestcases++; if (!dir.FullName.Equals(Directory.GetCurrentDirectory())) { iCountErrors++; printerr("Error_299xu! Incorrect dir constructed, dir==" + dir.ToString()); } // [] go one dir up strLoc = "Loc_9937a"; dir = new DirectoryInfo(".."); iCountTestcases++; if (!dir.FullName.Equals(Path.GetDirectoryName(Directory.GetCurrentDirectory()))) { iCountErrors++; printerr("Error_298xy! Incorrect dir returned, dir==" + dir.ToString()); } } 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()); } FailSafeDirectoryOperations.DeleteDirectory(dirName, true); Assert.Equal(0, iCountErrors); }