public void OneFileIsCopiedAndTheCopyIsRemoved() { FileSystemInfoFactory fsFactory = new FileSystemInfoFactory(); var fileNames = new List <string>(); string fileName = "file"; string content = "content"; this.remoteRootDir.CreateDocument(fileName + ".txt", content); this.repo.Initialize(); this.repo.Run(); 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.txt")); file.CopyTo(fileCopy.FullName); fileCopy.Refresh(); fileCopy.Uuid = uuid; fileCopy.Delete(); Thread.Sleep(500); this.repo.SingleStepQueue.SwallowExceptions = true; this.repo.SingleStepQueue.AddEvent(new StartNextSyncEvent(true)); 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 + ".txt")); }
public void OneFileIsCopiedAFewTimes() { FileSystemInfoFactory fsFactory = new FileSystemInfoFactory(); var fileNames = new List <string>(); string fileName = "file"; string content = "content"; this.remoteRootDir.CreateDocument(fileName + ".txt", content); this.repo.Initialize(); this.repo.Run(); 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 < 10; i++) { var fileCopy = fsFactory.CreateFileInfo(Path.Combine(this.localRootDir.FullName, fileName + i + ".txt")); file.CopyTo(fileCopy.FullName); Thread.Sleep(50); fileCopy.Refresh(); fileCopy.Uuid = uuid; fileNames.Add(fileCopy.FullName); } Thread.Sleep(500); this.repo.SingleStepQueue.AddEvent(new StartNextSyncEvent(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)); } } }
/// <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("The given fs watcher must not be null"); } 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("The given queue must not be null"); } if (storage == null) { throw new ArgumentNullException("The given storage must not be null"); } 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); }