Skip to content

Commit dc69fd2

Browse files
committed
Added integration tests plugin to support java toolchains testing
1 parent 0705a91 commit dc69fd2

File tree

19 files changed

+454
-1
lines changed

19 files changed

+454
-1
lines changed

docker_integration_tests.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,35 @@ export common_gradle_args="--console=plain --no-daemon -Porg.gradle.java.install
3737
--working-dir integrationTests \
3838
$common_gradle_args \
3939
testAll
40+
41+
# a set of tests with java toolchain:
42+
43+
#ci.yml matrix case #1 + toolchain java v17
44+
./docker_gradlew.sh \
45+
--java 17 --java 11 \
46+
--gradle 7 \
47+
--gradle-home .docker-gradle \
48+
--working-dir integrationTests \
49+
$common_gradle_args \
50+
-PtoolchainJavaVersion=17 \
51+
testAllJavaToolchain
52+
53+
#ci.yml matrix case #2 + toolchain java v21
54+
./docker_gradlew.sh \
55+
--java 21 --java 17 \
56+
--gradle 7 \
57+
--gradle-home .docker-gradle \
58+
--working-dir integrationTests \
59+
$common_gradle_args \
60+
-PtoolchainJavaVersion=21 \
61+
testAllJavaToolchain
62+
63+
#ci.yml matrix case #3 + toolchain java v21
64+
./docker_gradlew.sh \
65+
--java 21 --java 17 \
66+
--gradle 8 \
67+
--gradle-home .docker-gradle \
68+
--working-dir integrationTests \
69+
$common_gradle_args \
70+
-PtoolchainJavaVersion=21 \
71+
testAllJavaToolchain
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.akhikhl.gretty.internal.integrationTests;
2+
3+
import java.util.Objects;
4+
5+
public class AnyJavaVersion implements Comparable<AnyJavaVersion> {
6+
private int majorVersion;
7+
8+
private AnyJavaVersion(int majorVersion) {
9+
this.majorVersion = majorVersion;
10+
}
11+
12+
public int getMajorVersion() {
13+
return majorVersion;
14+
}
15+
16+
public boolean isJava9Compatible() {
17+
return majorVersion >= 9;
18+
}
19+
20+
public boolean isJava10Compatible() {
21+
return majorVersion >= 10;
22+
}
23+
24+
@Override
25+
public int compareTo(AnyJavaVersion o) {
26+
return Integer.compare(this.majorVersion, o.majorVersion);
27+
}
28+
29+
@Override
30+
public boolean equals(Object o) {
31+
if (this == o) return true;
32+
if (o == null || getClass() != o.getClass()) return false;
33+
AnyJavaVersion that = (AnyJavaVersion) o;
34+
return majorVersion == that.majorVersion;
35+
}
36+
37+
@Override
38+
public int hashCode() {
39+
return Objects.hashCode(majorVersion);
40+
}
41+
42+
public static AnyJavaVersion of(Integer integer) {
43+
return new AnyJavaVersion(Objects.requireNonNull(integer));
44+
}
45+
}

integrationTests/buildSrc/gretty-integrationTest/src/main/groovy/org/akhikhl/gretty/internal/integrationTests/BasePlugin.groovy

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ class BasePlugin implements Plugin<Project> {
2727
}
2828

2929
protected void configureExtensions(Project project) {
30-
// does nothing by default
30+
if (!project.extensions.findByName('javaVersion')) {
31+
project.extensions.add(AnyJavaVersion, 'javaVersion', JavaToolchainIntegrationTestPlugin.getToolchainJavaVersion(project))
32+
}
3133
}
3234

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

103+
if(!project.rootProject.tasks.findByName('testAllJavaToolchain'))
104+
project.rootProject.task 'testAllJavaToolchain'
105+
101106
project.tasks.withType(Test).configureEach {
102107
if (GradleVersion.current().baseVersion.version.startsWith("7.")) {
103108
useJUnitPlatform()

integrationTests/buildSrc/gretty-integrationTest/src/main/groovy/org/akhikhl/gretty/internal/integrationTests/IntegrationTestPlugin.groovy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ class IntegrationTestPlugin extends BasePlugin {
4747
protected void configureExtensions(Project project) {
4848
super.configureExtensions(project)
4949

50+
/**
51+
* Makes the project aware of java toolchain if it has -PtoolchainJavaVersion=17 parameter.
52+
* Toolchain DSL is configured automatically for the provided version of java.
53+
**/
54+
project.ext.defineAsJavaToolchainAwareIntegrationTest = {
55+
JavaToolchainIntegrationTestPlugin.applyPluginConditionally(project)
56+
}
57+
5058
project.ext.defineIntegrationTest = {
5159

5260
def integrationTestTask_ = project.tasks.findByName('integrationTest')
@@ -61,6 +69,10 @@ class IntegrationTestPlugin extends BasePlugin {
6169
else
6270
testClassesDirs = project.sourceSets.integrationTest.output.classesDirs
6371
classpath = project.sourceSets.integrationTest.runtimeClasspath
72+
73+
JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin ->
74+
plugin.forceTaskToUseGradleJvm(it)
75+
}
6476
}
6577

6678
integrationTestTask_
@@ -75,6 +87,10 @@ class IntegrationTestPlugin extends BasePlugin {
7587

7688
integrationTestAllContainersTask = project.task('integrationTestAllContainers')
7789

90+
JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin ->
91+
plugin.forceTaskToUseGradleJvm(integrationTestAllContainersTask)
92+
}
93+
7894
if (!integrationTestContainers)
7995
integrationTestContainers = ServletContainerConfig.getConfigNames().collect() // returns immutable and we want to filter later
8096

@@ -92,6 +108,10 @@ class IntegrationTestPlugin extends BasePlugin {
92108
else
93109
testClassesDirs = project.sourceSets.integrationTest.output.classesDirs
94110
classpath = project.sourceSets.integrationTest.runtimeClasspath
111+
112+
JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin ->
113+
plugin.forceTaskToUseGradleJvm(thisTask)
114+
}
95115
}
96116

97117
integrationTestAllContainersTask.dependsOn project.tasks['integrationTest_' + container]
@@ -169,6 +189,10 @@ class IntegrationTestPlugin extends BasePlugin {
169189
srcDir 'src/integrationTest/resources'
170190
}
171191
runtimeClasspath += project.rootProject.files('config/gebConfig')
192+
193+
JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin ->
194+
plugin.forceSourceSetToUseGradleJvm(project, it)
195+
}
172196
}
173197
}
174198
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.akhikhl.gretty.internal.integrationTests
2+
3+
import org.gradle.api.Action
4+
import org.gradle.api.JavaVersion
5+
import org.gradle.api.Plugin
6+
import org.gradle.api.Project
7+
import org.gradle.api.Task
8+
import org.gradle.api.tasks.SourceSet
9+
import org.gradle.api.tasks.compile.GroovyCompile
10+
import org.gradle.api.tasks.compile.JavaCompile
11+
import org.gradle.api.tasks.testing.Test
12+
import org.gradle.jvm.toolchain.JavaLanguageVersion
13+
import org.slf4j.Logger
14+
import org.slf4j.LoggerFactory
15+
16+
import java.util.function.Consumer
17+
18+
class JavaToolchainIntegrationTestPlugin implements Plugin<Project> {
19+
public static final String PLUGIN_ID = "org.gretty.internal.integrationTests.JavaToolchainIntegrationTestPlugin"
20+
private static final Logger log = LoggerFactory.getLogger(IntegrationTestPlugin)
21+
22+
public static void applyPluginConditionally(Project project) {
23+
if (project.findProperty('toolchainJavaVersion')) {
24+
project.apply plugin: PLUGIN_ID
25+
}
26+
}
27+
28+
public static void whenApplied(Project project, Consumer<JavaToolchainIntegrationTestPlugin> config) {
29+
project.plugins.withId(PLUGIN_ID, new Action<Plugin>() {
30+
@Override
31+
void execute(Plugin plugin) {
32+
config.accept((JavaToolchainIntegrationTestPlugin) plugin)
33+
}
34+
})
35+
}
36+
37+
@Override
38+
public void apply(Project project) {
39+
int javaVersion = Integer.parseInt("${project.toolchainJavaVersion}")
40+
41+
project.java {
42+
toolchain {
43+
languageVersion = JavaLanguageVersion.of(javaVersion)
44+
}
45+
}
46+
47+
project.rootProject.tasks.named('testAllJavaToolchain').configure {
48+
dependsOn project.tasks.testAll
49+
}
50+
}
51+
52+
public void forceSourceSetToUseGradleJvm(Project project, SourceSet sourceSet) {
53+
project.tasks.named(sourceSet.getCompileTaskName('java')).configure({ forceTaskToUseGradleJvm(it) })
54+
project.tasks.named(sourceSet.getCompileTaskName('groovy')).configure({ forceTaskToUseGradleJvm(it) })
55+
}
56+
57+
public void forceTaskToUseGradleJvm(Task task) {
58+
task.project.with { proj ->
59+
if (task instanceof JavaCompile) {
60+
task.javaCompiler.value(proj.javaToolchains.compilerFor(gradleJvmSpec))
61+
}
62+
63+
if (task instanceof GroovyCompile) {
64+
task.javaLauncher.value(proj.javaToolchains.launcherFor(gradleJvmSpec))
65+
}
66+
67+
if (task instanceof Test) {
68+
task.javaLauncher.value(proj.javaToolchains.launcherFor(gradleJvmSpec))
69+
}
70+
}
71+
}
72+
73+
public static AnyJavaVersion getToolchainJavaVersion(Project project) {
74+
//java 8 compatible, Optional.or() available from java 9
75+
String majorVersion = project.findProperty('toolchainJavaVersion') ?: JavaVersion.current().majorVersion
76+
77+
return Optional.ofNullable(majorVersion)
78+
.map({ Integer.parseInt("$it") })
79+
.map({ AnyJavaVersion.of(it) })
80+
.get()
81+
}
82+
83+
public static JavaVersion getGradleJavaVersion() {
84+
return JavaVersion.current()
85+
}
86+
87+
private static def getGradleJvmSpec() {
88+
def gradleJvmVerson = Integer.valueOf(getGradleJavaVersion().getMajorVersion())
89+
return { languageVersion = JavaLanguageVersion.of(gradleJvmVerson) }
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=org.akhikhl.gretty.internal.integrationTests.JavaToolchainIntegrationTestPlugin

integrationTests/extraResourceBases/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ gretty {
1313
extraResourceBase 'extra1'
1414
}
1515

16+
defineAsJavaToolchainAwareIntegrationTest()
1617
defineIntegrationTest()
1718
testAll.dependsOn defineIntegrationTestAllContainers()

integrationTests/farm/MyWebApp/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ product {
2121
additionalFiles = ['./farm/README.md': './README.md']
2222
}
2323

24+
defineAsJavaToolchainAwareIntegrationTest()
2425
defineIntegrationTest()
2526

2627
testAll.dependsOn defineFarmIntegrationTestAllContainers({

integrationTests/farm/MyWebService/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ apply plugin: 'war'
22
apply plugin: 'org.gretty'
33
apply plugin: 'org.gretty.internal.integrationTests.IntegrationTestPlugin'
44

5+
defineAsJavaToolchainAwareIntegrationTest()
56
defineIntegrationTest()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# gradle-java-toolchain
2+
3+
Simple gretty servlet application powered by gradle java toolchain.
4+
5+
## How to run
6+
7+
```bash
8+
cd integrationTests/gradle-java-toolchain
9+
gradle appRun
10+
```
11+
12+
13+
## How to test
14+
15+
```bash
16+
cd integrationTests/gradle-java-toolchain
17+
gradle integrationTest -PgeckoDriverPlatform=linux64 -PtoolchainJavaVersion=21
18+
```
19+
or
20+
```bash
21+
./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
22+
```
23+
## How to build a product
24+
25+
26+
```bash
27+
cd integrationTests/gradle-java-toolchain
28+
gradle buildProduct
29+
```
30+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apply plugin: 'org.gretty'
2+
apply plugin: 'org.gretty.internal.integrationTests.IntegrationTestPlugin'
3+
4+
dependencies {
5+
implementation 'org.webjars:bootstrap:3.2.0'
6+
implementation 'org.webjars:jquery:2.1.1'
7+
// We use Velocity for example of template processing within the webapp.
8+
implementation 'org.apache.velocity:velocity:1.7'
9+
}
10+
11+
gretty {
12+
if (project.javaVersion.isJava9Compatible()) jvmArgs "--add-opens", "java.base/java.lang=ALL-UNNAMED"
13+
}
14+
15+
defineAsJavaToolchainAwareIntegrationTest()
16+
defineIntegrationTest()
17+
testAll.dependsOn defineIntegrationTestAllContainers()
18+
19+
//typical toolchain DSL
20+
java {
21+
toolchain {
22+
languageVersion = JavaLanguageVersion.of("${project.javaVersion.majorVersion}")
23+
}
24+
}
25+
26+
//toolchain aware integration test
27+
tasks.withType(Test).configureEach {
28+
systemProperty 'toolchainJavaVersion', "${project.javaVersion.majorVersion}"
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Gretty
3+
*
4+
* Copyright (C) 2013-2015 Andrey Hihlovskiy and contributors.
5+
*
6+
* See the file "LICENSE" for copying and usage permission.
7+
* See the file "CONTRIBUTORS" for complete list of contributors.
8+
*/
9+
package org.akhikhl.examples.gretty.gradle.toolchain
10+
11+
import geb.spock.GebReportingSpec
12+
13+
class PageSpec extends GebReportingSpec {
14+
15+
private static String baseURI
16+
private static String toolchainJavaVersion
17+
18+
void setupSpec() {
19+
baseURI = System.getProperty('gretty.baseURI')
20+
toolchainJavaVersion = Objects.requireNonNull(System.getProperty('toolchainJavaVersion'))
21+
?.with({ it == '8' ? '1.8' : it })
22+
}
23+
24+
def 'should get expected static page'() {
25+
when:
26+
go "${baseURI}/index.html"
27+
then:
28+
$('h1').text() == 'Hello, world!'
29+
$('p', 0).text() == /This is static HTML page./
30+
}
31+
32+
def 'should get expected response from servlet'() {
33+
when:
34+
go "${baseURI}/dynamic"
35+
then:
36+
$('h1').text() == 'Hello, world!'
37+
$('p', 0).text() == /This is dynamic HTML page generated by servlet./
38+
$('h2').text().startsWith('javaVersion=' + "$toolchainJavaVersion")
39+
}
40+
}

0 commit comments

Comments
 (0)