test/java/nio/file/WatchService/FileTreeModifier.java

Print this page

        

*** 60,81 **** static void doTest(Path top) throws IOException { FileSystem fs = top.getFileSystem(); WatchService watcher = fs.newWatchService(); // create directories ! Path subdir = top ! .resolve("a").createDirectory() ! .resolve("b").createDirectory() ! .resolve("c").createDirectory(); // Test ENTRY_CREATE with FILE_TREE modifier. WatchKey key = top.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_CREATE }, FILE_TREE); // create file in a/b/c and check we get create event ! Path file = subdir.resolve("foo").createFile(); checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file)); key.reset(); // Test ENTRY_DELETE with FILE_TREE modifier. --- 60,78 ---- static void doTest(Path top) throws IOException { FileSystem fs = top.getFileSystem(); WatchService watcher = fs.newWatchService(); // create directories ! Path subdir = Files.createDirectories(top.resolve("a").resolve("b").resolve("c")); // Test ENTRY_CREATE with FILE_TREE modifier. WatchKey key = top.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_CREATE }, FILE_TREE); // create file in a/b/c and check we get create event ! Path file = Files.createFile(subdir.resolve("foo")); checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file)); key.reset(); // Test ENTRY_DELETE with FILE_TREE modifier.
*** 83,93 **** new WatchEvent.Kind<?>[]{ ENTRY_DELETE }, FILE_TREE); if (k != key) throw new RuntimeException("Existing key not returned"); // delete a/b/c/foo and check we get delete event ! file.delete(); checkExpectedEvent(watcher, ENTRY_DELETE, top.relativize(file)); key.reset(); // Test changing registration to ENTRY_CREATE without modifier --- 80,90 ---- new WatchEvent.Kind<?>[]{ ENTRY_DELETE }, FILE_TREE); if (k != key) throw new RuntimeException("Existing key not returned"); // delete a/b/c/foo and check we get delete event ! Files.delete(file); checkExpectedEvent(watcher, ENTRY_DELETE, top.relativize(file)); key.reset(); // Test changing registration to ENTRY_CREATE without modifier
*** 94,116 **** k = top.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_CREATE }); if (k != key) throw new RuntimeException("Existing key not returned"); // create a/b/c/foo ! file.createFile(); // check that key is not queued try { ! k = watcher.poll(3, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new RuntimeException(); } ! if (k != null) throw new RuntimeException("WatchKey not expected to be polled"); // create bar and check we get create event ! file = top.resolve("bar").createFile(); checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file)); key.reset(); // Test changing registration to <all> with FILE_TREE modifier --- 91,114 ---- k = top.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_CREATE }); if (k != key) throw new RuntimeException("Existing key not returned"); // create a/b/c/foo ! Files.createFile(file); // check that key is not queued + WatchKey nextKey; try { ! nextKey = watcher.poll(3, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new RuntimeException(); } ! if (nextKey != null) throw new RuntimeException("WatchKey not expected to be polled"); // create bar and check we get create event ! file = Files.createFile(top.resolve("bar")); checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file)); key.reset(); // Test changing registration to <all> with FILE_TREE modifier
*** 119,133 **** FILE_TREE); if (k != key) throw new RuntimeException("Existing key not returned"); // modify bar and check we get modify event ! OutputStream out = file.newOutputStream(); ! try { out.write("Double shot expresso please".getBytes("UTF-8")); - } finally { - out.close(); } checkExpectedEvent(watcher, ENTRY_MODIFY, top.relativize(file)); key.reset(); } --- 117,128 ---- FILE_TREE); if (k != key) throw new RuntimeException("Existing key not returned"); // modify bar and check we get modify event ! try (OutputStream out = Files.newOutputStream(file)) { out.write("Double shot expresso please".getBytes("UTF-8")); } checkExpectedEvent(watcher, ENTRY_MODIFY, top.relativize(file)); key.reset(); }