FilesystemTracker implements IFilesystemTracker { @Override public void start(final IPath directory) throws FilesystemTrackerStartupException, InvalidArgumentException { if (started) { throw new FilesystemTrackerStartupException("Filesystem tracker is already started."); } if (!Files.isDirectory(fileSystem.getPath(directory.getPath()))) { throw new InvalidArgumentException("Given file is not a directory."); } knownFiles.clear(); try { Runnable listeningTask = taskFactory.createRunnable(directory, new FileCreationHandler()); watchThread = new Thread(listeningTask); watchThread.setDaemon(true); watchThread.start(); started = true; } catch (IOException e) { throw new FilesystemTrackerStartupException("Could not start filesystem tracker.", e); } } FilesystemTracker(final IPathFilter filter, final ListeningTaskFactory taskFactory);   FilesystemTracker(final IPathFilter filter, final ListeningTaskFactory taskFactory, final FileSystem fileSystem); @Override void start(final IPath directory); @Override void addFileHandler(final IAction<IPath> handler); @Override void removeFileHandler(final IAction<IPath> handler); @Override void addErrorHandler(final IAction<Throwable> handler);  }
@Test(expected = InvalidArgumentException.class) public void Should_throw_When_directoryIsNotDirectory() throws Exception { when(directoryPathAttributesMock.isDirectory()).thenReturn(false); FilesystemTracker tracker = new FilesystemTracker(pathFilterMock, taskFactoryMock, fileSystemMock); tracker.start(directoryMock); }
@Test(expected = FilesystemTrackerStartupException.class) public void Should_throw_When_startCalledTwice() throws Exception { FilesystemTracker tracker = new FilesystemTracker(pathFilterMock, taskFactoryMock, fileSystemMock); try { tracker.start(directoryMock); verify(runnableMock, timeout(1000)).run(); } catch (FilesystemTrackerStartupException e) { fail(); } tracker.start(directoryMock); }
@Test(expected = FilesystemTrackerStartupException.class) public void Should_throw_When_taskFactoryThrows() throws Exception { FilesystemTracker tracker = new FilesystemTracker(pathFilterMock, taskFactoryMock, fileSystemMock); when(taskFactoryMock.createRunnable(any(), any())).thenThrow(new IOException()); tracker.start(directoryMock); }