Skip to content

Commit

Permalink
Limited toolchain tests to one
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-serjey committed May 23, 2024
1 parent 4fb64b3 commit c723c48
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class IntegrationTestPlugin extends BasePlugin {
protected void applyPlugins(Project project) {
super.applyPlugins(project)
project.apply plugin: 'groovy' // this is needed for spock
project.apply plugin: JavaToolchainIntegrationTestPlugin
}

@Override
Expand Down Expand Up @@ -51,6 +50,13 @@ class IntegrationTestPlugin extends BasePlugin {

project.extensions.add(JavaVersion, 'javaVersion', JavaToolchainIntegrationTestPlugin.getToolchainJavaVersion(project))

/**
* The caller project integration test would react on -PtoolchainJavaVersion=17 parameter and define appropriate toolchain DSL
**/
project.ext.defineAsJavaToolchainAwareIntegrationTest = {
JavaToolchainIntegrationTestPlugin.applyPluginConditionally(project)
}

project.ext.defineIntegrationTest = {

def integrationTestTask_ = project.tasks.findByName('integrationTest')
Expand All @@ -66,7 +72,10 @@ class IntegrationTestPlugin extends BasePlugin {
testClassesDirs = project.sourceSets.integrationTest.output.classesDirs
classpath = project.sourceSets.integrationTest.runtimeClasspath

JavaToolchainIntegrationTestPlugin.forceTaskToUseGradleJvm(it)
JavaToolchainIntegrationTestPlugin.skipIrrelevantTasks(it)
JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin ->
plugin.forceTaskToUseGradleJvm(it)
}
}

integrationTestTask_
Expand All @@ -81,6 +90,11 @@ class IntegrationTestPlugin extends BasePlugin {

integrationTestAllContainersTask = project.task('integrationTestAllContainers')

JavaToolchainIntegrationTestPlugin.skipIrrelevantTasks(integrationTestAllContainersTask)
JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin ->
plugin.forceTaskToUseGradleJvm(integrationTestAllContainersTask)
}

if (!integrationTestContainers)
integrationTestContainers = ServletContainerConfig.getConfigNames().collect() // returns immutable and we want to filter later

Expand Down Expand Up @@ -116,18 +130,24 @@ class IntegrationTestPlugin extends BasePlugin {
testClassesDirs = project.sourceSets.integrationTest.output.classesDirs
classpath = project.sourceSets.integrationTest.runtimeClasspath

JavaToolchainIntegrationTestPlugin.forceTaskToUseGradleJvm(thisTask)
JavaToolchainIntegrationTestPlugin.skipIrrelevantTasks(thisTask)
JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin ->
plugin.forceTaskToUseGradleJvm(thisTask)
}
}

integrationTestAllContainersTask.dependsOn project.tasks['integrationTest_' + container]

project.task('beforeIntegrationTest_' + container, type: AppBeforeIntegrationTestTask) {
servletContainer = container
integrationTestTask 'integrationTest_' + container

JavaToolchainIntegrationTestPlugin.skipIrrelevantTasks(it)
}

project.task('afterIntegrationTest_' + container, type: AppAfterIntegrationTestTask) {
integrationTestTask 'integrationTest_' + container
JavaToolchainIntegrationTestPlugin.skipIrrelevantTasks(it)
}
}

Expand Down Expand Up @@ -194,8 +214,10 @@ class IntegrationTestPlugin extends BasePlugin {
srcDir 'src/integrationTest/resources'
}
runtimeClasspath += project.rootProject.files('config/gebConfig')

JavaToolchainIntegrationTestPlugin.forceSourceSetToUseGradleJvm(project, it)

JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin ->
plugin.forceSourceSetToUseGradleJvm(project, it)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.akhikhl.gretty.internal.integrationTests

import org.gradle.api.Action
import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.SourceSet
Expand All @@ -11,14 +13,31 @@ import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class JavaToolchainIntegrationTestPlugin extends BasePlugin {
import java.util.function.Consumer

class JavaToolchainIntegrationTestPlugin implements Plugin<Project> {
public static final String PLUGIN_ID = "org.gretty.internal.integrationTests.JavaToolchainIntegrationTestPlugin"
private static final Logger log = LoggerFactory.getLogger(IntegrationTestPlugin)

protected void configureExtensions(Project project) {
static void applyPluginConditionally(Project project) {
if (project.findProperty('toolchainJavaVersion')) {
defineToolchainDSL(project, Integer.parseInt("${project.toolchainJavaVersion}"))
project.apply plugin: PLUGIN_ID
}
}

static void whenApplied(Project project, Consumer<JavaToolchainIntegrationTestPlugin> config) {
project.plugins.withId(PLUGIN_ID, new Action<Plugin>() {
@Override
void execute(Plugin plugin) {
config.accept((JavaToolchainIntegrationTestPlugin) plugin)
}
})
}

@Override
void apply(Project project) {
defineToolchainDSL(project, Integer.parseInt("${project.toolchainJavaVersion}"))
}

private void defineToolchainDSL(Project project, int javaVersion) {
project.java {
Expand All @@ -28,27 +47,23 @@ class JavaToolchainIntegrationTestPlugin extends BasePlugin {
}
}

public static void forceSourceSetToUseGradleJvm(Project project, SourceSet sourceSet) {
if (isPluginApplied(project)) {
project.tasks.named(sourceSet.getCompileTaskName('java')).configure({ forceTaskToUseGradleJvm(it) })
project.tasks.named(sourceSet.getCompileTaskName('groovy')).configure({ forceTaskToUseGradleJvm(it) })
}
public void forceSourceSetToUseGradleJvm(Project project, SourceSet sourceSet) {
project.tasks.named(sourceSet.getCompileTaskName('java')).configure({ forceTaskToUseGradleJvm(it) })
project.tasks.named(sourceSet.getCompileTaskName('groovy')).configure({ forceTaskToUseGradleJvm(it) })
}

public static void forceTaskToUseGradleJvm(Task task) {
public void forceTaskToUseGradleJvm(Task task) {
task.project.with { proj ->
if (isPluginApplied(proj)) {
if (task instanceof JavaCompile) {
task.javaCompiler.value(proj.javaToolchains.compilerFor(gradleJvmSpec))
}
if (task instanceof JavaCompile) {
task.javaCompiler.value(proj.javaToolchains.compilerFor(gradleJvmSpec))
}

if (task instanceof GroovyCompile) {
task.javaLauncher.value(proj.javaToolchains.launcherFor(gradleJvmSpec))
}
if (task instanceof GroovyCompile) {
task.javaLauncher.value(proj.javaToolchains.launcherFor(gradleJvmSpec))
}

if (task instanceof Test) {
task.javaLauncher.value(proj.javaToolchains.launcherFor(gradleJvmSpec))
}
if (task instanceof Test) {
task.javaLauncher.value(proj.javaToolchains.launcherFor(gradleJvmSpec))
}
}
}
Expand All @@ -64,13 +79,20 @@ class JavaToolchainIntegrationTestPlugin extends BasePlugin {
return JavaVersion.current()
}

public static void skipIrrelevantTasks(Task task) {
task.project.with {
if (task.project.findProperty('toolchainJavaVersion')) {
task.enabled = false
}

whenApplied(task.project) {
task.enabled = true
}
}
}

private static def getGradleJvmSpec() {
def gradleJvmVerson = Integer.valueOf(getGradleJavaVersion().getMajorVersion())
return { languageVersion = JavaLanguageVersion.of(gradleJvmVerson) }
}

private static boolean isPluginApplied(Project project) {
return project.plugins.hasPlugin(JavaToolchainIntegrationTestPlugin.class) ||
project.plugins.hasPlugin(JavaToolchainIntegrationTestPlugin.class.name)
}
}
2 changes: 1 addition & 1 deletion integrationTests/gradle-java-toolchain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ gretty {
if (project.javaVersion.isJava9Compatible()) jvmArgs "--add-opens", "java.base/java.lang=ALL-UNNAMED"
}

defineAsJavaToolchainAwareIntegrationTest()
defineIntegrationTest()
testAll.dependsOn defineIntegrationTestAllContainers(['jetty8', 'jetty9', 'jetty10', 'tomcat85', 'tomcat9'])

tasks.withType(Test).configureEach {
// enabled = project.findProperty('toolchainJavaVersion') as boolean
if(project.findProperty('toolchainJavaVersion')) {
systemProperty 'toolchainJavaVersion', project.findProperty('toolchainJavaVersion')
}
Expand Down

0 comments on commit c723c48

Please sign in to comment.