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]>
  • Loading branch information
wborn authored Feb 20, 2022
1 parent 355c134 commit 5e33cfc
Show file tree
Hide file tree
Showing 2 changed files with 25 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
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ Import-Package: \\
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<argLine>
--add-opens java.base/java.net=ALL-UNNAMED
--add-opens java.base/java.util=ALL-UNNAMED
</argLine>
<systemPropertyVariables>
<junit.jupiter.execution.timeout.default>15 m</junit.jupiter.execution.timeout.default>
</systemPropertyVariables>
Expand Down

0 comments on commit 5e33cfc

Please sign in to comment.