public void WatchedDirectoryProcessAvailableImagesWhenIOExceptionThrownShouldSortFile() { // Arrange var testBundle = new WatchedDirectoryTestBundle(); var watchDirectory = new WatchDirectory { FileExtensions = "abc", Mode = OperationMode.UploadOnly }; var mockWatchedFileRepository = new Mock <IWatchedFileRepository>(); var mockWatchedFile = new Mock <IWatchedFile>(); var mockLog = new Mock <ILog>(); var mockServer = new Mock <ImageServer>(); testBundle.MockDirectoryScanner.Setup(x => x.GetAvailableImages(mockWatchedFileRepository.Object, null, It.IsAny <IList <string> >(), false, null)) .Returns(new List <IWatchedFile> { mockWatchedFile.Object }); testBundle.MockLogProvider.Setup(x => x.GetLogger(It.IsAny <Type>())).Returns(mockLog.Object); mockWatchedFile.SetupGet(x => x.UploadSuccessful).Returns(false); mockWatchedFile.Setup(x => x.SendToServer(mockServer.Object)).Throws(new IOException()); // Act testBundle.WatchedDirectory.Configure(watchDirectory); testBundle.WatchedDirectory.ProcessAvailableImages(mockWatchedFileRepository.Object, mockServer.Object); // Assert mockWatchedFileRepository.Verify(x => x.LoadFileForPath(It.IsAny <string>()), Times.Never); mockWatchedFileRepository.Verify(x => x.CreateNew(), Times.Never); mockWatchedFileRepository.Verify(x => x.Save(mockWatchedFile.Object), Times.Once); mockWatchedFile.VerifySet(x => x.UploadSuccessful = false, Times.Once); mockWatchedFile.Verify(x => x.SendToServer(mockServer.Object), Times.Once); mockWatchedFile.Verify(x => x.SortFile(), Times.Never); mockWatchedFile.Verify(x => x.RemoveFromDisk(), Times.Never); }
public WatchDirectory ProvideDefaultWatchDirectory() { var wd = new WatchDirectory(_environment); wd.PopulateWithDefaults(); return(wd); }
private static void Main(string[] args) { WatchDirectory watchDir = new WatchDirectory(); ThreadPool.QueueUserWorkItem((e) => { watchDir.StartDirectoryWatcher(); }); var key = Console.ReadKey(); watchDir.Stop(); }
public override int GetHashCode() { unchecked { var hashCode = Filters?.GetHashCode() ?? 0; hashCode = (hashCode * 397) ^ AutoAddFiles.GetHashCode(); hashCode = (hashCode * 397) ^ AutoDeleteFiles.GetHashCode(); hashCode = (hashCode * 397) ^ Recursive.GetHashCode(); hashCode = (hashCode * 397) ^ (WatchDirectory?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (SyncDirectory?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (Delay.GetHashCode()); return(hashCode); } }
public void WatchedDirectoryProcessAvailableImagesWhenImageIsInBaseDirectoryAndModeIsUploadAndClearShouldThrowNotImplementedException() { // Arrange var testBundle = new WatchedDirectoryTestBundle(); var watchDirectory = new WatchDirectory { FileExtensions = "abc", Mode = OperationMode.UploadAndClear }; var mockWatchedFileRepository = new Mock <IWatchedFileRepository>(); var mockWatchedFile = new Mock <IWatchedFile>(); var mockLog = new Mock <ILog>(); NotImplementedException thrownException = null; var mockServer = new Mock <ImageServer>(); testBundle.MockDirectoryScanner.Setup(x => x.GetAvailableImages(mockWatchedFileRepository.Object, null, It.IsAny <IList <string> >(), false, null)) .Returns(new List <IWatchedFile> { mockWatchedFile.Object }); testBundle.MockLogProvider.Setup(x => x.GetLogger(It.IsAny <Type>())).Returns(mockLog.Object); mockWatchedFile.Setup(x => x.IsInBaseDirectory(null)).Returns(true); testBundle.WatchedDirectory.Configure(watchDirectory); // Act try { testBundle.WatchedDirectory.ProcessAvailableImages(mockWatchedFileRepository.Object, mockServer.Object); } catch (NotImplementedException ex) { thrownException = ex; } // Assert mockWatchedFileRepository.Verify(x => x.LoadFileForPath(It.IsAny <string>()), Times.Never); mockWatchedFileRepository.Verify(x => x.CreateNew(), Times.Never); mockWatchedFileRepository.Verify(x => x.Save(mockWatchedFile.Object), Times.Never); mockWatchedFile.VerifySet(x => x.UploadSuccessful = false, Times.Never); mockWatchedFile.Verify(x => x.SendToServer(mockServer.Object), Times.Never); mockWatchedFile.Verify(x => x.SortFile(), Times.Never); mockWatchedFile.Verify(x => x.RemoveFromDisk(), Times.Never); Assert.IsNotNull(thrownException); }
public void AddDirectory(WatchDirectory directory) { if (directory == null) { throw new ArgumentNullException("WatchDirectory"); } if (String.IsNullOrWhiteSpace(directory.Path)) { throw new ArgumentException("Path cannot be empty.", "directory"); } if (String.IsNullOrWhiteSpace(directory.FileMask)) { throw new ArgumentException("FileMask cannot be empty.", "directory"); } if (!Directory.Exists(directory.Path)) { throw new DirectoryNotFoundException(String.Format("Directory ({0}) does not exist.", directory.Path)); } var fileMasks = directory.FileMask.Split(';'); foreach (var fileMask in fileMasks) { if (_watchers.Any(x => x.Path == directory.Path && x.Filter == fileMask)) { throw new ArgumentException(String.Format("Directory ({0}) is already being watched.", Path.Combine(directory.Path, fileMask))); } var watcher = new FileSystemWatcher(directory.Path, fileMask) { IncludeSubdirectories = directory.WatchSubDirectories }; watcher.Created += FileCreated; watcher.Changed += FileChanged; watcher.Deleted += FileDeleted; watcher.Renamed += FileRenamed; _watchers.Add(watcher); } Directories.Add(directory); }
public void Watcher_StartDirectoryWatcher_ShouldHandleEvent() { // Arrange var watchDirectory = new WatchDirectory(_fileProcessorMoq.Object); // Act ThreadPool.QueueUserWorkItem((e) => { Thread.Sleep(2000); using (var file = File.CreateText(Path.Combine(watchDirectory.WorkDirectory, "test.txt"))) { file.WriteLine("Hello from test method: Watcher_StartDirectoryWatcher_ShouldHandleEvent "); } Thread.Sleep(1000); watchDirectory.Stop(); }); watchDirectory.StartDirectoryWatcher(); // Assert _fileProcessorMoq.Verify(x => x.Process(It.IsAny <string>())); }
public DirectoryWatcher(WatchDirectory _directory) : this(new List <WatchDirectory> { _directory }) { }
public WatchDirectoryTestBundle() { MockEnvironment = new Mock <IEnvironment>(); WatchDirectory = new WatchDirectory(MockEnvironment.Object); }