Skip to content

Commit

Permalink
Upgrade Gradle, build against Java 18/19, improve modular tests (juni…
Browse files Browse the repository at this point in the history
…t-pioneer#613, junit-pioneer#659 / junit-pioneer#651)

(Yes, this change does too much, but it all hangs together.)

Updates the Gradle wrapper version from 7.4 to 7.5 because that comes
with support for Java 18, which is also added to the build pipeline.
The experimental Java version is now (for another few days), Java 19.

The modular build is properly configured to really test from the
module path.

Furthermore, replaces the archived/deprecated GitHub action
sormuras/download-jdk with oracle-actions/setup-java.

Closes: junit-pioneer#613, junit-pioneer#659
PR: junit-pioneer#651
  • Loading branch information
beatngu13 authored and Bukama committed Sep 20, 2022
1 parent 7464249 commit 3997daa
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 53 deletions.
22 changes: 10 additions & 12 deletions .github/workflows/build.yml
@@ -1,7 +1,7 @@
name: Main build

env:
EXPERIMENTAL_JAVA: 18
EXPERIMENTAL_JAVA: 19

on:
# We want to trigger our builds all the time for the default branch
Expand Down Expand Up @@ -117,7 +117,7 @@ jobs:
timeout-minutes: 15
strategy:
matrix:
java: [ 11, 17 ]
java: [ 11, 17, 18 ]
junit-version: [ '5.9.0' ]
modular: [true, false]
os: [ubuntu, macos, windows]
Expand Down Expand Up @@ -151,23 +151,21 @@ jobs:
modular: [true, false]
os: [ubuntu, macos, windows]
name: Experimental build with newest JDK early-access build and Gradle release candidate
# Gradle doesn't work with JDK EA builds, so we start it with a supported Java version,
# but execute the build on the experimental version
# Gradle doesn't work with JDK EA builds, so we launch it with a supported Java version,
# but set the Gradle toolchain to use the experimental version.
steps:
- name: Check out repo
uses: actions/checkout@v2
- uses: sormuras/download-jdk@v1
- name: Set up experimental Java
uses: oracle-actions/setup-java@v1
with:
feature: ${{ env.EXPERIMENTAL_JAVA }}
- uses: actions/setup-java@v2
with:
java-version: ${{ env.JDK_VERSION }}
distribution: jdkfile
jdkFile: ${{ env.JDK_FILE }}
website: jdk.java.net
release: ${{ env.EXPERIMENTAL_JAVA }}
- name: Prepare JDK_EXPERIMENTAL env var
shell: bash
run: echo "JDK_EXPERIMENTAL=$JAVA_HOME" >> $GITHUB_ENV
- uses: actions/setup-java@v2
- name: Set up supported Java
uses: actions/setup-java@v2
with:
java-version: 17
distribution: temurin
Expand Down
101 changes: 81 additions & 20 deletions build.gradle.kts
Expand Up @@ -7,6 +7,7 @@ plugins {
id("com.diffplug.spotless") version "6.4.2"
id("at.zierler.yamlvalidator") version "1.5.0"
id("org.sonarqube") version "3.3"
id("org.moditect.gradleplugin") version "1.0.0-rc3"
id("org.shipkit.shipkit-changelog") version "1.1.15"
id("org.shipkit.shipkit-github-release") version "1.1.15"
id("com.github.ben-manes.versions") version "0.42.0"
Expand All @@ -22,18 +23,23 @@ plugins.withType<JavaPlugin>().configureEach {
group = "org.junit-pioneer"
description = "JUnit 5 Extension Pack"

val modularBuild : String by project
val experimentalJavaVersion : String? by project
val experimentalBuild: Boolean = experimentalJavaVersion?.isNotEmpty() ?: false

val targetJavaVersion = JavaVersion.VERSION_11
val targetJavaVersion = JavaVersion.VERSION_1_8

java {
if (experimentalBuild) {
toolchain {
languageVersion.set(JavaLanguageVersion.of(experimentalJavaVersion!!))
}
} else {
sourceCompatibility = targetJavaVersion
sourceCompatibility = if (modularBuild.toBoolean()) {
JavaVersion.VERSION_11
} else {
targetJavaVersion
}
}
withJavadocJar()
withSourcesJar()
Expand Down Expand Up @@ -107,6 +113,16 @@ sonarqube {
}
}

moditect {
addMainModuleInfo {
version = project.version
overwriteExistingFiles.set(true)
module {
moduleInfoFile = rootProject.file("src/main/module/module-info.java")
}
}
}

publishing {
publications {
create<MavenPublication>("maven") {
Expand Down Expand Up @@ -178,6 +194,14 @@ nexusPublishing {
tasks {

sourceSets {
main {
if (modularBuild.toBoolean())
java.srcDir("src/main/module")
}
test {
if (modularBuild.toBoolean())
java.srcDir("src/test/module")
}
create("demo") {
java {
srcDir("src/demo/java")
Expand All @@ -199,14 +223,40 @@ tasks {
compileJava {
options.encoding = "UTF-8"
options.compilerArgs.add("-Werror")
// do not break the build on "exports" warnings - see CONTRIBUTING.md for details
// Do not break the build on "exports" warnings (see CONTRIBUTING.md for details)
options.compilerArgs.add("-Xlint:all,-exports")
}

// Prepares test-related JVM args
val moduleName = "org.junitpioneer"
val targetModule = if (modularBuild.toBoolean()) moduleName else "ALL-UNNAMED"
// See https://docs.gradle.org/current/userguide/java_testing.html#sec:java_testing_modular_patching
val patchModuleArg = "--patch-module=$moduleName=${compileJava.get().destinationDirectory.asFile.get().path}"
val testJvmArgs = listOf(
// Ignore these options on Java 8
"-XX:+IgnoreUnrecognizedVMOptions",
// EnvironmentVariableUtils: make java.util.Map accessible
"--add-opens=java.base/java.util=$targetModule",
// EnvironmentVariableUtils: make java.lang.System accessible
"--add-opens=java.base/java.lang=$targetModule",
patchModuleArg
)

compileTestJava {
options.encoding = "UTF-8"
options.compilerArgs.add("-Werror")
options.compilerArgs.add("-Xlint:all")
if (modularBuild.toBoolean()) {
options.compilerArgs.add(patchModuleArg)
}
var xlintArg = "-Xlint:all"
if (modularBuild.toBoolean()) {
xlintArg += ",-exports,-requires-automatic"
// missing-explicit-ctor was added in Java 16. This causes errors on test classes, which don't have one.
if (JavaVersion.current() >= JavaVersion.VERSION_16) {
xlintArg += ",-missing-explicit-ctor"
}
}
options.compilerArgs.add(xlintArg)
}

test {
Expand All @@ -221,19 +271,20 @@ tasks {
includeTestsMatching("*Tests")
}
systemProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager")
// `EnvironmentVariableExtension` uses reflection to change environment variables;
// this prevents the corresponding warning (and keeps working on Java 8)
// IF YOU ADD MORE OPTIONS; CONSIDER REPLACING `-XX:+IgnoreUnrecognizedVMOptions WITH A CONDITIONAL
jvmArgs(
"-XX:+IgnoreUnrecognizedVMOptions",
"--add-opens=java.base/java.util=ALL-UNNAMED")
// java.security.manager was added in Java 12 (see
// https://www.oracle.com/java/technologies/javase/12-relnote-issues.html#JDK-8191053). We have to explicitly
// set it to "allow" for EnvironmentVariableUtilsTests$With_SecurityManager.
if (JavaVersion.current() >= JavaVersion.VERSION_12)
systemProperty("java.security.manager", "allow")
jvmArgs(testJvmArgs)
}

testing {
suites {
val test by getting(JvmTestSuite::class) {
useJUnitJupiter()
}

val demoTests by registering(JvmTestSuite::class) {
dependencies {
implementation(project)
Expand All @@ -242,23 +293,33 @@ tasks {
}

sources {
java { srcDir("src/demo/java") }
resources { srcDir("src/demo/resources") }
java {
srcDir("src/demo/java")
}
resources {
srcDir("src/demo/resources")
}
}
targets { all { testTask.configure {
shouldRunAfter(test)
filter {
includeTestsMatching("*Demo")

targets {
all {
testTask.configure {
shouldRunAfter(test)
filter {
includeTestsMatching("*Demo")
}
jvmArgs(testJvmArgs)
}
}
} } }
}
}
}
}

javadoc {
javadocTool.set(project.javaToolchains.javadocToolFor {
// Create Javadoc with newer JDK to get the latest features, e.g. search bar
languageVersion.set(JavaLanguageVersion.of(17))
// Create Javadoc with at least Java 17 to get the latest features, e.g. search bar
languageVersion.set(JavaLanguageVersion.of(maxOf(17, targetJavaVersion.majorVersion.toInt())))
})

options {
Expand All @@ -271,7 +332,7 @@ tasks {
// Set javadoc `--release` flag (affects which warnings and errors are reported)
// (Note: Gradle adds one leading '-' to the option on its own)
// Have to use at least Java 9 to support modular build
addStringOption("-release", targetJavaVersion.majorVersion.toInt().toString())
addStringOption("-release", maxOf(11, targetJavaVersion.majorVersion.toInt()).toString())

// Enable doclint, but ignore warnings for missing tags, see
// https://docs.oracle.com/en/java/javase/17/docs/specs/man/javadoc.html#additional-options-provided-by-the-standard-doclet
Expand Down
9 changes: 8 additions & 1 deletion docs/environment-variables.adoc
Expand Up @@ -66,8 +66,15 @@ module java.base does not "opens java.lang" to unnamed module [...]
----

The best way to prevent these warnings/errors, is to change the code under test, so this extension is no longer needed.
The next best thing is to allow access to that specific package with `--add-opens=java.base/java.util=ALL-UNNAMED` (if you place JUnit Pioneer on the class path) or `--add-opens=java.base/java.util=org.junitpioneer` (if you place it on the module path).
The next best thing is to allow access to that specific package:

[source]
----
--add-opens java.base/java.util=$TARGET_MODULE
--add-opens java.base/java.lang=$TARGET_MODULE
----

Where `$TARGET_MODULE` equals `ALL-UNNAMED` if you place JUnit Pioneer on the class path, or `org.junitpioneer` if you place JUnit Pioneer on the module path.
These command line options need to be added to the JVM that executes the tests:

* https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html[Gradle]
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
6 changes: 6 additions & 0 deletions gradlew
Expand Up @@ -205,6 +205,12 @@ set -- \
org.gradle.wrapper.GradleWrapperMain \
"$@"

# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi

# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
Expand Down
14 changes: 8 additions & 6 deletions gradlew.bat
Expand Up @@ -14,7 +14,7 @@
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
Expand All @@ -25,7 +25,7 @@
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
if "%DIRNAME%"=="" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

Expand All @@ -40,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Expand Down Expand Up @@ -75,13 +75,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/module-info.java
Expand Up @@ -22,11 +22,14 @@
exports org.junitpioneer.jupiter;
exports org.junitpioneer.jupiter.cartesian;
exports org.junitpioneer.jupiter.params;
exports org.junitpioneer.jupiter.json;

opens org.junitpioneer.vintage to org.junit.platform.commons;
opens org.junitpioneer.jupiter to org.junit.platform.commons;
opens org.junitpioneer.jupiter.cartesian to org.junit.platform.commons;
opens org.junitpioneer.jupiter.issue to org.junit.platform.commons;
opens org.junitpioneer.jupiter.params to org.junit.platform.commons;
opens org.junitpioneer.jupiter.json to org.junit.platform.commons, com.fasterxml.jackson.databind;

provides org.junit.platform.launcher.TestExecutionListener
with org.junitpioneer.jupiter.issue.IssueExtensionExecutionListener;
Expand Down
Expand Up @@ -35,9 +35,9 @@
* uses reflection to change them. This requires that the {@link SecurityManager}
* allows modifications and can potentially break on different operating systems and
* Java versions. Be aware that this is a fragile solution and consider finding a
* better one for your specific situation. If you're running on Java 9 or later, you
* may have to add {@code --add-opens=java.base/java.util=ALL-UNNAMED} to your test
* execution to prevent warnings or even errors.</p>
* better one for your specific situation. If you're running on Java 9 or later and
* are encountering warnings or errors, check
* <a href="https://junit-pioneer.org/docs/environment-variables/#warnings-for-reflective-access">the documentation</a>.</p>
*
* <p>During
* <a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution" target="_top">parallel test execution</a>,
Expand Down
Expand Up @@ -61,6 +61,9 @@ private static Locale createFromLanguageTag(DefaultLocale annotation) {
return Locale.forLanguageTag(annotation.value());
}

// On Java 19+, `Locale` constructors are deprecated.
// We ignore them until they're removed in #658
@SuppressWarnings("deprecation")
private static Locale createFromParts(DefaultLocale annotation) {
String language = annotation.language();
String country = annotation.country();
Expand Down

0 comments on commit 3997daa

Please sign in to comment.