test/java/nio/file/WatchService/Basic.java
Print this page
*** 39,50 ****
* Unit test for WatchService that exercises all methods in various scenarios.
*/
public class Basic {
! static void createFile(Path file) throws IOException {
! file.newOutputStream().close();
}
static void takeExpectedKey(WatchService watcher, WatchKey expected) {
System.out.println("take events...");
WatchKey key;
--- 39,51 ----
* Unit test for WatchService that exercises all methods in various scenarios.
*/
public class Basic {
! static void checkKey(WatchKey key, Path dir) {
! if (!key.isValid())
! throw new RuntimeException("Key is not valid");
}
static void takeExpectedKey(WatchService watcher, WatchKey expected) {
System.out.println("take events...");
WatchKey key;
*** 78,100 ****
System.out.println("-- Standard Events --");
FileSystem fs = FileSystems.getDefault();
Path name = fs.getPath("foo");
! WatchService watcher = fs.newWatchService();
! try {
// --- 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 });
// create file
Path file = dir.resolve("foo");
System.out.format("create %s\n", file);
! createFile(file);
// remove key and check that we got the ENTRY_CREATE event
takeExpectedKey(watcher, myKey);
checkExpectedEvent(myKey.pollEvents(),
StandardWatchEventKind.ENTRY_CREATE, name);
--- 79,101 ----
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(),
StandardWatchEventKind.ENTRY_CREATE, name);
*** 110,122 ****
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");
System.out.format("delete %s\n", file);
! file.delete();
takeExpectedKey(watcher, myKey);
checkExpectedEvent(myKey.pollEvents(),
StandardWatchEventKind.ENTRY_DELETE, name);
System.out.println("reset key");
--- 111,124 ----
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(),
StandardWatchEventKind.ENTRY_DELETE, name);
System.out.println("reset key");
*** 124,185 ****
throw new RuntimeException("key has been cancalled");
System.out.println("OKAY");
// create the file for the next test
! 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");
System.out.format("update: %s\n", file);
! OutputStream out = file.newOutputStream(StandardOpenOption.APPEND);
! try {
out.write("I am a small file".getBytes("UTF-8"));
- } finally {
- out.close();
}
// remove key and check that we got the ENTRY_MODIFY event
takeExpectedKey(watcher, myKey);
checkExpectedEvent(myKey.pollEvents(),
StandardWatchEventKind.ENTRY_MODIFY, name);
System.out.println("OKAY");
// done
! file.delete();
!
! } finally {
! watcher.close();
}
}
/**
* Check that a cancelled key will never be queued
*/
static void testCancel(Path dir) throws IOException {
System.out.println("-- Cancel --");
! WatchService watcher = FileSystems.getDefault().newWatchService();
! try {
System.out.format("register %s for events\n", dir);
WatchKey myKey = dir.register(watcher,
new WatchEvent.Kind<?>[]{ ENTRY_CREATE });
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);
! createFile(file);
// poll for keys - there will be none
System.out.println("poll...");
try {
WatchKey key = watcher.poll(3000, TimeUnit.MILLISECONDS);
--- 126,182 ----
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(),
StandardWatchEventKind.ENTRY_MODIFY, name);
System.out.println("OKAY");
// done
! Files.delete(file);
}
}
/**
* 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);
*** 188,203 ****
} catch (InterruptedException x) {
throw new RuntimeException(x);
}
// done
! file.delete();
System.out.println("OKAY");
-
- } finally {
- watcher.close();
}
}
/**
* Check that deleting a registered directory causes the key to be
--- 185,197 ----
} catch (InterruptedException x) {
throw new RuntimeException(x);
}
// done
! Files.delete(file);
System.out.println("OKAY");
}
}
/**
* Check that deleting a registered directory causes the key to be
*** 204,224 ****
* cancelled and queued.
*/
static void testAutomaticCancel(Path dir) throws IOException {
System.out.println("-- Automatic Cancel --");
! Path subdir = dir.resolve("bar").createDirectory();
! WatchService watcher = FileSystems.getDefault().newWatchService();
! try {
System.out.format("register %s for events\n", subdir);
WatchKey myKey = subdir.register(watcher,
new WatchEvent.Kind<?>[]{ ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY });
System.out.format("delete: %s\n", subdir);
! subdir.delete();
takeExpectedKey(watcher, myKey);
System.out.println("reset key");
if (myKey.reset())
throw new RuntimeException("Key was not cancelled");
--- 198,217 ----
* cancelled and queued.
*/
static void testAutomaticCancel(Path dir) throws IOException {
System.out.println("-- Automatic Cancel --");
! Path subdir = Files.createDirectory(dir.resolve("bar"));
! try (WatchService watcher = FileSystems.getDefault().newWatchService()) {
System.out.format("register %s for events\n", subdir);
WatchKey myKey = subdir.register(watcher,
new WatchEvent.Kind<?>[]{ ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY });
System.out.format("delete: %s\n", subdir);
! Files.delete(subdir);
takeExpectedKey(watcher, myKey);
System.out.println("reset key");
if (myKey.reset())
throw new RuntimeException("Key was not cancelled");
*** 225,236 ****
if (myKey.isValid())
throw new RuntimeException("Key is still valid");
System.out.println("OKAY");
- } finally {
- watcher.close();
}
}
/**
* Asynchronous close of watcher causes blocked threads to wakeup
--- 218,227 ----
*** 409,419 ****
Path name2 = fs.getPath("gus2");
// create gus1
Path file1 = dir.resolve(name1);
System.out.format("create %s\n", file1);
! 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 });
--- 400,410 ----
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 });
*** 424,434 ****
throw new RuntimeException("keys should be different");
// create gus2
Path file2 = dir.resolve(name2);
System.out.format("create %s\n", file2);
! createFile(file2);
// check that key1 got ENTRY_CREATE
takeExpectedKey(watcher1, key1);
checkExpectedEvent(key1.pollEvents(),
StandardWatchEventKind.ENTRY_CREATE, name2);
--- 415,425 ----
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(),
StandardWatchEventKind.ENTRY_CREATE, name2);
*** 437,447 ****
WatchKey key = watcher2.poll();
if (key != null)
throw new RuntimeException("key not expected");
// delete gus1
! file1.delete();
// check that key2 got ENTRY_DELETE
takeExpectedKey(watcher2, key2);
checkExpectedEvent(key2.pollEvents(),
StandardWatchEventKind.ENTRY_DELETE, name1);
--- 428,438 ----
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(),
StandardWatchEventKind.ENTRY_DELETE, name1);
*** 460,470 ****
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);
! createFile(file1);
takeExpectedKey(watcher2, key2);
checkExpectedEvent(key2.pollEvents(),
StandardWatchEventKind.ENTRY_CREATE, name1);
System.out.println("OKAY");
--- 451,461 ----
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(),
StandardWatchEventKind.ENTRY_CREATE, name1);
System.out.println("OKAY");