Fix ConfigDispatcherFileWatcher (#3384)

Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
J-N-K 2023-02-19 11:47:20 +01:00 committed by GitHub
parent 0da4a226f1
commit 1b18831495
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 31 deletions

View File

@ -12,7 +12,6 @@
*/
package org.openhab.core.config.dispatch.internal;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@ -65,22 +64,22 @@ public class ConfigDispatcherFileWatcher implements WatchService.WatchEventListe
@Override
public void processWatchEvent(WatchService.Kind kind, Path path) {
Path fullPath = watchService.getWatchPath().resolve(path);
try {
if (kind == WatchService.Kind.CREATE || kind == WatchService.Kind.MODIFY) {
if (!Files.isHidden(path) && path.toString().endsWith(".cfg")) {
configDispatcher.processConfigFile(path.toFile());
if (!Files.isHidden(fullPath) && fullPath.toString().endsWith(".cfg")) {
configDispatcher.processConfigFile(fullPath.toFile());
}
} else if (kind == WatchService.Kind.DELETE) {
// Detect if a service specific configuration file was removed. We want to
// notify the service in this case with an updated empty configuration.
File configFile = path.toFile();
if (Files.isHidden(path) || Files.isDirectory(path) || !path.toString().endsWith(".cfg")) {
if (Files.isHidden(fullPath) || Files.isDirectory(fullPath) || !fullPath.toString().endsWith(".cfg")) {
return;
}
configDispatcher.fileRemoved(configFile.getAbsolutePath());
configDispatcher.fileRemoved(fullPath.toString());
}
} catch (IOException e) {
logger.error("Failed to process watch event {} for {}", kind, path);
logger.error("Failed to process watch event {} for {}", kind, path, e);
}
}
}

View File

@ -14,12 +14,15 @@ package org.openhab.core.config.dispatch.internal;
import static org.mockito.Mockito.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
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.openhab.core.service.WatchService;
@ -36,57 +39,58 @@ public class ConfigDispatcherFileWatcherTest {
private @Mock @NonNullByDefault({}) ConfigDispatcher configDispatcherMock;
private @Mock @NonNullByDefault({}) WatchService watchService;
private @TempDir @NonNullByDefault({}) Path tempDir;
private @NonNullByDefault({}) Path cfgPath;
private @NonNullByDefault({}) Path nonCfgPath;
@BeforeEach
public void setUp() {
public void setUp() throws IOException {
configDispatcherFileWatcher = new ConfigDispatcherFileWatcher(configDispatcherMock, watchService);
verify(configDispatcherMock).processConfigFile(any());
when(watchService.getWatchPath()).thenReturn(tempDir.toAbsolutePath());
cfgPath = tempDir.resolve("myPath.cfg");
nonCfgPath = tempDir.resolve("myPath");
Files.createFile(cfgPath);
Files.createFile(nonCfgPath);
}
@Test
public void configurationFileCreated() {
Path path = Path.of("myPath.cfg");
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.CREATE, path);
verify(configDispatcherMock).processConfigFile(path.toFile());
public void configurationFileCreated() throws IOException {
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.CREATE, cfgPath);
verify(configDispatcherMock).processConfigFile(cfgPath.toAbsolutePath().toFile());
}
@Test
public void configurationFileModified() {
Path path = Path.of("myPath.cfg");
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.MODIFY, path);
verify(configDispatcherMock).processConfigFile(path.toFile());
public void configurationFileModified() throws IOException {
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.MODIFY, cfgPath);
verify(configDispatcherMock).processConfigFile(cfgPath.toAbsolutePath().toFile());
}
@Test
public void nonConfigurationFileCreated() {
Path path = Path.of("myPath");
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.CREATE, path);
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.CREATE, nonCfgPath);
verifyNoMoreInteractions(configDispatcherMock);
}
@Test
public void nonConfigurationFileModified() {
Path path = Path.of("myPath");
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.MODIFY, path);
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.MODIFY, nonCfgPath);
verifyNoMoreInteractions(configDispatcherMock);
}
@Test
public void configurationFileRemoved() {
Path path = Path.of("myPath.cfg");
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.DELETE, path);
verify(configDispatcherMock).fileRemoved(path.toAbsolutePath().toString());
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.DELETE, cfgPath);
verify(configDispatcherMock).fileRemoved(cfgPath.toAbsolutePath().toString());
}
@Test
public void nonConfigurationFileRemoved() {
Path path = Path.of("myPath");
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.DELETE, path);
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.DELETE, nonCfgPath);
verifyNoMoreInteractions(configDispatcherMock);
}
}