SensitivityModifier.java revision 6073:cea72c2bf071
1/* 2 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* @test 25 * @bug 4313887 26 * @summary Sanity test for Sun-specific sensitivity level watch event modifier 27 * @library .. 28 * @run main/timeout=240 SensitivityModifier 29 */ 30 31import 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; 38 39public class SensitivityModifier { 40 41 static final Random rand = new Random(); 42 43 static void register(Path[] dirs, WatchService watcher) throws IOException { 44 SensitivityWatchEventModifier[] sensitivtives = 45 SensitivityWatchEventModifier.values(); 46 for (int i=0; i<dirs.length; i++) { 47 SensitivityWatchEventModifier sensivity = 48 sensitivtives[ rand.nextInt(sensitivtives.length) ]; 49 Path dir = dirs[i]; 50 dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity); 51 } 52 } 53 54 @SuppressWarnings("unchecked") 55 static void doTest(Path top) throws Exception { 56 FileSystem fs = top.getFileSystem(); 57 WatchService watcher = fs.newWatchService(); 58 59 // create directories and files 60 int nDirs = 5 + rand.nextInt(20); 61 int nFiles = 50 + rand.nextInt(50); 62 Path[] dirs = new Path[nDirs]; 63 Path[] files = new Path[nFiles]; 64 for (int i=0; i<nDirs; i++) { 65 dirs[i] = Files.createDirectory(top.resolve("dir" + i)); 66 } 67 for (int i=0; i<nFiles; i++) { 68 Path dir = dirs[rand.nextInt(nDirs)]; 69 files[i] = Files.createFile(dir.resolve("file" + i)); 70 } 71 72 // register the directories (random sensitivity) 73 register(dirs, watcher); 74 75 // sleep a bit here to ensure that modification to the first file 76 // can be detected by polling implementations (ie: last modified time 77 // may not change otherwise). 78 try { Thread.sleep(1000); } catch (InterruptedException e) { } 79 80 // modify files and check that events are received 81 for (int i=0; i<10; i++) { 82 Path file = files[rand.nextInt(nFiles)]; 83 System.out.println("Modify: " + file); 84 try (OutputStream out = Files.newOutputStream(file)) { 85 out.write(new byte[100]); 86 } 87 System.out.println("Waiting for event..."); 88 WatchKey key = watcher.take(); 89 WatchEvent<?> event = key.pollEvents().iterator().next(); 90 if (event.kind() != ENTRY_MODIFY) 91 throw new RuntimeException("Unexpected event: " + event); 92 Path name = ((WatchEvent<Path>)event).context(); 93 if (!name.equals(file.getFileName())) 94 throw new RuntimeException("Unexpected context: " + name); 95 System.out.println("Event OK"); 96 97 // drain events (to avoid interference) 98 do { 99 key.pollEvents(); 100 key.reset(); 101 key = watcher.poll(1, TimeUnit.SECONDS); 102 } while (key != null); 103 104 // re-register the directories to force changing their sensitivity 105 // level 106 register(dirs, watcher); 107 } 108 109 // done 110 watcher.close(); 111 } 112 113 public static void main(String[] args) throws Exception { 114 Path dir = TestUtil.createTemporaryDirectory(); 115 try { 116 doTest(dir); 117 } finally { 118 TestUtil.removeAll(dir); 119 } 120 } 121} 122