SensitivityModifier.java revision 893:f06f30b29f36
1/* 2 * Copyright 2008-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 */ 23 24/* @test 25 * @bug 4313887 26 * @summary Sanity test for Sun-specific sensitivyt level watch event modifier 27 * @library .. 28 * @run main/timeout=330 Basic 29 */ 30 31import java.nio.file.*; 32import static java.nio.file.StandardWatchEventKind.*; 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 static void doTest(Path top) throws Exception { 55 FileSystem fs = top.getFileSystem(); 56 WatchService watcher = fs.newWatchService(); 57 58 // create directories and files 59 int nDirs = 5 + rand.nextInt(20); 60 int nFiles = 50 + rand.nextInt(50); 61 Path[] dirs = new Path[nDirs]; 62 Path[] files = new Path[nFiles]; 63 for (int i=0; i<nDirs; i++) { 64 dirs[i] = top.resolve("dir" + i).createDirectory(); 65 } 66 for (int i=0; i<nFiles; i++) { 67 Path dir = dirs[rand.nextInt(nDirs)]; 68 files[i] = dir.resolve("file" + i).createFile(); 69 } 70 71 // register the directories (random sensitivity) 72 register(dirs, watcher); 73 74 // sleep a bit here to ensure that modification to the first file 75 // can be detected by polling implementations (ie: last modified time 76 // may not change otherwise). 77 try { Thread.sleep(1000); } catch (InterruptedException e) { } 78 79 // modify files and check that events are received 80 for (int i=0; i<10; i++) { 81 Path file = files[rand.nextInt(nFiles)]; 82 System.out.println("Modify: " + file); 83 OutputStream out = file.newOutputStream(); 84 try { 85 out.write(new byte[100]); 86 } finally { 87 out.close(); 88 } 89 System.out.println("Waiting for event..."); 90 WatchKey key = watcher.take(); 91 WatchEvent<?> event = key.pollEvents().iterator().next(); 92 if (event.kind() != ENTRY_MODIFY) 93 throw new RuntimeException("Unexpected event: " + event); 94 Path name = ((WatchEvent<Path>)event).context(); 95 if (!name.equals(file.getName())) 96 throw new RuntimeException("Unexpected context: " + name); 97 System.out.println("Event OK"); 98 99 // drain events (to avoid interference) 100 do { 101 key.reset(); 102 key = watcher.poll(1, TimeUnit.SECONDS); 103 } while (key != null); 104 105 // re-register the directories to force changing their sensitivity 106 // level 107 register(dirs, watcher); 108 } 109 110 // done 111 watcher.close(); 112 } 113 114 public static void main(String[] args) throws Exception { 115 Path dir = TestUtil.createTemporaryDirectory(); 116 try { 117 doTest(dir); 118 } finally { 119 TestUtil.removeAll(dir); 120 } 121 } 122} 123