public void watch(File file) throws IOException {
    String dirString;
    if (file.isFile()) {
      dirString = file.getParentFile().getAbsolutePath();
    } else {
      throw new IOException(file.getName() + " is not a file");
    }

    if (dirString == null) {
      dirString = "/";
    }

    Path dir = FileSystems.getDefault().getPath(dirString);
    LOGGER.info("watch {}", dir);
    WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
    synchronized (watchKeys) {
      watchKeys.put(key, new File(dirString));
      watchFiles.add(file);
    }
  }
@Test
  void test() throws IOException, InterruptedException {

    assertNull(fileChanged);
    assertEquals(0, numChanged.get());
    // create new file
    File file1 = new File(tmpDir.toFile(), "test1");
    file1.createNewFile();

    File file2 = new File(tmpDir.toFile(), "test2");
    file2.createNewFile();

    watcher.watch(file1);

    FileOutputStream out1 = new FileOutputStream(file1);
    out1.write(1);
    out1.close();

    FileOutputStream out2 = new FileOutputStream(file2);
    out2.write(1);
    out2.close();

    synchronized (this) {
      wait(30 * 1000);
    }

    assertNotNull(fileChanged);
    assertEquals(fileChanged, file1);
    assertTrue(numChanged.get() >= 1, "Changes: " + numChanged.get());
  }