/* * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* @test * @bug 4313887 6838333 7017446 8011537 8042470 * @summary Unit test for java.nio.file.WatchService * @library .. * @run main Basic
*/
// 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");
// done
Files.delete(file);
}
}
/** * Check that a cancelled key will never be queued
*/ staticvoid testCancel(Path dir) throws IOException {
System.out.println("-- Cancel --");
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);
}
// done
Files.delete(file);
System.out.println("OKAY");
}
}
/** * Check that deleting a registered directory causes the key to be * cancelled and queued.
*/ staticvoid testAutomaticCancel(Path dir) throws IOException {
System.out.println("-- Automatic Cancel --");
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("OKAY");
}
}
/** * Asynchronous close of watcher causes blocked threads to wakeup
*/ staticvoid testWakeup(Path dir) throws IOException {
System.out.println("-- Wakeup Tests --"); final WatchService watcher = FileSystems.getDefault().newWatchService();
Runnable r = new Runnable() { publicvoid run() { try { Thread.sleep(5000);
System.out.println("close WatchService...");
watcher.close();
} catch (InterruptedException x) {
x.printStackTrace();
} catch (IOException x) {
x.printStackTrace();
}
}
};
// start thread to close watch service after delay newThread(r).start();
/** * Simple test to check exceptions and other cases
*/
@SuppressWarnings("unchecked") staticvoid testExceptions(Path dir) throws IOException {
System.out.println("-- Exceptions and other simple tests --");
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) {
}
try {
dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_CREATE }); thrownew RuntimeException("ClosedWatchServiceException not thrown");
} catch (ClosedWatchServiceException x) {
}
System.out.println("OKAY");
}
/** * Test that directory can be registered with more than one watch service * and that events don't interfere with each other
*/ staticvoid testTwoWatchers(Path dir) throws IOException {
System.out.println("-- Two watchers test --");
// 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);
/** * Test that thread interruped status is preserved upon a call * to register()
*/ staticvoid testThreadInterrupt(Path dir) throws IOException {
System.out.println("-- Thread interrupted status test --");
FileSystem fs = FileSystems.getDefault(); Thread curr = Thread.currentThread(); try (WatchService watcher = fs.newWatchService()) {
System.out.println("interrupting current thread");
curr.interrupt();
dir.register(watcher, ENTRY_CREATE); if (!curr.isInterrupted()) thrownew RuntimeException("thread should remain interrupted");
System.out.println("current thread is still interrupted");
System.out.println("OKAY");
} finally {
curr.interrupted();
}
}
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 ist noch experimentell.