if (notification.getKind() == Kind.ENTRY_DELETE) { for (Event event : notification.events()) { if (!notification.getPath().equals(event.getPath())) { changedInternal( new ResourceNotification( event.getPath(), Kind.ENTRY_DELETE, notification.getTimestamp(), notification.events())); if (notification.getKind() == Kind.ENTRY_CREATE) { for (Event event : notification.events()) { if (!notification.getPath().equals(event.getPath())) { createdParents.add(event.getPath()); String path = Paths.parent(notification.getPath()); while (path != null) { boolean isCreate = createdParents.contains(path); changedInternal( new ResourceNotification( path, isCreate ? Kind.ENTRY_CREATE : Kind.ENTRY_MODIFY, notification.getTimestamp(), relative(notification.events(), path)));
ResourceNotification.delta( watch.file, delta.created, new ResourceNotification( watch.getPath(), delta.kind, watch.last, events);
@Test public void fileEvents() throws Exception { File fileD = Paths.toFile(store.baseDirectory, "DirC/FileD"); AwaitResourceListener listener = new AwaitResourceListener(); store.get("DirC/FileD").addListener(listener); store.watcher.schedule(30, TimeUnit.MILLISECONDS); long before = fileD.lastModified(); long after = touch(fileD); assertTrue("touched", after > before); ResourceNotification n = listener.await(5, TimeUnit.SECONDS); assertNotNull("detected event", n); assertEquals("file modified", Kind.ENTRY_MODIFY, n.getKind()); assertTrue("Resource only", n.events().isEmpty()); listener.reset(); fileD.delete(); n = listener.await(5, TimeUnit.SECONDS); assertEquals("file removed", Kind.ENTRY_DELETE, n.getKind()); listener.reset(); fileD.createNewFile(); n = listener.await(5, TimeUnit.SECONDS); assertEquals("file created", Kind.ENTRY_CREATE, n.getKind()); store.get("DirC/FileD").removeListener(listener); } /**
ResourceNotification n = listener.await(5, TimeUnit.SECONDS); assertEquals(Kind.ENTRY_MODIFY, n.getKind()); assertEquals(Paths.BASE, n.getPath()); assertEquals(1, n.events().size()); Event e = n.events().get(0); assertEquals(Kind.ENTRY_MODIFY, e.getKind()); assertEquals("FileB", e.getPath()); fileA.delete(); n = listener.await(5, TimeUnit.SECONDS); assertEquals(Kind.ENTRY_MODIFY, n.getKind()); assertEquals(Paths.BASE, n.getPath()); e = n.events().get(0); assertEquals(Kind.ENTRY_DELETE, e.getKind()); assertEquals("FileA", e.getPath()); fileA.createNewFile(); n = listener.await(5, TimeUnit.SECONDS); assertEquals(Kind.ENTRY_MODIFY, n.getKind()); assertEquals(Paths.BASE, n.getPath()); e = n.events().get(0); assertEquals(Kind.ENTRY_CREATE, e.getKind()); assertEquals("FileA", e.getPath());
assertEquals(Kind.ENTRY_MODIFY, listener.getNotify().getKind()); assertEquals(1, listener.getNotify().events().size()); long timeStamp = listener.getNotify().getTimestamp(); assertTrue(timeStamp > before); assertEquals(Kind.ENTRY_DELETE, listener.getNotify().getKind()); assertEquals(1, listener.getNotify().events().size()); assertEquals(Kind.ENTRY_CREATE, listener.getNotify().getKind()); assertEquals(1, listener.getNotify().events().size());
@Test public void testDeleteWhileListening() { Resource res = store.get("DirA"); final ResourceListener deletingListener = new ResourceListener() { @Override public void changed(ResourceNotification notify) { assertTrue(watcher.removeListener(notify.getPath(), this)); } }; watcher.addListener(res.path(), deletingListener); watcher.changed( new ResourceNotification( "DirA", Kind.ENTRY_DELETE, System.currentTimeMillis(), Collections.emptyList())); // verify already deleted assertFalse(watcher.removeListener(res.path(), deletingListener)); }
@Override public void changed(ResourceNotification notify) { for (Event event : notify.events()) { if (event.getKind() == ResourceNotification.Kind.ENTRY_DELETE && event.getPath().equals(resource.name())) { // clean up deleted resources expanded.remove(resource.path()); } } } }
@Override public void changed(ResourceNotification notify) { if (kind == notify.getKind()) { checked = true; } }
/** * Send notification (without propagation), children may override this. * * @param notification */ protected void changedInternal(ResourceNotification notification) { List<ResourceListener> listeners = handlers.get(notification.getPath()); if (listeners != null) { // copy listeners into a new collection, // so that listeners can remove/add themselves if necessary listeners = new ArrayList<>(listeners); for (ResourceListener listener : listeners) { listener.changed(notification); } } }
ResourceNotification n = listener.await(5, TimeUnit.SECONDS); assertEquals(Kind.ENTRY_MODIFY, n.getKind()); assertEquals(Paths.BASE, n.getPath()); assertEquals(1, n.events().size()); Event e = n.events().get(0); assertEquals(Kind.ENTRY_MODIFY, e.getKind()); assertEquals("FileB", e.getPath()); fileA.delete(); n = listener.await(5, TimeUnit.SECONDS); assertEquals(Kind.ENTRY_MODIFY, n.getKind()); assertEquals(Paths.BASE, n.getPath()); e = n.events().get(0); assertEquals(Kind.ENTRY_DELETE, e.getKind()); assertEquals("FileA", e.getPath()); fileA.createNewFile(); n = listener.await(5, TimeUnit.SECONDS); assertEquals(Kind.ENTRY_MODIFY, n.getKind()); assertEquals(Paths.BASE, n.getPath()); e = n.events().get(0); assertEquals(Kind.ENTRY_CREATE, e.getKind()); assertEquals("FileA", e.getPath());
@Test public void fileEvents() throws Exception { File fileD = Paths.toFile(store.baseDirectory, "DirC/FileD"); AwaitResourceListener listener = new AwaitResourceListener(); store.get("DirC/FileD").addListener(listener); store.watcher.schedule(30, TimeUnit.MILLISECONDS); long before = fileD.lastModified(); long after = touch(fileD); assertTrue("touched", after > before); ResourceNotification n = listener.await(5, TimeUnit.SECONDS); assertNotNull("detected event", n); assertEquals("file modified", Kind.ENTRY_MODIFY, n.getKind()); assertTrue("Resource only", n.events().isEmpty()); listener.reset(); fileD.delete(); n = listener.await(5, TimeUnit.SECONDS); assertEquals("file removed", Kind.ENTRY_DELETE, n.getKind()); listener.reset(); fileD.createNewFile(); n = listener.await(5, TimeUnit.SECONDS); assertEquals("file created", Kind.ENTRY_CREATE, n.getKind()); store.get("DirC/FileD").removeListener(listener); } /**
@Test public void testModifyNotification() { Resource res = store.get("DirA/DirC/FileC1"); final CheckingResourceListener chkDirA = new CheckingResourceListener(Kind.ENTRY_MODIFY), chkDirC = new CheckingResourceListener(Kind.ENTRY_MODIFY), chkFileC1 = new CheckingResourceListener(Kind.ENTRY_MODIFY); watcher.addListener(res.path(), chkFileC1); watcher.addListener(store.get("DirA/DirC").path(), chkDirC); watcher.addListener(store.get("DirA").path(), chkDirA); List<Event> events = SimpleResourceNotificationDispatcher.createEvents(res, Kind.ENTRY_MODIFY); watcher.changed( new ResourceNotification( "DirA/DirC/FileC1", Kind.ENTRY_MODIFY, System.currentTimeMillis(), events)); // test that listeners received events assertFalse(chkDirA.isChecked()); assertTrue(chkDirC.isChecked()); assertTrue(chkFileC1.isChecked()); // remove listeners assertTrue(watcher.removeListener(res.path(), chkFileC1)); assertTrue(watcher.removeListener(store.get("DirA/DirC").path(), chkDirC)); assertTrue(watcher.removeListener(store.get("DirA").path(), chkDirA)); }
@Override public void changed(ResourceNotification notify) { for (Event event : notify.events()) { final String repoId;
@Override public void changed(ResourceNotification notify) { if (kind == notify.getKind()) { checked = true; } }
@Override public void changed(ResourceNotification notify) { assertTrue(watcher.removeListener(notify.getPath(), this)); } };
new ResourceNotification( "DirB/DirNew/FileNew", Kind.ENTRY_CREATE, assertEquals(Kind.ENTRY_MODIFY, dirEvent.get().getKind()); assertEquals("DirB", dirEvent.get().getPath()); assertEquals(2, dirEvent.get().events().size()); assertEquals(Kind.ENTRY_CREATE, dirEvent.get().events().get(0).getKind()); assertEquals("DirNew/FileNew", dirEvent.get().events().get(0).getPath()); assertEquals(Kind.ENTRY_CREATE, dirEvent.get().events().get(1).getKind()); assertEquals("DirNew", dirEvent.get().events().get(1).getPath());
SimpleResourceNotificationDispatcher.createEvents(res, Kind.ENTRY_CREATE); watcher.changed( new ResourceNotification( "DirA/DirC/DirD/FileQ", Kind.ENTRY_CREATE,
if (notify.getKind() == Kind.ENTRY_MODIFY) { try { loadConfigurations(resource);
@Override public void changed(ResourceNotification notify) { assertTrue(watcher.removeListener(notify.getPath(), this)); } };
assertEquals(Kind.ENTRY_MODIFY, listener.getNotify().getKind()); assertEquals(1, listener.getNotify().events().size()); assertEquals("DirC", listener.getNotify().getPath()); long timeStamp = listener.getNotify().getTimestamp(); assertTrue(timeStamp > before); Event e = listener.getNotify().events().get(0); assertEquals(Kind.ENTRY_MODIFY, e.getKind()); assertEquals("DirC/FileD", e.getPath()); assertEquals(Kind.ENTRY_MODIFY, listener.getNotify().getKind()); assertEquals(1, listener.getNotify().events().size()); assertEquals(Paths.BASE, listener.getNotify().getPath()); e = listener.getNotify().events().get(0); assertEquals(Kind.ENTRY_DELETE, e.getKind()); assertEquals("FileA", e.getPath()); try (OutputStream out = fileA.out()) { assertEquals(Kind.ENTRY_MODIFY, listener.getNotify().getKind()); assertEquals(1, listener.getNotify().events().size()); assertEquals(Paths.BASE, listener.getNotify().getPath()); e = listener.getNotify().events().get(0); assertEquals(Kind.ENTRY_CREATE, e.getKind()); assertEquals("FileA", e.getPath());