Allow changing watched directory of a WatchService (#2894)
Signed-off-by: Jan N. Klug <github@klug.nrw>pull/2932/head
parent
a192a1dfc6
commit
b331066ea9
|
@ -37,14 +37,25 @@ import org.eclipse.jdt.annotation.Nullable;
|
|||
public abstract class AbstractWatchService {
|
||||
protected @Nullable String pathToWatch;
|
||||
|
||||
/**
|
||||
* The queue reader
|
||||
*/
|
||||
protected WatchQueueReader watchQueueReader = WatchQueueReader.getInstance();
|
||||
|
||||
protected AbstractWatchService(String pathToWatch) {
|
||||
this.pathToWatch = pathToWatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* The queue reader
|
||||
* Change the watch directory for this WatchService
|
||||
*
|
||||
* @param pathToWatch the new path
|
||||
*/
|
||||
protected WatchQueueReader watchQueueReader = WatchQueueReader.getInstance();
|
||||
protected void changeWatchDirectory(String pathToWatch) {
|
||||
deactivate();
|
||||
this.pathToWatch = pathToWatch;
|
||||
activate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to call on service activation
|
||||
|
@ -61,7 +72,6 @@ public abstract class AbstractWatchService {
|
|||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public void deactivate() {
|
||||
WatchQueueReader watchQueueReader = this.watchQueueReader;
|
||||
watchQueueReader.stopWatchService(this);
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,8 @@ package org.openhab.core.service;
|
|||
import static java.nio.file.StandardWatchEventKinds.*;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
|
@ -175,6 +177,18 @@ public class AbstractWatchServiceTest extends JavaTest {
|
|||
assertNoEventsAreProcessed();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangeDirectory() {
|
||||
WatchQueueReader watchQueueReaderMock = mock(WatchQueueReader.class);
|
||||
watchService = new RelativeWatchService("foo", false);
|
||||
watchService.setWatchQueueReader(watchQueueReaderMock);
|
||||
watchService.activate();
|
||||
verify(watchQueueReaderMock).customizeWatchQueueReader(watchService, Path.of("foo"), false);
|
||||
watchService.changeWatchDirectory("bar");
|
||||
verify(watchQueueReaderMock).stopWatchService(watchService);
|
||||
verify(watchQueueReaderMock).customizeWatchQueueReader(watchService, Path.of("bar"), false);
|
||||
}
|
||||
|
||||
private void assertNoEventsAreProcessed() throws Exception {
|
||||
// Wait for a possible event for the maximum timeout
|
||||
Thread.sleep(noEventTimeoutInSeconds * 1000);
|
||||
|
@ -281,6 +295,15 @@ public class AbstractWatchServiceTest extends JavaTest {
|
|||
watchSubDirs = watchSubDirectories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject a mocked WatchQueueReader
|
||||
*
|
||||
* @param watchQueueReader the mock
|
||||
*/
|
||||
public void setWatchQueueReader(WatchQueueReader watchQueueReader) {
|
||||
this.watchQueueReader = watchQueueReader;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processWatchEvent(WatchEvent<?> event, Kind<?> kind, Path path) {
|
||||
FullEvent fullEvent = new FullEvent(event, kind, path);
|
||||
|
|
Loading…
Reference in New Issue