Fix WatchService tests (#3518)

Signed-off-by: Jan N. Klug <github@klug.nrw>
pull/3523/head
J-N-K 2023-04-04 07:33:16 +02:00 committed by GitHub
parent 38a6d1e87b
commit ee392e861f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 34 deletions

View File

@ -26,15 +26,14 @@ import java.util.concurrent.CopyOnWriteArrayList;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness; import org.mockito.quality.Strictness;
import org.openhab.core.JavaTest; import org.openhab.core.JavaTest;
import org.openhab.core.OpenHAB;
import org.openhab.core.service.WatchService; import org.openhab.core.service.WatchService;
import org.openhab.core.service.WatchService.Kind; import org.openhab.core.service.WatchService.Kind;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
@ -49,44 +48,37 @@ import org.osgi.framework.BundleContext;
@MockitoSettings(strictness = Strictness.LENIENT) @MockitoSettings(strictness = Strictness.LENIENT)
public class WatchServiceImplTest extends JavaTest { public class WatchServiceImplTest extends JavaTest {
private static final String SUB_DIR_PATH_NAME = "subDir"; private static final String SUB_DIR_PATH_NAME = "subDir";
private static final String TEST_FILE_NANE = "testFile"; private static final String TEST_FILE_NAME = "testFile";
private @NonNullByDefault({}) String systemConfDirProperty;
public @Mock @NonNullByDefault({}) WatchServiceImpl.WatchServiceConfiguration configurationMock; public @Mock @NonNullByDefault({}) WatchServiceImpl.WatchServiceConfiguration configurationMock;
public @Mock @NonNullByDefault({}) BundleContext bundleContextMock;
private @NonNullByDefault({}) WatchServiceImpl watchService; private @NonNullByDefault({}) WatchServiceImpl watchService;
private @NonNullByDefault({}) Path rootPath; private @NonNullByDefault({}) @TempDir Path rootPath;
private @NonNullByDefault({}) TestWatchEventListener listener; private @NonNullByDefault({}) TestWatchEventListener listener;
@BeforeEach @BeforeEach
public void setup() throws IOException { public void setup() throws IOException {
// store property so we can restore later
systemConfDirProperty = System.getProperty(OpenHAB.CONFIG_DIR_PROG_ARGUMENT);
rootPath = Files.createDirectories(Path.of("target", "test-watcher"));
System.setProperty(OpenHAB.CONFIG_DIR_PROG_ARGUMENT, rootPath.toString());
when(configurationMock.name()).thenReturn("unnamed"); when(configurationMock.name()).thenReturn("unnamed");
when(configurationMock.path()).thenReturn(""); when(configurationMock.path()).thenReturn(rootPath.toString());
watchService = new WatchServiceImpl(configurationMock, mock(BundleContext.class)); watchService = new WatchServiceImpl(configurationMock, bundleContextMock);
listener = new TestWatchEventListener(); listener = new TestWatchEventListener();
verify(bundleContextMock, timeout(5000)).registerService(eq(WatchService.class), eq(watchService), any());
} }
@AfterEach @AfterEach
public void tearDown() throws IOException { public void tearDown() throws IOException {
watchService.deactivate(); watchService.deactivate();
System.setProperty(OpenHAB.CONFIG_DIR_PROG_ARGUMENT, systemConfDirProperty);
} }
@Test @Test
@Disabled("Broken")
public void testFileInWatchedDir() throws IOException, InterruptedException { public void testFileInWatchedDir() throws IOException, InterruptedException {
watchService.registerListener(listener, Path.of(""), false); watchService.registerListener(listener, rootPath, false);
Path testFile = rootPath.resolve(TEST_FILE_NANE); Path testFile = rootPath.resolve(TEST_FILE_NAME);
Path relativeTestFilePath = Path.of(TEST_FILE_NANE); Path relativeTestFilePath = Path.of(TEST_FILE_NAME);
Files.writeString(testFile, "initial content", StandardCharsets.UTF_8); Files.writeString(testFile, "initial content", StandardCharsets.UTF_8);
assertEvent(relativeTestFilePath, Kind.CREATE); assertEvent(relativeTestFilePath, Kind.CREATE);
@ -102,13 +94,14 @@ public class WatchServiceImplTest extends JavaTest {
} }
@Test @Test
@Disabled("Broken")
public void testFileInWatchedSubDir() throws IOException, InterruptedException { public void testFileInWatchedSubDir() throws IOException, InterruptedException {
// listener is listening to root and sub-dir Files.createDirectories(rootPath.resolve(SUB_DIR_PATH_NAME));
watchService.registerListener(listener, Path.of(""), false);
Path testFile = rootPath.resolve(SUB_DIR_PATH_NAME).resolve(TEST_FILE_NANE); // listener is listening to root and sub-dir
Path relativeTestFilePath = Path.of(SUB_DIR_PATH_NAME, TEST_FILE_NANE); watchService.registerListener(listener, rootPath, true);
Path testFile = rootPath.resolve(SUB_DIR_PATH_NAME).resolve(TEST_FILE_NAME);
Path relativeTestFilePath = Path.of(SUB_DIR_PATH_NAME, TEST_FILE_NAME);
Files.writeString(testFile, "initial content", StandardCharsets.UTF_8); Files.writeString(testFile, "initial content", StandardCharsets.UTF_8);
assertEvent(relativeTestFilePath, Kind.CREATE); assertEvent(relativeTestFilePath, Kind.CREATE);
@ -124,13 +117,14 @@ public class WatchServiceImplTest extends JavaTest {
} }
@Test @Test
@Disabled("Broken")
public void testFileInWatchedSubDir2() throws IOException, InterruptedException { public void testFileInWatchedSubDir2() throws IOException, InterruptedException {
Files.createDirectories(rootPath.resolve(SUB_DIR_PATH_NAME));
// listener is only listening to sub-dir of root // listener is only listening to sub-dir of root
watchService.registerListener(listener, Path.of(SUB_DIR_PATH_NAME), false); watchService.registerListener(listener, Path.of(SUB_DIR_PATH_NAME), false);
Path testFile = rootPath.resolve(SUB_DIR_PATH_NAME).resolve(TEST_FILE_NANE); Path testFile = rootPath.resolve(SUB_DIR_PATH_NAME).resolve(TEST_FILE_NAME);
Path relativeTestFilePath = Path.of(TEST_FILE_NANE); Path relativeTestFilePath = Path.of(TEST_FILE_NAME);
Files.writeString(testFile, "initial content", StandardCharsets.UTF_8); Files.writeString(testFile, "initial content", StandardCharsets.UTF_8);
assertEvent(relativeTestFilePath, Kind.CREATE); assertEvent(relativeTestFilePath, Kind.CREATE);
@ -146,11 +140,12 @@ public class WatchServiceImplTest extends JavaTest {
} }
@Test @Test
@Disabled("Broken")
public void testFileInUnwatchedSubDir() throws IOException, InterruptedException { public void testFileInUnwatchedSubDir() throws IOException, InterruptedException {
watchService.registerListener(listener, Path.of(""), false); Files.createDirectories(rootPath.resolve(SUB_DIR_PATH_NAME));
Path testFile = rootPath.resolve(SUB_DIR_PATH_NAME).resolve(TEST_FILE_NANE); watchService.registerListener(listener, rootPath, false);
Path testFile = rootPath.resolve(SUB_DIR_PATH_NAME).resolve(TEST_FILE_NAME);
Files.writeString(testFile, "initial content", StandardCharsets.UTF_8); Files.writeString(testFile, "initial content", StandardCharsets.UTF_8);
assertNoEvent(); assertNoEvent();
@ -166,15 +161,14 @@ public class WatchServiceImplTest extends JavaTest {
} }
@Test @Test
@Disabled("Broken")
public void testNewSubDirAlsoWatched() throws IOException, InterruptedException { public void testNewSubDirAlsoWatched() throws IOException, InterruptedException {
watchService.registerListener(listener, Path.of(""), false); watchService.registerListener(listener, rootPath, true);
Path subDirSubDir = Files.createDirectories(rootPath.resolve(SUB_DIR_PATH_NAME).resolve(SUB_DIR_PATH_NAME)); Path subDirSubDir = Files.createDirectories(rootPath.resolve(SUB_DIR_PATH_NAME).resolve(SUB_DIR_PATH_NAME));
assertNoEvent(); assertNoEvent();
Path testFile = subDirSubDir.resolve(TEST_FILE_NANE); Path testFile = subDirSubDir.resolve(TEST_FILE_NAME);
Path relativeTestFilePath = testFile.relativize(rootPath); Path relativeTestFilePath = rootPath.relativize(testFile);
Files.writeString(testFile, "initial content", StandardCharsets.UTF_8); Files.writeString(testFile, "initial content", StandardCharsets.UTF_8);
assertEvent(relativeTestFilePath, Kind.CREATE); assertEvent(relativeTestFilePath, Kind.CREATE);