Example #1
0
        /// <inheritdoc />
        public FileType IdentifyFileType(FileInfo fileInfo)
        {
            if (fileInfo == null)
            {
                throw new ArgumentNullException("fileInfo");
            }

            using (var fileInspector = new LazyFileInspector(fileInfo))
                return(IdentifyFileTypeImpl(fileInspector, null));
        }
 public void TryGetContents_WhenFileDoesNotExist_ReturnsNullAndFalse()
 {
     var fileInfo = new FileInfo(@"C:\This\File\Does\Not\Exist.xxx");
     using (var inspector = new LazyFileInspector(fileInfo))
     {
         string returnedContents;
         Assert.IsFalse(inspector.TryGetContents(out returnedContents));
         Assert.IsNull(returnedContents);
     }
 }
 public void TryGetFileInfo_Always_ReturnsInitializedFileInfoAndTrue()
 {
     var fileInfo = new FileInfo(@"C:\foo.txt");
     using (var inspector = new LazyFileInspector(fileInfo))
     {
         FileInfo returnedFileInfo;
         Assert.IsTrue(inspector.TryGetFileInfo(out returnedFileInfo));
         Assert.AreSame(fileInfo, returnedFileInfo);
     }
 }
        public void TryGetContents_WhenFileExists_ReturnsContentsAndTrue()
        {
            string path = SpecialPathPolicy.For<LazyFileInspectorTest>().CreateTempFileWithUniqueName().FullName;
            File.WriteAllText(path, "Contents");

            var fileInfo = new FileInfo(path);
            using (var inspector = new LazyFileInspector(fileInfo))
            {
                string returnedContents;
                Assert.IsTrue(inspector.TryGetContents(out returnedContents));
                Assert.AreEqual("Contents", returnedContents);
            }
        }
Example #5
0
        /// <inheritdoc />
        public FileType IdentifyFileType(FileInfo fileInfo, IEnumerable <FileType> candidates)
        {
            if (fileInfo == null)
            {
                throw new ArgumentNullException("fileInfo");
            }
            if (candidates == null)
            {
                throw new ArgumentNullException("candidates");
            }

            using (var fileInspector = new LazyFileInspector(fileInfo))
                return(IdentifyFileTypeImpl(fileInspector, candidates));
        }
        public void TryGetStream_WhenFileExists_ReturnsStreamAndTrue()
        {
            string path = SpecialPathPolicy.For<LazyFileInspectorTest>().CreateTempFileWithUniqueName().FullName;
            File.WriteAllText(path, "Contents");

            var fileInfo = new FileInfo(path);
            using (var inspector = new LazyFileInspector(fileInfo))
            {
                Stream returnedStream;
                Assert.IsTrue(inspector.TryGetStream(out returnedStream));
                string contents = new StreamReader(returnedStream).ReadToEnd();
                Assert.AreEqual("Contents", contents);
            }
        }
        /// <inheritdoc />
        public FileType IdentifyFileType(FileInfo fileInfo, IEnumerable<FileType> candidates)
        {
            if (fileInfo == null)
                throw new ArgumentNullException("fileInfo");
            if (candidates == null)
                throw new ArgumentNullException("candidates");

            using (var fileInspector = new LazyFileInspector(fileInfo))
                return IdentifyFileTypeImpl(fileInspector, candidates);
        }
        /// <inheritdoc />
        public FileType IdentifyFileType(FileInfo fileInfo)
        {
            if (fileInfo == null)
                throw new ArgumentNullException("fileInfo");

            using (var fileInspector = new LazyFileInspector(fileInfo))
                return IdentifyFileTypeImpl(fileInspector, null);
        }
 public void TryGetStream_WhenFileDoesNotExist_ReturnsNullAndFalse()
 {
     var fileInfo = new FileInfo(@"C:\This\File\Does\Not\Exist.xxx");
     using (var inspector = new LazyFileInspector(fileInfo))
     {
         Stream returnedStream;
         Assert.IsFalse(inspector.TryGetStream(out returnedStream));
         Assert.IsNull(returnedStream);
     }
 }
        public void Dispose_WhenStreamWasOpen_ShouldCloseTheStream()
        {
            string path = SpecialPathPolicy.For<LazyFileInspectorTest>().CreateTempFileWithUniqueName().FullName;
            File.WriteAllText(path, "Contents");

            var fileInfo = new FileInfo(path);
            using (var inspector = new LazyFileInspector(fileInfo))
            {
                Stream returnedStream;
                inspector.TryGetStream(out returnedStream);

                inspector.Dispose();

                Assert.DoesNotThrow(() => File.Delete(path), "Should be able to delete the file because the stream was closed.");
            }
        }
        public void TryGetStream_WhenPreviouslyReturnedStreamWasClosed_ReturnsNewStream()
        {
            string path = SpecialPathPolicy.For<LazyFileInspectorTest>().CreateTempFileWithUniqueName().FullName;
            File.WriteAllText(path, "Contents");

            var fileInfo = new FileInfo(path);
            using (var inspector = new LazyFileInspector(fileInfo))
            {
                Stream returnedStream;
                inspector.TryGetStream(out returnedStream);
                returnedStream.Close();

                Stream secondReturnedStream;
                Assert.IsTrue(inspector.TryGetStream(out secondReturnedStream));
                Assert.AreNotSame(returnedStream, secondReturnedStream, "Should have returned a new stream since the previous one was closed.");
            }
        }
        public void TryGetStream_WhenPreviouslyReturnedStreamStillOpen_ReturnsSameStreamAtPositionZero()
        {
            string path = SpecialPathPolicy.For<LazyFileInspectorTest>().CreateTempFileWithUniqueName().FullName;
            File.WriteAllText(path, "Contents");

            var fileInfo = new FileInfo(path);
            using (var inspector = new LazyFileInspector(fileInfo))
            {
                Stream returnedStream;
                inspector.TryGetStream(out returnedStream);
                returnedStream.Position = 2;

                Stream secondReturnedStream;
                Assert.IsTrue(inspector.TryGetStream(out secondReturnedStream));
                Assert.AreEqual(0, secondReturnedStream.Position, "Should have moved stream position back to 0.");
                Assert.AreSame(returnedStream, secondReturnedStream, "Should have returned same stream since it was still open.");
            }
        }