2
0

SftpFileStreamTest_Dispose_Disposed.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.IO;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using Renci.SshNet.Sftp;
  6. namespace Renci.SshNet.Tests.Classes.Sftp
  7. {
  8. [TestClass]
  9. public class SftpFileStreamTest_Dispose_Disposed : SftpFileStreamTestBase
  10. {
  11. private SftpFileStream _target;
  12. private string _path;
  13. private byte[] _handle;
  14. private uint _bufferSize;
  15. private uint _readBufferSize;
  16. private uint _writeBufferSize;
  17. protected override void SetupData()
  18. {
  19. base.SetupData();
  20. var random = new Random();
  21. _path = random.Next().ToString();
  22. _handle = GenerateRandom(1, random);
  23. _bufferSize = (uint)random.Next(1, 1000);
  24. _readBufferSize = (uint)random.Next(1, 1000);
  25. _writeBufferSize = (uint)random.Next(1, 1000);
  26. }
  27. protected override void SetupMocks()
  28. {
  29. SftpSessionMock.InSequence(MockSequence)
  30. .Setup(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNewOrOpen, false))
  31. .Returns(_handle);
  32. SftpSessionMock.InSequence(MockSequence)
  33. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  34. .Returns(_readBufferSize);
  35. SftpSessionMock.InSequence(MockSequence)
  36. .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle))
  37. .Returns(_writeBufferSize);
  38. SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
  39. SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestClose(_handle));
  40. }
  41. protected override void Arrange()
  42. {
  43. base.Arrange();
  44. _target = new SftpFileStream(SftpSessionMock.Object,
  45. _path,
  46. FileMode.OpenOrCreate,
  47. FileAccess.Write,
  48. (int)_bufferSize);
  49. _target.Dispose();
  50. }
  51. protected override void Act()
  52. {
  53. _target.Dispose();
  54. }
  55. [TestMethod]
  56. public void IsOpenOnSftpSessionShouldBeInvokedOnce()
  57. {
  58. SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
  59. }
  60. [TestMethod]
  61. public void RequestCloseOnSftpSessionShouldBeInvokedOnce()
  62. {
  63. SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
  64. }
  65. }
  66. }