Created/Changed/Deleted file system events handler to report the events to the given queue.
Inheritance: IDisposable
        public void HandlesFileCreatedEvent() {
            using (var underTest = new CreatedChangedDeletedFileSystemEventHandler(this.queue.Object, this.storage.Object, this.fsFactory.Object)) {
                this.fsFactory.Setup(f => f.IsDirectory(this.path)).Returns((bool?)false);
                this.fsFactory.AddFile(this.path, true);
                underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Created, Directory, Name));

                this.WaitForThreshold();
                this.queue.Verify(q => q.AddEvent(It.Is<FSEvent>(e => e.IsDirectory == false && e.Name == Name && e.LocalPath == this.path && e.Type == WatcherChangeTypes.Created)), Times.Once());
                this.queue.VerifyThatNoOtherEventIsAddedThan<FSEvent>();
            }
        }
Example #2
0
        /// <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);
        }
        public void HandleDirectoryNotFoundExceptionOnExtendedAttributeByJustIgnoringTheEvent() {
            using (var underTest = new CreatedChangedDeletedFileSystemEventHandler(this.queue.Object, this.storage.Object, this.fsFactory.Object)) {
                this.fsFactory.Setup(f => f.IsDirectory(this.path)).Returns((bool?)true);
                var dirInfo = this.fsFactory.AddDirectory(this.path, Guid.Empty, true);

                underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Created, Directory, Name));
                dirInfo.Setup(f => f.Uuid).Throws(new DirectoryNotFoundException());

                this.WaitForThreshold();
                this.queue.Verify(q => q.AddEvent(It.IsAny<ISyncEvent>()), Times.Never);
            }
        }
        public void HandleChangeEventOnNoMoreExistingFileOrFolderByJustPassingTheEvent() {
            using (var underTest = new CreatedChangedDeletedFileSystemEventHandler(this.queue.Object, this.storage.Object, this.fsFactory.Object)) {
                this.fsFactory.Setup(f => f.IsDirectory(this.path)).Returns((bool?)true);
                var dirInfo = this.fsFactory.AddDirectory(this.path, Guid.Empty, true);

                underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Changed, Directory, Name));
                dirInfo.Setup(d => d.Exists).Returns(false);

                this.WaitForThreshold();
                this.queue.Verify(q => q.AddEvent(It.Is<FSEvent>(f => f.IsDirectory == true && f.LocalPath == this.path && f.Name == Name && f.Type == WatcherChangeTypes.Changed)));
                this.queue.VerifyThatNoOtherEventIsAddedThan<FSEvent>();
            }
        }
        public void HandleExceptionsOnUuidReadingByJustPassingEmptyUuid() {
            using (var underTest = new CreatedChangedDeletedFileSystemEventHandler(this.queue.Object, this.storage.Object, this.fsFactory.Object)) {
                this.fsFactory.Setup(f => f.IsDirectory(this.path)).Returns((bool?)false);
                var filemock = this.fsFactory.AddFile(this.path, Guid.Empty, true);
                filemock.Setup(f => f.Uuid).Throws<ExtendedAttributeException>();

                underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Created, Directory, Name));

                this.queue.Verify(q => q.AddEvent(It.IsAny<ISyncEvent>()), Times.Never);
                this.WaitForThreshold();
                this.queue.Verify(q => q.AddEvent(It.Is<FSEvent>(e => e.IsDirectory == false && e.Name == Name && e.LocalPath == this.path && e.Type == WatcherChangeTypes.Created)), Times.Once());
                this.queue.VerifyThatNoOtherEventIsAddedThan<FSEvent>();
            }
        }
        public void HandleExceptionsOnTransformingByInvokingCrawlSync() {
            using (var underTest = new CreatedChangedDeletedFileSystemEventHandler(this.queue.Object, this.storage.Object, this.fsFactory.Object)) {
                this.fsFactory.Setup(f => f.IsDirectory(this.path)).Returns((bool?)false);
                this.fsFactory.AddFile(this.path, Guid.Empty, true);

                underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Created, Directory, Name));
                this.queue.Verify(q => q.AddEvent(It.IsAny<ISyncEvent>()), Times.Never());

                this.fsFactory.Setup(f => f.CreateDirectoryInfo(It.IsAny<string>())).Throws(new Exception("Generic exception"));
                this.fsFactory.Setup(f => f.CreateFileInfo(It.IsAny<string>())).Throws(new Exception("Generic exception"));

                this.WaitForThreshold();
                this.queue.VerifyThatNoOtherEventIsAddedThan<StartNextSyncEvent>();
                this.queue.Verify(q => q.AddEvent(It.Is<StartNextSyncEvent>(e => e.FullSyncRequested == true)));
            }
        }
        public void DoesNotAggregateFolderChangedEventsToFSMovedEvent() {
            using (var underTest = new CreatedChangedDeletedFileSystemEventHandler(this.queue.Object, this.storage.Object, this.fsFactory.Object)) {
                Guid guid = Guid.NewGuid();
                this.fsFactory.Setup(f => f.IsDirectory(this.path)).Returns((bool?)true);

                this.fsFactory.AddDirectory(this.path, guid, true);
                this.storage.AddLocalFolder(this.path, "id", guid);

                underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Created, Directory, Name));
                underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Changed, Directory, Name));

                this.WaitForThreshold();
                this.queue.Verify(q => q.AddEvent(It.IsAny<FSMovedEvent>()), Times.Never());
            }
        }
        public void HandleExceptionsOnProcessingByInvokingCrawlSync() {
            using (var underTest = new CreatedChangedDeletedFileSystemEventHandler(this.queue.Object, this.storage.Object, this.fsFactory.Object)) {
                this.fsFactory.Setup(f => f.IsDirectory(this.path)).Throws(new Exception("Generic exception"));

                underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Created, Directory, Name));

                this.queue.Verify(q => q.AddEvent(It.Is<StartNextSyncEvent>(e => e.FullSyncRequested == true)));
                this.queue.VerifyThatNoOtherEventIsAddedThan<StartNextSyncEvent>();
                this.WaitForThreshold();
                this.queue.Verify(q => q.AddEvent(It.Is<StartNextSyncEvent>(e => e.FullSyncRequested == true)));
                this.queue.VerifyThatNoOtherEventIsAddedThan<StartNextSyncEvent>();
            }
        }
        public void AggregatesFolderDeletedAndCreatedEventToFSMovedEvent([Values(true, false)]bool ordered) {
            using (var underTest = new CreatedChangedDeletedFileSystemEventHandler(this.queue.Object, this.storage.Object, this.fsFactory.Object)) {
                string newName = "new";
                string oldName = "old";
                Guid guid = Guid.NewGuid();
                string newPath = Path.Combine(this.path, newName);
                string oldPath = Path.Combine(Directory, oldName);
                this.fsFactory.Setup(f => f.IsDirectory(newPath)).Returns((bool?)true);
                this.fsFactory.AddDirectory(newPath, guid, true);
                this.fsFactory.Setup(f => f.CreateFileInfo(oldPath)).Returns(Mock.Of<IFileInfo>(file => file.Exists == false && file.FullName == oldPath));
                this.storage.AddLocalFolder(oldPath, "id", guid);

                if (ordered) {
                    underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Deleted, Directory, oldName));
                    underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Created, this.path, newName));
                } else {
                    underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Created, this.path, newName));
                    underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Deleted, Directory, oldName));
                }

                this.WaitForThreshold();
                this.queue.Verify(q => q.AddEvent(It.Is<FSMovedEvent>(e => e.IsDirectory == true && e.Name == newName && e.OldPath == oldPath && e.LocalPath == newPath)), Times.Once());

                this.WaitForThreshold();
                this.queue.VerifyThatNoOtherEventIsAddedThan<FSMovedEvent>();
            }
        }
 public void IgnoresDeletionOfPathWithNoEntryInStorage() {
     using (var underTest = new CreatedChangedDeletedFileSystemEventHandler(this.queue.Object, this.storage.Object, this.fsFactory.Object)) {
         underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Deleted, Directory, Name));
         this.queue.Verify(q => q.AddEvent(It.IsAny<ISyncEvent>()), Times.Never());
     }
 }
        public void IgnoresEventOnNonExistingPath() {
            using (var underTest = new CreatedChangedDeletedFileSystemEventHandler(this.queue.Object, this.storage.Object, this.fsFactory.Object)) {
                this.fsFactory.Setup(f => f.IsDirectory(this.path)).Returns((bool?)null);

                underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Created, Directory, Name));

                this.queue.Verify(q => q.AddEvent(It.IsAny<ISyncEvent>()), Times.Never());
            }
        }
        public void HandlesTwoFileDeletedEvent() {
            using (var underTest = new CreatedChangedDeletedFileSystemEventHandler(this.queue.Object, this.storage.Object, this.fsFactory.Object, Threshold)) {
                this.fsFactory.Setup(f => f.IsDirectory(this.path)).Returns((bool?)null);
                var file = this.fsFactory.AddFile(this.path, false);
                this.storage.AddLocalFile(file.Object.FullName, "id", Guid.NewGuid());

                underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Deleted, Directory, Name));
                underTest.Handle(null, new FileSystemEventArgs(WatcherChangeTypes.Deleted, Directory, Name));
                this.queue.Verify(q => q.AddEvent(It.IsAny<ISyncEvent>()), Times.Never);
                this.WaitForThreshold();
                this.queue.Verify(q => q.AddEvent(It.Is<FSEvent>(e => e.IsDirectory == false && e.LocalPath == this.path && e.Name == Name && e.Type == WatcherChangeTypes.Deleted)), Times.Exactly(2));
                this.queue.VerifyThatNoOtherEventIsAddedThan<FSEvent>();
            }
        }
Example #13
0
        /// <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);
        }