### Question:
PollingWatchService extends AbstractWatchService { @Override public Key register(Watchable watchable, Iterable<? extends WatchEvent.Kind<?>> eventTypes) throws IOException { JimfsPath path = checkWatchable(watchable); Key key = super.register(path, eventTypes); Snapshot snapshot = takeSnapshot(path); synchronized (this) { snapshots.put(key, snapshot); if (pollingFuture == null) { startPolling(); } } return key; } PollingWatchService(
      FileSystemView view,
      PathService pathService,
      FileSystemState fileSystemState,
      long interval,
      TimeUnit timeUnit); @Override Key register(Watchable watchable, Iterable<? extends WatchEvent.Kind<?>> eventTypes); @Override synchronized void cancelled(Key key); @Override void close();  }### Answer:
@Test public void testRegister() throws IOException { Key key = watcher.register(createDirectory(), ImmutableList.of(ENTRY_CREATE)); assertThat(key.isValid()).isTrue(); assertThat(watcher.isPolling()).isTrue(); }
@Test public void testRegister_fileDoesNotExist() throws IOException { try { watcher.register(fs.getPath("/a/b/c"), ImmutableList.of(ENTRY_CREATE)); fail(); } catch (NoSuchFileException expected) { } }
@Test public void testRegister_fileIsNotDirectory() throws IOException { Path path = fs.getPath("/a.txt"); Files.createFile(path); try { watcher.register(path, ImmutableList.of(ENTRY_CREATE)); fail(); } catch (NotDirectoryException expected) { } }
@Test(timeout = 2000) public void testWatchForOneEventType() throws IOException, InterruptedException { JimfsPath path = createDirectory(); watcher.register(path, ImmutableList.of(ENTRY_CREATE)); Files.createFile(path.resolve("foo")); assertWatcherHasEvents(new Event<>(ENTRY_CREATE, 1, fs.getPath("foo"))); Files.createFile(path.resolve("bar")); Files.createFile(path.resolve("baz")); assertWatcherHasEvents( new Event<>(ENTRY_CREATE, 1, fs.getPath("bar")), new Event<>(ENTRY_CREATE, 1, fs.getPath("baz"))); }
@Test(timeout = 2000) public void testWatchForMultipleEventTypes() throws IOException, InterruptedException { JimfsPath path = createDirectory(); watcher.register(path, ImmutableList.of(ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY)); Files.createDirectory(path.resolve("foo")); Files.createFile(path.resolve("bar")); assertWatcherHasEvents( new Event<>(ENTRY_CREATE, 1, fs.getPath("bar")), new Event<>(ENTRY_CREATE, 1, fs.getPath("foo"))); Files.createFile(path.resolve("baz")); Files.delete(path.resolve("bar")); Files.createFile(path.resolve("foo/bar")); assertWatcherHasEvents( new Event<>(ENTRY_CREATE, 1, fs.getPath("baz")), new Event<>(ENTRY_DELETE, 1, fs.getPath("bar")), new Event<>(ENTRY_MODIFY, 1, fs.getPath("foo"))); Files.delete(path.resolve("foo/bar")); ensureTimeToPoll(); Files.delete(path.resolve("foo")); assertWatcherHasEvents( new Event<>(ENTRY_MODIFY, 1, fs.getPath("foo")), new Event<>(ENTRY_DELETE, 1, fs.getPath("foo"))); Files.createDirectories(path.resolve("foo/bar")); assertWatcherHasEvents( ImmutableList.<WatchEvent<?>>of(new Event<>(ENTRY_CREATE, 1, fs.getPath("foo"))), ImmutableList.<WatchEvent<?>>of( new Event<>(ENTRY_CREATE, 1, fs.getPath("foo")), new Event<>(ENTRY_MODIFY, 1, fs.getPath("foo")))); Files.delete(path.resolve("foo/bar")); Files.delete(path.resolve("foo")); assertWatcherHasEvents( ImmutableList.<WatchEvent<?>>of(new Event<>(ENTRY_DELETE, 1, fs.getPath("foo"))), ImmutableList.<WatchEvent<?>>of( new Event<>(ENTRY_MODIFY, 1, fs.getPath("foo")), new Event<>(ENTRY_DELETE, 1, fs.getPath("foo")))); }