Beispiel #1
0
 public void IOExceptionThrownIfIOExceptionOccursOnRead()
 {
     this.mockedMemStream.Setup(memstream => memstream.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())).Throws <IOException>();
     using (IFileDownloader downloader = new SimpleFileDownloader()) {
         Assert.Throws <IOException>(() => downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg));
     }
 }
        public void NormalDownloadTest()
        {
            double lastPercent = 0;

            this.transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs e) {
                if (e.ActualPosition != null)
                {
                    Assert.GreaterOrEqual((long)e.ActualPosition, 0);
                    Assert.LessOrEqual((long)e.ActualPosition, this.remoteLength);
                }

                if (e.Percent != null)
                {
                    Assert.GreaterOrEqual(e.Percent, 0);
                    Assert.LessOrEqual(e.Percent, 100);
                    Assert.GreaterOrEqual(e.Percent, lastPercent);
                    lastPercent = (double)e.Percent;
                }

                if (e.Length != null)
                {
                    Assert.GreaterOrEqual(e.Length, 0);
                    Assert.LessOrEqual(e.Length, this.remoteLength);
                }
            };

            using (IFileDownloader downloader = new SimpleFileDownloader())
            {
                downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmissionEvent, this.hashAlg);
                Assert.AreEqual(this.remoteContent.Length, this.localFileStream.Length);
                Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.remoteContent), this.hashAlg.Hash);
                Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.localFileStream.ToArray()), this.hashAlg.Hash);
            }
        }
        public void AbortWhileDownloadTest()
        {
            this.mockedMemStream.Setup(memstream => memstream.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())).Callback(() => Thread.Sleep(1)).Returns(1);
            this.transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs e)
            {
                Assert.AreEqual(null, e.Completed);
            };
            try
            {
                Task            t;
                IFileDownloader downloader = new SimpleFileDownloader();
                t = Task.Factory.StartNew(() => downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmissionEvent, this.hashAlg));
                t.Wait(100);
                this.transmissionEvent.ReportProgress(new TransmissionProgressEventArgs()
                {
                    Aborting = true
                });
                t.Wait();
                Assert.Fail();
            }
            catch (AggregateException e)
            {
                Assert.IsInstanceOf(typeof(AbortException), e.InnerException);
                Assert.True(this.transmissionEvent.Status.Aborted.GetValueOrDefault());
                Assert.AreEqual(false, this.transmissionEvent.Status.Aborting);
                return;
            }

            Assert.Fail();
        }
 public void ServerFailedExceptionTest()
 {
     this.mockedMemStream.Setup(memstream => memstream.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())).Throws <CmisConnectionException>();
     using (IFileDownloader downloader = new SimpleFileDownloader())
     {
         downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmissionEvent, this.hashAlg);
     }
 }
Beispiel #5
0
 public void DownloadWithThreeUpdate()
 {
     SetUp(2 * 1024 * 1024 + 1);
     this.hashAlg = new SHA1Reuse();
     using (IFileDownloader downloader = new SimpleFileDownloader()) {
         int count = 0;
         downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg, (byte[] checksum, long length) => { ++count; });
         Assert.AreEqual(3, count);
         Assert.AreEqual(this.remoteContent.Length, this.localFileStream.Length);
         Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.remoteContent), this.hashAlg.Hash);
         Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.localFileStream.ToArray()), this.hashAlg.Hash);
     }
 }
Beispiel #6
0
        public void DisposeWhileDownload()
        {
            this.mockedMemStream.Setup(memstream => memstream.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())).Callback(() => Thread.Sleep(1)).Returns(1);
            try {
                Task t;
                using (IFileDownloader downloader = new SimpleFileDownloader()) {
                    t = Task.Factory.StartNew(() => downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg));
                }

                t.Wait();
                Assert.Fail();
            } catch (AggregateException e) {
                Assert.IsInstanceOf(typeof(ObjectDisposedException), e.InnerException);
            }
        }
Beispiel #7
0
        public void NormalDownload()
        {
            double lastPercent = 0;

            this.transmission.AddPositionConstraint(Is.LessThanOrEqualTo(this.remoteLength));
            this.transmission.AddLengthConstraint(Is.EqualTo(this.remoteLength).Or.EqualTo(0));

            this.transmission.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
                var t = sender as Transmission;
                if (e.PropertyName == Utils.NameOf(() => t.Percent))
                {
                    Assert.That(t.Percent, Is.Null.Or.GreaterThanOrEqualTo(lastPercent));
                    lastPercent = t.Percent.GetValueOrDefault();
                }
            };

            using (IFileDownloader downloader = new SimpleFileDownloader()) {
                downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg);
                Assert.AreEqual(this.remoteContent.Length, this.localFileStream.Length);
                Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.remoteContent), this.hashAlg.Hash);
                Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.localFileStream.ToArray()), this.hashAlg.Hash);
            }
        }
Beispiel #8
0
        public void AbortWhileDownload()
        {
            this.mockedMemStream.Setup(memstream => memstream.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())).Callback(() => Thread.Sleep(1)).Returns(1);
            this.transmission.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
                Assert.That((sender as Transmission).Status, Is.Not.EqualTo(TransmissionStatus.FINISHED));
            };

            try {
                Task            t;
                IFileDownloader downloader = new SimpleFileDownloader();
                t = Task.Factory.StartNew(() => downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg));
                t.Wait(100);
                this.transmission.Abort();
                t.Wait();
                Assert.Fail();
            } catch (AggregateException e) {
                Assert.IsInstanceOf(typeof(AbortException), e.InnerException);
                Assert.That(this.transmission.Status, Is.EqualTo(TransmissionStatus.ABORTED));
                return;
            }

            Assert.Fail();
        }