Skip to content

Commit

Permalink
Generate JRE enum and dependent tests and add JAVA_24 (#3855)
Browse files Browse the repository at this point in the history
* Generate code based on YaML file
* Use Java Template Engine (JTE)
* Add JRE.JAVA_24
  • Loading branch information
marcphilipp authored Jun 12, 2024
1 parent abf5ac3 commit 4c0fa63
Show file tree
Hide file tree
Showing 30 changed files with 644 additions and 1,178 deletions.
5 changes: 5 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ repository on GitHub.
arguments for a `@ParameterizedTest`. See the
<<../user-guide/index.adoc#writing-tests-parameterized-tests-display-names, User Guide>>
for details.
* `JAVA_24` has been added to the `JRE` enum for use with JRE-based execution conditions.


[[release-notes-5.11.0-M3-junit-vintage]]
Expand Down
9 changes: 9 additions & 0 deletions gradle/base/code-generator-model/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
`kotlin-dsl`
}

group = "junitbuild.base"

repositories {
gradlePluginPortal()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package junitbuild.generator.model

data class JRE(val version: Int, val since: String?)
30 changes: 30 additions & 0 deletions gradle/base/code-generator-model/src/main/resources/jre.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
- version: 8
- version: 9
- version: 10
- version: 11
- version: 12
since: '5.4'
- version: 13
since: '5.4'
- version: 14
since: '5.5'
- version: 15
since: '5.6'
- version: 16
since: '5.7'
- version: 17
since: '5.7.1'
- version: 18
since: '5.8.1'
- version: 19
since: '5.9'
- version: 20
since: '5.9'
- version: 21
since: '5.9.2'
- version: 22
since: '5.10'
- version: 23
since: '5.11'
- version: 24
since: '5.11'
3 changes: 3 additions & 0 deletions gradle/base/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rootProject.name = "base"

include("code-generator-model")
2 changes: 2 additions & 0 deletions gradle/config/checkstyle/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
files="junit-platform-commons[\\/]src[\\/]main[\\/]java.+?[\\/]org[\\/]junit[\\/]platform[\\/]commons[\\/]util[\\/]*"/>
<suppress checks="JavadocPackage"
files="junit-platform-console[\\/]src[\\/]main[\\/]java.+?[\\/]org[\\/]junit[\\/]platform[\\/]console[\\/]*"/>
<suppress checks="JavadocPackage"
files="junit-jupiter-api[\\/]build[\\/]generated[\\/]sources[\\/]jte[\\/]main[\\/]org[\\/]junit[\\/]jupiter[\\/]api[\\/]condition[\\/]*"/>
</suppressions>
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ bnd = "7.0.0"
checkstyle = "10.17.0"
eclipse = "4.31.100"
gradleVersionsPlugin = "0.51.0"
jackson = "2.17.1"
jacoco = "0.8.7"
jmh = "1.37"
junit4 = "4.13.2"
Expand Down Expand Up @@ -42,12 +43,15 @@ gradle-versions = { module = "com.github.ben-manes:gradle-versions-plugin", vers
groovy4 = { module = "org.apache.groovy:groovy", version = "4.0.21" }
groovy2-bom = { module = "org.codehaus.groovy:groovy-bom", version = "2.5.21" }
hamcrest = { module = "org.hamcrest:hamcrest", version = "2.2" }
jackson-dataformat-yaml = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml", version.ref = "jackson" }
jackson-module-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson" }
jfrPolyfill = { module = "org.gradle.jfr.polyfill:jfr-polyfill", version = "1.0.2" }
jfrunit = { module = "org.moditect.jfrunit:jfrunit-core", version = "1.0.0.Alpha2" }
jimfs = { module = "com.google.jimfs:jimfs", version = "1.3.0" }
jmh-core = { module = "org.openjdk.jmh:jmh-core", version.ref = "jmh" }
jmh-generator-annprocess = { module = "org.openjdk.jmh:jmh-generator-annprocess", version.ref = "jmh" }
joox = { module = "org.jooq:joox", version = "2.0.1" }
jte = { module = "gg.jte:jte", version = "3.1.12" }
junit4 = { module = "junit:junit", version = { require = "[4.12,)", prefer = "4.13.2" } }
kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version = "1.8.1" }
log4j-core = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j" }
Expand Down
15 changes: 15 additions & 0 deletions gradle/plugins/code-generator/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
`kotlin-dsl`
}

repositories {
gradlePluginPortal()
}

dependencies {
implementation("junitbuild.base:code-generator-model")
implementation(projects.common)
implementation(libs.jackson.dataformat.yaml)
implementation(libs.jackson.module.kotlin)
implementation(libs.jte)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import junitbuild.generator.GenerateJreRelatedSourceCode

plugins {
java
}

val templates by sourceSets.registering
dependencies {
add(templates.get().compileOnlyConfigurationName, dependencyFromLibs("jte"))
add(templates.get().compileOnlyConfigurationName, "junitbuild.base:code-generator-model")
}

val license: License by rootProject.extra
val rootTargetDir = layout.buildDirectory.dir("generated/sources/jte")
val generateCode by tasks.registering

sourceSets.named { it != templates.name }.configureEach {

val sourceSetName = name
val sourceSetTargetDir = rootTargetDir.map { it.dir(sourceSetName) }

val task = tasks.register(getTaskName("generateJreRelated", "SourceCode"), GenerateJreRelatedSourceCode::class) {
templateDir.convention(layout.dir(templates.map {
it.resources.srcDirs.single().resolve(sourceSetName)
}))
targetDir.convention(sourceSetTargetDir)
licenseHeaderFile.convention(license.headerFile)
}

java.srcDir(files(sourceSetTargetDir).builtBy(task))

generateCode {
dependsOn(task)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package junitbuild.generator

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.kotlin.KotlinModule
import gg.jte.ContentType
import gg.jte.TemplateEngine
import gg.jte.output.FileOutput
import gg.jte.resolve.DirectoryCodeResolver
import junitbuild.generator.model.JRE
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.SkipWhenEmpty
import org.gradle.api.tasks.TaskAction

@CacheableTask
abstract class GenerateJreRelatedSourceCode : DefaultTask() {

@get:InputDirectory
@get:SkipWhenEmpty
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val templateDir: DirectoryProperty

@get:OutputDirectory
abstract val targetDir: DirectoryProperty

@get:InputFile
@get:PathSensitive(PathSensitivity.NONE)
abstract val licenseHeaderFile: RegularFileProperty

@TaskAction
fun generateSourceCode() {
val mainTargetDir = targetDir.get().asFile
mainTargetDir.deleteRecursively()

val templateDir = templateDir.get().asFile
val codeResolver = DirectoryCodeResolver(templateDir.toPath())
val templateEngine =
TemplateEngine.create(codeResolver, temporaryDir.toPath(), ContentType.Plain, javaClass.classLoader)

val templates = templateDir.walkTopDown()
.filter { it.extension == "jte" }
.map { it.relativeTo(templateDir) }
.toList()

if (templates.isNotEmpty()) {
val jres = javaClass.getResourceAsStream("/jre.yaml").use { input ->
val mapper = ObjectMapper(YAMLFactory())
mapper.registerModule(KotlinModule.Builder().build())
mapper.readValue(input, object : TypeReference<List<JRE>>() {})
}
val params = mapOf(
"jres" to jres,
"jresSortedByStringValue" to jres.sortedBy { it.version.toString() },
"licenseHeader" to licenseHeaderFile.asFile.get().readText()
)
templates.forEach {
val targetFile = mainTargetDir.toPath().resolve(it.resolveSibling(it.nameWithoutExtension).path)

FileOutput(targetFile).use { output ->
templateEngine.render(it.path, params, output)
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import org.gradle.api.Named
import org.gradle.api.Project
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.the
import org.gradle.process.CommandLineArgumentProvider
Expand All @@ -28,7 +33,7 @@ abstract class PatchModuleArgumentProvider @Inject constructor(compiledProject:
compiledProject.files(compiledProject.the<SourceSetContainer>().matching { it.name.startsWith("main") }
.map { it.output })
else
patchModuleProject.files(patchModuleProject.the<SourceSetContainer>()["main"].java.srcDirs)
patchModuleProject.files(patchModuleProject.the<SourceSetContainer>()["main"].java.sourceDirectories)
})
}

Expand Down
3 changes: 3 additions & 0 deletions gradle/plugins/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ dependencyResolutionManagement {

rootProject.name = "plugins"

includeBuild("../base")

include("build-parameters")
include("common")
include("code-generator")

enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
1 change: 1 addition & 0 deletions junit-jupiter-api/junit-jupiter-api.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("junitbuild.kotlin-library-conventions")
id("junitbuild.code-generator")
`java-test-fixtures`
}

Expand Down
Loading

0 comments on commit 4c0fa63

Please sign in to comment.