예제 #1
0
        public void Visit(FileCopyCommand command)
        {
            Visit((ICommand)command);

            if (Root.TryFindNode(command.FileId, out var node) &&
                node is File file)
            {
                if (Root.TryFindParent(command.FileId, out var parent) &&
                    parent is Directory parentDirectory)
                {
                    var existingFile = parentDirectory.Children.FirstOrDefault(c => c.Name == command.CopyName);

                    if (existingFile != null)
                    {
                        OnNodeAlreadyExists(command.CopyName, parentDirectory.Id);
                    }
                    else
                    {
                        var copy = _factory.CreateFile(command.CopyName, file.Size);
                        parentDirectory.Children.Add(copy);
                        _timestamp.Increment();
                        Message = $"ID={copy.Id}";
                    }
                }
                else
                {
                    Message = $"File with ID {command.FileId} has no parent.";
                }
            }
            else
            {
                OnFileDoesNotExist(command.FileId);
            }
        }
예제 #2
0
        public void itShouldFailIfSourceFileDoesNotExist()
        {
            var fileSysCommand  = new MockFileSystemCommand();
            var fakeFileSystem  = new FakeFileSystem(fileSysCommand);
            var fileToCopy      = @"c:\dummysourcefile.txt";
            var targetFileCopy  = @"c:\somefolder\dummysourcefile.doc";
            var fileCopyCommand = new FileCopyCommand(@"c:\dummybackupdir", fileSysCommand, fileToCopy, targetFileCopy);

            fileCopyCommand.Do();

            Assert.IsFalse(fileCopyCommand.DidCommandSucceed);
            Assert.IsFalse(fakeFileSystem.FileExists(targetFileCopy));
        }
예제 #3
0
        public void itShouldFailIfFileCopyPairCountIsOdd()
        {
            var fileSysCommand  = new MockFileSystemCommand();
            var fakeFileSystem  = new FakeFileSystem(fileSysCommand);
            var fileToCopy1     = @"c:\dummysourcefile.txt";
            var targetFileCopy1 = @"c:\somefolder\dummytargetfile.doc";
            var fileToCopy2     = @"c:\dummysourcefile2.txt";

            fakeFileSystem.AddFiles(fileToCopy1, fileToCopy2);
            var fileCopyCommand = new FileCopyCommand(@"c:\dummybackupdir", fileSysCommand, fileToCopy1, targetFileCopy1, fileToCopy2);

            fileCopyCommand.Do();
        }
예제 #4
0
        public void itShouldFailIfDestinationFileAlreadyExists()
        {
            var fileSysCommand = new MockFileSystemCommand();
            var fakeFileSystem = new FakeFileSystem(fileSysCommand);
            var fileToCopy     = @"c:\dummysourcefile.txt";
            var targetFileCopy = @"c:\somefolder\dummysourcefile.doc";

            fakeFileSystem.AddFiles(fileToCopy);
            fakeFileSystem.AddFiles(targetFileCopy);
            var fileCopyCommand = new FileCopyCommand(@"c:\dummybackupdir", fileSysCommand, fileToCopy, targetFileCopy);

            fileCopyCommand.Do();

            Assert.IsFalse(fileCopyCommand.DidCommandSucceed);
        }
예제 #5
0
        public void Visit(FileCopyCommand command)
        {
            if (!TryGetPrefixedPathTo(command.FileId, out var path))
            {
                return;
            }

            var directory = Path.GetDirectoryName(path) ?? string.Empty;
            var fileData  = File.ReadAllBytes(path);
            var copyPath  = Path.Combine(directory, command.CopyName);

            if (!File.Exists(copyPath))
            {
                File.WriteAllBytes(copyPath, fileData);
            }
        }
예제 #6
0
        public void itShouldHaveCopiedFileIfSuccessful()
        {
            var fileSysCommand = new MockFileSystemCommand();
            var fakeFileSystem = new FakeFileSystem(fileSysCommand);

            var fileToCopy     = @"c:\dummysourcefile.txt";
            var targetFileCopy = @"c:\somefolder\dummysourcefile.doc";

            fakeFileSystem.AddFiles(fileToCopy);
            var fileCopyCommand = new FileCopyCommand(@"c:\dummybackupdir", fileSysCommand, fileToCopy, targetFileCopy);

            fileCopyCommand.Do();

            Assert.IsTrue(fileCopyCommand.DidCommandSucceed);
            Assert.IsTrue(fakeFileSystem.FileExists(targetFileCopy));
        }
예제 #7
0
        public void itShouldAllowMultipleFileCopyPairs()
        {
            var fileSysCommand  = new MockFileSystemCommand();
            var fakeFileSystem  = new FakeFileSystem(fileSysCommand);
            var fileToCopy1     = @"c:\dummysourcefile.txt";
            var targetFileCopy1 = @"c:\somefolder\dummytargetfile.doc";
            var fileToCopy2     = @"c:\dummysourcefile2.txt";
            var targetFileCopy2 = @"c:\somefolder\dummytargetfile2.doc";

            fakeFileSystem.AddFiles(fileToCopy1, fileToCopy2);
            var fileCopyCommand = new FileCopyCommand(@"c:\dummybackupdir", fileSysCommand, fileToCopy1, targetFileCopy1, fileToCopy2, targetFileCopy2);


            Assert.IsTrue(!fakeFileSystem.FileExists(targetFileCopy1));
            Assert.IsTrue(!fakeFileSystem.FileExists(targetFileCopy2));

            fileCopyCommand.Do();

            Assert.IsTrue(fakeFileSystem.FileExists(targetFileCopy1));
            Assert.IsTrue(fakeFileSystem.FileExists(targetFileCopy2));
        }
        public void CopyFile(string source, string destination)
        {
            var command = new FileCopyCommand(filesystem, source, destination);

            ExecuteOrRecover(command);
        }