Skip to content

Commit

Permalink
Fix WatchService tests (openhab#3518)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <github@klug.nrw>
GitOrigin-RevId: ee392e8
  • Loading branch information
J-N-K authored and splatch committed Jul 12, 2023
1 parent fe8659d commit ada9ba3
Showing 1 changed file with 27 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.core.JavaTest;
import org.openhab.core.OpenHAB;
import org.openhab.core.service.WatchService;
import org.openhab.core.service.WatchService.Kind;
import org.osgi.framework.BundleContext;
Expand All @@ -49,44 +48,37 @@
@MockitoSettings(strictness = Strictness.LENIENT)
public class WatchServiceImplTest extends JavaTest {
private static final String SUB_DIR_PATH_NAME = "subDir";
private static final String TEST_FILE_NANE = "testFile";

private @NonNullByDefault({}) String systemConfDirProperty;
private static final String TEST_FILE_NAME = "testFile";

public @Mock @NonNullByDefault({}) WatchServiceImpl.WatchServiceConfiguration configurationMock;
public @Mock @NonNullByDefault({}) BundleContext bundleContextMock;

private @NonNullByDefault({}) WatchServiceImpl watchService;
private @NonNullByDefault({}) Path rootPath;
private @NonNullByDefault({}) @TempDir Path rootPath;
private @NonNullByDefault({}) TestWatchEventListener listener;

@BeforeEach
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.path()).thenReturn("");
when(configurationMock.path()).thenReturn(rootPath.toString());

watchService = new WatchServiceImpl(configurationMock, mock(BundleContext.class));
watchService = new WatchServiceImpl(configurationMock, bundleContextMock);
listener = new TestWatchEventListener();

verify(bundleContextMock, timeout(5000)).registerService(eq(WatchService.class), eq(watchService), any());
}

@AfterEach
public void tearDown() throws IOException {
watchService.deactivate();
System.setProperty(OpenHAB.CONFIG_DIR_PROG_ARGUMENT, systemConfDirProperty);
}

@Test
@Disabled("Broken")
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 relativeTestFilePath = Path.of(TEST_FILE_NANE);
Path testFile = rootPath.resolve(TEST_FILE_NAME);
Path relativeTestFilePath = Path.of(TEST_FILE_NAME);

Files.writeString(testFile, "initial content", StandardCharsets.UTF_8);
assertEvent(relativeTestFilePath, Kind.CREATE);
Expand All @@ -102,13 +94,14 @@ public void testFileInWatchedDir() throws IOException, InterruptedException {
}

@Test
@Disabled("Broken")
public void testFileInWatchedSubDir() throws IOException, InterruptedException {
Files.createDirectories(rootPath.resolve(SUB_DIR_PATH_NAME));

// listener is listening to root and sub-dir
watchService.registerListener(listener, Path.of(""), false);
watchService.registerListener(listener, rootPath, true);

Path testFile = rootPath.resolve(SUB_DIR_PATH_NAME).resolve(TEST_FILE_NANE);
Path relativeTestFilePath = Path.of(SUB_DIR_PATH_NAME, TEST_FILE_NANE);
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);
assertEvent(relativeTestFilePath, Kind.CREATE);
Expand All @@ -124,13 +117,14 @@ public void testFileInWatchedSubDir() throws IOException, InterruptedException {
}

@Test
@Disabled("Broken")
public void testFileInWatchedSubDir2() throws IOException, InterruptedException {
Files.createDirectories(rootPath.resolve(SUB_DIR_PATH_NAME));

// listener is only listening to sub-dir of root
watchService.registerListener(listener, Path.of(SUB_DIR_PATH_NAME), false);

Path testFile = rootPath.resolve(SUB_DIR_PATH_NAME).resolve(TEST_FILE_NANE);
Path relativeTestFilePath = Path.of(TEST_FILE_NANE);
Path testFile = rootPath.resolve(SUB_DIR_PATH_NAME).resolve(TEST_FILE_NAME);
Path relativeTestFilePath = Path.of(TEST_FILE_NAME);

Files.writeString(testFile, "initial content", StandardCharsets.UTF_8);
assertEvent(relativeTestFilePath, Kind.CREATE);
Expand All @@ -146,11 +140,12 @@ public void testFileInWatchedSubDir2() throws IOException, InterruptedException
}

@Test
@Disabled("Broken")
public void testFileInUnwatchedSubDir() throws IOException, InterruptedException {
watchService.registerListener(listener, Path.of(""), false);
Files.createDirectories(rootPath.resolve(SUB_DIR_PATH_NAME));

watchService.registerListener(listener, rootPath, 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);

Files.writeString(testFile, "initial content", StandardCharsets.UTF_8);
assertNoEvent();
Expand All @@ -166,15 +161,14 @@ public void testFileInUnwatchedSubDir() throws IOException, InterruptedException
}

@Test
@Disabled("Broken")
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));
assertNoEvent();

Path testFile = subDirSubDir.resolve(TEST_FILE_NANE);
Path relativeTestFilePath = testFile.relativize(rootPath);
Path testFile = subDirSubDir.resolve(TEST_FILE_NAME);
Path relativeTestFilePath = rootPath.relativize(testFile);

Files.writeString(testFile, "initial content", StandardCharsets.UTF_8);
assertEvent(relativeTestFilePath, Kind.CREATE);
Expand Down

0 comments on commit ada9ba3

Please sign in to comment.