From 5e33cfc26afdd6a8f6017834fd39ad1927286dac Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Sun, 20 Feb 2022 20:40:10 +0100 Subject: [PATCH] Fix test failures when building with Java 17 (#2788) * 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 --- .../defaultscope/ScriptScopeOSGiTest.java | 22 ++++++++++++++++++- pom.xml | 4 ++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/itests/org.openhab.core.automation.module.script.tests/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptScopeOSGiTest.java b/itests/org.openhab.core.automation.module.script.tests/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptScopeOSGiTest.java index 5a3152f6df3..b67af8c1182 100644 --- a/itests/org.openhab.core.automation.module.script.tests/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptScopeOSGiTest.java +++ b/itests/org.openhab.core.automation.module.script.tests/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptScopeOSGiTest.java @@ -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; @@ -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(); diff --git a/pom.xml b/pom.xml index 6152a13cb12..e6a5f8fee61 100644 --- a/pom.xml +++ b/pom.xml @@ -389,6 +389,10 @@ Import-Package: \\ maven-surefire-plugin 3.0.0-M5 + + --add-opens java.base/java.net=ALL-UNNAMED + --add-opens java.base/java.util=ALL-UNNAMED + 15 m