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() { 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 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); }