Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/WatchService/FileTreeModifier.java
38828 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4313887 683833325* @summary Sanity test for Sun-specific FILE_TREE watch event modifier26* @library ..27*/2829import java.nio.file.*;30import static java.nio.file.StandardWatchEventKinds.*;31import java.io.IOException;32import java.io.OutputStream;33import java.util.*;34import java.util.concurrent.*;35import static com.sun.nio.file.ExtendedWatchEventModifier.*;3637public class FileTreeModifier {3839static void checkExpectedEvent(WatchService watcher,40WatchEvent.Kind<?> expectedType,41Object expectedContext)42{43WatchKey key;44try {45key = watcher.take();46} catch (InterruptedException x) {47// should not happen48throw new RuntimeException(x);49}50WatchEvent<?> event = key.pollEvents().iterator().next();51System.out.format("Event: type=%s, count=%d, context=%s\n",52event.kind(), event.count(), event.context());53if (event.kind() != expectedType)54throw new RuntimeException("unexpected event");55if (!expectedContext.equals(event.context()))56throw new RuntimeException("unexpected context");57}5859static void doTest(Path top) throws IOException {60FileSystem fs = top.getFileSystem();61WatchService watcher = fs.newWatchService();6263// create directories64Path subdir = Files.createDirectories(top.resolve("a").resolve("b").resolve("c"));6566// Test ENTRY_CREATE with FILE_TREE modifier.6768WatchKey key = top.register(watcher,69new WatchEvent.Kind<?>[]{ ENTRY_CREATE }, FILE_TREE);7071// create file in a/b/c and check we get create event72Path file = Files.createFile(subdir.resolve("foo"));73checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file));74key.reset();7576// Test ENTRY_DELETE with FILE_TREE modifier.7778WatchKey k = top.register(watcher,79new WatchEvent.Kind<?>[]{ ENTRY_DELETE }, FILE_TREE);80if (k != key)81throw new RuntimeException("Existing key not returned");8283// delete a/b/c/foo and check we get delete event84Files.delete(file);85checkExpectedEvent(watcher, ENTRY_DELETE, top.relativize(file));86key.reset();8788// Test changing registration to ENTRY_CREATE without modifier8990k = top.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_CREATE });91if (k != key)92throw new RuntimeException("Existing key not returned");9394// create a/b/c/foo95Files.createFile(file);9697// check that key is not queued98WatchKey nextKey;99try {100nextKey = watcher.poll(3, TimeUnit.SECONDS);101} catch (InterruptedException e) {102throw new RuntimeException();103}104if (nextKey != null)105throw new RuntimeException("WatchKey not expected to be polled");106107// create bar and check we get create event108file = Files.createFile(top.resolve("bar"));109checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file));110key.reset();111112// Test changing registration to <all> with FILE_TREE modifier113114k = top.register(watcher,115new WatchEvent.Kind<?>[]{ ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY },116FILE_TREE);117if (k != key)118throw new RuntimeException("Existing key not returned");119120// modify bar and check we get modify event121try (OutputStream out = Files.newOutputStream(file)) {122out.write("Double shot expresso please".getBytes("UTF-8"));123}124checkExpectedEvent(watcher, ENTRY_MODIFY, top.relativize(file));125key.reset();126}127128129public static void main(String[] args) throws IOException {130if (!System.getProperty("os.name").startsWith("Windows")) {131System.out.println("This is Windows-only test at this time!");132return;133}134135Path dir = TestUtil.createTemporaryDirectory();136try {137doTest(dir);138} finally {139TestUtil.removeAll(dir);140}141}142}143144145