-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run integration test with a given Java version and vendor (#1749)
- Loading branch information
Showing
14 changed files
with
524 additions
and
152 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
plugins { | ||
id 'java-gradle-plugin' | ||
id 'com.diffplug.spotless' version '6.13.0' | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
gradlePlugin { | ||
plugins { | ||
jdkConfiguration { | ||
id = 'com.scalar.db.jdk-configuration' | ||
implementationClass = 'com.scalar.db.JdkConfigurationPlugin' | ||
} | ||
} | ||
} | ||
|
||
spotless { | ||
java { | ||
target 'src/*/java/**/*.java' | ||
importOrder() | ||
removeUnusedImports() | ||
googleJavaFormat("1.7") | ||
} | ||
} |
152 changes: 152 additions & 0 deletions
152
buildSrc/src/main/java/com/scalar/db/JdkConfigurationPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package com.scalar.db; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
import javax.annotation.Nullable; | ||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.plugins.JavaPluginExtension; | ||
import org.gradle.api.tasks.testing.Test; | ||
import org.gradle.jvm.toolchain.JavaLanguageVersion; | ||
import org.gradle.jvm.toolchain.JavaToolchainService; | ||
import org.gradle.jvm.toolchain.JvmVendorSpec; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Plugin configuring all tasks and integration test tasks to use a given JDK defined by the | ||
* following Gradle properties: | ||
* | ||
* <ul> | ||
* <li>`javaVersion` : configure all Java related tasks to use the given JDK version. The default | ||
* java version 8. | ||
* <li>`javaVendor` : configure all Java related tasks to use the given JDK vendor. The default | ||
* java vendor is Adoptium (also known as Temurin). | ||
* <li>`integrationTestJavaRuntimeVersion` : configure all test tasks name starting with | ||
* "integrationTest" to run with the given JDK version. The default Java runtime version is 8. | ||
* <li>`integrationTestJavaRuntimeVendor` : configure all test tasks name starting with | ||
* "integrationTest" to run with the given JDK vendor. The default Java runtime vendor is | ||
* Adoptium (also known as Temurin). | ||
* </ul> | ||
* | ||
* <p>Usage example using the CLI: | ||
* | ||
* <p>1. To use JDK 11 (amazon) for all Java tasks including integration tests | ||
* | ||
* <pre><code> | ||
* gradle integrationTestJdbc -PjavaVersion=11 -PjavaVendor=amazon | ||
* </code></pre> | ||
* | ||
* 2. To use JDK 11 (amazon) for all Java tasks while having integration test use JDK 17 (microsoft) | ||
* | ||
* <pre><code> | ||
* gradle integrationTestJdbc -PjavaVersion=11 -PjavaVendor=amazon -PintegrationTestJavaRuntimeVersion=17 -PintegrationTestJavaRuntimeVendor=microsoft | ||
* </code></pre> | ||
*/ | ||
public class JdkConfigurationPlugin implements Plugin<Project> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(JdkConfigurationPlugin.class); | ||
// JDK 8 (temurin) is used as default for all tasks (compilation, tests, etc.) | ||
private static final JavaLanguageVersion DEFAULT_JAVA_VERSION = JavaLanguageVersion.of(8); | ||
private static final JvmVendorSpec DEFAULT_JAVA_VENDOR = JvmVendorSpec.ADOPTIUM; | ||
|
||
private static final String JAVA_VERSION_PROP = "javaVersion"; | ||
private static final String JAVA_VENDOR_PROP = "javaVendor"; | ||
private static final String INTEGRATION_TEST_JAVA_RUNTIME_VERSION_PROP = | ||
"integrationTestJavaRuntimeVersion"; | ||
private static final String INTEGRATION_TEST_JAVA_RUNTIME_VENDOR_PROP = | ||
"integrationTestJavaRuntimeVendor"; | ||
|
||
private JavaLanguageVersion javaVersion = DEFAULT_JAVA_VERSION; | ||
private JvmVendorSpec javaVendor = DEFAULT_JAVA_VENDOR; | ||
@Nullable private JavaLanguageVersion integrationTestJavaVersion; | ||
@Nullable private JvmVendorSpec integrationTestJavaVendor; | ||
|
||
@Override | ||
public void apply(@NotNull Project project) { | ||
parseIntegrationTestInputProperties(project); | ||
configureJdkForAllJavaTasks(project); | ||
configureJdkForIntegrationTestTasks(project); | ||
} | ||
|
||
private void configureJdkForAllJavaTasks(Project project) { | ||
JavaPluginExtension javaPlugin = project.getExtensions().getByType(JavaPluginExtension.class); | ||
javaPlugin.getToolchain().getLanguageVersion().set(javaVersion); | ||
javaPlugin.getToolchain().getVendor().set(javaVendor); | ||
logger.debug("Configure JDK {} ({}) for Java tasks", javaVersion, javaVendor); | ||
} | ||
|
||
private void configureJdkForIntegrationTestTasks(Project project) { | ||
if (integrationTestJavaVersion == null) { | ||
return; | ||
} | ||
project | ||
.getTasks() | ||
.withType(Test.class) | ||
.matching(testTask -> testTask.getName().startsWith("integrationTest")) | ||
.configureEach( | ||
integrationTestTask -> | ||
integrationTestTask | ||
.getJavaLauncher() | ||
.set( | ||
getJavaToolchainService(project) | ||
.launcherFor( | ||
config -> { | ||
config.getLanguageVersion().set(integrationTestJavaVersion); | ||
logger.debug( | ||
"Configure task '{}' to use JDK version {}", | ||
integrationTestTask.getName(), | ||
integrationTestJavaVersion); | ||
if (integrationTestJavaVendor != null) { | ||
config.getVendor().set(integrationTestJavaVendor); | ||
logger.debug( | ||
"Configure task '{}' to use {} JDK vendor", | ||
integrationTestTask.getName(), | ||
integrationTestJavaVendor); | ||
} | ||
}))); | ||
} | ||
|
||
private void parseIntegrationTestInputProperties(Project project) { | ||
parseVersionInputProperty(project, JAVA_VERSION_PROP, (version) -> javaVersion = version); | ||
parseVendorInputProperty(project, JAVA_VENDOR_PROP, (vendor) -> javaVendor = vendor); | ||
parseVersionInputProperty( | ||
project, | ||
INTEGRATION_TEST_JAVA_RUNTIME_VERSION_PROP, | ||
(version) -> integrationTestJavaVersion = version); | ||
parseVendorInputProperty( | ||
project, | ||
INTEGRATION_TEST_JAVA_RUNTIME_VENDOR_PROP, | ||
(vendor) -> integrationTestJavaVendor = vendor); | ||
} | ||
|
||
private void parseVersionInputProperty( | ||
Project project, String property, Consumer<JavaLanguageVersion> attributeSetter) { | ||
if (project.hasProperty(property)) { | ||
attributeSetter.accept(JavaLanguageVersion.of(Objects.toString(project.property(property)))); | ||
} | ||
} | ||
|
||
private void parseVendorInputProperty( | ||
Project project, String property, Consumer<JvmVendorSpec> attributeSetter) { | ||
if (!project.getRootProject().hasProperty(property)) { | ||
return; | ||
} | ||
String propertyValue = Objects.toString(project.property(property)); | ||
switch (propertyValue) { | ||
case "corretto": | ||
attributeSetter.accept(JvmVendorSpec.AMAZON); | ||
break; | ||
case "temurin": | ||
attributeSetter.accept(JvmVendorSpec.ADOPTIUM); | ||
break; | ||
default: | ||
attributeSetter.accept(JvmVendorSpec.matching(propertyValue)); | ||
} | ||
} | ||
|
||
private @NotNull JavaToolchainService getJavaToolchainService(Project project) { | ||
return project.getExtensions().getByType(JavaToolchainService.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.