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

This commit is contained in:
Dawid Weiss 2020-01-09 19:00:32 +01:00
parent 52291c8ce8
commit 4599c51f0d
4 changed files with 90 additions and 0 deletions

View File

@ -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')

View File

@ -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
}
}
}

View File

@ -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

View File

@ -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")
));
}
}