示例#1
0
            public async Task WillOverwriteFileIfOverwriteTrueAndRealFileSystemIsUsed()
            {
                // Arrange
                using (var testDirectory = TestDirectory.Create())
                {
                    var fileSystemService = new FileSystemService();

                    var configuration = new Mock <IAppConfiguration>();
                    configuration
                    .Setup(x => x.FileStorageDirectory)
                    .Returns(testDirectory);

                    var service = new FileSystemFileStorageService(
                        configuration.Object,
                        fileSystemService);

                    var directory = Path.Combine(testDirectory, FolderName);
                    var filePath  = Path.Combine(directory, FileName);
                    Directory.CreateDirectory(directory);
                    File.WriteAllText(filePath, string.Empty);

                    // Act
                    await service.SaveFileAsync(
                        FolderName,
                        FileName,
                        new MemoryStream(Encoding.ASCII.GetBytes(FileContent)),
                        overwrite : true);

                    Assert.True(File.Exists(filePath), $"The file at path {filePath} should exist, but does not.");
                    Assert.Equal(FileContent, File.ReadAllText(filePath));
                }
            }
            public async Task CopiesFileWhenDestinationDoesNotExist()
            {
                // Arrange
                var content = "Hello, world!";
                await _target.SaveFileAsync(
                    _srcFolderName,
                    _srcFileName,
                    new MemoryStream(Encoding.ASCII.GetBytes(content)));

                // Act
                await _target.CopyFileAsync(
                    _srcFolderName,
                    _srcFileName,
                    _destFolderName,
                    _destFileName,
                    destAccessCondition : null);

                // Assert
                using (var destStream = await _target.GetFileAsync(_destFolderName, _destFileName))
                    using (var reader = new StreamReader(destStream))
                    {
                        Assert.Equal(content, reader.ReadToEnd());
                    }
            }
示例#3
0
            private static async Task <bool> SaveFileAsync(FileSystemFileStorageService service, string fileName, Barrier barrier)
            {
                await Task.Yield();

                try
                {
                    barrier.SignalAndWait();
                    await service.SaveFileAsync(
                        FolderName,
                        fileName,
                        new MemoryStream(),
                        overwrite : false);

                    return(true);
                }
                catch
                {
                    return(false);
                }
            }
示例#4
0
            public async Task WillThrowIfFileExistsAndOverwriteFalseAndRealFileSystemIsUsed()
            {
                // Arrange
                using (var testDirectory = TestDirectory.Create())
                {
                    var fileSystemService = new FileSystemService();

                    var configuration = new Mock <IAppConfiguration>();
                    configuration
                    .Setup(x => x.FileStorageDirectory)
                    .Returns(testDirectory);

                    var service = new FileSystemFileStorageService(
                        configuration.Object,
                        fileSystemService);

                    var directory = Path.Combine(testDirectory, FolderName);
                    var filePath  = Path.Combine(directory, FileName);
                    Directory.CreateDirectory(directory);
                    File.WriteAllText(filePath, FileContent);

                    // Act
                    var exception = await Assert.ThrowsAsync <InvalidOperationException>(() => service.SaveFileAsync(
                                                                                             FolderName,
                                                                                             FileName,
                                                                                             new MemoryStream(),
                                                                                             overwrite: false));

                    Assert.True(File.Exists(filePath), $"The file at path {filePath} should exist, but does not.");
                    Assert.Equal(FileContent, File.ReadAllText(filePath));
                }
            }