Ejemplo n.º 1
0
  /** Check that a cancelled key will never be queued */
  static void testCancel(Path dir) throws IOException {
    System.out.println("-- Cancel --");

    try (WatchService watcher = FileSystems.getDefault().newWatchService()) {

      System.out.format("register %s for events\n", dir);
      WatchKey myKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
      checkKey(myKey, dir);

      System.out.println("cancel key");
      myKey.cancel();

      // create a file in the directory
      Path file = dir.resolve("mars");
      System.out.format("create: %s\n", file);
      Files.createFile(file);

      // poll for keys - there will be none
      System.out.println("poll...");
      try {
        WatchKey key = watcher.poll(3000, TimeUnit.MILLISECONDS);
        if (key != null) throw new RuntimeException("key should not be queued");
      } catch (InterruptedException x) {
        throw new RuntimeException(x);
      }

      // done
      Files.delete(file);

      System.out.println("OKAY");
    }
  }
Ejemplo n.º 2
0
  /** Simple test of each of the standard events */
  static void testEvents(Path dir) throws IOException {
    System.out.println("-- Standard Events --");

    FileSystem fs = FileSystems.getDefault();
    Path name = fs.getPath("foo");

    try (WatchService watcher = fs.newWatchService()) {
      // --- ENTRY_CREATE ---

      // register for event
      System.out.format("register %s for ENTRY_CREATE\n", dir);
      WatchKey myKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
      checkKey(myKey, dir);

      // create file
      Path file = dir.resolve("foo");
      System.out.format("create %s\n", file);
      Files.createFile(file);

      // remove key and check that we got the ENTRY_CREATE event
      takeExpectedKey(watcher, myKey);
      checkExpectedEvent(myKey.pollEvents(), StandardWatchEventKinds.ENTRY_CREATE, name);

      System.out.println("reset key");
      if (!myKey.reset()) throw new RuntimeException("key has been cancalled");

      System.out.println("OKAY");

      // --- ENTRY_DELETE ---

      System.out.format("register %s for ENTRY_DELETE\n", dir);
      WatchKey deleteKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_DELETE});
      if (deleteKey != myKey) throw new RuntimeException("register did not return existing key");
      checkKey(deleteKey, dir);

      System.out.format("delete %s\n", file);
      Files.delete(file);
      takeExpectedKey(watcher, myKey);
      checkExpectedEvent(myKey.pollEvents(), StandardWatchEventKinds.ENTRY_DELETE, name);

      System.out.println("reset key");
      if (!myKey.reset()) throw new RuntimeException("key has been cancalled");

      System.out.println("OKAY");

      // create the file for the next test
      Files.createFile(file);

      // --- ENTRY_MODIFY ---

      System.out.format("register %s for ENTRY_MODIFY\n", dir);
      WatchKey newKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_MODIFY});
      if (newKey != myKey) throw new RuntimeException("register did not return existing key");
      checkKey(newKey, dir);

      System.out.format("update: %s\n", file);
      try (OutputStream out = Files.newOutputStream(file, StandardOpenOption.APPEND)) {
        out.write("I am a small file".getBytes("UTF-8"));
      }

      // remove key and check that we got the ENTRY_MODIFY event
      takeExpectedKey(watcher, myKey);
      checkExpectedEvent(myKey.pollEvents(), StandardWatchEventKinds.ENTRY_MODIFY, name);
      System.out.println("OKAY");

      // done
      Files.delete(file);
    }
  }