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:
@@ -53,7 +53,6 @@ public final class CreateBridgeServlet extends AbstractRedirectionServlet {
|
||||
|
||||
private static final String DEFAULT_LOCALE = "en";
|
||||
|
||||
private static final long ONLINE_WAIT_TIMEOUT_IN_MILLISECONDS = 5000;
|
||||
private static final long DISCOVERY_COMPLETION_TIMEOUT_IN_MILLISECONDS = 5000;
|
||||
private static final long CHECK_INTERVAL_IN_MILLISECONDS = 100;
|
||||
|
||||
@@ -62,6 +61,8 @@ public final class CreateBridgeServlet extends AbstractRedirectionServlet {
|
||||
private final Inbox inbox;
|
||||
private final ThingRegistry thingRegistry;
|
||||
|
||||
private long onlineWaitTimeoutInMilliseconds = 5000;
|
||||
|
||||
/**
|
||||
* Creates a new {@link CreateBridgeServlet}.
|
||||
*
|
||||
@@ -73,6 +74,10 @@ public final class CreateBridgeServlet extends AbstractRedirectionServlet {
|
||||
this.thingRegistry = thingRegistry;
|
||||
}
|
||||
|
||||
public void setOnlineWaitTimeoutInMilliseconds(long onlineWaitTimeoutInMilliseconds) {
|
||||
this.onlineWaitTimeoutInMilliseconds = onlineWaitTimeoutInMilliseconds;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRedirectionDestination(HttpServletRequest request) {
|
||||
String bridgeUidString = request.getParameter(BRIDGE_UID_PARAMETER_NAME);
|
||||
@@ -175,7 +180,7 @@ public final class CreateBridgeServlet extends AbstractRedirectionServlet {
|
||||
private void waitForBridgeToComeOnline(Thing bridge) {
|
||||
try {
|
||||
waitForConditionWithTimeout(() -> bridge.getStatus() == ThingStatus.ONLINE,
|
||||
ONLINE_WAIT_TIMEOUT_IN_MILLISECONDS);
|
||||
onlineWaitTimeoutInMilliseconds);
|
||||
waitForConditionWithTimeout(new DiscoveryResultCountDoesNotChangeCondition(),
|
||||
DISCOVERY_COMPLETION_TIMEOUT_IN_MILLISECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
@@ -75,7 +75,7 @@ public class ExponentialBackoffWithJitterTest {
|
||||
@Test
|
||||
public void whenTheNumberOfFailedAttemptsIsNegativeThenZeroIsAssumedInstead() {
|
||||
// given:
|
||||
Random random = mock(Random.class);
|
||||
Random random = mock(Random.class, withSettings().withoutAnnotations());
|
||||
when(random.nextLong()).thenReturn(RETRY_INTERVAL);
|
||||
|
||||
ExponentialBackoffWithJitter backoffStrategy = new ExponentialBackoffWithJitter(MINIMUM_WAIT_TIME,
|
||||
@@ -91,7 +91,7 @@ public class ExponentialBackoffWithJitterTest {
|
||||
@Test
|
||||
public void whenThereIsNoFailedAttemptThenTheMaximalResultIsMinimumWaitTimePlusRetryInterval() {
|
||||
// given:
|
||||
Random random = mock(Random.class);
|
||||
Random random = mock(Random.class, withSettings().withoutAnnotations());
|
||||
when(random.nextLong()).thenReturn(RETRY_INTERVAL);
|
||||
|
||||
ExponentialBackoffWithJitter backoffStrategy = new ExponentialBackoffWithJitter(MINIMUM_WAIT_TIME,
|
||||
@@ -107,7 +107,7 @@ public class ExponentialBackoffWithJitterTest {
|
||||
@Test
|
||||
public void whenThereIsOneFailedAttemptThenTheMaximalResultIsMinimumWaitTimePlusTwiceTheRetryInterval() {
|
||||
// given:
|
||||
Random random = mock(Random.class);
|
||||
Random random = mock(Random.class, withSettings().withoutAnnotations());
|
||||
when(random.nextLong()).thenReturn(RETRY_INTERVAL * 2);
|
||||
|
||||
ExponentialBackoffWithJitter backoffStrategy = new ExponentialBackoffWithJitter(MINIMUM_WAIT_TIME,
|
||||
@@ -123,7 +123,7 @@ public class ExponentialBackoffWithJitterTest {
|
||||
@Test
|
||||
public void whenThereAreTwoFailedAttemptsThenTheMaximalResultIsMinimumWaitTimePlusFourTimesTheRetryInterval() {
|
||||
// given:
|
||||
Random random = mock(Random.class);
|
||||
Random random = mock(Random.class, withSettings().withoutAnnotations());
|
||||
when(random.nextLong()).thenReturn(RETRY_INTERVAL * 4);
|
||||
|
||||
ExponentialBackoffWithJitter backoffStrategy = new ExponentialBackoffWithJitter(MINIMUM_WAIT_TIME,
|
||||
@@ -139,7 +139,7 @@ public class ExponentialBackoffWithJitterTest {
|
||||
@Test
|
||||
public void whenThereAreTwoFailedAttemptsThenTheMinimalResultIsTheMinimumWaitTime() {
|
||||
// given:
|
||||
Random random = mock(Random.class);
|
||||
Random random = mock(Random.class, withSettings().withoutAnnotations());
|
||||
when(random.nextLong()).thenReturn(0L);
|
||||
|
||||
ExponentialBackoffWithJitter backoffStrategy = new ExponentialBackoffWithJitter(MINIMUM_WAIT_TIME,
|
||||
@@ -155,7 +155,7 @@ public class ExponentialBackoffWithJitterTest {
|
||||
@Test
|
||||
public void whenTheDrawnRandomValueIsNegativeThenItIsProjectedToAPositiveValue() {
|
||||
// given:
|
||||
Random random = mock(Random.class);
|
||||
Random random = mock(Random.class, withSettings().withoutAnnotations());
|
||||
when(random.nextLong()).thenReturn(-RETRY_INTERVAL * 4 - 1);
|
||||
|
||||
ExponentialBackoffWithJitter backoffStrategy = new ExponentialBackoffWithJitter(MINIMUM_WAIT_TIME,
|
||||
@@ -171,7 +171,7 @@ public class ExponentialBackoffWithJitterTest {
|
||||
@Test
|
||||
public void whenTheResultWouldBeLargerThanTheMaximumThenItIsCappedToTheMaximum() {
|
||||
// given:
|
||||
Random random = mock(Random.class);
|
||||
Random random = mock(Random.class, withSettings().withoutAnnotations());
|
||||
when(random.nextLong()).thenReturn(MAXIMUM_WAIT_TIME - ALTERNATIVE_MINIMUM_WAIT_TIME);
|
||||
|
||||
ExponentialBackoffWithJitter backoffStrategy = new ExponentialBackoffWithJitter(ALTERNATIVE_MINIMUM_WAIT_TIME,
|
||||
@@ -187,7 +187,7 @@ public class ExponentialBackoffWithJitterTest {
|
||||
@Test
|
||||
public void whenTheResultWouldBeLargerThanTheAlternativeMaximumThenItIsCappedToTheAlternativeMaximum() {
|
||||
// given:
|
||||
Random random = mock(Random.class);
|
||||
Random random = mock(Random.class, withSettings().withoutAnnotations());
|
||||
when(random.nextLong()).thenReturn(ALTERNATIVE_MAXIMUM_WAIT_TIME - ALTERNATIVE_MINIMUM_WAIT_TIME);
|
||||
|
||||
ExponentialBackoffWithJitter backoffStrategy = new ExponentialBackoffWithJitter(ALTERNATIVE_MINIMUM_WAIT_TIME,
|
||||
|
||||
@@ -112,6 +112,14 @@
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<!-- Required for JDK 17 compatibility, see: https://github.com/swagger-api/swagger-codegen/issues/11253 -->
|
||||
<groupId>com.github.jknack</groupId>
|
||||
<artifactId>handlebars</artifactId>
|
||||
<version>4.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@@ -46,6 +46,14 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<!-- Required for JDK 17 compatibility, see: https://github.com/highsource/maven-jaxb2-plugin/issues/207 -->
|
||||
<groupId>org.glassfish.jaxb</groupId>
|
||||
<artifactId>jaxb-runtime</artifactId>
|
||||
<version>2.3.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@@ -89,7 +89,7 @@ public class ScheduleTests {
|
||||
commonSetup.start(new ResourceConfig().registerInstances(subject));
|
||||
|
||||
// Mock random -> always return int=10 or the highest possible int if bounded
|
||||
Random random = mock(Random.class);
|
||||
Random random = mock(Random.class, withSettings().withoutAnnotations());
|
||||
doReturn(10).when(random).nextInt();
|
||||
doAnswer(a -> {
|
||||
Integer bound = a.getArgument(0);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user