forked from akhikhl/gretty
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added integration tests plugin to support java toolchains testing
- Loading branch information
Showing
19 changed files
with
454 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...tionTest/src/main/groovy/org/akhikhl/gretty/internal/integrationTests/AnyJavaVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...vy/org/akhikhl/gretty/internal/integrationTests/JavaToolchainIntegrationTestPlugin.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...lugins/org.gretty.internal.integrationTests.JavaToolchainIntegrationTestPlugin.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
implementation-class=org.akhikhl.gretty.internal.integrationTests.JavaToolchainIntegrationTestPlugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} |
40 changes: 40 additions & 0 deletions
40
...n/src/integrationTest/groovy/org/akhikhl/examples/gretty/gradle/toolchain/PageSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
Oops, something went wrong.