Path: blob/master/test/jdk/java/nio/file/WatchService/FileTreeModifier.java
41153 views
/*1* Copyright (c) 2008, 2016, 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 JDK-specific FILE_TREE watch event modifier26* @library ..27* @modules jdk.unsupported28*/2930import java.nio.file.*;31import static java.nio.file.StandardWatchEventKinds.*;32import java.io.IOException;33import java.io.OutputStream;34import java.util.*;35import java.util.concurrent.*;36import static com.sun.nio.file.ExtendedWatchEventModifier.*;3738public class FileTreeModifier {3940static void checkExpectedEvent(WatchService watcher,41WatchEvent.Kind<?> expectedType,42Object expectedContext)43{44WatchKey key;45try {46key = watcher.take();47} catch (InterruptedException x) {48// should not happen49throw new RuntimeException(x);50}51WatchEvent<?> event = key.pollEvents().iterator().next();52System.out.format("Event: type=%s, count=%d, context=%s\n",53event.kind(), event.count(), event.context());54if (event.kind() != expectedType)55throw new RuntimeException("unexpected event");56if (!expectedContext.equals(event.context()))57throw new RuntimeException("unexpected context");58}5960static void doTest(Path top) throws IOException {61FileSystem fs = top.getFileSystem();62WatchService watcher = fs.newWatchService();6364// create directories65Path subdir = Files.createDirectories(top.resolve("a").resolve("b").resolve("c"));6667// Test ENTRY_CREATE with FILE_TREE modifier.6869WatchKey key = top.register(watcher,70new WatchEvent.Kind<?>[]{ ENTRY_CREATE }, FILE_TREE);7172// create file in a/b/c and check we get create event73Path file = Files.createFile(subdir.resolve("foo"));74checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file));75key.reset();7677// Test ENTRY_DELETE with FILE_TREE modifier.7879WatchKey k = top.register(watcher,80new WatchEvent.Kind<?>[]{ ENTRY_DELETE }, FILE_TREE);81if (k != key)82throw new RuntimeException("Existing key not returned");8384// delete a/b/c/foo and check we get delete event85Files.delete(file);86checkExpectedEvent(watcher, ENTRY_DELETE, top.relativize(file));87key.reset();8889// Test changing registration to ENTRY_CREATE without modifier9091k = top.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_CREATE });92if (k != key)93throw new RuntimeException("Existing key not returned");9495// create a/b/c/foo96Files.createFile(file);9798// check that key is not queued99WatchKey nextKey;100try {101nextKey = watcher.poll(3, TimeUnit.SECONDS);102} catch (InterruptedException e) {103throw new RuntimeException();104}105if (nextKey != null)106throw new RuntimeException("WatchKey not expected to be polled");107108// create bar and check we get create event109file = Files.createFile(top.resolve("bar"));110checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file));111key.reset();112113// Test changing registration to <all> with FILE_TREE modifier114115k = top.register(watcher,116new WatchEvent.Kind<?>[]{ ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY },117FILE_TREE);118if (k != key)119throw new RuntimeException("Existing key not returned");120121// modify bar and check we get modify event122try (OutputStream out = Files.newOutputStream(file)) {123out.write("Double shot expresso please".getBytes("UTF-8"));124}125checkExpectedEvent(watcher, ENTRY_MODIFY, top.relativize(file));126key.reset();127}128129130public static void main(String[] args) throws IOException {131if (!System.getProperty("os.name").startsWith("Windows")) {132System.out.println("This is Windows-only test at this time!");133return;134}135136Path dir = TestUtil.createTemporaryDirectory();137try {138doTest(dir);139} finally {140TestUtil.removeAll(dir);141}142}143}144145146