Exemplo n.º 1
0
        public async Task TestCopyOperation()
        {
            var directoryServiceMock = new Mock <IDirectoryService>();
            var filesServiceMock     = new Mock <IFileService>();

            filesServiceMock
            .Setup(m => m.CopyAsync(SourceFile, DestinationFile))
            .Verifiable();
            var operationsFactory = new OperationsFactory(
                _taskPool,
                directoryServiceMock.Object,
                filesServiceMock.Object,
                _pathService);
            var copyOperation = operationsFactory.CreateCopyOperation(
                new[]
            {
                new BinaryFileOperationSettings(SourceFile, DestinationFile)
            });

            var callbackCalled = false;

            copyOperation.OperationFinished += (sender, args) => callbackCalled = true;

            await copyOperation.RunAsync();

            Assert.True(callbackCalled);
            filesServiceMock.Verify(m => m.CopyAsync(SourceFile, DestinationFile), Times.Once());
        }
Exemplo n.º 2
0
        public async Task TestBlockedCopyOperation()
        {
            var directoryServiceMock = new Mock <IDirectoryService>();
            var filesServiceMock     = new Mock <IFileService>();

            filesServiceMock
            .Setup(m => m.CopyAsync(SourceName, DestinationName, false))
            .Verifiable();
            filesServiceMock
            .Setup(m => m.CopyAsync(SourceName, DestinationName, true))
            .Verifiable();
            filesServiceMock
            .Setup(m => m.CheckIfExists(DestinationName))
            .Returns(true);
            var operationsFactory = new OperationsFactory(
                _taskPool,
                directoryServiceMock.Object,
                filesServiceMock.Object,
                _pathService,
                _fileNameGenerationService);
            var settings = new BinaryFileSystemOperationSettings(
                new string[] { },
                new[] { SourceName },
                new string[] { },
                new[] { SourceName },
                new Dictionary <string, string> {
                [SourceName] = DestinationName
            }
                );
            var copyOperation = operationsFactory.CreateCopyOperation(settings);

            var callbackCalled = false;

            copyOperation.StateChanged += async(sender, args) =>
            {
                if (args.OperationState != OperationState.Blocked)
                {
                    return;
                }

                callbackCalled = true;
                filesServiceMock.Verify(m => m.CopyAsync(SourceName, DestinationName, false), Times.Never);

                var operation = (IOperation)sender;
                Assert.NotNull(operation);

                var(sourceFilePath, _) = operation.CurrentBlockedFile;
                var options = OperationContinuationOptions.CreateContinuationOptions(sourceFilePath, false, OperationContinuationMode.Overwrite);

                await copyOperation.ContinueAsync(options);
            };

            await copyOperation.RunAsync();

            Assert.True(callbackCalled);

            Assert.Equal(OperationState.Finished, copyOperation.State);
            filesServiceMock.Verify(m => m.CopyAsync(SourceName, DestinationName, true), Times.Once);
        }
Exemplo n.º 3
0
        public async Task TestCopyOperation(bool throws, OperationState state)
        {
            var directoryServiceMock = new Mock <IDirectoryService>();
            var filesServiceMock     = new Mock <IFileService>();
            var copySetup            = filesServiceMock
                                       .Setup(m => m.CopyAsync(SourceName, DestinationName, false));

            if (throws)
            {
                copySetup.ThrowsAsync(new AccessViolationException()).Verifiable();
            }
            else
            {
                copySetup.Verifiable();
            }

            var operationsFactory = new OperationsFactory(
                _taskPool,
                directoryServiceMock.Object,
                filesServiceMock.Object,
                _pathService,
                _fileNameGenerationService);
            var settings = new BinaryFileSystemOperationSettings(
                new string[] { },
                new[] { SourceName },
                new string[] { },
                new[] { SourceName },
                new Dictionary <string, string> {
                [SourceName] = DestinationName
            },
                new string[] { }
                );
            var copyOperation = operationsFactory.CreateCopyOperation(settings);

            Assert.Equal(OperationState.NotStarted, copyOperation.State);

            var isCallbackCalled = false;

            copyOperation.StateChanged += (sender, args) => isCallbackCalled = true;

            await copyOperation.RunAsync();

            Assert.Equal(state, copyOperation.State);

            Assert.True(isCallbackCalled);
            filesServiceMock.Verify(m => m.CopyAsync(SourceName, DestinationName, false), Times.Once());
        }
Exemplo n.º 4
0
        public async Task TestCopeEmptyDirectoryOperation(bool success, OperationState state)
        {
            var directoryServiceMock = new Mock <IDirectoryService>();

            directoryServiceMock
            .Setup(m => m.GetEmptyDirectoriesRecursively(SourceName))
            .Returns(new[] { SourceName });
            directoryServiceMock
            .Setup(m => m.Create(DestinationName))
            .Returns(success)
            .Verifiable();
            var filesServiceMock = new Mock <IFileService>();
            var pathServiceMock  = new Mock <IPathService>();

            var operationsFactory = new OperationsFactory(
                _taskPool,
                directoryServiceMock.Object,
                filesServiceMock.Object,
                pathServiceMock.Object,
                _fileNameGenerationService);
            var settings = new BinaryFileSystemOperationSettings(
                new[] { SourceName },
                new string[] { },
                new[] { DestinationName },
                new string[] { },
                new Dictionary <string, string>(),
                new[] { DestinationName }
                );
            var copyOperation = operationsFactory.CreateCopyOperation(settings);

            Assert.Equal(OperationState.NotStarted, copyOperation.State);

            var callbackCalled = false;

            copyOperation.StateChanged += (sender, args) => callbackCalled = true;

            await copyOperation.RunAsync();

            Assert.Equal(state, copyOperation.State);

            Assert.True(callbackCalled);
            directoryServiceMock.Verify(m => m.Create(DestinationName), Times.Once());
        }
Exemplo n.º 5
0
        public async Task TestCopyOperation()
        {
            var directoryServiceMock = new Mock <IDirectoryService>();
            var filesServiceMock     = new Mock <IFileService>();

            filesServiceMock
            .Setup(m => m.CopyAsync(SourceName, DestinationName))
            .Verifiable();
            var operationsFactory = new OperationsFactory(
                _taskPool,
                directoryServiceMock.Object,
                filesServiceMock.Object,
                _pathService);
            var settings = new BinaryFileSystemOperationSettings(
                new string[] { },
                new[] { SourceName },
                new string[] { },
                new[] { SourceName },
                new Dictionary <string, string> {
                [SourceName] = DestinationName
            }
                );
            var copyOperation = operationsFactory.CreateCopyOperation(settings);

            Assert.Equal(OperationState.NotStarted, copyOperation.OperationState);

            var callbackCalled = false;

            copyOperation.StateChanged += (sender, args) => callbackCalled = true;

            await copyOperation.RunAsync();

            Assert.Equal(OperationState.Finished, copyOperation.OperationState);

            Assert.True(callbackCalled);
            filesServiceMock.Verify(m => m.CopyAsync(SourceName, DestinationName), Times.Once());
        }
Exemplo n.º 6
0
        public async Task TestBlockedCopyOperation(bool applyToAll, int expectedCallbackCallsCount,
                                                   OperationContinuationMode mode, int expectedWriteCallsCountFirstFile, int expectedWriteCallsCountSecondFile)
        {
            var now           = DateTime.UtcNow;
            var hourBeforeNow = now.AddHours(-1);

            var directoryServiceMock = new Mock <IDirectoryService>();
            var filesServiceMock     = new Mock <IFileService>();

            filesServiceMock
            .Setup(m => m.GetFile(SourceName))
            .Returns(new FileModel {
                LastModifiedDateTime = now
            });
            filesServiceMock
            .Setup(m => m.GetFile(DestinationName))
            .Returns(new FileModel {
                LastModifiedDateTime = hourBeforeNow
            });
            filesServiceMock
            .Setup(m => m.GetFile(SecondSourceName))
            .Returns(new FileModel {
                LastModifiedDateTime = hourBeforeNow
            });
            filesServiceMock
            .Setup(m => m.GetFile(SecondDestinationName))
            .Returns(new FileModel {
                LastModifiedDateTime = now
            });
            filesServiceMock
            .Setup(m => m.CopyAsync(SourceName, DestinationName, false))
            .Verifiable();
            filesServiceMock
            .Setup(m => m.CopyAsync(SourceName, DestinationName, true))
            .Returns(Task.CompletedTask)
            .Verifiable();
            filesServiceMock
            .Setup(m => m.CopyAsync(SecondSourceName, SecondDestinationName, false))
            .Verifiable();
            filesServiceMock
            .Setup(m => m.CopyAsync(SecondSourceName, SecondDestinationName, true))
            .Returns(Task.CompletedTask)
            .Verifiable();
            filesServiceMock
            .Setup(m => m.CheckIfExists(It.IsAny <string>()))
            .Returns(true);
            var operationsFactory = new OperationsFactory(
                _taskPool,
                directoryServiceMock.Object,
                filesServiceMock.Object,
                _pathService,
                _fileNameGenerationService);
            var settings = new BinaryFileSystemOperationSettings(
                new string[] { },
                new[] { SourceName, SecondSourceName },
                new string[] { },
                new[] { DestinationName, SecondDestinationName },
                new Dictionary <string, string>
            {
                [SourceName]       = DestinationName,
                [SecondSourceName] = SecondDestinationName
            },
                new string[] { }
                );
            var copyOperation = operationsFactory.CreateCopyOperation(settings);

            var callbackCallsCount = 0;

            copyOperation.StateChanged += async(sender, args) =>
            {
                if (args.OperationState != OperationState.Blocked)
                {
                    return;
                }

                var operation = (IOperation)sender;
                if (operation is null)
                {
                    return;
                }

                callbackCallsCount++;

                var(sourceFilePath, _) = operation.CurrentBlockedFile;
                var options = OperationContinuationOptions.CreateContinuationOptions(sourceFilePath, applyToAll, mode);

                await copyOperation.ContinueAsync(options);
            };

            await copyOperation.RunAsync();

            Assert.Equal(expectedCallbackCallsCount, callbackCallsCount);

            Assert.Equal(OperationState.Finished, copyOperation.State);
            filesServiceMock.Verify(m => m.CopyAsync(SourceName, DestinationName, true), Times.Exactly(expectedWriteCallsCountFirstFile));
            filesServiceMock.Verify(m => m.CopyAsync(SecondSourceName, SecondDestinationName, true), Times.Exactly(expectedWriteCallsCountSecondFile));
            filesServiceMock.Verify(m => m.CopyAsync(SourceName, DestinationName, false), Times.Never);
            filesServiceMock.Verify(m => m.CopyAsync(SecondSourceName, SecondDestinationName, false), Times.Never);
        }