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); } }
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 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 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_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."); } }