test/java/nio/file/WatchService/SensitivityModifier.java

Print this page




  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     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();


  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         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.reset();
 100                 key = watcher.poll(1, TimeUnit.SECONDS);
 101             } while (key != null);
 102 
 103             // re-register the directories to force changing their sensitivity
 104             // level
 105             register(dirs, watcher);
 106         }
 107 
 108         // done
 109         watcher.close();
 110     }
 111 
 112     public static void main(String[] args) throws Exception {
 113         Path dir = TestUtil.createTemporaryDirectory();