public void CreatedShouldRaiseFileFound()
 {
     MockFileSystemWatcher fsw = new MockFileSystemWatcher();
       PluginDirectory tested = new PluginDirectory(fsw);
       int FileFoundRaised = 0;
       tested.FileFound += (s, e) => FileFoundRaised++;
       fsw.RaiseCreated(GetType().Assembly.Location);
       Assert.AreEqual(1, FileFoundRaised);
 }
 public void RaseEventThrowsArgumentExceptionOnUnknownEvent()
 {
     MockFileSystemWatcher mockWatcher = new MockFileSystemWatcher();
       using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockWatcher))
       {
     var evt = new FileSystemEventArgs((WatcherChangeTypes)666, "", "");
     DoAssert.Throws<ArgumentException>(() => tested.RaiseEvent(evt));
       }
 }
 public void DisposeShouldRaiseDisposed()
 {
     MockFileSystemWatcher mockInternal = new MockFileSystemWatcher();
       using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockInternal))
       {
     bool wasDisposed = false;
     tested.Disposed += (s, e) => wasDisposed = true;
     tested.Dispose();
     Assert.IsTrue(wasDisposed);
       }
 }
 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 ShouldDelayRaiseRenamedUntilFileIsUnlocked()
        {
            AutoResetEvent raisedEvent = new AutoResetEvent(false);
              MockFileSystemWatcher mockInternal = new MockFileSystemWatcher();
              using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockInternal))
              {
            int RenamedRaised = 0;
            tested.Renamed += (s, e) =>
            {
              RenamedRaised++;
              raisedEvent.Set();
            };

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

            mockInternal.RaiseRenamed(fileName, newFileName);
            Assert.AreEqual(0, RenamedRaised);
            raisedEvent.WaitOne(500);
              }
              raisedEvent.WaitOne(500);
              Assert.AreEqual(1, RenamedRaised);
            }
            finally
            {
              System.IO.File.Delete(fileName);
            }
              }
        }
 public void ShouldListenToAllEventsOnInternalWatcher()
 {
     MockFileSystemWatcher mockWatcher = new MockFileSystemWatcher();
       using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockWatcher))
       {
     Assert.AreEqual(1, mockWatcher.CreatedListeners().Length);
     Assert.AreEqual(1, mockWatcher.ChangedListeners().Length);
     Assert.AreEqual(1, mockWatcher.DeletedListeners().Length);
     Assert.AreEqual(1, mockWatcher.RenamedListeners().Length);
       }
 }
 public void ShouldNotDisposeExternalIFileSystemWatcher()
 {
     MockFileSystemWatcher mockwatcher = new MockFileSystemWatcher();
       IPluginDirectory tested = new PluginDirectory(mockwatcher);
       bool wasDisposed = false;
       mockwatcher.Disposed += (s, e) => wasDisposed = true;
       tested.Dispose();
       Assert.IsFalse(wasDisposed);
 }
        public void FileLostShouldNotBeRasedForNonDlls()
        {
            MockFileSystemWatcher fsw = new MockFileSystemWatcher();

              PluginDirectory tested = new PluginDirectory(fsw);
              int FileLostRaised = 0;
              tested.FileLost += (s, e) => FileLostRaised++;

              fsw.RaiseDeleted(@"file.img");

              Assert.AreEqual(0, FileLostRaised);
        }
 public void ShouldUpdateFilterOnInternal()
 {
     MockFileSystemWatcher mockWatcher = new MockFileSystemWatcher();
       using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockWatcher))
       {
     tested.Filter = "FAKEFILTER";
     Assert.AreEqual("FAKEFILTER", mockWatcher.Filter);
       }
 }
 public void ShouldUpdateEnableRaisingEventsOnInternal()
 {
     MockFileSystemWatcher mockWatcher = new MockFileSystemWatcher();
       using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockWatcher))
       {
     tested.EnableRaisingEvents = false;
     Assert.AreEqual(false, mockWatcher.EnableRaisingEvents);
     tested.EnableRaisingEvents = true;
     Assert.AreEqual(true, mockWatcher.EnableRaisingEvents);
       }
 }
        public void ShouldRaiseDeletedIfInternalRaisesDeleted()
        {
            AutoResetEvent raisedEvent = new AutoResetEvent(false);
              MockFileSystemWatcher mockInternal = new MockFileSystemWatcher();
              using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockInternal))
              {
            int DeletedRaised = 0;
            tested.Deleted += (s, e) =>
            {
              DeletedRaised++;
              raisedEvent.Set();
            };

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

              mockInternal.RaiseDeleted(fileName);
              raisedEvent.WaitOne(TimeSpan.FromSeconds(1));

              Assert.AreEqual(1, DeletedRaised);
            }
            finally
            {
              System.IO.File.Delete(fileName);
            }
              }
        }
        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 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 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);
            }
              }
        }
        public void FileFoundShouldNotBeReportedAfterRemoval()
        {
            MockFileSystemWatcher fsw = new MockFileSystemWatcher();

              PluginDirectory tested = new PluginDirectory(fsw);
              int FileFoundRaised = 0;

              EventHandler<PluginDirectoryEventArgs> handler = ((s, e) => FileFoundRaised++);
              tested.FileFound += handler;
              tested.FileFound -= handler;

              fsw.RaiseCreated(@"file.dll");
              Assert.AreEqual(0, FileFoundRaised);
        }
 public void ShouldUpdateIncludeSubdirectoriesOnInternal()
 {
     MockFileSystemWatcher mockWatcher = new MockFileSystemWatcher();
       using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockWatcher))
       {
     tested.IncludeSubdirectories = false;
     Assert.AreEqual(false, mockWatcher.IncludeSubdirectories);
     tested.IncludeSubdirectories = true;
     Assert.AreEqual(true, mockWatcher.IncludeSubdirectories);
       }
 }
        public void FileFoundShouldReportCurrentFilesRecursiveOnAdd()
        {
            MockFileSystemWatcher fsw = new MockFileSystemWatcher();
              fsw.Path = new FileInfo(GetType().Assembly.Location).Directory.Parent.FullName;
              fsw.IncludeSubdirectories = true;

              PluginDirectory tested = new PluginDirectory(fsw);
              int FileFoundRaised = 0;
              tested.FileFound += (s, e) => FileFoundRaised++;

              Assert.AreNotEqual(0, FileFoundRaised);
        }
 public void ShouldUpdateNotifyFilterOnInternal()
 {
     MockFileSystemWatcher mockWatcher = new MockFileSystemWatcher();
       using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockWatcher))
       {
     tested.NotifyFilter = NotifyFilters.DirectoryName;
     Assert.AreEqual(NotifyFilters.DirectoryName, mockWatcher.NotifyFilter);
     tested.NotifyFilter = NotifyFilters.LastAccess;
     Assert.AreEqual(NotifyFilters.LastAccess, mockWatcher.NotifyFilter);
       }
 }
 public void ShouldDisposeOwnedFileSystemWatcher()
 {
     MockFileSystemWatcher mockwatcher = new MockFileSystemWatcher();
       PluginDirectory tested = new PluginDirectory(new MockFileSystemWatcher());
       bool wasDisposed = false;
       tested.WatcherConnect(mockwatcher, true);
       mockwatcher.Disposed += (s, e) => wasDisposed = true;
       tested.Dispose();
       Assert.IsTrue(wasDisposed);
 }
 public void ShouldUpdatePathOnInternal()
 {
     MockFileSystemWatcher mockWatcher = new MockFileSystemWatcher();
       using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockWatcher))
       {
     tested.Path = "FAKEPATH";
     Assert.AreEqual("FAKEPATH", mockWatcher.Path);
       }
 }
 public void ShouldLogToDebugWhenRaisingFileLost()
 {
     string expected = "SomePath";
       MockFileSystemWatcher mockWatcher = new MockFileSystemWatcher();
       PluginDirectory tested = new PluginDirectory(mockWatcher);
       MockLog mockLog = new MockLog(tested);
       mockWatcher.RaiseDeleted(expected);
       Assert.IsTrue(mockLog.Any(x => x.Level == MockLog.Level.Debug && x.Message.Contains(expected)));
 }
        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 ConstructionWithIFileSystemWatcher()
 {
     MockFileSystemWatcher mockwatcher = new MockFileSystemWatcher();
       IPluginDirectory tested = new PluginDirectory(mockwatcher);
 }
 public void RaiseEventRequiresArgument()
 {
     MockFileSystemWatcher mockWatcher = new MockFileSystemWatcher();
       using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockWatcher))
     DoAssert.Throws<ArgumentNullException>(() => tested.RaiseEvent(null));
 }
        public void FileFoundShouldBeRasedForDlls()
        {
            MockFileSystemWatcher fsw = new MockFileSystemWatcher();

              PluginDirectory tested = new PluginDirectory(fsw);
              int FileFoundRaised = 0;
              tested.FileFound += (s, e) => FileFoundRaised++;

              fsw.RaiseCreated(@"file.dll");

              Assert.AreEqual(1, FileFoundRaised);
        }
 public void ShouldExposeEnableRaisingEventsFromInternal()
 {
     MockFileSystemWatcher mockWatcher = new MockFileSystemWatcher();
       using (SafeEventFileSystemWatcher tested = new SafeEventFileSystemWatcher(mockWatcher))
       {
     mockWatcher.EnableRaisingEvents = true;
     Assert.AreEqual(true, tested.EnableRaisingEvents);
     mockWatcher.EnableRaisingEvents = false;
     Assert.AreEqual(false, tested.EnableRaisingEvents);
       }
 }