Example #1
0
        private static void TestFileProcessorRecordAndPlay()
        {
            string undoFileName = VirtualDrive.VirtualFileName(
                @"TestFileProcessorRecordAndPlay\UndoFile.txt");

            FileInfo[] fileInfos =
                (from x in TestTags.Demotags select VirtualDrive.CreateVirtualFileInfo(
                     @"TestFileProcessorRecordAndPlay\TestFileProcessor" + x + ".mp3")).ToArray();

            fileInfos.ForEach((n) => TagUtils.WriteTag(TestTags.CreateDemoTag(Version.v2_0), n));

            using (UndoFileWriter undoFileWriter = new UndoFileWriter(undoFileName))
            {
                FileProcessor processor = new FileProcessor(new TagVersionProcessor(Version.v2_3));
                processor.UndoFile = undoFileWriter;

                foreach (var obj in fileInfos)
                {
                    processor.Process(obj);
                }
            }

            fileInfos.ForEach((n) => UnitTest.Test(TagUtils.ReadVersion(n) == Version.v2_3));

            UndoFilePlayer.Undo(undoFileName);

            fileInfos.ForEach((n) => UnitTest.Test(TagUtils.ReadVersion(n) == Version.v2_0));

            UndoFilePlayer.Redo(undoFileName);

            fileInfos.ForEach((n) => UnitTest.Test(TagUtils.ReadVersion(n) == Version.v2_3));

            VirtualDrive.DeleteDirectory(VirtualDrive.VirtualFileName(
                                             @"TestFileProcessorRecordAndPlay"), true);
        }
Example #2
0
        private static void TestUndoFileWriter()
        {
            UndoFileWriter writer = new UndoFileWriter(
                VirtualDrive.VirtualFileName("TestUndoFileWriter.txt"));

            for (int i = 0; i < 4; i++)
            {
                List <string> parameters = new List <string>();
                for (int j = 0; j < i; j++)
                {
                    parameters.Add("line" + j);
                }

                writer.Write(new SerializedCommand(
                                 typeof(TestClass),
                                 SerializedCommand.UndoRedo.Do,
                                 "TestUndoFileWriter",
                                 parameters.ToArray()));
            }

            writer.Close();

            UnitTest.Test(VirtualDrive.FileLength(
                              VirtualDrive.VirtualFileName("TestUndoFileWriter.txt")) > 0);
        }
Example #3
0
        private static void TestFilenameToTagProcessorSimple()
        {
            string undoFilename = VirtualDrive.VirtualFileName(
                @"TestFilenameToTagProcessor\TestFilenameToTagProcessorUndoFile.txt");

            string filename = VirtualDrive.VirtualFileName(
                @"TestFilenameToTagProcessor\My Artist - 03 - My Title.mp3");

            string pattern = "Artist - TrackNumber - Title";

            VirtualDrive.Store(filename, TestTags.mpegDummy);

            FilenameToTagProcessor processor = new FilenameToTagProcessor();

            processor.ProcessMessage(new FilenameToTagProcessor.Message(pattern));

            using (UndoFileWriter undoFileWriter = new UndoFileWriter(undoFilename))
            {
                processor.UndoFile = undoFileWriter;
                processor.Process(new FileInfo(filename));
            }

            TagEditor editor = new TagEditor(TagUtils.ReadTag(new FileInfo(filename)));

            UnitTest.Test(editor.Artist == "My Artist");
            UnitTest.Test(editor.TrackNumber == "03");
            UnitTest.Test(editor.Title == "My Title");

            UndoFilePlayer.Undo(undoFilename);

            UnitTest.Test(Object.ReferenceEquals(TagUtils.ReadTag(new FileInfo(filename)), null));
        }
Example #4
0
        private static void TestMoveDir()
        {
            string undoFileName = VirtualDrive.VirtualFileName(
                "TestFileCopyProcessorUndoFile.txt");

            string srcDir   = VirtualDrive.VirtualFileName(@"srcFolder\folder0");
            string srcFile0 = Path.Combine(srcDir, "t0.bin");
            string srcFile1 = Path.Combine(srcDir, "t1.bin");

            string dstDir   = VirtualDrive.VirtualFileName(@"dstFolder\folder1");
            string dstFile0 = Path.Combine(dstDir, "t0.bin");
            string dstFile1 = Path.Combine(dstDir, "t1.bin");

            VirtualDrive.Store(VirtualDrive.VirtualFileName(srcFile0), null);
            VirtualDrive.Store(VirtualDrive.VirtualFileName(srcFile1), null);

            UnitTest.Test(VirtualDrive.ExistsDirectory(srcDir));
            UnitTest.Test(!VirtualDrive.ExistsDirectory(dstDir));
            UnitTest.Test(VirtualDrive.ExistsFile(srcFile0));
            UnitTest.Test(VirtualDrive.ExistsFile(srcFile1));

            using (UndoFileWriter undoFileWriter = new UndoFileWriter(undoFileName))
            {
                FileOperationProcessor p = new FileOperationProcessor();
                p.Operation = FileOperationProcessor.FileOperation.Move;
                p.UndoFile  = undoFileWriter;
                p.NewName   = dstDir;

                p.Process(new DirectoryInfo(srcDir));
            }

            UnitTest.Test(!VirtualDrive.ExistsDirectory(srcDir));
            UnitTest.Test(VirtualDrive.ExistsDirectory(dstDir));
            UnitTest.Test(VirtualDrive.ExistsFile(dstFile0));
            UnitTest.Test(VirtualDrive.ExistsFile(dstFile1));

            UndoFilePlayer.Undo(undoFileName);

            UnitTest.Test(VirtualDrive.ExistsDirectory(srcDir));
            UnitTest.Test(!VirtualDrive.ExistsDirectory(dstDir));
            UnitTest.Test(VirtualDrive.ExistsFile(srcFile0));
            UnitTest.Test(VirtualDrive.ExistsFile(srcFile1));
            UnitTest.Test(!VirtualDrive.ExistsFile(dstFile0));
            UnitTest.Test(!VirtualDrive.ExistsFile(dstFile1));

            UndoFilePlayer.Redo(undoFileName);

            UnitTest.Test(!VirtualDrive.ExistsDirectory(srcDir));
            UnitTest.Test(VirtualDrive.ExistsDirectory(dstDir));
            UnitTest.Test(!VirtualDrive.ExistsFile(srcFile0));
            UnitTest.Test(!VirtualDrive.ExistsFile(srcFile1));
            UnitTest.Test(VirtualDrive.ExistsFile(dstFile0));
            UnitTest.Test(VirtualDrive.ExistsFile(dstFile1));

            VirtualDrive.DeleteDirectory(dstDir, true);
        }
Example #5
0
        public virtual void ProcessMessage(IProcessorMessage message)
        {
            if (message is UndoFileMessage)
            {
                UndoFileMessage msg = message as UndoFileMessage;
                UndoFile = msg.UndoFile;
            }

            Processor.ProcessMessage(message);
        }
Example #6
0
        private static void TestCopyDir()
        {
            string undoFileName = VirtualDrive.VirtualFileName(
                "TestFileCopyProcessorUndoFile.txt");

            string folder0    = VirtualDrive.VirtualFileName("folder0");
            string filename00 = Path.Combine(folder0, "t0.bin");
            string filename01 = Path.Combine(folder0, "t1.bin");

            string folder1    = VirtualDrive.VirtualFileName("folder1");
            string filename10 = Path.Combine(folder1, "t0.bin");
            string filename11 = Path.Combine(folder1, "t1.bin");

            VirtualDrive.Store(VirtualDrive.VirtualFileName(filename00), null);
            VirtualDrive.Store(VirtualDrive.VirtualFileName(filename01), null);

            UnitTest.Test(VirtualDrive.ExistsDirectory(folder0));
            UnitTest.Test(!VirtualDrive.ExistsDirectory(folder1));
            UnitTest.Test(VirtualDrive.ExistsFile(filename00));
            UnitTest.Test(VirtualDrive.ExistsFile(filename01));

            using (UndoFileWriter undoFileWriter = new UndoFileWriter(undoFileName))
            {
                FileOperationProcessor p = new FileOperationProcessor();
                p.Operation = FileOperationProcessor.FileOperation.Copy;
                p.UndoFile  = undoFileWriter;
                p.NewName   = folder1;

                p.Process(new DirectoryInfo(folder0));
            }

            UnitTest.Test(VirtualDrive.ExistsDirectory(folder0));
            UnitTest.Test(VirtualDrive.ExistsDirectory(folder1));
            UnitTest.Test(VirtualDrive.ExistsFile(filename10));
            UnitTest.Test(VirtualDrive.ExistsFile(filename11));

            UndoFilePlayer.Undo(undoFileName);

            UnitTest.Test(VirtualDrive.ExistsDirectory(folder0));
            UnitTest.Test(!VirtualDrive.ExistsDirectory(folder1));
            UnitTest.Test(VirtualDrive.ExistsFile(filename00));
            UnitTest.Test(VirtualDrive.ExistsFile(filename01));
            UnitTest.Test(!VirtualDrive.ExistsFile(filename10));
            UnitTest.Test(!VirtualDrive.ExistsFile(filename11));

            UndoFilePlayer.Redo(undoFileName);

            UnitTest.Test(VirtualDrive.ExistsDirectory(folder0));
            UnitTest.Test(VirtualDrive.ExistsDirectory(folder1));
            UnitTest.Test(VirtualDrive.ExistsFile(filename10));
            UnitTest.Test(VirtualDrive.ExistsFile(filename11));

            VirtualDrive.DeleteDirectory(folder0, true);
            VirtualDrive.DeleteDirectory(folder1, true);
        }
Example #7
0
        private static void TestMoveFile_AlreadyExists_Overwrite()
        {
            SetupRecycleBin();

            string undoFileName = VirtualDrive.VirtualFileName(
                "TestMoveFile_AlreadyExists_Overwrite.txt");

            string folder0   = VirtualDrive.VirtualFileName("folder0");
            string filename0 = Path.Combine(folder0, "t0.bin");
            string filename1 = Path.Combine(folder0, "t1.bin");

            string recycleBin   = VirtualDrive.VirtualFileName(@"recycle");
            string recycleFile0 = Path.Combine(recycleBin, "1.trash");

            byte[] data0 = new byte[] { 0 };
            VirtualDrive.Store(filename0, data0);

            byte[] data1 = new byte[] { 1 };
            VirtualDrive.Store(filename1, data1);

            UnitTest.Test(VirtualDrive.ExistsFile(filename0));
            UnitTest.Test(VirtualDrive.ExistsFile(filename1));
            UnitTest.Test(!VirtualDrive.ExistsFile(recycleFile0));

            using (UndoFileWriter undoFileWriter = new UndoFileWriter(undoFileName))
            {
                FileOperationProcessor p = new FileOperationProcessor();
                p.Operation = FileOperationProcessor.FileOperation.Move;
                p.UndoFile  = undoFileWriter;
                p.NewName   = "t1.bin";
                p.Conflicts = FileOperationProcessor.ConflictSolving.Overwrite;

                p.Process(new FileInfo(filename0));
            }

            UnitTest.Test(!VirtualDrive.ExistsFile(filename0));
            UnitTest.Test(VirtualDrive.ExistsFile(filename1));
            UnitTest.Test(VirtualDrive.ExistsFile(recycleFile0));
            UnitTest.Test(VirtualDrive.Load(filename1).SequenceEqual(data0));

            UndoFilePlayer.Undo(undoFileName);

            UnitTest.Test(VirtualDrive.ExistsFile(filename0));
            UnitTest.Test(VirtualDrive.ExistsFile(filename1));
            UnitTest.Test(!VirtualDrive.ExistsFile(recycleFile0));

            UndoFilePlayer.Redo(undoFileName);

            UnitTest.Test(!VirtualDrive.ExistsFile(filename0));
            UnitTest.Test(VirtualDrive.ExistsFile(filename1));
            UnitTest.Test(VirtualDrive.ExistsFile(recycleFile0));
        }
Example #8
0
        private static void TestCopyFile()
        {
            string undoFileName = VirtualDrive.VirtualFileName(
                "TestFileCopyProcessorUndoFile.txt");

            string folder0   = VirtualDrive.VirtualFileName("folder0");
            string filename0 = Path.Combine(folder0, "t0.bin");
            string filename1 = Path.Combine(folder0, "t1.bin");

            byte[] data = new byte[] { 0 };
            VirtualDrive.Store(filename0, data);

            UnitTest.Test(VirtualDrive.ExistsFile(filename0));
            UnitTest.Test(!VirtualDrive.ExistsFile(filename1));

            using (UndoFileWriter undoFileWriter = new UndoFileWriter(undoFileName))
            {
                FileOperationProcessor p = new FileOperationProcessor();
                p.Operation = FileOperationProcessor.FileOperation.Copy;
                p.UndoFile  = undoFileWriter;
                p.NewName   = "t1.bin";

                p.Process(new FileInfo(filename0));
            }

            UnitTest.Test(VirtualDrive.ExistsFile(filename0));
            UnitTest.Test(VirtualDrive.ExistsFile(filename1));

            UndoFilePlayer.Undo(undoFileName);

            UnitTest.Test(VirtualDrive.ExistsFile(filename0));
            UnitTest.Test(!VirtualDrive.ExistsFile(filename1));

            UndoFilePlayer.Redo(undoFileName);

            UnitTest.Test(VirtualDrive.ExistsFile(filename0));
            UnitTest.Test(VirtualDrive.ExistsFile(filename1));

            VirtualDrive.DeleteDirectory(folder0, true);
        }
Example #9
0
        private static void TestFileProcessorV2()
        {
            FileProcessor.WriteStrategy = Rewriter.Strategy.Exact;

            string undoFileName = VirtualDrive.VirtualFileName(@"TestFileProcessorV2\UndoFile.txt");

            FileInfo mp3File = VirtualDrive.CreateVirtualFileInfo(@"TestFileProcessorV2\test.mp3");

            TagUtils.WriteTag(TestTags.demoTag1_0, mp3File);
            TagUtils.WriteTag(TestTags.demoTag2_0, mp3File);

            UnitTest.Test(TagUtils.HasTagV1(mp3File));
            UnitTest.Test(TagUtils.HasTagV2(mp3File));

            using (UndoFileWriter undoFileWriter = new UndoFileWriter(undoFileName))
            {
                FileProcessor processor = new FileProcessor(new TagVersionProcessor(Version.v2_3));
                FileProcessor.WriteStrategy = Rewriter.Strategy.Exact;
                processor.UndoFile          = undoFileWriter;
                processor.Process(mp3File);
            }
            UnitTest.Test(!TagUtils.HasTagV1(mp3File));
            UnitTest.Test(TagUtils.HasTagV2(mp3File));

            UndoFilePlayer.Undo(undoFileName);
            UnitTest.Test(ArrayUtils.IsEqual(TagUtils.ReadTagV1Raw(mp3File), TestTags.demoTag1_0));
            UnitTest.Test(ArrayUtils.IsEqual(TagUtils.ReadTagV2Raw(mp3File), TestTags.demoTag2_0));

            UndoFilePlayer.Redo(undoFileName);
            UnitTest.Test(!TagUtils.HasTagV1(mp3File));
            UnitTest.Test(TagUtils.HasTagV2(mp3File));

            UndoFilePlayer.Undo(undoFileName);
            UnitTest.Test(ArrayUtils.IsEqual(TagUtils.ReadTagV1Raw(mp3File), TestTags.demoTag1_0));
            UnitTest.Test(ArrayUtils.IsEqual(TagUtils.ReadTagV2Raw(mp3File), TestTags.demoTag2_0));

            VirtualDrive.DeleteDirectory(VirtualDrive.VirtualFileName(
                                             @"TestFileProcessorV2"), true);
        }
Example #10
0
        private static void TestMoveDir_AlreadyExists_Overwrite()
        {
            SetupRecycleBin();

            string undoFileName = VirtualDrive.VirtualFileName(
                "TestFileCopyProcessorUndoFile.txt");

            string folder0 = VirtualDrive.VirtualFileName(@"fold00\folder0");
            string folder1 = VirtualDrive.VirtualFileName(@"fold01\folder1");

            string srcFile0     = Path.Combine(folder0, "t0.bin");
            string replaceFile0 = Path.Combine(folder1, "t1.bin");
            string dstFile0     = Path.Combine(folder1, "t0.bin");

            string recycleBin   = VirtualDrive.VirtualFileName(@"recycle");
            string recycleFile0 = Path.Combine(Path.Combine(recycleBin, "0"), "t1.bin");

            VirtualDrive.Store(VirtualDrive.VirtualFileName(srcFile0), null);
            VirtualDrive.Store(VirtualDrive.VirtualFileName(replaceFile0), null);

            UnitTest.Test(VirtualDrive.ExistsDirectory(folder0));
            UnitTest.Test(VirtualDrive.ExistsDirectory(folder1));
            UnitTest.Test(VirtualDrive.ExistsFile(srcFile0));
            UnitTest.Test(!VirtualDrive.ExistsFile(dstFile0));
            UnitTest.Test(VirtualDrive.ExistsFile(replaceFile0));
            UnitTest.Test(!VirtualDrive.ExistsFile(recycleFile0));

            using (UndoFileWriter undoFileWriter = new UndoFileWriter(undoFileName))
            {
                FileOperationProcessor p = new FileOperationProcessor();
                p.Operation = FileOperationProcessor.FileOperation.Move;
                p.UndoFile  = undoFileWriter;
                p.NewName   = folder1;
                p.Conflicts = FileOperationProcessor.ConflictSolving.Overwrite;

                p.Process(new DirectoryInfo(folder0));
            }

            UnitTest.Test(!VirtualDrive.ExistsDirectory(folder0));
            UnitTest.Test(VirtualDrive.ExistsDirectory(folder1));
            UnitTest.Test(!VirtualDrive.ExistsFile(srcFile0));
            UnitTest.Test(VirtualDrive.ExistsFile(dstFile0));
            UnitTest.Test(!VirtualDrive.ExistsFile(replaceFile0));
            UnitTest.Test(VirtualDrive.ExistsFile(recycleFile0));

            UndoFilePlayer.Undo(undoFileName);

            UnitTest.Test(VirtualDrive.ExistsDirectory(folder0));
            UnitTest.Test(VirtualDrive.ExistsDirectory(folder1));
            UnitTest.Test(VirtualDrive.ExistsFile(srcFile0));
            UnitTest.Test(!VirtualDrive.ExistsFile(dstFile0));
            UnitTest.Test(VirtualDrive.ExistsFile(replaceFile0));
            UnitTest.Test(!VirtualDrive.ExistsFile(recycleFile0));

            UndoFilePlayer.Redo(undoFileName);

            UnitTest.Test(!VirtualDrive.ExistsDirectory(folder0));
            UnitTest.Test(VirtualDrive.ExistsDirectory(folder1));
            UnitTest.Test(!VirtualDrive.ExistsFile(srcFile0));
            UnitTest.Test(VirtualDrive.ExistsFile(dstFile0));
            UnitTest.Test(!VirtualDrive.ExistsFile(replaceFile0));
            UnitTest.Test(VirtualDrive.ExistsFile(recycleFile0));

            VirtualDrive.DeleteDirectory(folder1, true);
            VirtualDrive.DeleteDirectory(recycleBin, true);
        }
Example #11
0
        private static void TestMoveDir_AlreadyExists_Skip()
        {
            string undoFileName = VirtualDrive.VirtualFileName(
                "TestMoveDir_AlreadyExists_Skip.txt");

            string folder0    = VirtualDrive.VirtualFileName(@"fold00\folder0");
            string filename00 = Path.Combine(folder0, "t0.bin");
            string filename01 = Path.Combine(folder0, "t1.bin");

            string folder1    = VirtualDrive.VirtualFileName(@"fold01\folder1");
            string filename10 = Path.Combine(folder1, "t0.bin");
            string filename11 = Path.Combine(folder1, "t1.bin");

            VirtualDrive.Store(VirtualDrive.VirtualFileName(filename00), null);
            VirtualDrive.Store(VirtualDrive.VirtualFileName(filename01), null);
            VirtualDrive.Store(VirtualDrive.VirtualFileName(filename10), null);

            UnitTest.Test(VirtualDrive.ExistsDirectory(folder0));
            UnitTest.Test(VirtualDrive.ExistsDirectory(folder1));
            UnitTest.Test(VirtualDrive.ExistsFile(filename00));
            UnitTest.Test(VirtualDrive.ExistsFile(filename01));
            UnitTest.Test(VirtualDrive.ExistsFile(filename10));
            UnitTest.Test(!VirtualDrive.ExistsFile(filename11));

            using (UndoFileWriter undoFileWriter = new UndoFileWriter(undoFileName))
            {
                FileOperationProcessor p = new FileOperationProcessor();
                p.Operation = FileOperationProcessor.FileOperation.Move;
                p.UndoFile  = undoFileWriter;
                p.NewName   = folder1;
                p.Conflicts = FileOperationProcessor.ConflictSolving.Skip;

                p.Process(new DirectoryInfo(folder0));
            }

            UnitTest.Test(VirtualDrive.ExistsDirectory(folder0));
            UnitTest.Test(VirtualDrive.ExistsDirectory(folder1));
            UnitTest.Test(VirtualDrive.ExistsFile(filename00));
            UnitTest.Test(VirtualDrive.ExistsFile(filename01));
            UnitTest.Test(VirtualDrive.ExistsFile(filename10));
            UnitTest.Test(!VirtualDrive.ExistsFile(filename11));

            UndoFilePlayer.Undo(undoFileName);

            UnitTest.Test(VirtualDrive.ExistsDirectory(folder0));
            UnitTest.Test(VirtualDrive.ExistsDirectory(folder1));
            UnitTest.Test(VirtualDrive.ExistsFile(filename00));
            UnitTest.Test(VirtualDrive.ExistsFile(filename01));
            UnitTest.Test(VirtualDrive.ExistsFile(filename10));
            UnitTest.Test(!VirtualDrive.ExistsFile(filename11));

            UndoFilePlayer.Redo(undoFileName);

            UnitTest.Test(VirtualDrive.ExistsDirectory(folder0));
            UnitTest.Test(VirtualDrive.ExistsDirectory(folder1));
            UnitTest.Test(VirtualDrive.ExistsFile(filename00));
            UnitTest.Test(VirtualDrive.ExistsFile(filename01));
            UnitTest.Test(VirtualDrive.ExistsFile(filename10));
            UnitTest.Test(!VirtualDrive.ExistsFile(filename11));

            VirtualDrive.DeleteDirectory(folder0, true);
            VirtualDrive.DeleteDirectory(folder1, true);
        }
Example #12
0
 public UndoFileMessage(UndoFileWriter writer)
 {
     UndoFile = writer;
 }