コード例 #1
0
        public void FileUpdaterCanUpdateFileName()
        {
            // Arrange.
            const string ExpectedName = "ExpectedName.test";

            var nameValue = new object();
            var typeConverter = Mock.Create<ITypeConverter>(Behavior.Loose);
            Mock.Arrange(() => typeConverter.Convert<string>(nameValue)).Returns(ExpectedName);

            var fileManager = Mock.Create<IFileManager>(Behavior.Loose);

            var dataContext = Mock.Create<IDataContext>(Behavior.Loose);
            var mapping = new FileFieldMapping(
                ReflectionHelper.GetProperty<TestEdit>(x => x.File),
                dc => dc == dataContext ? nameValue : null,
                null,
                null,
                typeConverter,
                fileManager);

            var file = Mock.Create<IFileProcess>(Behavior.Loose);
            var editableRoot = new TestEdit { File = file };

            // Act.
            mapping.Update(dataContext, editableRoot);

            // Assert.
            Assert.AreEqual(ExpectedName, file.OriginalFileName);
        }
コード例 #2
0
        public void FileUpdaterCanUpdateFileFromBytes()
        {
            // Arrange.
            const string TempFileFullName = @"z:\TempFolder\TestFile.tmp";
            const string TempFileName = "TestFile.tmp";

            var contentValue = new object();
            var expectedContent = new byte[0];

            var typeConverter = Mock.Create<ITypeConverter>(Behavior.Loose);
            Mock.Arrange(() => typeConverter.Convert<byte[]>(contentValue)).Returns(expectedContent);

            var fileManager = Mock.Create<IFileManager>();
            Mock.Arrange(() => fileManager.GetTempFileName(Arg.AnyString)).Returns(TempFileFullName);

            var dataContext = Mock.Create<IDataContext>(Behavior.Loose);
            var mapping = new FileFieldMapping(
                ReflectionHelper.GetProperty<TestEdit>(x => x.File),
                null,
                dc => dc == dataContext ? contentValue : null,
                null,
                typeConverter,
                fileManager);

            var file = Mock.Create<IFileProcess>(Behavior.Loose);
            var editableRoot = new TestEdit { File = file };

            Mock.Arrange(() => File.WriteAllBytes(TempFileFullName, expectedContent)).DoNothing().InOrder();
            Mock.Arrange(() => Path.GetFileName(TempFileFullName)).Returns(TempFileName);
            Mock.Arrange(() => file.UploadFile()).DoNothing().InOrder();

            // Act.
            mapping.Update(dataContext, editableRoot);

            // Assert.
            Mock.Assert(() => File.WriteAllBytes(TempFileFullName, expectedContent), Occurs.Once());
            Assert.AreEqual(TempFileName, file.OriginalFileName);
            Assert.AreEqual(TempFileName, file.FileName);
            Mock.Assert(() => file.UploadFile(), Occurs.Once());
        }
コード例 #3
0
        public void WhenWebResponseContainsContentDisposition_Update_ChangesOriginalFileNameAndExtension()
        {
            // Arrange.
            const string ExpectedLocation = @"http://www.example.com/";
            const string OriginalFileName = "TestFile.png";
            const string TempFileFullName = @"z:\TempFolder\TempFile";
            const string TempFileFullNameWithExtension = @"z:\TempFolder\TempFile.png";
            const string TempFileNameWithExtension = "TempFile.png";

            var locationValue = new object();
            var typeConverter = Mock.Create<ITypeConverter>(Behavior.Loose);
            Mock.Arrange(() => typeConverter.Convert<string>(locationValue)).Returns(ExpectedLocation);

            var fileManager = Mock.Create<IFileManager>();
            Mock.Arrange(() => fileManager.GetTempFileName(Arg.AnyString)).Returns(TempFileFullName);

            var dataContext = Mock.Create<IDataContext>(Behavior.Loose);
            var mapping = new FileFieldMapping(
                ReflectionHelper.GetProperty<TestEdit>(x => x.File),
                null,
                null,
                dc => dc == dataContext ? locationValue : null,
                typeConverter,
                fileManager);

            var file = Mock.Create<IFileProcess>(Behavior.Loose);
            var editableRoot = new TestEdit { File = file };

            var responseHeaders = new WebHeaderCollection();
            responseHeaders["content-disposition"] = "attachment; filename=" + OriginalFileName;

            Mock.Arrange(() => Arg.IsAny<WebClient>().ResponseHeaders).Returns(responseHeaders);
            Mock.Arrange(() => Arg.IsAny<WebClient>().DownloadFile(ExpectedLocation, TempFileFullName)).DoNothing();
            Mock.Arrange(() => File.Move(TempFileFullName, TempFileFullNameWithExtension)).DoNothing().InOrder();
            Mock.Arrange(() => file.UploadFile()).DoNothing().InOrder();

            // Act.
            mapping.Update(dataContext, editableRoot);

            // Assert.
            Mock.Assert(() => Arg.IsAny<WebClient>().DownloadFile(ExpectedLocation, TempFileFullName), Occurs.Once());
            Mock.Assert(() => File.Move(TempFileFullName, TempFileFullNameWithExtension), Occurs.Once());
            Mock.Assert(() => file.UploadFile(), Occurs.Once());
            Assert.AreEqual(OriginalFileName, file.OriginalFileName);
            Assert.AreEqual(TempFileNameWithExtension, file.FileName);
        }
コード例 #4
0
        public void TryCreateFilter_ReturnsFalse()
        {
            // Arrange.
            var typeConverter = Mock.Create<ITypeConverter>(Behavior.Loose);
            var fileManager = Mock.Create<IFileManager>(Behavior.Loose);
            var mapping = new FileFieldMapping(ReflectionHelper.GetProperty<TestEdit>(x => x.File), null, null, null, typeConverter, fileManager);
            var dataContext = Mock.Create<IDataContext>(Behavior.Loose);

            // Act / Assert.
            ColumnFilter filter;

            Assert.IsFalse(mapping.TryCreateFilter(dataContext, out filter));
        }
コード例 #5
0
        public void FileUpdaterCanUpdateFileFromLocation()
        {
            // Arrange.
            const string ExpectedLocation = @"http://www.example.com/";
            const string TempFileFullName = @"z:\TempFolder\TestFile.tmp";
            const string TempFileName = "TestFile.tmp";

            var locationValue = new object();
            var typeConverter = Mock.Create<ITypeConverter>(Behavior.Loose);
            Mock.Arrange(() => typeConverter.Convert<string>(locationValue)).Returns(ExpectedLocation);

            var fileManager = Mock.Create<IFileManager>();
            Mock.Arrange(() => fileManager.GetTempFileName(Arg.AnyString)).Returns(TempFileFullName);

            var dataContext = Mock.Create<IDataContext>(Behavior.Loose);
            var mapping = new FileFieldMapping(
                ReflectionHelper.GetProperty<TestEdit>(x => x.File),
                null,
                null,
                dc => dc == dataContext ? locationValue : null,
                typeConverter,
                fileManager);

            var file = Mock.Create<IFileProcess>(Behavior.Loose);
            var editableRoot = new TestEdit { File = file };

            var responseHeaders = new WebHeaderCollection();

            Mock.Arrange(() => Arg.IsAny<WebClient>().ResponseHeaders).Returns(responseHeaders);
            Mock.Arrange(() => Arg.IsAny<WebClient>().DownloadFile(ExpectedLocation, TempFileFullName)).DoNothing();
            Mock.Arrange(() => file.UploadFile()).DoNothing();

            // Act.
            mapping.Update(dataContext, editableRoot);

            // Assert.
            Mock.Assert(() => Arg.IsAny<WebClient>().DownloadFile(ExpectedLocation, TempFileFullName), Occurs.Once());
            Mock.Assert(() => file.UploadFile(), Occurs.Once());
            Assert.AreEqual(TempFileName, file.OriginalFileName);
            Assert.AreEqual(TempFileName, file.FileName);
        }