IOWatchServiceExecutorImpl implements IOWatchServiceExecutor { @Override public void execute(final WatchKey wk, final Filter<WatchEvent<?>> filter) { final List<WatchEvent<?>> events = wk.pollEvents(); WatchContext firstContext = null; if (events.size() > 1) { final Map<Path, Collection<ResourceChange>> changes = new HashMap<>(); for (final WatchEvent event : events) { if (!filter.doFilter(event)) { if (firstContext == null) { firstContext = (WatchContext) event.context(); } final Pair<Path, ResourceChange> result = buildChange(event); if (result != null) { if (!changes.containsKey(result.getK1())) { changes.put(result.getK1(), new ArrayList<>()); } changes.get(result.getK1()).add(result.getK2()); } } } if (changes.size() == 1 && changes.entrySet().iterator().next().getValue().size() == 1) { final ResourceChange _event = changes.values().iterator().next().iterator().next(); if (_event instanceof ResourceUpdated) { resourceUpdatedEvent.fire((ResourceUpdatedEvent) toEvent(changes.keySet().iterator().next(), _event, firstContext)); } else if (_event instanceof ResourceAdded) { resourceAddedEvent.fire((ResourceAddedEvent) toEvent(changes.keySet().iterator().next(), _event, firstContext)); } else if (_event instanceof ResourceRenamed) { resourceRenamedEvent.fire((ResourceRenamedEvent) toEvent(changes.keySet().iterator().next(), _event, firstContext)); } else if (_event instanceof ResourceDeleted) { resourceDeletedEvent.fire((ResourceDeletedEvent) toEvent(changes.keySet().iterator().next(), _event, firstContext)); } } else if (changes.size() >= 1) { resourceBatchChanges.fire(new ResourceBatchChangesEvent(changes, message(firstContext), sessionInfo(firstContext))); } } else if (events.size() == 1) { final WatchEvent<?> event = events.get(0); if (!filter.doFilter(event)) { if (event.kind().equals(StandardWatchEventKind.ENTRY_MODIFY)) { resourceUpdatedEvent.fire(buildEvent(ResourceUpdatedEvent.class, event).getK2()); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_CREATE)) { resourceAddedEvent.fire(buildEvent(ResourceAddedEvent.class, event).getK2()); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_RENAME)) { resourceRenamedEvent.fire(buildEvent(ResourceRenamedEvent.class, event).getK2()); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_DELETE)) { resourceDeletedEvent.fire(buildEvent(ResourceDeletedEvent.class, event).getK2()); } } } }  void setEvents(final Event<ResourceBatchChangesEvent> resourceBatchChanges,
                          final Event<ResourceUpdatedEvent> resourceUpdatedEvent,
                          final Event<ResourceRenamedEvent> resourceRenamedEvent,
                          final Event<ResourceDeletedEvent> resourceDeletedEvent,
                          final Event<ResourceAddedEvent> resourceAddedEvent); @Override void execute(final WatchKey wk,
                        final Filter<WatchEvent<?>> filter);  }
@SuppressWarnings("unchecked") @Test public void testMultipleEvents() throws Exception { List<WatchEvent<?>> events = new ArrayList<>(); events.add(mockWatchEvent(StandardWatchEventKind.ENTRY_MODIFY, ORIGINAL_FILE1_URI, NEW_FILE1_URI, SESSION_ID, USER, COMMIT_MESSAGE)); events.add(mockWatchEvent(StandardWatchEventKind.ENTRY_RENAME, ORIGINAL_FILE1_URI, NEW_FILE1_URI, SESSION_ID, USER, COMMIT_MESSAGE)); events.add(mockWatchEvent(StandardWatchEventKind.ENTRY_CREATE, ORIGINAL_FILE2_URI, NEW_FILE2_URI, SESSION_ID, USER, COMMIT_MESSAGE)); events.add(mockWatchEvent(StandardWatchEventKind.ENTRY_DELETE, ORIGINAL_FILE3_URI, NEW_FILE3_URI, SESSION_ID, USER, COMMIT_MESSAGE)); events.add(mockWatchEvent(StandardWatchEventKind.ENTRY_CREATE, ORIGINAL_FILE4_URI, NEW_FILE4_URI, SESSION_ID, USER, COMMIT_MESSAGE)); events.add(mockWatchEvent(StandardWatchEventKind.ENTRY_MODIFY, ORIGINAL_FILE4_URI, NEW_FILE4_URI, SESSION_ID, USER, COMMIT_MESSAGE)); events.add(mockWatchEvent(StandardWatchEventKind.ENTRY_DELETE, ORIGINAL_FILE4_URI, NEW_FILE4_URI, SESSION_ID, USER, COMMIT_MESSAGE)); WatchKey watchKey = mock(WatchKey.class); Filter<WatchEvent<?>> filter = mock(Filter.class); when(watchKey.pollEvents()).thenReturn(events); when(filter.doFilter(any(WatchEvent.class))).thenReturn(false); watchServiceExecutor.execute(watchKey, filter); verify(resourceBatchChanges).fire(resourceBatchChangesEventCaptor.capture()); verifyResourceChange(resourceBatchChangesEventCaptor.getValue(), ORIGINAL_FILE1_URI, COMMIT_MESSAGE, ResourceChangeType.UPDATE); verifyResourceChange(resourceBatchChangesEventCaptor.getValue(), ORIGINAL_FILE1_URI, COMMIT_MESSAGE, ResourceChangeType.RENAME); verifyResourceChange(resourceBatchChangesEventCaptor.getValue(), NEW_FILE2_URI, COMMIT_MESSAGE, ResourceChangeType.ADD); verifyResourceChange(resourceBatchChangesEventCaptor.getValue(), ORIGINAL_FILE3_URI, COMMIT_MESSAGE, ResourceChangeType.DELETE); verifyResourceChange(resourceBatchChangesEventCaptor.getValue(), NEW_FILE4_URI, COMMIT_MESSAGE, ResourceChangeType.ADD); verifyResourceChange(resourceBatchChangesEventCaptor.getValue(), ORIGINAL_FILE4_URI, COMMIT_MESSAGE, ResourceChangeType.UPDATE); verifyResourceChange(resourceBatchChangesEventCaptor.getValue(), ORIGINAL_FILE4_URI, COMMIT_MESSAGE, ResourceChangeType.DELETE); }