test/java/nio/file/WatchService/FileTreeModifier.java

Print this page




  45         try {
  46             key = watcher.take();
  47         } catch (InterruptedException x) {
  48             // should not happen
  49             throw new RuntimeException(x);
  50         }
  51         WatchEvent<?> event = key.pollEvents().iterator().next();
  52         System.out.format("Event: type=%s, count=%d, context=%s\n",
  53             event.kind(), event.count(), event.context());
  54         if (event.kind() != expectedType)
  55             throw new RuntimeException("unexpected event");
  56         if (!expectedContext.equals(event.context()))
  57             throw new RuntimeException("unexpected context");
  58     }
  59 
  60     static void doTest(Path top) throws IOException {
  61         FileSystem fs = top.getFileSystem();
  62         WatchService watcher = fs.newWatchService();
  63 
  64         // create directories
  65         Path subdir = top
  66            .resolve("a").createDirectory()
  67            .resolve("b").createDirectory()
  68            .resolve("c").createDirectory();
  69 
  70         // Test ENTRY_CREATE with FILE_TREE modifier.
  71 
  72         WatchKey key = top.register(watcher,
  73             new WatchEvent.Kind<?>[]{ ENTRY_CREATE }, FILE_TREE);
  74 
  75         // create file in a/b/c and check we get create event
  76         Path file = subdir.resolve("foo").createFile();
  77         checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file));
  78         key.reset();
  79 
  80         // Test ENTRY_DELETE with FILE_TREE modifier.
  81 
  82         WatchKey k = top.register(watcher,
  83             new WatchEvent.Kind<?>[]{ ENTRY_DELETE }, FILE_TREE);
  84         if (k != key)
  85             throw new RuntimeException("Existing key not returned");
  86 
  87         // delete a/b/c/foo and check we get delete event
  88         file.delete();
  89         checkExpectedEvent(watcher, ENTRY_DELETE, top.relativize(file));
  90         key.reset();
  91 
  92         // Test changing registration to ENTRY_CREATE without modifier
  93 
  94         k = top.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_CREATE });
  95         if (k != key)
  96             throw new RuntimeException("Existing key not returned");
  97 
  98         // create a/b/c/foo
  99         file.createFile();
 100 
 101         // check that key is not queued

 102         try {
 103             k = watcher.poll(3, TimeUnit.SECONDS);
 104         } catch (InterruptedException e) {
 105             throw new RuntimeException();
 106         }
 107         if (k != null)
 108             throw new RuntimeException("WatchKey not expected to be polled");
 109 
 110         // create bar and check we get create event
 111         file = top.resolve("bar").createFile();
 112         checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file));
 113         key.reset();
 114 
 115         // Test changing registration to <all> with FILE_TREE modifier
 116 
 117         k = top.register(watcher,
 118             new WatchEvent.Kind<?>[]{ ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY },
 119             FILE_TREE);
 120         if (k != key)
 121             throw new RuntimeException("Existing key not returned");
 122 
 123         // modify bar and check we get modify event
 124         OutputStream out = file.newOutputStream();
 125         try {
 126             out.write("Double shot expresso please".getBytes("UTF-8"));
 127         } finally {
 128             out.close();
 129         }
 130         checkExpectedEvent(watcher, ENTRY_MODIFY, top.relativize(file));
 131         key.reset();
 132     }
 133 
 134 
 135     public static void main(String[] args) throws IOException {
 136         if (!System.getProperty("os.name").startsWith("Windows")) {
 137             System.out.println("This is Windows-only test at this time!");
 138             return;
 139         }
 140 
 141         Path dir = TestUtil.createTemporaryDirectory();
 142         try {
 143             doTest(dir);
 144         } finally {
 145             TestUtil.removeAll(dir);
 146         }
 147     }
 148 }


  45         try {
  46             key = watcher.take();
  47         } catch (InterruptedException x) {
  48             // should not happen
  49             throw new RuntimeException(x);
  50         }
  51         WatchEvent<?> event = key.pollEvents().iterator().next();
  52         System.out.format("Event: type=%s, count=%d, context=%s\n",
  53             event.kind(), event.count(), event.context());
  54         if (event.kind() != expectedType)
  55             throw new RuntimeException("unexpected event");
  56         if (!expectedContext.equals(event.context()))
  57             throw new RuntimeException("unexpected context");
  58     }
  59 
  60     static void doTest(Path top) throws IOException {
  61         FileSystem fs = top.getFileSystem();
  62         WatchService watcher = fs.newWatchService();
  63 
  64         // create directories
  65         Path subdir = Files.createDirectories(top.resolve("a").resolve("b").resolve("c"));



  66 
  67         // Test ENTRY_CREATE with FILE_TREE modifier.
  68 
  69         WatchKey key = top.register(watcher,
  70             new WatchEvent.Kind<?>[]{ ENTRY_CREATE }, FILE_TREE);
  71 
  72         // create file in a/b/c and check we get create event
  73         Path file = Files.createFile(subdir.resolve("foo"));
  74         checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file));
  75         key.reset();
  76 
  77         // Test ENTRY_DELETE with FILE_TREE modifier.
  78 
  79         WatchKey k = top.register(watcher,
  80             new WatchEvent.Kind<?>[]{ ENTRY_DELETE }, FILE_TREE);
  81         if (k != key)
  82             throw new RuntimeException("Existing key not returned");
  83 
  84         // delete a/b/c/foo and check we get delete event
  85         Files.delete(file);
  86         checkExpectedEvent(watcher, ENTRY_DELETE, top.relativize(file));
  87         key.reset();
  88 
  89         // Test changing registration to ENTRY_CREATE without modifier
  90 
  91         k = top.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_CREATE });
  92         if (k != key)
  93             throw new RuntimeException("Existing key not returned");
  94 
  95         // create a/b/c/foo
  96         Files.createFile(file);
  97 
  98         // check that key is not queued
  99         WatchKey nextKey;
 100         try {
 101             nextKey = watcher.poll(3, TimeUnit.SECONDS);
 102         } catch (InterruptedException e) {
 103             throw new RuntimeException();
 104         }
 105         if (nextKey != null)
 106             throw new RuntimeException("WatchKey not expected to be polled");
 107 
 108         // create bar and check we get create event
 109         file = Files.createFile(top.resolve("bar"));
 110         checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file));
 111         key.reset();
 112 
 113         // Test changing registration to <all> with FILE_TREE modifier
 114 
 115         k = top.register(watcher,
 116             new WatchEvent.Kind<?>[]{ ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY },
 117             FILE_TREE);
 118         if (k != key)
 119             throw new RuntimeException("Existing key not returned");
 120 
 121         // modify bar and check we get modify event
 122         try (OutputStream out = Files.newOutputStream(file)) {

 123             out.write("Double shot expresso please".getBytes("UTF-8"));


 124         }
 125         checkExpectedEvent(watcher, ENTRY_MODIFY, top.relativize(file));
 126         key.reset();
 127     }
 128 
 129 
 130     public static void main(String[] args) throws IOException {
 131         if (!System.getProperty("os.name").startsWith("Windows")) {
 132             System.out.println("This is Windows-only test at this time!");
 133             return;
 134         }
 135 
 136         Path dir = TestUtil.createTemporaryDirectory();
 137         try {
 138             doTest(dir);
 139         } finally {
 140             TestUtil.removeAll(dir);
 141         }
 142     }
 143 }