1N/A/*
1N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
1N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1N/A *
1N/A * This code is free software; you can redistribute it and/or modify it
1N/A * under the terms of the GNU General Public License version 2 only, as
1N/A * published by the Free Software Foundation.
1N/A *
1N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1N/A * version 2 for more details (a copy is included in the LICENSE file that
1N/A * accompanied this code).
1N/A *
1N/A * You should have received a copy of the GNU General Public License version
1N/A * 2 along with this work; if not, write to the Free Software Foundation,
1N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1N/A *
1N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1N/A * or visit www.oracle.com if you need additional information or have any
1N/A * questions.
1N/A */
1N/A
1N/A/* @test
1N/A * @bug 4313887
1N/A * @summary Sanity test for Sun-specific sensitivity level watch event modifier
1N/A * @library ..
1N/A * @run main/timeout=240 SensitivityModifier
1N/A */
1N/A
1N/Aimport java.nio.file.*;
1N/Aimport static java.nio.file.StandardWatchEventKinds.*;
1N/Aimport java.io.OutputStream;
1N/Aimport java.io.IOException;
1N/Aimport java.util.Random;
1N/Aimport java.util.concurrent.TimeUnit;
1N/Aimport com.sun.nio.file.SensitivityWatchEventModifier;
1N/A
1N/Apublic class SensitivityModifier {
1N/A
1N/A static final Random rand = new Random();
1N/A
1N/A static void register(Path[] dirs, WatchService watcher) throws IOException {
1N/A SensitivityWatchEventModifier[] sensitivtives =
1N/A SensitivityWatchEventModifier.values();
1N/A for (int i=0; i<dirs.length; i++) {
1N/A SensitivityWatchEventModifier sensivity =
1N/A sensitivtives[ rand.nextInt(sensitivtives.length) ];
1N/A Path dir = dirs[i];
1N/A dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
1N/A }
1N/A }
1N/A
1N/A @SuppressWarnings("unchecked")
1N/A static void doTest(Path top) throws Exception {
1N/A FileSystem fs = top.getFileSystem();
1N/A WatchService watcher = fs.newWatchService();
// create directories and files
int nDirs = 5 + rand.nextInt(20);
int nFiles = 50 + rand.nextInt(50);
Path[] dirs = new Path[nDirs];
Path[] files = new Path[nFiles];
for (int i=0; i<nDirs; i++) {
dirs[i] = Files.createDirectory(top.resolve("dir" + i));
}
for (int i=0; i<nFiles; i++) {
Path dir = dirs[rand.nextInt(nDirs)];
files[i] = Files.createFile(dir.resolve("file" + i));
}
// register the directories (random sensitivity)
register(dirs, watcher);
// sleep a bit here to ensure that modification to the first file
// can be detected by polling implementations (ie: last modified time
// may not change otherwise).
try { Thread.sleep(1000); } catch (InterruptedException e) { }
// modify files and check that events are received
for (int i=0; i<10; i++) {
Path file = files[rand.nextInt(nFiles)];
System.out.println("Modify: " + file);
try (OutputStream out = Files.newOutputStream(file)) {
out.write(new byte[100]);
}
System.out.println("Waiting for event...");
WatchKey key = watcher.take();
WatchEvent<?> event = key.pollEvents().iterator().next();
if (event.kind() != ENTRY_MODIFY)
throw new RuntimeException("Unexpected event: " + event);
Path name = ((WatchEvent<Path>)event).context();
if (!name.equals(file.getFileName()))
throw new RuntimeException("Unexpected context: " + name);
System.out.println("Event OK");
// drain events (to avoid interference)
do {
key.pollEvents();
key.reset();
key = watcher.poll(1, TimeUnit.SECONDS);
} while (key != null);
// re-register the directories to force changing their sensitivity
// level
register(dirs, watcher);
}
// done
watcher.close();
}
public static void main(String[] args) throws Exception {
Path dir = TestUtil.createTemporaryDirectory();
try {
doTest(dir);
} finally {
TestUtil.removeAll(dir);
}
}
}