| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Globalization;
- using System.IO;
- using System.Text;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Moq;
- using Renci.SshNet.Channels;
- using Renci.SshNet.Common;
- using Renci.SshNet.Tests.Common;
- namespace Renci.SshNet.Tests.Classes
- {
- [TestClass]
- public class SshCommandTest_Dispose : TestBase
- {
- private Mock<ISession> _sessionMock;
- private Mock<IChannelSession> _channelSessionMock;
- private string _commandText;
- private Encoding _encoding;
- private SshCommand _sshCommand;
- private Stream _outputStream;
- private Stream _extendedOutputStream;
- protected override void OnInit()
- {
- base.OnInit();
- Arrange();
- Act();
- }
- private void Arrange()
- {
- _sessionMock = new Mock<ISession>(MockBehavior.Strict);
- _commandText = new Random().Next().ToString(CultureInfo.InvariantCulture);
- _encoding = Encoding.UTF8;
- _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
- var seq = new MockSequence();
- _sessionMock.InSequence(seq).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
- _channelSessionMock.InSequence(seq).Setup(p => p.Open());
- _channelSessionMock.InSequence(seq).Setup(p => p.SendExecRequest(_commandText)).Returns(true);
- _channelSessionMock.InSequence(seq).Setup(p => p.Dispose());
- _sshCommand = new SshCommand(_sessionMock.Object, _commandText, _encoding);
- _sshCommand.BeginExecute();
- _outputStream = _sshCommand.OutputStream;
- _extendedOutputStream = _sshCommand.ExtendedOutputStream;
- }
- private void Act()
- {
- _sshCommand.Dispose();
- }
- [TestMethod]
- public void ChannelSessionShouldBeDisposedOnce()
- {
- _channelSessionMock.Verify(p => p.Dispose(), Times.Once);
- }
- [TestMethod]
- public void OutputStreamShouldHaveBeenDisposed()
- {
- Assert.AreEqual(-1, _outputStream.ReadByte());
- }
- [TestMethod]
- public void ExtendedOutputStreamShouldHaveBeenDisposed()
- {
- Assert.AreEqual(-1, _extendedOutputStream.ReadByte());
- }
- [TestMethod]
- public void RaisingDisconnectedOnSessionShouldDoNothing()
- {
- _sessionMock.Raise(s => s.Disconnected += null, new EventArgs());
- }
- [TestMethod]
- public void RaisingErrorOccuredOnSessionShouldDoNothing()
- {
- _sessionMock.Raise(s => s.ErrorOccured += null, new ExceptionEventArgs(new Exception()));
- }
- [TestMethod]
- public void InvokingDisposeAgainShouldNotRaiseAnException()
- {
- _sshCommand.Dispose();
- }
- }
- }
|