SftpClientTest_Dispose_Connected.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Moq;
  4. using Renci.SshNet.Sftp;
  5. namespace Renci.SshNet.Tests.Classes
  6. {
  7. [TestClass]
  8. public class SftpClientTest_Dispose_Connected
  9. {
  10. private Mock<IServiceFactory> _serviceFactoryMock;
  11. private Mock<ISession> _sessionMock;
  12. private SftpClient _sftpClient;
  13. private ConnectionInfo _connectionInfo;
  14. private Mock<ISftpSession> _sftpSessionMock;
  15. private TimeSpan _operationTimeout;
  16. [TestInitialize]
  17. public void Setup()
  18. {
  19. Arrange();
  20. Act();
  21. }
  22. [TestCleanup]
  23. public void Cleanup()
  24. {
  25. }
  26. protected void Arrange()
  27. {
  28. _serviceFactoryMock = new Mock<IServiceFactory>(MockBehavior.Strict);
  29. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  30. _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
  31. _connectionInfo = new ConnectionInfo("host", "user", new NoneAuthenticationMethod("userauth"));
  32. _operationTimeout = TimeSpan.FromSeconds(new Random().Next(1, 10));
  33. _sftpClient = new SftpClient(_connectionInfo, false, _serviceFactoryMock.Object);
  34. _sftpClient.OperationTimeout = _operationTimeout;
  35. var sequence = new MockSequence();
  36. _serviceFactoryMock.InSequence(sequence)
  37. .Setup(p => p.CreateSession(_connectionInfo))
  38. .Returns(_sessionMock.Object);
  39. _sessionMock.InSequence(sequence).Setup(p => p.Connect());
  40. _serviceFactoryMock.InSequence(sequence)
  41. .Setup(p => p.CreateSftpSession(_sessionMock.Object, _operationTimeout, _connectionInfo.Encoding))
  42. .Returns(_sftpSessionMock.Object);
  43. _sftpSessionMock.InSequence(sequence).Setup(p => p.Connect());
  44. _sessionMock.InSequence(sequence).Setup(p => p.OnDisconnecting());
  45. _sftpSessionMock.InSequence(sequence).Setup(p => p.Disconnect());
  46. _sftpSessionMock.InSequence(sequence).Setup(p => p.Dispose());
  47. _sessionMock.InSequence(sequence).Setup(p => p.Disconnect());
  48. _sessionMock.InSequence(sequence).Setup(p => p.Dispose());
  49. _sftpClient.Connect();
  50. }
  51. protected void Act()
  52. {
  53. _sftpClient.Dispose();
  54. }
  55. [TestMethod]
  56. public void CreateSftpSessionOnServiceFactoryShouldBeInvokedOnce()
  57. {
  58. _serviceFactoryMock.Verify(
  59. p => p.CreateSftpSession(_sessionMock.Object, _operationTimeout, _connectionInfo.Encoding),
  60. Times.Once);
  61. }
  62. [TestMethod]
  63. public void CreateSessionOnServiceFactoryShouldBeInvokedOnce()
  64. {
  65. _serviceFactoryMock.Verify(p => p.CreateSession(_connectionInfo), Times.Once);
  66. }
  67. [TestMethod]
  68. public void DisconnectOnNetConfSessionShouldBeInvokedOnce()
  69. {
  70. _sftpSessionMock.Verify(p => p.Disconnect(), Times.Once);
  71. }
  72. [TestMethod]
  73. public void DisconnectOnSessionShouldBeInvokedOnce()
  74. {
  75. _sessionMock.Verify(p => p.Disconnect(), Times.Once);
  76. }
  77. [TestMethod]
  78. public void DisposeOnNetConfSessionShouldBeInvokedOnce()
  79. {
  80. _sftpSessionMock.Verify(p => p.Dispose(), Times.Once);
  81. }
  82. [TestMethod]
  83. public void DisposeOnSessionShouldBeInvokedOnce()
  84. {
  85. _sessionMock.Verify(p => p.Dispose(), Times.Once);
  86. }
  87. [TestMethod]
  88. public void OnDisconnectingOnSessionShouldBeInvokedOnce()
  89. {
  90. _sessionMock.Verify(p => p.OnDisconnecting(), Times.Once);
  91. }
  92. }
  93. }