ServiceFactoryTest_CreateSftpFileReader_FileSizeIsZero.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Moq;
  4. using Renci.SshNet.Abstractions;
  5. using Renci.SshNet.Sftp;
  6. using Renci.SshNet.Tests.Common;
  7. namespace Renci.SshNet.Tests.Classes
  8. {
  9. [TestClass]
  10. public class ServiceFactoryTest_CreateSftpFileReader_FileSizeIsZero
  11. {
  12. private ServiceFactory _serviceFactory;
  13. private Mock<ISftpSession> _sftpSessionMock;
  14. private Mock<ISftpFileReader> _sftpFileReaderMock;
  15. private uint _bufferSize;
  16. private string _fileName;
  17. private SftpOpenAsyncResult _openAsyncResult;
  18. private byte[] _handle;
  19. private SFtpStatAsyncResult _statAsyncResult;
  20. private uint _chunkSize;
  21. private long _fileSize;
  22. private SftpFileAttributes _fileAttributes;
  23. private ISftpFileReader _actual;
  24. private void SetupData()
  25. {
  26. var random = new Random();
  27. _bufferSize = (uint) random.Next(1, int.MaxValue);
  28. _openAsyncResult = new SftpOpenAsyncResult(null, null);
  29. _handle = CryptoAbstraction.GenerateRandom(random.Next(1, 10));
  30. _statAsyncResult = new SFtpStatAsyncResult(null, null);
  31. _fileName = random.Next().ToString();
  32. _chunkSize = (uint) random.Next(1, int.MaxValue);
  33. _fileSize = 0L;
  34. _fileAttributes = new SftpFileAttributesBuilder().WithSize(_fileSize).Build();
  35. }
  36. private void CreateMocks()
  37. {
  38. _sftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict);
  39. _sftpFileReaderMock = new Mock<ISftpFileReader>(MockBehavior.Strict);
  40. }
  41. private void SetupMocks()
  42. {
  43. var seq = new MockSequence();
  44. _sftpSessionMock.InSequence(seq)
  45. .Setup(p => p.BeginOpen(_fileName, Flags.Read, null, null))
  46. .Returns(_openAsyncResult);
  47. _sftpSessionMock.InSequence(seq)
  48. .Setup(p => p.EndOpen(_openAsyncResult))
  49. .Returns(_handle);
  50. _sftpSessionMock.InSequence(seq)
  51. .Setup(p => p.BeginLStat(_fileName, null, null))
  52. .Returns(_statAsyncResult);
  53. _sftpSessionMock.InSequence(seq)
  54. .Setup(p => p.CalculateOptimalReadLength(_bufferSize))
  55. .Returns(_chunkSize);
  56. _sftpSessionMock.InSequence(seq)
  57. .Setup(p => p.EndLStat(_statAsyncResult))
  58. .Returns(_fileAttributes);
  59. _sftpSessionMock.InSequence(seq)
  60. .Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 1, _fileSize))
  61. .Returns(_sftpFileReaderMock.Object);
  62. }
  63. private void Arrange()
  64. {
  65. SetupData();
  66. CreateMocks();
  67. SetupMocks();
  68. _serviceFactory = new ServiceFactory();
  69. }
  70. [TestInitialize]
  71. public void Initialize()
  72. {
  73. Arrange();
  74. Act();
  75. }
  76. private void Act()
  77. {
  78. _actual = _serviceFactory.CreateSftpFileReader(_fileName, _sftpSessionMock.Object, _bufferSize);
  79. }
  80. [TestMethod]
  81. public void CreateSftpFileReaderShouldReturnCreatedInstance()
  82. {
  83. Assert.IsNotNull(_actual);
  84. Assert.AreSame(_sftpFileReaderMock.Object, _actual);
  85. }
  86. }
  87. }