예제 #1
0
        // File helpers

        private string FilePath(Uri uri, FileBlobStorage storage)
        => uri
        .ChangeBase(
            storage.BaseUri,
            new Uri(Configuration.Path).EnsurePathTrailingSlash()
            )
        .LocalPath;
예제 #2
0
        public void PutAsync_NullStream()
        {
            var storage = new FileBlobStorage(Configuration);

            storage
            .Awaiting(s => s.PutAsync(null, ".dat"))
            .Should().Throw <ArgumentNullException>();
        }
예제 #3
0
        public void DeleteAsync_NullUri()
        {
            var storage = new FileBlobStorage(Configuration);

            storage
            .Awaiting(s => s.DeleteAsync(null))
            .Should().Throw <ArgumentNullException>();
        }
예제 #4
0
        public void DeleteAsync_NotMyUri()
        {
            var storage = new FileBlobStorage(Configuration);
            var uri     = new Uri("some://other/base/file.txt");

            storage
            .Awaiting(s => s.DeleteAsync(uri))
            .Should().Throw <ArgumentException>();
        }
예제 #5
0
        public void DeleteAsync_RelativeUri()
        {
            var storage = new FileBlobStorage(Configuration);
            var uri     = new Uri("relative/file.txt", UriKind.Relative);

            storage
            .Awaiting(s => s.DeleteAsync(uri))
            .Should().Throw <ArgumentException>();
        }
예제 #6
0
        public async Task DeleteAsync_DoesNotExist()
        {
            var storage = new FileBlobStorage(Configuration);
            var uri     = new Uri(storage.BaseUri, "a/b/file.txt");

            var result = await storage.DeleteAsync(uri);

            result.Should().BeFalse();
        }
예제 #7
0
        public void PutAsync_UnreadableStream()
        {
            var storage = new FileBlobStorage(Configuration);
            var stream  = Mock.Of <Stream>(s => s.CanRead == false);

            storage
            .Awaiting(s => s.PutAsync(stream, ".dat"))
            .Should().Throw <ArgumentException>();
        }
예제 #8
0
        public void GetAsync_NotFound()
        {
            var storage = new FileBlobStorage(Configuration);
            var uri     = new Uri(storage.BaseUri, "does/not/exist.txt");

            storage
            .Awaiting(s => s.GetAsync(uri))
            .Should().Throw <IOException>();
        }
예제 #9
0
        public async Task DeleteAsync_Exists_DirectoryEmpty()
        {
            var storage = new FileBlobStorage(Configuration);
            var uri     = new Uri(storage.BaseUri, "a/b/file.txt");

            CreateDirectory(@"a", "b");
            WriteFile(@"a", "b", "file.txt");
            FileExists(@"a", "b", "file.txt").Should().BeTrue("file should exist prior to deletion");

            var result = await storage.DeleteAsync(uri);

            result.Should().BeTrue();

            FileExists(@"a", "b", "file.txt").Should().BeFalse("file should have been deleted");
            DirectoryExists(@"a", "b").Should().BeFalse("empty subdirectory should have been deleted");
            DirectoryExists(@"a").Should().BeFalse("empty subdirectory should have been deleted");
            DirectoryExists(@"").Should().BeTrue("repository base directory should NOT have been deleted");
        }
예제 #10
0
        public async Task PutAsync(string extension)
        {
            var storage = new FileBlobStorage(Configuration);
            var bytes   = Utf8.GetBytes(TestText);

            Uri uri;

            using (var stream = new MemoryStream(bytes))
                uri = await storage.PutAsync(stream, extension);

            uri.Should().NotBeNull();
            uri.Should().Match <Uri>(u => storage.BaseUri.IsBaseOf(u));
            uri.AbsolutePath.Should().EndWith(".txt");

            var path = FilePath(uri, storage);

            ReadFile(path).Should().Be(TestText);
        }
예제 #11
0
        public void DeleteAsync_PathTooLong()
        {
            var storage = new FileBlobStorage(Configuration);
            var uri     = new Uri(storage.BaseUri, "a/b/file.txt");

            storage.FileSystem = FileSystem.Object;

            FileSystem
            .Setup(f => f.FileExists(It.IsAny <string>()))
            .Returns(true);

            FileSystem
            .Setup(f => f.DeleteFile(It.IsAny <string>()))
            .Throws <PathTooLongException>();    // should never happen, but coverage

            storage
            .Awaiting(s => s.DeleteAsync(uri))
            .Should().Throw <PathTooLongException>();
        }
예제 #12
0
        public void DeleteAsync_Retry_Fail()
        {
            var storage = new FileBlobStorage(Configuration);
            var uri     = new Uri(storage.BaseUri, "a/b/file.txt");

            storage.FileSystem = FileSystem.Object;

            FileSystem
            .Setup(f => f.FileExists(It.IsAny <string>()))
            .Returns(true);

            FileSystem
            .Setup(f => f.DeleteFile(It.IsAny <string>()))
            .Throws <IOException>();

            storage
            .Awaiting(s => s.DeleteAsync(uri))
            .Should().Throw <IOException>();
        }
예제 #13
0
        public async Task GetAsync()
        {
            var storage = new FileBlobStorage(Configuration);
            var uri     = new Uri(storage.BaseUri, "a/file.txt");

            CreateDirectory(@"a");
            WriteFile(@"a", "file.txt");

            byte[] bytes;
            using (var stream = await storage.GetAsync(uri))
                using (var memory = new MemoryStream())
                {
                    await stream.CopyToAsync(memory);

                    bytes = memory.ToArray();
                }

            Utf8.GetString(bytes).Should().Be(TestText);
        }
예제 #14
0
        public async Task DeleteAsync_Retry_Succeed()
        {
            var storage = new FileBlobStorage(Configuration);
            var uri     = new Uri(storage.BaseUri, "a/b/file.txt");

            storage.FileSystem = FileSystem.Object;

            FileSystem
            .Setup(f => f.FileExists(It.IsAny <string>()))
            .Returns(true);

            FileSystem
            .SetupSequence(f => f.DeleteFile(It.IsAny <string>()))
            .Throws <IOException>()
            .Pass();

            var result = await storage.DeleteAsync(uri);

            result.Should().BeTrue();
        }
예제 #15
0
        public async Task DeleteAsync_DirectoryNotFound()
        {
            var storage = new FileBlobStorage(Configuration);
            var uri     = new Uri(storage.BaseUri, "a/b/file.txt");

            storage.FileSystem = FileSystem.Object;

            FileSystem
            .Setup(f => f.FileExists(It.IsAny <string>()))
            .Returns(true)
            .Verifiable();

            FileSystem
            .Setup(f => f.DeleteFile(It.IsAny <string>()))
            .Throws <DirectoryNotFoundException>()
            .Verifiable();     // directory deleted after existence check

            var result = await storage.DeleteAsync(uri);

            result.Should().BeTrue();
            FileSystem.Verify();
        }
예제 #16
0
        public void PutAsync_FileSystemChanged()
        {
            try
            {
                var storage = new FileBlobStorage(Configuration);
                var bytes   = Utf8.GetBytes(TestText);

                DeleteDirectory(@"");
                WriteFile(@"");        // same name as repository directory

                using (var stream = new MemoryStream(bytes))
                {
                    storage
                    .Awaiting(s => s.PutAsync(stream, ".txt"))
                    .Should().Throw <IOException>()
                    .Which.Message.Should().Contain("already exists");
                }
            }
            finally
            {
                DeleteFile(@"");
            }
        }