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  * @key randomness
  30  */
  31 
  32 import java.nio.file.*;
  33 import static java.nio.file.StandardWatchEventKinds.*;
  34 import java.io.OutputStream;
  35 import java.io.IOException;
  36 import java.util.Random;
  37 import java.util.concurrent.TimeUnit;
  38 import com.sun.nio.file.SensitivityWatchEventModifier;
  39 
  40 public class SensitivityModifier {
  41 
  42     static final Random rand = new Random();
  43 
  44     static void register(Path[] dirs, WatchService watcher) throws IOException {
  45         SensitivityWatchEventModifier[] sensitivtives =
  46             SensitivityWatchEventModifier.values();
  47         for (int i=0; i<dirs.length; i++) {
  48             SensitivityWatchEventModifier sensivity =
  49                 sensitivtives[ rand.nextInt(sensitivtives.length) ];
  50             Path dir = dirs[i];
  51             dir.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_MODIFY }, sensivity);
  52         }
  53     }
  54 
  55     @SuppressWarnings("unchecked")
  56     static void doTest(Path top) throws Exception {
  57         FileSystem fs = top.getFileSystem();
  58         try (WatchService watcher = fs.newWatchService()) {
  59 
  60             // create directories and files
  61             int nDirs = 5 + rand.nextInt(20);
  62             int nFiles = 50 + rand.nextInt(50);
  63             Path[] dirs = new Path[nDirs];
  64             Path[] files = new Path[nFiles];
  65             for (int i=0; i<nDirs; i++) {
  66                 dirs[i] = Files.createDirectory(top.resolve("dir" + i));
  67             }
  68             for (int i=0; i<nFiles; i++) {
  69                 Path dir = dirs[rand.nextInt(nDirs)];
  70                 files[i] = Files.createFile(dir.resolve("file" + i));
  71             }
  72 
  73             // register the directories (random sensitivity)
  74             register(dirs, watcher);
  75 
  76             // sleep a bit here to ensure that modification to the first file
  77             // can be detected by polling implementations (ie: last modified time
  78             // may not change otherwise).
  79             try { Thread.sleep(1000); } catch (InterruptedException e) { }
  80 
  81             // modify files and check that events are received
  82             for (int i=0; i<10; i++) {
  83                 Path file = files[rand.nextInt(nFiles)];
  84                 System.out.println("Modify: " + file);
  85                 try (OutputStream out = Files.newOutputStream(file)) {
  86                     out.write(new byte[100]);
  87                 }
  88 
  89                 System.out.println("Waiting for event(s)...");
  90                 boolean eventReceived = false;
  91                 WatchKey key = watcher.take();
  92                 do {
  93                     for (WatchEvent<?> event: key.pollEvents()) {
  94                         if (event.kind() != ENTRY_MODIFY)
  95                             throw new RuntimeException("Unexpected event: " + event);
  96                         Path name = ((WatchEvent<Path>)event).context();
  97                         if (name.equals(file.getFileName())) {
  98                             eventReceived = true;
  99                             break;
 100                         }
 101                     }
 102                     key.reset();
 103                     key = watcher.poll(1, TimeUnit.SECONDS);
 104                 } while (key != null);
 105 
 106                 // we should have received at least one ENTRY_MODIFY event
 107                 if (eventReceived) {
 108                     System.out.println("Event OK");
 109                 } else {
 110                     throw new RuntimeException("No ENTRY_MODIFY event received for " + file);
 111                 }
 112 
 113                 // re-register the directories to force changing their sensitivity
 114                 // level
 115                 register(dirs, watcher);
 116             }
 117         }
 118     }
 119 
 120     public static void main(String[] args) throws Exception {
 121         Path dir = TestUtil.createTemporaryDirectory();
 122         try {
 123             doTest(dir);
 124         } finally {
 125             TestUtil.removeAll(dir);
 126         }
 127     }
 128 }