893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A/* @test
1319N/A * @bug 4313887 6838333
893N/A * @summary Sanity test for Sun-specific FILE_TREE watch event modifier
893N/A * @library ..
893N/A */
893N/A
893N/Aimport java.nio.file.*;
4216N/Aimport static java.nio.file.StandardWatchEventKinds.*;
893N/Aimport java.io.IOException;
893N/Aimport java.io.OutputStream;
893N/Aimport java.util.*;
893N/Aimport java.util.concurrent.*;
893N/Aimport static com.sun.nio.file.ExtendedWatchEventModifier.*;
893N/A
893N/Apublic class FileTreeModifier {
893N/A
893N/A static void checkExpectedEvent(WatchService watcher,
893N/A WatchEvent.Kind<?> expectedType,
893N/A Object expectedContext)
893N/A {
893N/A WatchKey key;
893N/A try {
893N/A key = watcher.take();
893N/A } catch (InterruptedException x) {
893N/A // should not happen
893N/A throw new RuntimeException(x);
893N/A }
893N/A WatchEvent<?> event = key.pollEvents().iterator().next();
893N/A System.out.format("Event: type=%s, count=%d, context=%s\n",
893N/A event.kind(), event.count(), event.context());
893N/A if (event.kind() != expectedType)
893N/A throw new RuntimeException("unexpected event");
893N/A if (!expectedContext.equals(event.context()))
893N/A throw new RuntimeException("unexpected context");
893N/A }
893N/A
893N/A static void doTest(Path top) throws IOException {
893N/A FileSystem fs = top.getFileSystem();
893N/A WatchService watcher = fs.newWatchService();
893N/A
893N/A // create directories
3471N/A Path subdir = Files.createDirectories(top.resolve("a").resolve("b").resolve("c"));
893N/A
893N/A // Test ENTRY_CREATE with FILE_TREE modifier.
893N/A
893N/A WatchKey key = top.register(watcher,
893N/A new WatchEvent.Kind<?>[]{ ENTRY_CREATE }, FILE_TREE);
893N/A
893N/A // create file in a/b/c and check we get create event
3471N/A Path file = Files.createFile(subdir.resolve("foo"));
893N/A checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file));
893N/A key.reset();
893N/A
893N/A // Test ENTRY_DELETE with FILE_TREE modifier.
893N/A
893N/A WatchKey k = top.register(watcher,
893N/A new WatchEvent.Kind<?>[]{ ENTRY_DELETE }, FILE_TREE);
893N/A if (k != key)
893N/A throw new RuntimeException("Existing key not returned");
893N/A
893N/A // delete a/b/c/foo and check we get delete event
3471N/A Files.delete(file);
893N/A checkExpectedEvent(watcher, ENTRY_DELETE, top.relativize(file));
893N/A key.reset();
893N/A
893N/A // Test changing registration to ENTRY_CREATE without modifier
893N/A
893N/A k = top.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_CREATE });
893N/A if (k != key)
893N/A throw new RuntimeException("Existing key not returned");
893N/A
893N/A // create a/b/c/foo
3471N/A Files.createFile(file);
893N/A
893N/A // check that key is not queued
3471N/A WatchKey nextKey;
893N/A try {
3471N/A nextKey = watcher.poll(3, TimeUnit.SECONDS);
893N/A } catch (InterruptedException e) {
893N/A throw new RuntimeException();
893N/A }
3471N/A if (nextKey != null)
893N/A throw new RuntimeException("WatchKey not expected to be polled");
893N/A
893N/A // create bar and check we get create event
3471N/A file = Files.createFile(top.resolve("bar"));
893N/A checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file));
893N/A key.reset();
893N/A
893N/A // Test changing registration to <all> with FILE_TREE modifier
893N/A
893N/A k = top.register(watcher,
893N/A new WatchEvent.Kind<?>[]{ ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY },
893N/A FILE_TREE);
893N/A if (k != key)
893N/A throw new RuntimeException("Existing key not returned");
893N/A
893N/A // modify bar and check we get modify event
3471N/A try (OutputStream out = Files.newOutputStream(file)) {
893N/A out.write("Double shot expresso please".getBytes("UTF-8"));
893N/A }
893N/A checkExpectedEvent(watcher, ENTRY_MODIFY, top.relativize(file));
893N/A key.reset();
893N/A }
893N/A
893N/A
893N/A public static void main(String[] args) throws IOException {
893N/A if (!System.getProperty("os.name").startsWith("Windows")) {
893N/A System.out.println("This is Windows-only test at this time!");
893N/A return;
893N/A }
893N/A
893N/A Path dir = TestUtil.createTemporaryDirectory();
893N/A try {
893N/A doTest(dir);
893N/A } finally {
893N/A TestUtil.removeAll(dir);
893N/A }
893N/A }
893N/A}