/// <summary> /// Initializes a new instance of the <see cref="NetWatcher"/> class. /// Takes the given file system watcher and listens for events and passes them to the given queue /// </summary> /// <param name="watcher">File System Watcher.</param> /// <param name="queue">Queue for the occured events.</param> /// <param name="storage">Meta Data Storage to verify, if a deleted object is a file or folder.</param> /// <param name="fsFactory">File system info factory. If factory is null, the normal file system is used, otherwise the given factory.</param> public NetWatcher( FileSystemWatcher watcher, ISyncEventQueue queue, IMetaDataStorage storage, FileSystemInfoFactory fsFactory = null) { if (watcher == null) { throw new ArgumentNullException("watcher"); } if (string.IsNullOrEmpty(watcher.Path)) { throw new ArgumentException("The given watcher must contain a path, where it is listening"); } if (queue == null) { throw new ArgumentNullException("queue"); } if (storage == null) { throw new ArgumentNullException("storage"); } this.fsFactory = fsFactory ?? new FileSystemInfoFactory(); this.queue = queue; this.storage = storage; this.fileSystemWatcher = watcher; this.fileSystemWatcher.IncludeSubdirectories = true; this.fileSystemWatcher.Filter = "*"; this.fileSystemWatcher.InternalBufferSize = 4 * 1024 * 16; this.fileSystemWatcher.NotifyFilter = NotifyFilters.Size | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite | NotifyFilters.Security; this.createChangeDeleteHandler = new CreatedChangedDeletedFileSystemEventHandler(this.queue, this.storage, this.fsFactory); this.renamedHandler = new RenamedFileSystemEventHandler(this.queue, this.fsFactory); this.fileSystemWatcher.Created += new FileSystemEventHandler(this.createChangeDeleteHandler.Handle); this.fileSystemWatcher.Deleted += new FileSystemEventHandler(this.createChangeDeleteHandler.Handle); this.fileSystemWatcher.Changed += new FileSystemEventHandler(this.createChangeDeleteHandler.Handle); this.fileSystemWatcher.Renamed += new RenamedEventHandler(this.renamedHandler.Handle); }
public void OneFileIsCopiedAFewTimes([Values(true, false)]bool contentChanges, [Values(1,2,5,10)]int times) { this.ContentChangesActive = contentChanges; FileSystemInfoFactory fsFactory = new FileSystemInfoFactory(); var fileNames = new List<string>(); string fileName = "file"; string content = "content"; this.remoteRootDir.CreateDocument(fileName + ".bin", content); this.InitializeAndRunRepo(); var file = this.localRootDir.GetFiles().First(); fileNames.Add(file.FullName); var fileInfo = fsFactory.CreateFileInfo(file.FullName); Guid uuid = (Guid)fileInfo.Uuid; for (int i = 0; i < times; i++) { var fileCopy = fsFactory.CreateFileInfo(Path.Combine(this.localRootDir.FullName, string.Format("{0}{1}.bin", fileName, i))); file.CopyTo(fileCopy.FullName); Thread.Sleep(50); fileCopy.Refresh(); fileCopy.Uuid = uuid; fileNames.Add(fileCopy.FullName); } Thread.Sleep(500); this.AddStartNextSyncEvent(forceCrawl: true); this.repo.Run(); Assert.That(this.localRootDir.GetFiles().Length, Is.EqualTo(fileNames.Count)); foreach (var localFile in this.localRootDir.GetFiles()) { Assert.That(fileNames.Contains(localFile.FullName)); var syncedFileInfo = fsFactory.CreateFileInfo(localFile.FullName); Assert.That(syncedFileInfo.Length, Is.EqualTo(content.Length)); if (localFile.FullName.Equals(file.FullName)) { Assert.That(syncedFileInfo.Uuid, Is.EqualTo(uuid)); } else { Assert.That(syncedFileInfo.Uuid, Is.Not.Null); Assert.That(syncedFileInfo.Uuid, Is.Not.EqualTo(uuid)); } } Assert.That(this.repo.NumberOfChanges, Is.EqualTo(0)); }
public void OneFileIsCopiedAndTheCopyIsRemoved([Values(true, false)]bool contentChanges) { this.ContentChangesActive = contentChanges; FileSystemInfoFactory fsFactory = new FileSystemInfoFactory(); var fileNames = new List<string>(); string fileName = "file"; string content = "content"; this.remoteRootDir.CreateDocument(fileName + ".bin", content); this.InitializeAndRunRepo(); var file = this.localRootDir.GetFiles().First(); fileNames.Add(file.FullName); var fileInfo = fsFactory.CreateFileInfo(file.FullName); Guid uuid = (Guid)fileInfo.Uuid; var fileCopy = fsFactory.CreateFileInfo(Path.Combine(this.localRootDir.FullName, fileName + " - copy.bin")); file.CopyTo(fileCopy.FullName); fileCopy.Refresh(); fileCopy.Uuid = uuid; fileCopy.Delete(); Thread.Sleep(500); this.repo.SingleStepQueue.SwallowExceptions = true; this.AddStartNextSyncEvent(); this.repo.Run(); Assert.That(this.localRootDir.GetFiles().Length, Is.EqualTo(1)); var child = this.localRootDir.GetFiles().First(); Assert.That(child.Length, Is.EqualTo(content.Length)); Assert.That(child.Name, Is.EqualTo(fileName + ".bin")); }