From 92df9a1ea3a2bca913cd6e3d90574b522d544fa3 Mon Sep 17 00:00:00 2001 From: Osip Fatkullin Date: Fri, 22 Nov 2024 16:02:30 +0100 Subject: [PATCH] Use test.jdk property to determine JDK to be used for tests --- buildSrc/src/main/kotlin/JvmConfig.kt | 19 ++++++++++--------- .../src/main/kotlin/KtorBuildProperties.kt | 19 ++++++++++++++++++- ...-resolution-management.settings.gradle.kts | 2 +- .../ktor-client-tests/build.gradle.kts | 2 +- settings.gradle.kts | 14 ++++++-------- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/buildSrc/src/main/kotlin/JvmConfig.kt b/buildSrc/src/main/kotlin/JvmConfig.kt index ca3b3d1f14..351345000f 100644 --- a/buildSrc/src/main/kotlin/JvmConfig.kt +++ b/buildSrc/src/main/kotlin/JvmConfig.kt @@ -50,11 +50,12 @@ fun Project.configureJvm() { } } + val testJdk = project.testJdk val jvmTest = tasks.named("jvmTest") { maxHeapSize = "2g" exclude("**/*StressTest*") useJUnitPlatform() - configureJavaToolchain(compileJdk) + configureJavaToolchain(compileJdk, testJdk) } tasks.register("stressTest") { @@ -67,7 +68,7 @@ fun Project.configureJvm() { systemProperty("enable.stress.tests", "true") include("**/*StressTest*") useJUnitPlatform() - configureJavaToolchain(compileJdk) + configureJavaToolchain(compileJdk, testJdk) } val configuredVersion: String by rootProject.extra @@ -83,14 +84,14 @@ fun Project.configureJvm() { } } -/** - * On local machine use for tests the JDK used for compilation. - * On CI use the default JDK. - */ -private fun Test.configureJavaToolchain(compileJdk: Int) { - val testJdk = if (CI) currentJdk else compileJdk - val javaToolchains = project.the() +/** Configure tests against different JDK versions. */ +private fun Test.configureJavaToolchain(compileJdk: Int, testJdk: Int) { + if (testJdk < compileJdk) { + enabled = false + return + } + val javaToolchains = project.the() javaLauncher = javaToolchains.launcherFor { languageVersion = JavaLanguageVersion.of(testJdk) } diff --git a/buildSrc/src/main/kotlin/KtorBuildProperties.kt b/buildSrc/src/main/kotlin/KtorBuildProperties.kt index 38768aed4d..8450b18ace 100644 --- a/buildSrc/src/main/kotlin/KtorBuildProperties.kt +++ b/buildSrc/src/main/kotlin/KtorBuildProperties.kt @@ -16,7 +16,24 @@ val HOST_NAME = when { else -> error("Unknown os name `$OS_NAME`") } -val currentJdk = JavaVersion.current().majorVersion.toInt() +private var _testJdk = 0 + +/** + * Retrieves the JDK version for running tests. + * + * Takes the version from property "test.jdk" or uses Gradle JDK by default. + * For example, to run tests against JDK 8, run test task with flag "-Ptest.jdk=8" + * or put this property to `gradle.properties`. + */ +val Project.testJdk: Int + get() { + if (_testJdk == 0) { + _testJdk = rootProject.properties["test.jdk"]?.toString()?.toInt() + ?: JavaVersion.current().majorVersion.toInt() + logger.info("Running tests against JDK $_testJdk") + } + return _testJdk + } val Project.requiredJdkVersion: Int get() = when { diff --git a/gradle-settings-conventions/src/main/kotlin/conventions-dependency-resolution-management.settings.gradle.kts b/gradle-settings-conventions/src/main/kotlin/conventions-dependency-resolution-management.settings.gradle.kts index 363b6addf9..27985787df 100644 --- a/gradle-settings-conventions/src/main/kotlin/conventions-dependency-resolution-management.settings.gradle.kts +++ b/gradle-settings-conventions/src/main/kotlin/conventions-dependency-resolution-management.settings.gradle.kts @@ -88,7 +88,7 @@ dependencyResolutionManagement { } fun VersionCatalogBuilder.downgradeTestDependencies() { - val testJdk = JavaVersion.current().majorVersion.toInt() + val testJdk = providers.gradleProperty("test.jdk").orNull?.toInt() ?: return if (testJdk < 11) version("logback", "1.3.14") // Java 8 support dropped in Logback 1.4.x } diff --git a/ktor-client/ktor-client-tests/build.gradle.kts b/ktor-client/ktor-client-tests/build.gradle.kts index 811f2d63e2..cf6c91fdbd 100644 --- a/ktor-client/ktor-client-tests/build.gradle.kts +++ b/ktor-client/ktor-client-tests/build.gradle.kts @@ -58,7 +58,7 @@ kotlin.sourceSets { api(project(":ktor-client:ktor-client-apache5")) runtimeOnly(project(":ktor-client:ktor-client-android")) runtimeOnly(project(":ktor-client:ktor-client-okhttp")) - if (currentJdk >= 11) { + if (testJdk >= 11) { runtimeOnly(project(":ktor-client:ktor-client-java")) } implementation(project(":ktor-client:ktor-client-plugins:ktor-client-logging")) diff --git a/settings.gradle.kts b/settings.gradle.kts index b80d5536b7..eb60c53a47 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -24,8 +24,12 @@ include(":ktor-server:ktor-server-test-base") include(":ktor-server:ktor-server-test-suites") include(":ktor-server:ktor-server-jetty") include(":ktor-server:ktor-server-jetty:ktor-server-jetty-test-http2") +include(":ktor-server:ktor-server-jetty-jakarta") +include(":ktor-server:ktor-server-jetty-jakarta:ktor-server-jetty-test-http2-jakarta") include(":ktor-server:ktor-server-servlet") +include(":ktor-server:ktor-server-servlet-jakarta") include(":ktor-server:ktor-server-tomcat") +include(":ktor-server:ktor-server-tomcat-jakarta") include(":ktor-server:ktor-server-netty") include(":ktor-server:ktor-server-cio") include(":ktor-client") @@ -40,15 +44,9 @@ include(":ktor-client:ktor-client-ios") include(":ktor-client:ktor-client-darwin") include(":ktor-client:ktor-client-darwin-legacy") include(":ktor-client:ktor-client-winhttp") -if (JavaVersion.current() >= JavaVersion.VERSION_11) { - include(":ktor-client:ktor-client-java") - include(":ktor-client:ktor-client-jetty-jakarta") - include(":ktor-server:ktor-server-servlet-jakarta") - include(":ktor-server:ktor-server-jetty-jakarta") - include(":ktor-server:ktor-server-jetty-jakarta:ktor-server-jetty-test-http2-jakarta") - include(":ktor-server:ktor-server-tomcat-jakarta") -} +include(":ktor-client:ktor-client-java") include(":ktor-client:ktor-client-jetty") +include(":ktor-client:ktor-client-jetty-jakarta") include(":ktor-client:ktor-client-js") include(":ktor-client:ktor-client-mock") include(":ktor-client:ktor-client-okhttp")