Example #1
0
      private void DumpFileTrailingDotSpace(bool isLocal)
      {
         Console.WriteLine("\n=== TEST {0} ===\n", isLocal ? Local : Network);
         const string characterDot = ".";
         const string characterSpace = " ";
         string random = Path.GetRandomFileName();
         string tempPathDot = Path.GetTempPath("File.Create()-" + random + "-file-with-dot-" + characterDot);
         string tempPathSpace = Path.GetTempPath("File.Create()-" + random + "-file-with-space-" + characterSpace);
         if (!isLocal) tempPathDot = Path.LocalToUnc(tempPathDot);
         if (!isLocal) tempPathSpace = Path.LocalToUnc(tempPathSpace);

         Console.WriteLine("Input File Path (with dot)  : [{0}]", tempPathDot);
         Console.WriteLine("Input File Path (with space): [{0}]", tempPathSpace);

         Assert.IsTrue(tempPathDot.EndsWith(characterDot), "Path should have a trailing dot.");
         Assert.IsTrue(tempPathSpace.EndsWith(characterSpace), "Path should have a trailing space.");

         #region Path.GetFullPath(), Path Normalization

         string sysIo = System.IO.Path.GetFullPath(tempPathDot);
         Assert.IsFalse(sysIo.EndsWith(characterDot), "Path should not have a trailing dot.");

         sysIo = System.IO.Path.GetFullPath(tempPathSpace);
         Assert.IsFalse(sysIo.EndsWith(characterSpace), "Path should not have a trailing space.");


         string alphaFs = Path.GetFullPath(tempPathDot);
         Assert.IsFalse(alphaFs.EndsWith(characterDot), "Path should not have a trailing dot.");

         alphaFs = Path.GetFullPath(tempPathSpace);
         Assert.IsFalse(alphaFs.EndsWith(characterSpace), "Path should not have a trailing space.");


         Assert.AreEqual(sysIo, alphaFs, "Paths should be the same.");

         #endregion // Path.GetFullPath(), Path Normalization

         #region Path.GetLongPath(), No Path Normalization

         alphaFs = Path.GetLongPath(tempPathDot);
         Assert.IsTrue(alphaFs.EndsWith(characterDot), "Path should have a trailing dot.");

         alphaFs = Path.GetLongPath(tempPathSpace);
         Assert.IsTrue(alphaFs.EndsWith(characterSpace), "Path should have a trailing space.");

         Assert.AreNotEqual(alphaFs, sysIo, "Paths should not be the same.");

         #endregion // Path.GetLongPath(), No Path Normalization

         #region Path.GetRegularPath(), No Path Normalization

         alphaFs = Path.GetRegularPath(tempPathDot);
         Assert.IsTrue(alphaFs.EndsWith(characterDot), "Path should have a trailing dot.");

         alphaFs = Path.GetRegularPath(tempPathSpace);
         Assert.IsTrue(alphaFs.EndsWith(characterSpace), "Path should have a trailing space.");

         Assert.AreNotEqual(alphaFs, sysIo, "Paths should not be the same.");

         #endregion // Path.GetRegularPath(), No Path Normalization

         Console.WriteLine();

         #region File() Class

         StopWatcher(true);

         #region TrailingDot

         #region System.IO

         // tempPathDot contains a trailing dot but gets stripped on path normalization.
         // System.IO handles the file without the trailing dot. Therefore, the file exists.
         // AlphaFS has the same behaviour as .NET for default methods.

         using (FileStream fs = System.IO.File.Create(tempPathDot)) { fs.WriteByte(1); }
         Assert.IsTrue(System.IO.File.Exists(tempPathDot), "File should exist.");
         Assert.IsTrue(File.Exists(tempPathDot), "File should be visible to AlphaFS.");
         Assert.IsFalse(File.Exists(tempPathDot, PathFormat.FullPath), "File should be invisible to AlphaFS.");

         using (StreamWriter sw = System.IO.File.AppendText(tempPathDot))
            sw.WriteLine(TextHelloWorld);

         string lineSysIo;
         using (StreamReader sr = System.IO.File.OpenText(tempPathDot))
            lineSysIo = sr.ReadToEnd();

         System.IO.File.Delete(tempPathDot);
         Assert.IsFalse(System.IO.File.Exists(tempPathDot), "File should not exist.");

         #endregion // System.IO

         #region AlphaFS

         using (FileStream fs = File.Create(tempPathDot, PathFormat.FullPath)) { fs.WriteByte(1); } // Create file without path normalization.
         Assert.IsTrue(File.Exists(tempPathDot, PathFormat.FullPath), "File should exist and visible to AlphaFS.");
         Assert.IsFalse(System.IO.File.Exists(tempPathDot), "File should be invisible to System.IO.");

         using (StreamWriter sw = File.AppendText(tempPathDot, PathFormat.FullPath))
            sw.WriteLine(TextHelloWorld);

         string lineAlphaFs;
         using (StreamReader sr = File.OpenText(tempPathDot, PathFormat.FullPath))
            lineAlphaFs = sr.ReadToEnd();

         File.Delete(tempPathDot, true, PathFormat.FullPath); // Delete file without path normalization.
         Assert.IsFalse(File.Exists(tempPathDot, PathFormat.FullPath), "File should not exist.");

         #endregion // AlphaFS

         Assert.AreEqual(lineSysIo, lineAlphaFs);

         #endregion // TrailingDot

         #region TrailingSpace

         #region System.IO

         // tempPathSpace contains a trailing space but gets stripped on path normalization.
         // System.IO handles the file without the trailing space. Therefore, the file exists.
         // AlphaFS has the same behaviour as .NET for default methods.

         using (FileStream fs = System.IO.File.Create(tempPathSpace)) { fs.WriteByte(1); }
         Assert.IsTrue(System.IO.File.Exists(tempPathSpace), "File should exist.");
         Assert.IsTrue(File.Exists(tempPathSpace), "File should be visible to AlphaFS.");
         Assert.IsFalse(File.Exists(tempPathSpace, PathFormat.FullPath), "File should be invisible to AlphaFS.");

         using (StreamWriter sw = System.IO.File.AppendText(tempPathSpace))
            sw.WriteLine(TextHelloWorld);

         using (StreamReader sr = System.IO.File.OpenText(tempPathSpace))
            lineSysIo = sr.ReadToEnd();

         System.IO.File.Delete(tempPathSpace);
         Assert.IsFalse(System.IO.File.Exists(tempPathSpace), "File should not exist.");

         #endregion // System.IO

         #region AlphaFS

         using (FileStream fs = File.Create(tempPathSpace, PathFormat.FullPath)) { fs.WriteByte(1); } // Create file without path normalization.
         Assert.IsTrue(File.Exists(tempPathSpace, PathFormat.FullPath), "File should exist and visible to AlphaFS.");
         Assert.IsFalse(System.IO.File.Exists(tempPathSpace), "File should be invisible to System.IO.");

         using (StreamWriter sw = File.AppendText(tempPathSpace, PathFormat.FullPath))
            sw.WriteLine(TextHelloWorld);

         using (StreamReader sr = File.OpenText(tempPathSpace, PathFormat.FullPath))
            lineAlphaFs = sr.ReadToEnd();

         File.Delete(tempPathSpace, true, PathFormat.FullPath); // Delete file without path normalization.
         Assert.IsFalse(File.Exists(tempPathSpace, PathFormat.FullPath), "File should not exist.");

         #endregion // AlphaFS

         Assert.AreEqual(lineSysIo, lineAlphaFs);

         #endregion // TrailingSpace

         Console.WriteLine("\tClass File(){0}", Reporter());

         #endregion // File() Class

         #region FileInfo() Class

         StopWatcher(true);

         #region TrailingDot

         #region System.IO

         // tempPathDot contains a trailing dot but gets stripped on path normalization.
         // System.IO handles the file without the trailing dot. Therefore, the file exists.
         // AlphaFS has the same behaviour as .NET for default methods.

         System.IO.FileInfo sysIoFi = new System.IO.FileInfo(tempPathDot);
         Assert.IsTrue(sysIoFi.Name.EndsWith(characterDot), "Path should have a trailing dot.");

         using (FileStream fs = sysIoFi.Create())
         {
            fs.WriteByte(100);

            Assert.IsTrue(sysIoFi.Exists, "File should exist.");
            Assert.IsTrue(File.Exists(tempPathDot), "File should be visible to AlphaFS.");
            Assert.IsFalse(File.Exists(tempPathDot, PathFormat.FullPath), "File should be invisible to AlphaFS.");
         }

         using (StreamWriter sw = sysIoFi.AppendText())
            sw.WriteLine(TextHelloWorld);

         using (StreamReader sr = System.IO.File.OpenText(tempPathDot))
            lineSysIo = sr.ReadToEnd();

         sysIoFi.Delete();
         Assert.IsFalse(System.IO.File.Exists(tempPathDot), "File should not exist.");

         #endregion // System.IO

         #region AlphaFS

         FileInfo alphaFsFi = new FileInfo(tempPathDot, PathFormat.FullPath);
         Assert.IsTrue(alphaFsFi.Name.EndsWith(characterDot), "Path should have a trailing dot.");

         using (FileStream fs = alphaFsFi.Create())
         {
            fs.WriteByte(100);

            Assert.IsTrue(alphaFsFi.Exists, "File should exist.");
            Assert.IsTrue(File.Exists(tempPathDot, PathFormat.FullPath), "File should be visible to AlphaFS.");
            Assert.IsFalse(File.Exists(tempPathDot), "File should be invisible to AlphaFS.");
         }

         using (StreamWriter sw = alphaFsFi.AppendText())
            sw.WriteLine(TextHelloWorld);

         using (StreamReader sr = File.OpenText(tempPathDot, PathFormat.FullPath))
            lineAlphaFs = sr.ReadToEnd();

         alphaFsFi.Delete();
         alphaFsFi.Refresh(); // Must Refresh() to get actual state.
         Assert.IsFalse(File.Exists(tempPathDot, PathFormat.FullPath), "File should not exist.");

         #endregion // AlphaFS

         Assert.AreEqual(lineSysIo, lineAlphaFs);

         #endregion // TrailingDot

         #region TrailingSpace

         #region System.IO

         // tempPathSpace contains a trailing space but gets stripped on path normalization.
         // System.IO handles the file without the trailing space. Therefore, the file exists.
         // AlphaFS has the same behaviour as .NET for default methods.

         sysIoFi = new System.IO.FileInfo(tempPathSpace);
         Assert.IsTrue(sysIoFi.Name.EndsWith(characterSpace), "Path should have a trailing space.");

         using (FileStream fs = sysIoFi.Create())
         {
            fs.WriteByte(100);

            Assert.IsTrue(sysIoFi.Exists, "File should exist.");
            Assert.IsTrue(File.Exists(tempPathSpace), "File should be visible to AlphaFS.");
            Assert.IsFalse(File.Exists(tempPathSpace, PathFormat.FullPath), "File should be invisible to AlphaFS.");
         }

         using (StreamWriter sw = sysIoFi.AppendText())
            sw.WriteLine(TextHelloWorld);

         using (StreamReader sr = System.IO.File.OpenText(tempPathSpace))
            lineSysIo = sr.ReadToEnd();

         sysIoFi.Delete();
         Assert.IsFalse(System.IO.File.Exists(tempPathSpace), "File should not exist.");

         #endregion // System.IO

         #region AlphaFS

         alphaFsFi = new FileInfo(tempPathSpace, PathFormat.FullPath);
         Assert.IsTrue(alphaFsFi.Name.EndsWith(characterSpace), "Path should have a trailing space.");

         using (FileStream fs = alphaFsFi.Create())
         {
            fs.WriteByte(100);

            Assert.IsTrue(alphaFsFi.Exists, "File should exist.");
            Assert.IsTrue(File.Exists(tempPathSpace, PathFormat.FullPath), "File should be visible to AlphaFS.");
            Assert.IsFalse(File.Exists(tempPathSpace), "File should be invisible to AlphaFS.");
         }

         using (StreamWriter sw = alphaFsFi.AppendText())
            sw.WriteLine(TextHelloWorld);

         using (StreamReader sr = File.OpenText(tempPathSpace, PathFormat.FullPath))
            lineAlphaFs = sr.ReadToEnd();

         alphaFsFi.Delete();
         alphaFsFi.Refresh(); // Must Refresh() to get actual state.
         Assert.IsFalse(File.Exists(tempPathSpace, PathFormat.FullPath), "File should not exist.");

         #endregion // AlphaFS

         Assert.AreEqual(lineSysIo, lineAlphaFs);

         #endregion // TrailingSpace

         Console.WriteLine("\tClass FileInfo(){0}", Reporter());

         #endregion // FileInfo() Class

         Console.WriteLine();
      }
Example #2
0
      private void DumpGetXxxTimeXxx(bool isLocal)
      {
         #region Setup

         Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network);
         string path = NotepadExe;
         if (!isLocal) path = Path.LocalToUnc(path);

         Console.WriteLine("\nInput File Path: [{0}]\n", path);

         #endregion // Setup

         StopWatcher(true);

         #region GetCreationTimeXxx

         DateTime actual = File.GetCreationTime(path);
         DateTime expected = System.IO.File.GetCreationTime(path);
         Console.WriteLine("\tGetCreationTime()     : [{0}]    System.IO: [{1}]", actual, expected);
         Assert.AreEqual(expected, actual, "AlphaFS != System.IO");

         actual = File.GetCreationTimeUtc(path);
         expected = System.IO.File.GetCreationTimeUtc(path);
         Console.WriteLine("\tGetCreationTimeUtc()  : [{0}]    System.IO: [{1}]\n", actual, expected);
         Assert.AreEqual(expected, actual, "AlphaFS != System.IO");

         #endregion // GetCreationTimeXxx

         #region GetLastAccessTimeXxx

         actual = File.GetLastAccessTime(path);
         expected = System.IO.File.GetLastAccessTime(path);
         Console.WriteLine("\tGetLastAccessTime()   : [{0}]    System.IO: [{1}]", actual, expected);
         Assert.AreEqual(expected, actual, "AlphaFS != System.IO");

         actual = File.GetLastAccessTimeUtc(path);
         expected = System.IO.File.GetLastAccessTimeUtc(path);
         Console.WriteLine("\tGetLastAccessTimeUtc(): [{0}]    System.IO: [{1}]\n", actual, expected);
         Assert.AreEqual(expected, actual, "AlphaFS != System.IO");

         #endregion // GetLastAccessTimeXxx

         #region GetLastWriteTimeXxx

         actual = File.GetLastWriteTime(path);
         expected = System.IO.File.GetLastWriteTime(path);
         Console.WriteLine("\tGetLastWriteTime()    : [{0}]    System.IO: [{1}]", actual, expected);
         Assert.AreEqual(expected, actual, "AlphaFS != System.IO");

         actual = File.GetLastWriteTimeUtc(path);
         expected = System.IO.File.GetLastWriteTimeUtc(path);
         Console.WriteLine("\tGetLastWriteTimeUtc() : [{0}]    System.IO: [{1}]\n", actual, expected);
         Assert.AreEqual(expected, actual, "AlphaFS != System.IO");

         #endregion // GetLastWriteTimeXxx


         #region GetChangeTimeXxx

         Console.WriteLine("\tGetChangeTime()       : [{0}]    System.IO: [N/A]", File.GetChangeTime(path));
         Console.WriteLine("\tGetChangeTimeUtc()    : [{0}]    System.IO: [N/A]", File.GetChangeTimeUtc(path));

         #endregion // GetChangeTimeXxx

         Console.WriteLine();
         Console.WriteLine(Reporter());
         Console.WriteLine();

         #region Trigger GetChangeTimeXxx

         // We can not compare ChangeTime against .NET because it does not exist.
         // Creating a file and renaming it triggers ChangeTime, so test for that.

         path = Path.GetTempPath("File-GetChangeTimeXxx()-file-" + Path.GetRandomFileName());
         if (!isLocal) path = Path.LocalToUnc(path);

         FileInfo fi = new FileInfo(path);
         using (fi.Create()) { }
         string fileName = fi.Name;

         DateTime lastAccessTimeActual = File.GetLastAccessTime(path);
         DateTime lastAccessTimeUtcActual = File.GetLastAccessTimeUtc(path);

         DateTime changeTimeActual = File.GetChangeTime(path);
         DateTime changeTimeUtcActual = File.GetChangeTimeUtc(path);

         Console.WriteLine("\nTesting ChangeTime on a temp file.");
         Console.WriteLine("\nInput File Path: [{0}]\n", path);
         Console.WriteLine("\tGetChangeTime()       : [{0}]\t", changeTimeActual);
         Console.WriteLine("\tGetChangeTimeUtc()    : [{0}]\t", changeTimeUtcActual);

         fi.MoveTo(fi.FullName.Replace(fileName, fileName + "-Renamed"));

         // Pause for at least a second so that the difference in time can be seen.
         int sleep = new Random().Next(2000, 4000);
         Thread.Sleep(sleep);

         fi.MoveTo(fi.FullName.Replace(fileName + "-Renamed", fileName));

         DateTime lastAccessTimeExpected = File.GetLastAccessTime(path);
         DateTime lastAccessTimeUtcExpected = File.GetLastAccessTimeUtc(path);
         DateTime changeTimeExpected = File.GetChangeTime(path);
         DateTime changeTimeUtcExpected = File.GetChangeTimeUtc(path);

         Console.WriteLine("\nTrigger ChangeTime by renaming the file.");
         Console.WriteLine("For Unit Test, ChangeTime should differ approximately: [{0}] seconds.\n", sleep / 1000);
         Console.WriteLine("\tGetChangeTime()       : [{0}]\t", changeTimeExpected);
         Console.WriteLine("\tGetChangeTimeUtc()    : [{0}]\t\n", changeTimeUtcExpected);


         Assert.AreNotEqual(changeTimeActual, changeTimeExpected);
         Assert.AreNotEqual(changeTimeUtcActual, changeTimeUtcExpected);

         Assert.AreEqual(lastAccessTimeExpected, lastAccessTimeActual);
         Assert.AreEqual(lastAccessTimeUtcExpected, lastAccessTimeUtcActual);

         #endregion // Trigger GetChangeTimeXxx


         fi.Delete();
         fi.Refresh(); // Must Refresh() to get actual state.
         Assert.IsFalse(fi.Exists, "Cleanup failed: File should have been removed.");
         Console.WriteLine();
      }