public void ChangedShouldRaiseFileLostFollowedByFileFound()
 {
     MockFileSystemWatcher fsw = new MockFileSystemWatcher();
       PluginDirectory tested = new PluginDirectory(fsw);
       DateTime FileFoundRaised = DateTime.MinValue;
       DateTime FileLostRaised = DateTime.MinValue;
       tested.FileFound += (s, e) => { FileFoundRaised = DateTime.Now; Thread.Sleep(1); };
       tested.FileLost += (s, e) => { FileLostRaised = DateTime.Now; Thread.Sleep(1); };
       fsw.RaiseChanged(GetType().Assembly.Location);
       Assert.AreNotEqual(DateTime.MinValue, FileFoundRaised);
       Assert.AreNotEqual(DateTime.MinValue, FileLostRaised);
       Assert.IsTrue(FileLostRaised < FileFoundRaised);
 }
        public void ShouldWaitRaisingChangedUntilFileIsUnlocked()
        {
            AutoResetEvent raisedEvent = new AutoResetEvent(false);
              MockFileSystemWatcher mockInternal = new MockFileSystemWatcher();
              using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockInternal))
              {
            int ChangedRaised = 0;
            tested.Changed += (s, e) =>
            {
              ChangedRaised++;
              raisedEvent.Set();
            };

            string fileName = Guid.NewGuid().ToString();
            try
            {
              using (var file = System.IO.File.CreateText(fileName))
              {
            file.WriteLine("somedata");

            mockInternal.RaiseChanged(fileName);
            raisedEvent.WaitOne(TimeSpan.FromSeconds(1));
            Thread.Sleep(500);
            Assert.AreEqual(0, ChangedRaised);
              }
              raisedEvent.WaitOne(TimeSpan.FromSeconds(1));
              Assert.AreEqual(1, ChangedRaised);
            }
            finally
            {
              System.IO.File.Delete(fileName);
            }
              }
        }
        public void ShouldNotRaiseEventsOnNonExistingFileUntilPendingDeleted()
        {
            MockFileSystemWatcher mockInternal = new MockFileSystemWatcher();
              using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockInternal))
              {
            int ChangedRaised = 0;
            int DeletedRaised = 0;
            tested.Changed += (s, e) => ChangedRaised++;
            tested.Deleted += (s, e) => DeletedRaised++;

            string fileName = Guid.NewGuid().ToString();
            mockInternal.RaiseChanged(fileName);
            Thread.Sleep(500);
            Assert.AreEqual(0, ChangedRaised);

            mockInternal.RaiseDeleted(fileName);
            Thread.Sleep(500);

            Assert.AreEqual(1, ChangedRaised);
            Assert.AreEqual(1, DeletedRaised);
              }
        }
        public void ShouldOnlyRaiseChangedOnceIfSeveralIsPending()
        {
            MockFileSystemWatcher mockInternal = new MockFileSystemWatcher();
              using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockInternal))
              {
            int ChangedRaised = 0;
            tested.Changed += (s, e) => ChangedRaised++;

            string fileName = Guid.NewGuid().ToString();
            try
            {
              using (var file = System.IO.File.CreateText(fileName))
              {
            file.WriteLine("somedata");
            mockInternal.RaiseChanged(fileName);
            mockInternal.RaiseChanged(fileName);
            mockInternal.RaiseChanged(fileName);
              }
              Thread.Sleep(500);
              Assert.AreEqual(1, ChangedRaised);
            }
            finally
            {
              System.IO.File.Delete(fileName);
            }
              }
        }
        public void ShouldNotEmitAnythingIfDeletedWhileStillPendingRaiseCreated()
        {
            MockFileSystemWatcher mockInternal = new MockFileSystemWatcher();
              using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockInternal))
              {
            int CreatedRaised = 0;
            int ChangedRaised = 0;
            int DeletedRaised = 0;
            tested.Created += (s, e) => CreatedRaised++;
            tested.Changed += (s, e) => ChangedRaised++;
            tested.Deleted += (s, e) => DeletedRaised++;

            string fileName = Guid.NewGuid().ToString();
            try
            {
              using (var file = System.IO.File.CreateText(fileName))
              {
            file.WriteLine("somedata");

            mockInternal.RaiseCreated(fileName);
            mockInternal.RaiseChanged(fileName);
            Thread.Sleep(500);
            Assert.AreEqual(0, CreatedRaised);
            Assert.AreEqual(0, ChangedRaised);
            mockInternal.RaiseDeleted(fileName);
              }
              Thread.Sleep(500);

              Assert.AreEqual(0, CreatedRaised);
              Assert.AreEqual(0, ChangedRaised);
              Assert.AreEqual(0, DeletedRaised);
            }
            finally
            {
              System.IO.File.Delete(fileName);
            }
              }
        }