Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/WatchService/SensitivityModifier.java
38828 views
/*1* Copyright (c) 2008, 2013, 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 431388725* @summary Sanity test for Sun-specific sensitivity level watch event modifier26* @library ..27* @run main/timeout=240 SensitivityModifier28* @key randomness29*/3031import java.nio.file.*;32import static java.nio.file.StandardWatchEventKinds.*;33import java.io.OutputStream;34import java.io.IOException;35import java.util.Random;36import java.util.concurrent.TimeUnit;37import com.sun.nio.file.SensitivityWatchEventModifier;3839public class SensitivityModifier {4041static final Random rand = new Random();4243static void register(Path[] dirs, WatchService watcher) throws IOException {44SensitivityWatchEventModifier[] sensitivtives =45SensitivityWatchEventModifier.values();46for (int i=0; i<dirs.length; i++) {47SensitivityWatchEventModifier sensivity =48sensitivtives[ rand.nextInt(sensitivtives.length) ];49Path dir = dirs[i];50dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);51}52}5354@SuppressWarnings("unchecked")55static void doTest(Path top) throws Exception {56FileSystem fs = top.getFileSystem();57try (WatchService watcher = fs.newWatchService()) {5859// create directories and files60int nDirs = 5 + rand.nextInt(20);61int nFiles = 50 + rand.nextInt(50);62Path[] dirs = new Path[nDirs];63Path[] files = new Path[nFiles];64for (int i=0; i<nDirs; i++) {65dirs[i] = Files.createDirectory(top.resolve("dir" + i));66}67for (int i=0; i<nFiles; i++) {68Path dir = dirs[rand.nextInt(nDirs)];69files[i] = Files.createFile(dir.resolve("file" + i));70}7172// register the directories (random sensitivity)73register(dirs, watcher);7475// sleep a bit here to ensure that modification to the first file76// can be detected by polling implementations (ie: last modified time77// may not change otherwise).78try { Thread.sleep(1000); } catch (InterruptedException e) { }7980// modify files and check that events are received81for (int i=0; i<10; i++) {82Path file = files[rand.nextInt(nFiles)];83System.out.println("Modify: " + file);84try (OutputStream out = Files.newOutputStream(file)) {85out.write(new byte[100]);86}8788System.out.println("Waiting for event(s)...");89boolean eventReceived = false;90WatchKey key = watcher.take();91do {92for (WatchEvent<?> event: key.pollEvents()) {93if (event.kind() != ENTRY_MODIFY)94throw new RuntimeException("Unexpected event: " + event);95Path name = ((WatchEvent<Path>)event).context();96if (name.equals(file.getFileName())) {97eventReceived = true;98break;99}100}101key.reset();102key = watcher.poll(1, TimeUnit.SECONDS);103} while (key != null);104105// we should have received at least one ENTRY_MODIFY event106if (eventReceived) {107System.out.println("Event OK");108} else {109throw new RuntimeException("No ENTRY_MODIFY event received for " + file);110}111112// re-register the directories to force changing their sensitivity113// level114register(dirs, watcher);115}116}117}118119public static void main(String[] args) throws Exception {120Path dir = TestUtil.createTemporaryDirectory();121try {122doTest(dir);123} finally {124TestUtil.removeAll(dir);125}126}127}128129130