Skip to content

Commit

Permalink
Fix test failures when building with Java 17 (openhab#2788)
Browse files Browse the repository at this point in the history
* Adds some --add-opens to maven-surefire-plugin for:
  * modbus transport tests (java.net)
  * tests using Gson/XStream (java.util)
* Only run ScriptScopeOSGiTest when Nashorn is available as it has been removed since JDK 15

Signed-off-by: Wouter Born <[email protected]>
GitOrigin-RevId: 5e33cfc
  • Loading branch information
wborn authored and splatch committed Jul 12, 2023
1 parent 4d5abf4 commit b107c8f
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.core.automation.module.script.internal.defaultscope;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.IOException;
import java.io.InputStreamReader;
Expand All @@ -29,21 +30,40 @@
import org.openhab.core.test.java.JavaOSGiTest;

/**
* This tests the script modules
* This tests the script modules if the Nashorn JavaScript engine is available.
*
* @author Kai Kreuzer - Initial contribution
*/
@NonNullByDefault
public class ScriptScopeOSGiTest extends JavaOSGiTest {

private static final boolean NASHORN_AVAILABLE = isNashornAvailable();

private @NonNullByDefault({}) ScriptEngine engine;

private final String path = "OH-INF/automation/jsr223/";
private final String workingFile = "scopeWorking.js";
private final String failureFile = "scopeFailure.js";

/**
* Returns if the Nashorn JavaScript engine is available based on the Java specification version property.
* Nashorn has been removed from JDK 15 and onwards.
*
* @return {@code true} if Nashorn is available, {@code false} otherwise
*/
private static boolean isNashornAvailable() {
try {
String javaVersion = System.getProperty("java.specification.version");
return javaVersion == null ? false : Long.parseLong(javaVersion) < 15;
} catch (NumberFormatException e) {
return false;
}
}

@BeforeEach
public void init() {
assumeTrue(NASHORN_AVAILABLE);

ScriptEngineManager scriptManager = getService(ScriptEngineManager.class);
ScriptEngineContainer container = scriptManager.createScriptEngine("js", "myJSEngine");
engine = container.getScriptEngine();
Expand Down

0 comments on commit b107c8f

Please sign in to comment.