Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-9122: add support for running tests against alternate jvms.

Dawid Weiss4599c51f0d32
diff --git a/build.gradle b/build.gradle
index 2f2fe07a074..17f63afd7b6 100644
--- a/build.gradle
+++ b/build.gradle
@@ -25,6 +25,7 @@ apply from: file('gradle/defaults-java.gradle')
apply from: file('gradle/testing/defaults-tests.gradle')
apply from: file('gradle/testing/randomization.gradle')
apply from: file('gradle/testing/fail-on-no-tests.gradle')
+apply from: file('gradle/testing/runtime-jvm-support.gradle')
// Maven publishing.
apply from: file('gradle/maven/defaults-maven.gradle')
diff --git a/gradle/testing/runtime-jvm-support.gradle b/gradle/testing/runtime-jvm-support.gradle
new file mode 100644
index 00000000000..beb87c274a1
--- /dev/null
+++ b/gradle/testing/runtime-jvm-support.gradle
@@ -0,0 +1,35 @@
+// This adds support for compiling and testing against a different Java runtime.
+// This is the only way to build against JVMs not yet supported by Gradle itself.
+
+import org.gradle.internal.jvm.Jvm
+
+def jvmForTests = {
+ def runtimeJavaHome = propertyOrDefault("runtime.java.home", System.getenv('RUNTIME_JAVA_HOME'))
+ if (!runtimeJavaHome) {
+ return Jvm.current()
+ } else {
+ return Jvm.forHome(file(runtimeJavaHome))
+ }
+}()
+def jvmGradle = Jvm.current()
+
+def differentTestJvm = (jvmGradle.javaHome.canonicalPath != jvmForTests.javaHome.canonicalPath)
+
+// Set up tasks to use the alternative Java.
+if (differentTestJvm) {
+ configure(rootProject) {
+ task testJvmWarning() {
+ doFirst {
+ logger.warn("This Java will be used for running tests: ${jvmForTests.javaExecutable}")
+ }
+ }
+ }
+
+ // Set up test tasks to use the alternative JVM.
+ allprojects {
+ tasks.withType(Test) {
+ dependsOn ":testJvmWarning"
+ executable = jvmForTests.javaExecutable
+ }
+ }
+}
\ No newline at end of file
diff --git a/help/tests.txt b/help/tests.txt
index 9f7a1f8c23e..dac11d08e61 100644
--- a/help/tests.txt
+++ b/help/tests.txt
@@ -110,3 +110,19 @@ to be dumped directly to the console. Run your verbose tests explicitly
specifying the project and test task or a fully qualified task path. Example:
gradlew -p lucene/core test -Ptests.verbose=true --tests "TestDemo"
+
+
+Testing against different JVMs
+------------------------------
+
+By default tests are executed with the same Java gradle is using internally.
+To run tests against a different Java version define a property called
+"runtime.java.home" or define an environment variable "RUNTIME_JAVA_HOME"
+pointing at the JDK installation folder.
+
+If property is used, it can be a system property (-D...) or a project
+property (-P...).
+
+Example:
+
+gradlew test -p lucene/test-framework --tests TestJvmInfo -Dtests.verbose=true -Druntime.java.home=/jvms/jdk14
diff --git a/lucene/test-framework/src/test/org/apache/lucene/util/TestJvmInfo.java b/lucene/test-framework/src/test/org/apache/lucene/util/TestJvmInfo.java
new file mode 100644
index 00000000000..e59937edaf1
--- /dev/null
+++ b/lucene/test-framework/src/test/org/apache/lucene/util/TestJvmInfo.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.util;
+
+import com.carrotsearch.randomizedtesting.RandomizedTest;
+import org.junit.Test;
+
+import java.util.Locale;
+
+/**
+ *
+ */
+public class TestJvmInfo extends RandomizedTest {
+ @Test
+ public void testEchoJvmInfo() {
+ System.out.println(String.format(Locale.ROOT,
+ "This test runs with Java %s (%s, %s %s).",
+ System.getProperty("java.version"),
+ System.getProperty("java.vendor"),
+ System.getProperty("java.vm.name"),
+ System.getProperty("java.vm.version")
+ ));
+ }
+}