@Override public boolean doFilter(WatchEvent<?> object) { final WatchContext context = (WatchContext) object.context(); if (object.kind().equals(StandardWatchEventKind.ENTRY_MODIFY)) { if (shouldFilter(context.getOldPath())) { return true; } } else if (object.kind().equals(StandardWatchEventKind.ENTRY_CREATE)) { if (shouldFilter(context.getPath())) { return true; } } else if (object.kind().equals(StandardWatchEventKind.ENTRY_RENAME)) { if (shouldFilter(context.getOldPath())) { return true; } } else if (object.kind().equals(StandardWatchEventKind.ENTRY_DELETE)) { if (shouldFilter(context.getOldPath())) { return true; } } return false; }
private SessionInfo sessionInfo(final WatchContext context) { final String sessionId; final String user; if (context.getSessionId() == null) { sessionId = "<system>"; } else { sessionId = context.getSessionId(); } if (context.getUser() == null) { user = "<system>"; } else { user = context.getUser(); } return new SessionInfoImpl(sessionId, new UserImpl(user)); } }
private Pair<Path, ResourceChange> buildChange(final WatchEvent<?> event) { final WatchContext context = (WatchContext) event.context(); final Path _affectedPath; final ResourceChange result; if (event.kind().equals(StandardWatchEventKind.ENTRY_MODIFY)) { _affectedPath = convert(context.getOldPath()); result = new ResourceUpdated(context.getMessage()); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_CREATE)) { _affectedPath = convert(context.getPath()); result = new ResourceAdded(context.getMessage()); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_RENAME)) { _affectedPath = convert(context.getOldPath()); result = new ResourceRenamed(convert(context.getPath()), context.getMessage()); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_DELETE)) { _affectedPath = convert(context.getOldPath()); result = new ResourceDeleted(context.getMessage()); } else { _affectedPath = null; result = null; } if (_affectedPath == null) { return null; } return Pair.newPair(_affectedPath, result); }
@SuppressWarnings("unchecked") private WatchEvent<?> mockWatchEvent(WatchEvent.Kind kind, String originalPathURI, String newPathURI, String sessionId, String userId, String commitMessage) throws URISyntaxException { WatchEvent<?> event = mock(WatchEvent.class); when(event.kind()).thenReturn(kind); FileSystem fileSystem = mock(FileSystem.class); Set<String> supportedViews = new HashSet<>(); when(fileSystem.supportedFileAttributeViews()).thenReturn(supportedViews); WatchContext context = mock(WatchContext.class); when(context.getSessionId()).thenReturn(sessionId); when(context.getUser()).thenReturn(userId); when(context.getMessage()).thenReturn(commitMessage); Path oldPath = mock(Path.class); URI oldPathUri = new URI(originalPathURI); when(oldPath.toUri()).thenReturn(oldPathUri); when(oldPath.getFileSystem()).thenReturn(fileSystem); Path path = mock(Path.class); URI pathUri = new URI(newPathURI); when(path.toUri()).thenReturn(pathUri); when(path.getFileSystem()).thenReturn(fileSystem); when(context.getOldPath()).thenReturn(oldPath); when(context.getPath()).thenReturn(path); when(event.context()).thenReturn(context); return event; }
@Test public void deleteOnBatchEventShouldKeepUserInfo() { final Path init = ioService.get(URI.create("git://amend-repo-test/file.txt")); final WatchService ws = init.getFileSystem().newWatchService(); String user = "dora"; String message = "message"; ioService.write(init, "init!", new CommentedOption(user, message)); { List<WatchEvent<?>> events = ws.poll().pollEvents(); } ioService.startBatch(init.getFileSystem()); ioService.delete(init, new CommentedOption(user, message)); ioService.endBatch(); { List<WatchEvent<?>> events = ws.poll().pollEvents(); JGitWatchEvent event = (JGitWatchEvent) events.get(0); WatchContext context = (WatchContext) event.context(); assertEquals(user, context.getUser()); } }
private String message(final WatchContext context) { if (context == null) { return null; } return context.getMessage(); }
@SuppressWarnings("unchecked") private WatchEvent<?> mockWatchEvent(WatchEvent.Kind kind, String originalPathURI, String newPathURI, String sessionId, String userId, String commitMessage) throws URISyntaxException { WatchEvent<?> event = mock(WatchEvent.class); when(event.kind()).thenReturn(kind); FileSystem fileSystem = mock(FileSystem.class); Set<String> supportedViews = new HashSet<>(); when(fileSystem.supportedFileAttributeViews()).thenReturn(supportedViews); WatchContext context = mock(WatchContext.class); when(context.getSessionId()).thenReturn(sessionId); when(context.getUser()).thenReturn(userId); when(context.getMessage()).thenReturn(commitMessage); Path oldPath = mock(Path.class); URI oldPathUri = new URI(originalPathURI); when(oldPath.toUri()).thenReturn(oldPathUri); when(oldPath.getFileSystem()).thenReturn(fileSystem); Path path = mock(Path.class); URI pathUri = new URI(newPathURI); when(path.toUri()).thenReturn(pathUri); when(path.getFileSystem()).thenReturn(fileSystem); when(context.getOldPath()).thenReturn(oldPath); when(context.getPath()).thenReturn(path); when(event.context()).thenReturn(context); return event; }
@Test public void deleteOnBatchEventShouldKeepUserInfo() { final Path init = ioService.get(URI.create("git://amend-repo-test/file.txt")); final WatchService ws = init.getFileSystem().newWatchService(); String user = "dora"; String message = "message"; ioService.write(init, "init!", new CommentedOption(user, message)); { List<WatchEvent<?>> events = ws.poll().pollEvents(); } ioService.startBatch(init.getFileSystem()); ioService.delete(init, new CommentedOption(user, message)); ioService.endBatch(); { List<WatchEvent<?>> events = ws.poll().pollEvents(); JGitWatchEvent event = (JGitWatchEvent) events.get(0); WatchContext context = (WatchContext) event.context(); assertEquals(user, context.getUser()); } }
private ResourceEvent toEvent(final Path path, final ResourceChange change, final WatchContext context) { if (change instanceof ResourceUpdated) { return new ResourceUpdatedEvent(path, context.getMessage(), sessionInfo(context)); } else if (change instanceof ResourceAdded) { return new ResourceAddedEvent(path, context.getMessage(), sessionInfo(context)); } else if (change instanceof ResourceRenamed) { return new ResourceRenamedEvent(path, ((ResourceRenamed) change).getDestinationPath(), context.getMessage(), sessionInfo(context)); } else if (change instanceof ResourceDeleted) { return new ResourceDeletedEvent(path, context.getMessage(), sessionInfo(context)); } return null; }
final WatchContext context = (WatchContext) event.context(); if (event.kind().equals(StandardWatchEventKind.ENTRY_MODIFY)) { if (context.getOldPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break; if (context.getPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break; if (context.getOldPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break; if (context.getOldPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break;
final T result; if (event.kind().equals(StandardWatchEventKind.ENTRY_MODIFY)) { _affectedPath = convert(context.getOldPath()); result = (T) new ResourceUpdatedEvent(_affectedPath, context.getMessage(), sessionInfo(context)); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_CREATE)) { _affectedPath = convert(context.getPath()); result = (T) new ResourceAddedEvent(_affectedPath, context.getMessage(), sessionInfo(context)); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_RENAME)) { _affectedPath = convert(context.getOldPath()); result = (T) new ResourceRenamedEvent(_affectedPath, convert(context.getPath()), context.getMessage(), sessionInfo(context)); } else if (event.kind().equals(StandardWatchEventKind.ENTRY_DELETE)) { _affectedPath = convert(context.getOldPath()); result = (T) new ResourceDeletedEvent(_affectedPath, context.getMessage(), sessionInfo(context)); } else {
final WatchContext context = (WatchContext) event.context(); if (event.kind().equals(StandardWatchEventKind.ENTRY_MODIFY)) { if (context.getOldPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break; if (context.getPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break; if (context.getOldPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break; if (context.getOldPath().getFileName().toString().equals(LAST_MODIFIED_MARKER_FILE)) { markerFileModified = true; break;
final Path path = (created) ? context.getPath() : context.getOldPath();