diff --git a/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptFileReference.java b/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptFileReference.java index 237ba9e1794..d00ae9525d3 100644 --- a/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptFileReference.java +++ b/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptFileReference.java @@ -16,12 +16,14 @@ import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.service.StartLevelService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -114,4 +116,21 @@ public int compareTo(ScriptFileReference other) { return 0; } } + + @Override + public boolean equals(@Nullable Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ScriptFileReference that = (ScriptFileReference) o; + return scriptFileURL.equals(that.scriptFileURL); + } + + @Override + public int hashCode() { + return Objects.hash(scriptFileURL); + } } diff --git a/bundles/org.openhab.core.automation.module.script.rulesupport/src/test/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptFileWatcherTest.java b/bundles/org.openhab.core.automation.module.script.rulesupport/src/test/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptFileWatcherTest.java index 443710ff268..c7a623dc6e0 100644 --- a/bundles/org.openhab.core.automation.module.script.rulesupport/src/test/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptFileWatcherTest.java +++ b/bundles/org.openhab.core.automation.module.script.rulesupport/src/test/java/org/openhab/core/automation/module/script/rulesupport/internal/loader/ScriptFileWatcherTest.java @@ -13,6 +13,7 @@ package org.openhab.core.automation.module.script.rulesupport.internal.loader; import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; +import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @@ -322,4 +323,22 @@ public void testRegisterDependency() { // verify the dependency was tracked verify(dependencyTracker).addLibForScript(p.toFile().toURI().toString(), "test"); } + + @Test + public void testRemoveBeforeReAdd_bug2246() { + when(scriptEngineManager.isSupported("js")).thenReturn(true); + ScriptEngineContainer scriptEngineContainer = mock(ScriptEngineContainer.class); + when(scriptEngineContainer.getScriptEngine()).thenReturn(mock(ScriptEngine.class)); + when(scriptEngineManager.createScriptEngine(anyString(), anyString())).thenReturn(scriptEngineContainer); + + updateStartLevel(100); + + Path p = getFile("script.js"); + + scriptFileWatcher.processWatchEvent(null, ENTRY_CREATE, p); + scriptFileWatcher.processWatchEvent(null, ENTRY_MODIFY, p); + + verify(scriptEngineManager).removeEngine(p.toFile().toURI().toString()); + verify(scriptEngineManager, times(2)).createScriptEngine("js", p.toFile().toURI().toString()); + } }