-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run integration test with a given Java version and vendor #1749
Changes from all commits
affa33c
7c0d807
6869be5
c7a46ab
f2a51aa
1da16c7
2829b9e
280d8b9
a41f5b0
05aba75
a9b341b
1ed1997
44ad4cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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") | ||
} | ||
} |
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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a Gradle plugin used to configure the compilation and integration test tasks to use a given SDK configured through Gradle properties. Instead of writing a plugin, I could have added logic directly in
Comment on lines
+17
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a way to configure the SDK used for all Java tasks (compilation, unit tests, integration tests, etc...).
This differs from what we decided earlier because if we could only configure the compiler JDK, we could run into an issue where the test tasks fail to run because it uses a JDK version anterior to the compiler JDK version. So, for simplicity, we configure all Java tasks to use a common JDK, but we can still set a specific JDK to run the integration test with:
|
||
|
||
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ plugins { | |
id 'net.ltgt.errorprone' version "${errorpronePluginVersion}" | ||
id 'maven-publish' | ||
id 'signing' | ||
id 'base' | ||
} | ||
|
||
sourceSets { | ||
|
@@ -128,15 +129,14 @@ javadoc { | |
title = "ScalarDB" | ||
} | ||
|
||
|
||
task integrationTestCassandra(type: Test) { | ||
description = 'Runs the integration tests for Cassandra.' | ||
group = 'verification' | ||
testClassesDirs = sourceSets.integrationTestCassandra.output.classesDirs | ||
classpath = sourceSets.integrationTestCassandra.runtimeClasspath | ||
outputs.upToDateWhen { false } // ensures integration tests are run every time when called | ||
options { | ||
systemProperties(System.getProperties()) | ||
systemProperties(System.getProperties().findAll{it.key.toString().startsWith("scalardb")}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When running the integration test with Java 9 but using Java 8 to run IDEA, some Java 8 environment properties were unwillingly added by IDEA that caused the test to crash, so I restricted only to passing properties starting with |
||
} | ||
} | ||
|
||
|
@@ -147,7 +147,7 @@ task integrationTestCosmos(type: Test) { | |
classpath = sourceSets.integrationTestCosmos.runtimeClasspath | ||
outputs.upToDateWhen { false } // ensures integration tests are run every time when called | ||
options { | ||
systemProperties(System.getProperties()) | ||
systemProperties(System.getProperties().findAll{it.key.toString().startsWith("scalardb")}) | ||
} | ||
maxParallelForks = 3 | ||
jvmArgs '-XX:MaxDirectMemorySize=4g', '-Xmx4g' | ||
|
@@ -160,7 +160,7 @@ task integrationTestDynamo(type: Test) { | |
classpath = sourceSets.integrationTestDynamo.runtimeClasspath | ||
outputs.upToDateWhen { false } // ensures integration tests are run every time when called | ||
options { | ||
systemProperties(System.getProperties()) | ||
systemProperties(System.getProperties().findAll{it.key.toString().startsWith("scalardb")}) | ||
} | ||
maxParallelForks = 10 | ||
} | ||
|
@@ -172,7 +172,7 @@ task integrationTestJdbc(type: Test) { | |
classpath = sourceSets.integrationTestJdbc.runtimeClasspath | ||
outputs.upToDateWhen { false } // ensures integration tests are run every time when called | ||
options { | ||
systemProperties(System.getProperties()) | ||
systemProperties(System.getProperties().findAll{it.key.toString().startsWith("scalardb")}) | ||
} | ||
} | ||
|
||
|
@@ -183,7 +183,7 @@ task integrationTestMultiStorage(type: Test) { | |
classpath = sourceSets.integrationTestMultiStorage.runtimeClasspath | ||
outputs.upToDateWhen { false } // ensures integration tests are run every time when called | ||
options { | ||
systemProperties(System.getProperties()) | ||
systemProperties(System.getProperties().findAll{it.key.toString().startsWith("scalardb")}) | ||
} | ||
} | ||
|
||
|
@@ -229,7 +229,9 @@ check.dependsOn += spotbugsMain | |
check.dependsOn += spotbugsTest | ||
check.dependsOn += spotbugsIntegrationTest | ||
|
||
archivesBaseName = "scalardb" | ||
base { | ||
archivesName = "scalardb" | ||
} | ||
|
||
// for archiving and uploading to maven central | ||
publishing { | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Upgraded Gradlew to the latest stable version 8.7 |
||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't have this anymore since the
com.scalar.db.jdk-configuration
plugin sets the JDK for all Java tasks and we need it to be configurable.