Skip to content

Commit

Permalink
[automation] Correctly unload script before reload. (openhab#2254)
Browse files Browse the repository at this point in the history
- Correctly unload script before reload.

Fixes openhab#2246

Signed-off-by: Jonathan Gilbert <[email protected]>
GitOrigin-RevId: 6961db7
  • Loading branch information
jpg0 authored and splatch committed Jul 11, 2023
1 parent 0244d2b commit ad82576
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit ad82576

Please sign in to comment.