Skip to content

Commit

Permalink
HHH-15146 Run tests for hibernate-jpamodelgen-jakarta
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Mar 28, 2022
1 parent 3b9c9ae commit 3fba2f2
Showing 1 changed file with 127 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ configurations {
// we do not want the much of the normal java plugin's behavior
compileJava.enabled false
processResources.enabled false
compileTestJava.enabled false
processTestResources.enabled false
jar.enabled false
javadocJar.enabled false
sourcesJar.enabled false

ext {
transformedJarName = project(':hibernate-jpamodelgen').tasks.jar.archiveFileName.get().
replaceAll( 'hibernate-jpamodelgen', 'hibernate-jpamodelgen-jakarta' )

originalTestSrcDir = "${project(':hibernate-jpamodelgen').projectDir}/src/test"
transformedTestSrcDirRelative = 'generated-src/test'
transformedTestSrcDir = "${buildDir}/${transformedTestSrcDirRelative}"
}

dependencies {
// JAXB
compile( libraries.jakarta_jaxb_api )
Expand All @@ -35,7 +42,18 @@ dependencies {
'org.slf4j:slf4j-api:1.7.26',
'org.eclipse.transformer:org.eclipse.transformer:0.2.0',
'org.eclipse.transformer:org.eclipse.transformer.cli:0.2.0'

testCompile project(':hibernate-testing-jakarta')
testCompile fileTree(dir: 'libs', include: '*.jar')
testCompile libraries.junit
testCompile libraries.jakarta_jpa
testCompile libraries.jakarta_validation
}

//
sourceSets.test {
java.srcDir "${project.transformedTestSrcDir}/java"
resources.srcDir "${project.transformedTestSrcDir}/resources"
}

jar {
Expand Down Expand Up @@ -108,6 +126,18 @@ task transformJavadocJar(type: JakartaJarTransformation) {
targetJar tasks.javadocJar.archiveFile.get().asFile
}

// jpamodelgen tests need access to test sources, so we transform test sources instead of the test JAR.
task transformTestSources(type: JakartaSourcesTransformation) {
description 'Transforms the hibernate-jpamodelgen test sources using the JakartaTransformer tool'

// Only run this if JavaEE tests compile
dependsOn project(':hibernate-jpamodelgen').tasks.compileTestJava
mustRunAfter project(':hibernate-jpamodelgen').tasks.compileTestJava

sourceDir project.originalTestSrcDir
targetDir project.transformedTestSrcDir
}

configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf {
Expand All @@ -122,6 +152,48 @@ configurations {
it.outgoing.artifact(tasks.transformJavadocJar.targetJar) {
builtBy tasks.transformJavadocJar
}
it.outgoing.artifact(tasks.transformTestSources.targetDir) {
builtBy tasks.transformTestSources
}
}
}

compileTestJava {
dependsOn tasks.transformJar
dependsOn tasks.transformTestSources

mustRunAfter tasks.transformJar
mustRunAfter tasks.transformTestSources

classpath += files(
"${buildDir}/libs/${project.transformedJarName}"
)

options.compilerArgs += [
"-proc:none"
]
}

test {
classpath += files(
"${buildDir}/libs/${project.transformedJarName}"
)

systemProperty 'file.encoding', 'utf-8'
systemProperty 'sourceBaseDir', "${project.transformedTestSrcDir}/java"

if ( gradle.ext.javaVersions.test.launcher.asInt() >= 9 ) {
// Weld needs this to generate proxies
jvmArgs( ['--add-opens', 'java.base/java.security=ALL-UNNAMED'] )
jvmArgs( ['--add-opens', 'java.base/java.lang=ALL-UNNAMED'] )
}

maxHeapSize = '3G'
// Allow to exclude specific tests
if (project.hasProperty('excludeTests')) {
filter {
excludeTestsMatching project.property('excludeTests').toString()
}
}
}

Expand Down Expand Up @@ -173,4 +245,57 @@ abstract class JakartaJarTransformation extends DefaultTask {
}
});
}
}

@CacheableTask
abstract class JakartaSourcesTransformation extends DefaultTask {
private final DirectoryProperty sourceDir;
private final DirectoryProperty targetDir;

@Inject
JakartaSourcesTransformation(ObjectFactory objectFactory) {
sourceDir = objectFactory.directoryProperty();
targetDir = objectFactory.directoryProperty();
}

@InputDirectory
@PathSensitive( PathSensitivity.RELATIVE )
DirectoryProperty getSourceDir() {
return sourceDir;
}

void sourceDir(Object directoryReference) {
sourceDir.set( project.file( directoryReference ) )
}

@OutputDirectory
DirectoryProperty getTargetDir() {
return targetDir;
}

void targetDir(Object directoryReference) {
targetDir.set( project.file( directoryReference ) )
}

@TaskAction
void transform() {
project.javaexec( new Action<JavaExecSpec>() {
@Override
void execute(JavaExecSpec javaExecSpec) {
javaExecSpec.classpath( getProject().getConfigurations().getByName( "jakartaeeTransformTool" ) );
javaExecSpec.setMain( "org.eclipse.transformer.jakarta.JakartaTransformer" );
javaExecSpec.args(
sourceDir.get().getAsFile().getAbsolutePath(),
targetDir.get().getAsFile().getAbsolutePath(),
// The transformer won't run if the target directory exist,
// except if we allow it to overwrite the target directory through this option.
'-o',
"-q",
"-tr", getProject().getRootProject().file( "rules/jakarta-renames.properties" ).getAbsolutePath(),
"-tv", getProject().getRootProject().file( "rules/jakarta-versions.properties" ).getAbsolutePath(),
"-td", getProject().getRootProject().file( "rules/jakarta-direct-modelgen.properties" ).getAbsolutePath()
);
}
});
}
}

0 comments on commit 3fba2f2

Please sign in to comment.