ChannelTest_OnSessionChannelSuccessReceived_OnSuccess_Exception.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using Renci.SshNet.Common;
  6. using Renci.SshNet.Messages.Connection;
  7. namespace Renci.SshNet.Tests.Classes.Channels
  8. {
  9. [TestClass]
  10. public class ChannelTest_OnSessionChannelSuccessReceived_OnSuccess_Exception
  11. {
  12. private Mock<ISession> _sessionMock;
  13. private uint _localWindowSize;
  14. private uint _localPacketSize;
  15. private uint _localChannelNumber;
  16. private ChannelStub _channel;
  17. private IList<ExceptionEventArgs> _channelExceptionRegister;
  18. private Exception _onSuccessException;
  19. [TestInitialize]
  20. public void Initialize()
  21. {
  22. Arrange();
  23. Act();
  24. }
  25. private void Arrange()
  26. {
  27. var random = new Random();
  28. _localChannelNumber = (uint)random.Next(0, int.MaxValue);
  29. _localWindowSize = (uint)random.Next(0, 1000);
  30. _localPacketSize = (uint)random.Next(1001, int.MaxValue);
  31. _onSuccessException = new SystemException();
  32. _channelExceptionRegister = new List<ExceptionEventArgs>();
  33. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  34. _channel = new ChannelStub(_sessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  35. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  36. _channel.OnSuccessException = _onSuccessException;
  37. }
  38. private void Act()
  39. {
  40. _sessionMock.Raise(s => s.ChannelSuccessReceived += null,
  41. new MessageEventArgs<ChannelSuccessMessage>(new ChannelSuccessMessage(_localChannelNumber)));
  42. }
  43. [TestMethod]
  44. public void ExceptionEventShouldHaveFiredOnce()
  45. {
  46. Assert.AreEqual(1, _channelExceptionRegister.Count);
  47. Assert.AreSame(_onSuccessException, _channelExceptionRegister[0].Exception);
  48. }
  49. [TestMethod]
  50. public void OnErrorOccuredShouldBeInvokedOnce()
  51. {
  52. Assert.AreEqual(1, _channel.OnErrorOccurredInvocations.Count);
  53. Assert.AreSame(_onSuccessException, _channel.OnErrorOccurredInvocations[0]);
  54. }
  55. }
  56. }