Exemple #1
0
  /**
   * Test that directory can be registered with more than one watch service and that events don't
   * interfere with each other
   */
  static void testTwoWatchers(Path dir) throws IOException {
    System.out.println("-- Two watchers test --");

    FileSystem fs = FileSystems.getDefault();
    WatchService watcher1 = fs.newWatchService();
    WatchService watcher2 = fs.newWatchService();
    try {
      Path name1 = fs.getPath("gus1");
      Path name2 = fs.getPath("gus2");

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

      // register with both watch services (different events)
      System.out.println("register for different events");
      WatchKey key1 = dir.register(watcher1, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
      WatchKey key2 = dir.register(watcher2, new WatchEvent.Kind<?>[] {ENTRY_DELETE});

      if (key1 == key2) throw new RuntimeException("keys should be different");

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

      // check that key1 got ENTRY_CREATE
      takeExpectedKey(watcher1, key1);
      checkExpectedEvent(key1.pollEvents(), StandardWatchEventKinds.ENTRY_CREATE, name2);

      // check that key2 got zero events
      WatchKey key = watcher2.poll();
      if (key != null) throw new RuntimeException("key not expected");

      // delete gus1
      Files.delete(file1);

      // check that key2 got ENTRY_DELETE
      takeExpectedKey(watcher2, key2);
      checkExpectedEvent(key2.pollEvents(), StandardWatchEventKinds.ENTRY_DELETE, name1);

      // check that key1 got zero events
      key = watcher1.poll();
      if (key != null) throw new RuntimeException("key not expected");

      // reset for next test
      key1.reset();
      key2.reset();

      // change registration with watcher2 so that they are both
      // registered for the same event
      System.out.println("register for same event");
      key2 = dir.register(watcher2, new WatchEvent.Kind<?>[] {ENTRY_CREATE});

      // create file and key2 should be queued
      System.out.format("create %s\n", file1);
      Files.createFile(file1);
      takeExpectedKey(watcher2, key2);
      checkExpectedEvent(key2.pollEvents(), StandardWatchEventKinds.ENTRY_CREATE, name1);

      System.out.println("OKAY");

    } finally {
      watcher2.close();
      watcher1.close();
    }
  }
Exemple #2
0
  /** Simple test to check exceptions and other cases */
  @SuppressWarnings("unchecked")
  static void testExceptions(Path dir) throws IOException {
    System.out.println("-- Exceptions and other simple tests --");

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

      // Poll tests

      WatchKey key;
      System.out.println("poll...");
      key = watcher.poll();
      if (key != null) throw new RuntimeException("no keys registered");

      System.out.println("poll with timeout...");
      try {
        long start = System.currentTimeMillis();
        key = watcher.poll(3000, TimeUnit.MILLISECONDS);
        if (key != null) throw new RuntimeException("no keys registered");
        long waited = System.currentTimeMillis() - start;
        if (waited < 2900) throw new RuntimeException("poll was too short");
      } catch (InterruptedException x) {
        throw new RuntimeException(x);
      }

      // IllegalArgumentException
      System.out.println("IllegalArgumentException tests...");
      try {
        dir.register(watcher, new WatchEvent.Kind<?>[] {});
        throw new RuntimeException("IllegalArgumentException not thrown");
      } catch (IllegalArgumentException x) {
      }
      try {
        // OVERFLOW is ignored so this is equivalent to the empty set
        dir.register(watcher, new WatchEvent.Kind<?>[] {OVERFLOW});
        throw new RuntimeException("IllegalArgumentException not thrown");
      } catch (IllegalArgumentException x) {
      }

      // UnsupportedOperationException
      try {
        dir.register(
            watcher,
            new WatchEvent.Kind<?>[] {
              new WatchEvent.Kind<Object>() {
                @Override
                public String name() {
                  return "custom";
                }

                @Override
                public Class<Object> type() {
                  return Object.class;
                }
              }
            });
      } catch (UnsupportedOperationException x) {
      }
      try {
        dir.register(
            watcher,
            new WatchEvent.Kind<?>[] {ENTRY_CREATE},
            new WatchEvent.Modifier() {
              @Override
              public String name() {
                return "custom";
              }
            });
        throw new RuntimeException("UnsupportedOperationException not thrown");
      } catch (UnsupportedOperationException x) {
      }

      // NullPointerException
      System.out.println("NullPointerException tests...");
      try {
        dir.register(null, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
        throw new RuntimeException("NullPointerException not thrown");
      } catch (NullPointerException x) {
      }
      try {
        dir.register(watcher, new WatchEvent.Kind<?>[] {null});
        throw new RuntimeException("NullPointerException not thrown");
      } catch (NullPointerException x) {
      }
      try {
        dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE}, (WatchEvent.Modifier) null);
        throw new RuntimeException("NullPointerException not thrown");
      } catch (NullPointerException x) {
      }
    } finally {
      watcher.close();
    }

    // -- ClosedWatchServiceException --

    System.out.println("ClosedWatchServiceException tests...");

    try {
      watcher.poll();
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (ClosedWatchServiceException x) {
    }

    // assume that poll throws exception immediately
    long start = System.currentTimeMillis();
    try {
      watcher.poll(10000, TimeUnit.MILLISECONDS);
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (InterruptedException x) {
      throw new RuntimeException(x);
    } catch (ClosedWatchServiceException x) {
      long waited = System.currentTimeMillis() - start;
      if (waited > 5000) throw new RuntimeException("poll was too long");
    }

    try {
      watcher.take();
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (InterruptedException x) {
      throw new RuntimeException(x);
    } catch (ClosedWatchServiceException x) {
    }

    try {
      dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (ClosedWatchServiceException x) {
    }

    System.out.println("OKAY");
  }