Fix Java 17 compilation and test issues (#12353)

* Adds --add-opens to the surefire-maven-plugin config required for deserialization using Gson/XStream
* Upgrades plugin dependencies to JDK 17 compatible versions
* Replaces some reflection that no longer works on JDK 17
* Fixes issues when mocking Random
* Run Nashorn dependant tests only on JDK < 15

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born
2022-02-23 16:13:56 +01:00
committed by GitHub
parent 223c9f929b
commit c028deefbe
9 changed files with 62 additions and 39 deletions

View File

@@ -13,6 +13,7 @@
package org.openhab.transform.javascript.internal;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.io.File;
import java.io.IOException;
@@ -40,6 +41,8 @@ import org.osgi.framework.BundleContext;
@MockitoSettings(strictness = Strictness.WARN)
public class JavaScriptTransformationServiceTest {
private static final boolean NASHORN_AVAILABLE = isNashornAvailable();
private static final String BASE_FOLDER = "target";
private static final String SRC_FOLDER = "conf";
private static final String CONFIG_FOLDER = BASE_FOLDER + File.separator + SRC_FOLDER;
@@ -54,8 +57,25 @@ public class JavaScriptTransformationServiceTest {
}
};
/**
* 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 setUp() throws IOException {
assumeTrue(NASHORN_AVAILABLE);
JavaScriptEngineManager manager = new JavaScriptEngineManager();
processor = new TestableJavaScriptTransformationService(manager);
copyDirectory(SRC_FOLDER, CONFIG_FOLDER);
@@ -63,8 +83,11 @@ public class JavaScriptTransformationServiceTest {
@AfterEach
public void tearDown() throws IOException {
try (Stream<Path> walk = Files.walk(Path.of(CONFIG_FOLDER))) {
walk.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
Path path = Path.of(CONFIG_FOLDER);
if (Files.exists(path)) {
try (Stream<Path> walk = Files.walk(path)) {
walk.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
}
}
}