// 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()) thrownew 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) thrownew RuntimeException("register did not return existing key");
checkKey(deleteKey, dir);
System.out.println("reset key"); if (!myKey.reset()) thrownew 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) thrownew 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");
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) thrownew RuntimeException("key should not be queued");
} catch (InterruptedException x) { thrownew RuntimeException(x);
}
System.out.println("reset key"); if (myKey.reset()) thrownew RuntimeException("Key was not cancelled"); if (myKey.isValid()) thrownew RuntimeException("Key is still valid");
System.out.println("poll with timeout..."); try { long start = System.nanoTime();
key = watcher.poll(3000, TimeUnit.MILLISECONDS); if (key != null) thrownew RuntimeException("no keys registered"); long waited = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); if (waited < 2900) thrownew RuntimeException("poll was too short");
} catch (InterruptedException x) { thrownew RuntimeException(x);
}
// IllegalArgumentException
System.out.println("IllegalArgumentException tests..."); try {
dir.register(watcher /*empty event list*/); thrownew RuntimeException("IllegalArgumentException not thrown");
} catch (IllegalArgumentException x) {
} try { // OVERFLOW is ignored so this is equivalent to the empty set
dir.register(watcher, OVERFLOW); thrownew RuntimeException("IllegalArgumentException not thrown");
} catch (IllegalArgumentException x) {
} try { // OVERFLOW is ignored even if specified multiple times
dir.register(watcher, OVERFLOW, OVERFLOW); thrownew RuntimeException("IllegalArgumentException not thrown");
} catch (IllegalArgumentException x) {
}
// UnsupportedOperationException try {
dir.register(watcher, new WatchEvent.Kind<Object>() {
@Override public String name() { return"custom"; }
@Override publicClass<Object> type() { return Object.class; }
}); thrownew RuntimeException("UnsupportedOperationException not thrown");
} catch (UnsupportedOperationException x) {
} try {
dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_CREATE }, new WatchEvent.Modifier() {
@Override public String name() { return"custom"; }
}); thrownew RuntimeException("UnsupportedOperationException not thrown");
} catch (UnsupportedOperationException x) {
}
// 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) thrownew RuntimeException("keys should be different");
// check that key1 got zero events
key = watcher1.poll(); if (key != null) thrownew 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);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.