ListenerTask implements Runnable { @Override public void run() { try (WatchService watcher = watchService) { scanExistFiles(); for (;;) { WatchKey key = watcher.take(); for (WatchEvent<?> event : key.pollEvents()) { if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) { Path file = ((Path) event.context()); Path dirAndFile = fileSystem.getPath(directory.getPath()).resolve(file); newFileAction.execute(new info.smart_tools.smartactors.base.path.Path(dirAndFile)); } } if (!key.reset()) { break; } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (IOException | ActionExecuteException | InvalidArgumentException e) { throw new RuntimeException(e); } } ListenerTask(final IPath directory, final IAction<IPath> newFileAction);   ListenerTask(final IPath directory, final IAction<IPath> newFileAction, final FileSystem fileSystem); @Override void run();  }
@Test public void Should_queryListOfExistFilesAndPollWatchServiceEvents() throws Exception { DirectoryStream directoryStreamMock = mock(DirectoryStream.class); Iterator directoryStreamIteratorMock = mock(Iterator.class); java.nio.file.Path[] filesMock = new java.nio.file.Path[] { mock(java.nio.file.Path.class), mock(java.nio.file.Path.class) }; when(filesMock[0].toString()).thenReturn("dir/one"); when(filesMock[1].toString()).thenReturn("dir/two"); when(directoryStreamIteratorMock.hasNext()).thenReturn(true, true, false); when(directoryStreamIteratorMock.next()).thenReturn(filesMock[0], filesMock[1]); when(directoryStreamMock.iterator()).thenReturn(directoryStreamIteratorMock); when(fileSystemProviderMock.newDirectoryStream(any(), any())).thenReturn(directoryStreamMock); java.nio.file.Path newFileEventPathMock = mock(java.nio.file.Path.class); when(newFileEventPathMock.toString()).thenReturn("new"); java.nio.file.Path newFileActualPathMock = mock(java.nio.file.Path.class); when(newFileActualPathMock.toString()).thenReturn("dir/new"); when(directoryPathMock.resolve(newFileEventPathMock)).thenReturn(newFileActualPathMock); when(event1Mock.kind()).thenReturn((WatchEvent.Kind)StandardWatchEventKinds.ENTRY_CREATE); when(event1Mock.context()).thenReturn(newFileEventPathMock); when(event2Mock.kind()).thenReturn((WatchEvent.Kind)StandardWatchEventKinds.ENTRY_DELETE); when(watchKeyMock.pollEvents()) .thenReturn(Arrays.asList(event1Mock, event2Mock)) .thenReturn(Collections.emptyList()); when(watchServiceMock.take()).thenReturn(watchKeyMock); when(watchKeyMock.reset()).thenReturn(true).thenReturn(false); ListenerTask task = new ListenerTask(directoryMock, actionMock, fileSystemMock); task.run(); verify(actionMock, times(1)).execute(eq(new info.smart_tools.smartactors.base.path.Path("dir/one"))); verify(actionMock, times(1)).execute(eq(new info.smart_tools.smartactors.base.path.Path("dir/two"))); verify(actionMock, times(1)).execute(eq(new info.smart_tools.smartactors.base.path.Path("dir/new"))); verify(actionMock, times(3)).execute(any()); }
@Test public void Should_handleThreadInterruption() throws Exception { DirectoryStream directoryStreamMock = mock(DirectoryStream.class); Iterator directoryStreamIteratorMock = mock(Iterator.class); when(directoryStreamIteratorMock.hasNext()).thenReturn(false); when(directoryStreamMock.iterator()).thenReturn(directoryStreamIteratorMock); when(fileSystemProviderMock.newDirectoryStream(any(), any())).thenReturn(directoryStreamMock); when(watchServiceMock.take()).thenThrow(new InterruptedException()); ListenerTask task = new ListenerTask(directoryMock, actionMock, fileSystemMock); task.run(); assertTrue(Thread.interrupted()); }
@Test public void Should_wrapActionExceptionsIntoRuntimeExceptions() throws Exception { DirectoryStream directoryStreamMock = mock(DirectoryStream.class); Iterator directoryStreamIteratorMock = mock(Iterator.class); java.nio.file.Path[] filesMock = new java.nio.file.Path[] { mock(java.nio.file.Path.class) }; when(directoryStreamIteratorMock.hasNext()).thenReturn(true, false); when(directoryStreamIteratorMock.next()).thenReturn(filesMock[0]); when(directoryStreamMock.iterator()).thenReturn(directoryStreamIteratorMock); when(fileSystemProviderMock.newDirectoryStream(any(), any())).thenReturn(directoryStreamMock); ListenerTask task = new ListenerTask(directoryMock, actionMock, fileSystemMock); doThrow(new ActionExecuteException("")).when(actionMock).execute(any(IPath.class)); try { task.run(); fail(); } catch (RuntimeException e) { assertTrue(e.getCause() instanceof ActionExecuteException); } }