Skip to content

Commit

Permalink
Added integration tests plugin to support java toolchains testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-serjey committed Jun 21, 2024
1 parent 0705a91 commit dc69fd2
Show file tree
Hide file tree
Showing 19 changed files with 454 additions and 1 deletion.
32 changes: 32 additions & 0 deletions docker_integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,35 @@ export common_gradle_args="--console=plain --no-daemon -Porg.gradle.java.install
--working-dir integrationTests \
$common_gradle_args \
testAll

# a set of tests with java toolchain:

#ci.yml matrix case #1 + toolchain java v17
./docker_gradlew.sh \
--java 17 --java 11 \
--gradle 7 \
--gradle-home .docker-gradle \
--working-dir integrationTests \
$common_gradle_args \
-PtoolchainJavaVersion=17 \
testAllJavaToolchain

#ci.yml matrix case #2 + toolchain java v21
./docker_gradlew.sh \
--java 21 --java 17 \
--gradle 7 \
--gradle-home .docker-gradle \
--working-dir integrationTests \
$common_gradle_args \
-PtoolchainJavaVersion=21 \
testAllJavaToolchain

#ci.yml matrix case #3 + toolchain java v21
./docker_gradlew.sh \
--java 21 --java 17 \
--gradle 8 \
--gradle-home .docker-gradle \
--working-dir integrationTests \
$common_gradle_args \
-PtoolchainJavaVersion=21 \
testAllJavaToolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.akhikhl.gretty.internal.integrationTests;

import java.util.Objects;

public class AnyJavaVersion implements Comparable<AnyJavaVersion> {
private int majorVersion;

private AnyJavaVersion(int majorVersion) {
this.majorVersion = majorVersion;
}

public int getMajorVersion() {
return majorVersion;
}

public boolean isJava9Compatible() {
return majorVersion >= 9;
}

public boolean isJava10Compatible() {
return majorVersion >= 10;
}

@Override
public int compareTo(AnyJavaVersion o) {
return Integer.compare(this.majorVersion, o.majorVersion);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AnyJavaVersion that = (AnyJavaVersion) o;
return majorVersion == that.majorVersion;
}

@Override
public int hashCode() {
return Objects.hashCode(majorVersion);
}

public static AnyJavaVersion of(Integer integer) {
return new AnyJavaVersion(Objects.requireNonNull(integer));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class BasePlugin implements Plugin<Project> {
}

protected void configureExtensions(Project project) {
// does nothing by default
if (!project.extensions.findByName('javaVersion')) {
project.extensions.add(AnyJavaVersion, 'javaVersion', JavaToolchainIntegrationTestPlugin.getToolchainJavaVersion(project))
}
}

protected void configurePublications(Project project) {
Expand Down Expand Up @@ -98,6 +100,9 @@ class BasePlugin implements Plugin<Project> {
if(!project.rootProject.tasks.findByName('testAll'))
project.rootProject.task 'testAll'

if(!project.rootProject.tasks.findByName('testAllJavaToolchain'))
project.rootProject.task 'testAllJavaToolchain'

project.tasks.withType(Test).configureEach {
if (GradleVersion.current().baseVersion.version.startsWith("7.")) {
useJUnitPlatform()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ class IntegrationTestPlugin extends BasePlugin {
protected void configureExtensions(Project project) {
super.configureExtensions(project)

/**
* Makes the project aware of java toolchain if it has -PtoolchainJavaVersion=17 parameter.
* Toolchain DSL is configured automatically for the provided version of java.
**/
project.ext.defineAsJavaToolchainAwareIntegrationTest = {
JavaToolchainIntegrationTestPlugin.applyPluginConditionally(project)
}

project.ext.defineIntegrationTest = {

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

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

integrationTestTask_
Expand All @@ -75,6 +87,10 @@ class IntegrationTestPlugin extends BasePlugin {

integrationTestAllContainersTask = project.task('integrationTestAllContainers')

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

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

Expand All @@ -92,6 +108,10 @@ class IntegrationTestPlugin extends BasePlugin {
else
testClassesDirs = project.sourceSets.integrationTest.output.classesDirs
classpath = project.sourceSets.integrationTest.runtimeClasspath

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

integrationTestAllContainersTask.dependsOn project.tasks['integrationTest_' + container]
Expand Down Expand Up @@ -169,6 +189,10 @@ class IntegrationTestPlugin extends BasePlugin {
srcDir 'src/integrationTest/resources'
}
runtimeClasspath += project.rootProject.files('config/gebConfig')

JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin ->
plugin.forceSourceSetToUseGradleJvm(project, it)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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
import org.gradle.api.tasks.compile.GroovyCompile
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.api.tasks.testing.Test
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.slf4j.Logger
import org.slf4j.LoggerFactory

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)

public static void applyPluginConditionally(Project project) {
if (project.findProperty('toolchainJavaVersion')) {
project.apply plugin: PLUGIN_ID
}
}

public 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
public void apply(Project project) {
int javaVersion = Integer.parseInt("${project.toolchainJavaVersion}")

project.java {
toolchain {
languageVersion = JavaLanguageVersion.of(javaVersion)
}
}

project.rootProject.tasks.named('testAllJavaToolchain').configure {
dependsOn project.tasks.testAll
}
}

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 void forceTaskToUseGradleJvm(Task task) {
task.project.with { proj ->
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 Test) {
task.javaLauncher.value(proj.javaToolchains.launcherFor(gradleJvmSpec))
}
}
}

public static AnyJavaVersion getToolchainJavaVersion(Project project) {
//java 8 compatible, Optional.or() available from java 9
String majorVersion = project.findProperty('toolchainJavaVersion') ?: JavaVersion.current().majorVersion

return Optional.ofNullable(majorVersion)
.map({ Integer.parseInt("$it") })
.map({ AnyJavaVersion.of(it) })
.get()
}

public static JavaVersion getGradleJavaVersion() {
return JavaVersion.current()
}

private static def getGradleJvmSpec() {
def gradleJvmVerson = Integer.valueOf(getGradleJavaVersion().getMajorVersion())
return { languageVersion = JavaLanguageVersion.of(gradleJvmVerson) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implementation-class=org.akhikhl.gretty.internal.integrationTests.JavaToolchainIntegrationTestPlugin
1 change: 1 addition & 0 deletions integrationTests/extraResourceBases/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ gretty {
extraResourceBase 'extra1'
}

defineAsJavaToolchainAwareIntegrationTest()
defineIntegrationTest()
testAll.dependsOn defineIntegrationTestAllContainers()
1 change: 1 addition & 0 deletions integrationTests/farm/MyWebApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ product {
additionalFiles = ['./farm/README.md': './README.md']
}

defineAsJavaToolchainAwareIntegrationTest()
defineIntegrationTest()

testAll.dependsOn defineFarmIntegrationTestAllContainers({
Expand Down
1 change: 1 addition & 0 deletions integrationTests/farm/MyWebService/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ apply plugin: 'war'
apply plugin: 'org.gretty'
apply plugin: 'org.gretty.internal.integrationTests.IntegrationTestPlugin'

defineAsJavaToolchainAwareIntegrationTest()
defineIntegrationTest()
30 changes: 30 additions & 0 deletions integrationTests/gradle-java-toolchain/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# gradle-java-toolchain

Simple gretty servlet application powered by gradle java toolchain.

## How to run

```bash
cd integrationTests/gradle-java-toolchain
gradle appRun
```


## How to test

```bash
cd integrationTests/gradle-java-toolchain
gradle integrationTest -PgeckoDriverPlatform=linux64 -PtoolchainJavaVersion=21
```
or
```bash
./docker_gradlew.sh --java 21 --java 11 --gradle 7 --working-dir integrationTests/gradle-java-toolchain -PtoolchainJavaVersion=21 -Pspock_version=2.3-groovy-3.0 -PgebVersion=5.1 integrationTest
```
## How to build a product


```bash
cd integrationTests/gradle-java-toolchain
gradle buildProduct
```

29 changes: 29 additions & 0 deletions integrationTests/gradle-java-toolchain/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apply plugin: 'org.gretty'
apply plugin: 'org.gretty.internal.integrationTests.IntegrationTestPlugin'

dependencies {
implementation 'org.webjars:bootstrap:3.2.0'
implementation 'org.webjars:jquery:2.1.1'
// We use Velocity for example of template processing within the webapp.
implementation 'org.apache.velocity:velocity:1.7'
}

gretty {
if (project.javaVersion.isJava9Compatible()) jvmArgs "--add-opens", "java.base/java.lang=ALL-UNNAMED"
}

defineAsJavaToolchainAwareIntegrationTest()
defineIntegrationTest()
testAll.dependsOn defineIntegrationTestAllContainers()

//typical toolchain DSL
java {
toolchain {
languageVersion = JavaLanguageVersion.of("${project.javaVersion.majorVersion}")
}
}

//toolchain aware integration test
tasks.withType(Test).configureEach {
systemProperty 'toolchainJavaVersion', "${project.javaVersion.majorVersion}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Gretty
*
* Copyright (C) 2013-2015 Andrey Hihlovskiy and contributors.
*
* See the file "LICENSE" for copying and usage permission.
* See the file "CONTRIBUTORS" for complete list of contributors.
*/
package org.akhikhl.examples.gretty.gradle.toolchain

import geb.spock.GebReportingSpec

class PageSpec extends GebReportingSpec {

private static String baseURI
private static String toolchainJavaVersion

void setupSpec() {
baseURI = System.getProperty('gretty.baseURI')
toolchainJavaVersion = Objects.requireNonNull(System.getProperty('toolchainJavaVersion'))
?.with({ it == '8' ? '1.8' : it })
}

def 'should get expected static page'() {
when:
go "${baseURI}/index.html"
then:
$('h1').text() == 'Hello, world!'
$('p', 0).text() == /This is static HTML page./
}

def 'should get expected response from servlet'() {
when:
go "${baseURI}/dynamic"
then:
$('h1').text() == 'Hello, world!'
$('p', 0).text() == /This is dynamic HTML page generated by servlet./
$('h2').text().startsWith('javaVersion=' + "$toolchainJavaVersion")
}
}
Loading

0 comments on commit dc69fd2

Please sign in to comment.