Example #1
0
        private void File_Copy_WithProgress(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);
            Console.WriteLine();


            var tempPath = UnitTestConstants.TempFolder;

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, MethodBase.GetCurrentMethod().Name))
            {
                // Min: 1 bytes, Max: 10485760 = 10 MB.
                var fileLength = new Random().Next(1, 10485760);
                var fileSource = UnitTestConstants.CreateFile(rootDir.Directory.FullName, fileLength);
                var fileCopy   = rootDir.RandomFileFullPath;

                Console.WriteLine("Src File Path: [{0:N0} ({1}) [{2}]", fileLength, Alphaleonis.Utils.UnitSizeToText(fileLength), fileSource);
                Console.WriteLine("Dst File Path: [{0}]", fileCopy);

                Assert.IsTrue(System.IO.File.Exists(fileSource.FullName), "The file does not exists, but is expected to.");



                // Allow copy to overwrite an existing file.
                const Alphaleonis.Win32.Filesystem.CopyOptions copyOptions = Alphaleonis.Win32.Filesystem.CopyOptions.None;

                // The copy progress handler.
                var callback = new Alphaleonis.Win32.Filesystem.CopyMoveProgressRoutine(FileCopyProgressHandler);

                Console.WriteLine();


                // You can pass any piece of data to userProgressData. This data can be accessed from the callback method.
                // Specify file length for assertion.
                var userProgressData = fileLength;


                var copyResult = Alphaleonis.Win32.Filesystem.File.Copy(fileSource.FullName, fileCopy, copyOptions, callback, userProgressData);

                UnitTestConstants.Dump(copyResult, -18);


                Assert.IsTrue(System.IO.File.Exists(fileCopy), "The file does not exists, but is expected to.");


                var fileLen = new System.IO.FileInfo(fileCopy).Length;

                Assert.AreEqual(fileLength, fileLen, "The file copy is: {0} bytes, but is expected to be: {1} bytes.", fileLen, fileLength);

                Assert.IsTrue(System.IO.File.Exists(fileSource.FullName), "The original file does not exist, but is expected to.");
            }


            Console.WriteLine();
        }
        private void AlphaFS_Directory_Copy_ExistingDirectory_WithProgress(bool isNetwork)
        {
            using (var tempRoot = new TemporaryDirectory(isNetwork))
            {
                var folderSrc = tempRoot.CreateRecursiveTree(5);
                var folderDst = tempRoot.RandomDirectoryFullPath;

                Console.WriteLine("Src Directory Path: [{0}]", folderSrc.FullName);
                Console.WriteLine("Dst Directory Path: [{0}]", folderDst);


                var dirEnumOptions = Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.FilesAndFolders | Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.Recursive;

                var props = Alphaleonis.Win32.Filesystem.Directory.GetProperties(folderSrc.FullName, dirEnumOptions);

                var sourceTotal      = props["Total"];
                var sourceTotalFiles = props["File"];
                var sourceTotalSize  = props["Size"];

                Console.WriteLine("\n\tTotal size: [{0}] - Total Folders: [{1}] - Files: [{2}]", Alphaleonis.Utils.UnitSizeToText(sourceTotalSize), sourceTotal - sourceTotalFiles, sourceTotalFiles);


                // The copy progress handler.
                var callback = new Alphaleonis.Win32.Filesystem.CopyMoveProgressRoutine(FolderCopyProgressHandler);

                Console.WriteLine();


                var copyResult = Alphaleonis.Win32.Filesystem.Directory.Copy(folderSrc.FullName, folderDst, Alphaleonis.Win32.Filesystem.CopyOptions.FailIfExists, callback, null);

                UnitTestConstants.Dump(copyResult, -18);


                props = Alphaleonis.Win32.Filesystem.Directory.GetProperties(folderDst, dirEnumOptions);
                Assert.AreEqual(sourceTotal, props["Total"], "The number of total file system objects do not match.");
                Assert.AreEqual(sourceTotalFiles, props["File"], "The number of total files do not match.");
                Assert.AreEqual(sourceTotalSize, props["Size"], "The total file size does not match.");
                Assert.AreNotEqual(null, copyResult);


                // Test against copyResult results.

                Assert.AreEqual(sourceTotal, copyResult.TotalFolders + copyResult.TotalFiles, "The number of total file system objects do not match.");
                Assert.AreEqual(sourceTotalFiles, copyResult.TotalFiles, "The number of total files do not match.");
                Assert.AreEqual(sourceTotalSize, copyResult.TotalBytes, "The total file size does not match.");
                Assert.IsTrue(copyResult.IsCopy);
                Assert.IsFalse(copyResult.IsMove);
                Assert.IsTrue(copyResult.IsDirectory);
                Assert.IsFalse(copyResult.IsFile);

                Assert.IsTrue(System.IO.Directory.Exists(folderSrc.FullName), "The original directory does not exist, but is expected to.");
            }


            Console.WriteLine();
        }
Example #3
0
        private void AlphaFS_File_Copy_ExistingFile_WithProgress(bool isNetwork)
        {
            using (var tempRoot = new TemporaryDirectory(isNetwork))
            {
                var fileSource = tempRoot.CreateFile();
                var fileCopy   = tempRoot.CreateFile();

                Console.WriteLine("Src File Path: [{0}] [{1}]", Alphaleonis.Utils.UnitSizeToText(fileSource.Length), fileSource.FullName);
                Console.WriteLine("Dst File Path: [{0}]", fileCopy.FullName);


                // Allow copy to overwrite an existing file.
                const Alphaleonis.Win32.Filesystem.CopyOptions copyOptions = Alphaleonis.Win32.Filesystem.CopyOptions.None;


                // The copy progress handler.
                var callback = new Alphaleonis.Win32.Filesystem.CopyMoveProgressRoutine(FileCopyProgressHandler);


                Console.WriteLine();


                // You can pass any piece of data to userProgressData. This data can be accessed from the callback method.
                // Specify file length for assertion.
                var userProgressData = fileSource.Length;


                var copyResult = Alphaleonis.Win32.Filesystem.File.Copy(fileSource.FullName, fileCopy.FullName, copyOptions, callback, userProgressData);

                UnitTestConstants.Dump(copyResult);


                Assert.IsTrue(System.IO.File.Exists(fileCopy.FullName), "The file does not exists, but is expected to.");


                Assert.AreEqual(fileSource.Length, fileCopy.Length, "The file sizes do no match, but are expected to.");

                Assert.IsTrue(System.IO.File.Exists(fileSource.FullName), "The original file does not exist, but is expected to.");
            }

            Console.WriteLine();
        }
Example #4
0
        private void Directory_Copy_WithProgress(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);
            Console.WriteLine();


            var tempPath = System.IO.Path.GetTempPath();

            if (isNetwork)
            {
                tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);
            }


            using (var rootDir = new TemporaryDirectory(tempPath, MethodBase.GetCurrentMethod().Name))
            {
                var folderSrc = System.IO.Directory.CreateDirectory(System.IO.Path.Combine(rootDir.Directory.FullName, "Source Folder"));
                var folderDst = System.IO.Directory.CreateDirectory(System.IO.Path.Combine(rootDir.Directory.FullName, "Destination Folder"));

                Console.WriteLine("Src Directory Path: [{0}]", folderSrc.FullName);
                Console.WriteLine("Dst Directory Path: [{0}]", folderDst.FullName);

                UnitTestConstants.CreateDirectoriesAndFiles(folderSrc.FullName, new Random().Next(5, 15), false, false, true);


                var dirEnumOptions = Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.FilesAndFolders | Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.Recursive;

                var props = Alphaleonis.Win32.Filesystem.Directory.GetProperties(folderSrc.FullName, dirEnumOptions);

                var sourceTotal      = props["Total"];
                var sourceTotalFiles = props["File"];
                var sourceTotalSize  = props["Size"];

                Console.WriteLine("\n\tTotal size: [{0}] - Total Folders: [{1}] - Files: [{2}]", Alphaleonis.Utils.UnitSizeToText(sourceTotalSize), sourceTotal - sourceTotalFiles, sourceTotalFiles);



                // Allow copy to overwrite an existing file.
                const Alphaleonis.Win32.Filesystem.CopyOptions copyOptions = Alphaleonis.Win32.Filesystem.CopyOptions.None;

                // The copy progress handler.
                var callback = new Alphaleonis.Win32.Filesystem.CopyMoveProgressRoutine(FolderCopyProgressHandler);

                Console.WriteLine();


                var copyResult = Alphaleonis.Win32.Filesystem.Directory.Copy(folderSrc.FullName, folderDst.FullName, copyOptions, callback, null);

                UnitTestConstants.Dump(copyResult, -18);


                props = Alphaleonis.Win32.Filesystem.Directory.GetProperties(folderDst.FullName, dirEnumOptions);
                Assert.AreEqual(sourceTotal, props["Total"], "The number of total file system objects do not match.");
                Assert.AreEqual(sourceTotalFiles, props["File"], "The number of total files do not match.");
                Assert.AreEqual(sourceTotalSize, props["Size"], "The total file size does not match.");
                Assert.AreNotEqual(null, copyResult);


                // Test against copyResult results.

                Assert.AreEqual(sourceTotal, copyResult.TotalFolders + copyResult.TotalFiles, "The number of total file system objects do not match.");
                Assert.AreEqual(sourceTotalFiles, copyResult.TotalFiles, "The number of total files do not match.");
                Assert.AreEqual(sourceTotalSize, copyResult.TotalBytes, "The total file size does not match.");
                Assert.IsTrue(copyResult.IsCopy);
                Assert.IsFalse(copyResult.IsMove);
                Assert.IsTrue(copyResult.IsDirectory);
                Assert.IsFalse(copyResult.IsFile);

                Assert.IsTrue(System.IO.Directory.Exists(folderSrc.FullName), "The original directory does not exist, but is expected to.");
            }


            Console.WriteLine();
        }