1 /*
   2  * Copyright (c) 2008, 2013, 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 
  31 import java.nio.file.*;
  32 import static java.nio.file.StandardWatchEventKinds.*;
  33 import java.io.OutputStream;
  34 import java.io.IOException;
  35 import java.util.Random;
  36 import java.util.concurrent.TimeUnit;
  37 import com.sun.nio.file.SensitivityWatchEventModifier;
  38 
  39 public 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         try (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 
  88                 System.out.println("Waiting for event(s)...");
  89                 boolean eventReceived = false;
  90                 WatchKey key = watcher.take();
  91                 do {
  92                     for (WatchEvent<?> event: key.pollEvents()) {
  93                         if (event.kind() != ENTRY_MODIFY)
  94                             throw new RuntimeException("Unexpected event: " + event);
  95                         Path name = ((WatchEvent<Path>)event).context();
  96                         if (name.equals(file.getFileName())) {
  97                             eventReceived = true;
  98                             break;
  99                         }
 100                     }
 101                     key.reset();
 102                     key = watcher.poll(1, TimeUnit.SECONDS);
 103                 } while (key != null);
 104 
 105                 // we should have received at least one ENTRY_MODIFY event
 106                 if (eventReceived) {
 107                     System.out.println("Event OK");
 108                 } else {
 109                     throw new RuntimeException("No ENTRY_MODIFY event received for " + file);
 110                 }
 111 
 112                 // re-register the directories to force changing their sensitivity
 113                 // level
 114                 register(dirs, watcher);
 115             }
 116         }
 117     }
 118 
 119     public static void main(String[] args) throws Exception {
 120         Path dir = TestUtil.createTemporaryDirectory();
 121         try {
 122             doTest(dir);
 123         } finally {
 124             TestUtil.removeAll(dir);
 125         }
 126     }
 127 }