From 7c453fb6b658ee77b598f103863a8a2234518d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20=C3=89pardaud?= Date: Wed, 28 Feb 2024 15:29:25 +0100 Subject: [PATCH] Start of tests --- .../hibernate-jpamodelgen.gradle | 65 ++++++++++++++++ .../test/ormPanache/PanacheBook.java | 27 +++++++ .../ormPanache/PanacheBookRepository.java | 18 +++++ .../ormPanache/QuarkusOrmPanacheTest.java | 75 +++++++++++++++++++ 4 files changed, 185 insertions(+) create mode 100644 tooling/metamodel-generator/src/quarkusOrmPanache/java/org/hibernate/jpamodelgen/test/ormPanache/PanacheBook.java create mode 100644 tooling/metamodel-generator/src/quarkusOrmPanache/java/org/hibernate/jpamodelgen/test/ormPanache/PanacheBookRepository.java create mode 100644 tooling/metamodel-generator/src/quarkusOrmPanache/java/org/hibernate/jpamodelgen/test/ormPanache/QuarkusOrmPanacheTest.java diff --git a/tooling/metamodel-generator/hibernate-jpamodelgen.gradle b/tooling/metamodel-generator/hibernate-jpamodelgen.gradle index 90bb81ad4d..1c15bcb72c 100644 --- a/tooling/metamodel-generator/hibernate-jpamodelgen.gradle +++ b/tooling/metamodel-generator/hibernate-jpamodelgen.gradle @@ -18,6 +18,28 @@ ext { xsdDir = file( "${projectDir}/src/main/xsd" ) } +sourceSets { + quarkusOrmPanache { + compileClasspath += sourceSets.main.output + compileClasspath += sourceSets.test.output + runtimeClasspath += sourceSets.main.output + runtimeClasspath += sourceSets.test.output + } + quarkusHrPanache { + compileClasspath += sourceSets.main.output + compileClasspath += sourceSets.test.output + runtimeClasspath += sourceSets.main.output + runtimeClasspath += sourceSets.test.output + } +} + +configurations { + quarkusOrmPanacheImplementation.extendsFrom testImplementation + quarkusOrmPanacheRuntimeOnly.extendsFrom runtimeOnly + quarkusHrPanacheImplementation.extendsFrom testImplementation + quarkusHrPanacheRuntimeOnly.extendsFrom runtimeOnly +} + dependencies { // api - ewww... but Maven needs them this way api project( ':hibernate-core' ) @@ -31,8 +53,51 @@ dependencies { xjc jakartaLibs.xjc xjc jakartaLibs.jaxb xjc rootProject.fileTree(dir: 'patched-libs/jaxb2-basics', include: '*.jar') + + quarkusOrmPanacheImplementation "io.quarkus:quarkus-hibernate-orm-panache:3.6.2" + quarkusOrmPanacheImplementation project( ":hibernate-testing" ) + quarkusOrmPanacheImplementation 'org.checkerframework:checker-qual:3.4.0' + + quarkusHrPanacheImplementation "io.quarkus:quarkus-hibernate-hr-panache:3.6.2" + quarkusHrPanacheImplementation project( ":hibernate-testing" ) + quarkusHrPanacheImplementation 'org.checkerframework:checker-qual:3.4.0' } +def quarkusOrmPanacheTestTask = tasks.register( 'quarkusOrmPanacheTest', Test ) { + description = 'Runs the Quarkus ORM Panache tests.' + group = 'verification' + + testClassesDirs = sourceSets.quarkusOrmPanache.output.classesDirs + classpath = sourceSets.quarkusOrmPanache.runtimeClasspath + shouldRunAfter test + dependsOn test + + useJUnitPlatform() + + testLogging { + events "passed" + } +} + +def quarkusHrPanacheTestTask = tasks.register( 'quarkusHrPanacheTest', Test ) { + description = 'Runs the Quarkus HR Panache tests.' + group = 'verification' + + testClassesDirs = sourceSets.quarkusHrPanache.output.classesDirs + classpath = sourceSets.quarkusHrPanache.runtimeClasspath + shouldRunAfter test + dependsOn test + + useJUnitPlatform() + + testLogging { + events "passed" + } +} + +check.dependsOn quarkusHrPanacheTestTask +check.dependsOn quarkusOrmPanacheTestTask + sourceSets.main { java.srcDir xjcTargetDir resources.srcDir xsdDir diff --git a/tooling/metamodel-generator/src/quarkusOrmPanache/java/org/hibernate/jpamodelgen/test/ormPanache/PanacheBook.java b/tooling/metamodel-generator/src/quarkusOrmPanache/java/org/hibernate/jpamodelgen/test/ormPanache/PanacheBook.java new file mode 100644 index 0000000000..fa24dc3e7e --- /dev/null +++ b/tooling/metamodel-generator/src/quarkusOrmPanache/java/org/hibernate/jpamodelgen/test/ormPanache/PanacheBook.java @@ -0,0 +1,27 @@ +package org.hibernate.jpamodelgen.test.ormPanache; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +import java.util.List; + +import org.hibernate.annotations.NaturalId; +import org.hibernate.annotations.processing.Find; +import org.hibernate.annotations.processing.HQL; + +import io.quarkus.hibernate.orm.panache.PanacheEntity; + +@Entity +public class PanacheBook extends PanacheEntity { + public @Id String isbn; + public @NaturalId String title; + public @NaturalId String author; + public String text; + public int pages; + + @Find + public static native List findBook(String isbn); + + @HQL("WHERE isbn = :isbn") + public static native List hqlBook(String isbn); +} diff --git a/tooling/metamodel-generator/src/quarkusOrmPanache/java/org/hibernate/jpamodelgen/test/ormPanache/PanacheBookRepository.java b/tooling/metamodel-generator/src/quarkusOrmPanache/java/org/hibernate/jpamodelgen/test/ormPanache/PanacheBookRepository.java new file mode 100644 index 0000000000..f420728306 --- /dev/null +++ b/tooling/metamodel-generator/src/quarkusOrmPanache/java/org/hibernate/jpamodelgen/test/ormPanache/PanacheBookRepository.java @@ -0,0 +1,18 @@ +package org.hibernate.jpamodelgen.test.ormPanache; + +import java.util.List; + +import org.hibernate.annotations.processing.Find; +import org.hibernate.annotations.processing.HQL; + +import io.quarkus.hibernate.orm.panache.PanacheRepository; +import jakarta.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class PanacheBookRepository implements PanacheRepository { + @Find + public native List findBook(String isbn); + + @HQL("WHERE isbn = :isbn") + public native List hqlBook(String isbn); +} diff --git a/tooling/metamodel-generator/src/quarkusOrmPanache/java/org/hibernate/jpamodelgen/test/ormPanache/QuarkusOrmPanacheTest.java b/tooling/metamodel-generator/src/quarkusOrmPanache/java/org/hibernate/jpamodelgen/test/ormPanache/QuarkusOrmPanacheTest.java new file mode 100644 index 0000000000..4e86cd0838 --- /dev/null +++ b/tooling/metamodel-generator/src/quarkusOrmPanache/java/org/hibernate/jpamodelgen/test/ormPanache/QuarkusOrmPanacheTest.java @@ -0,0 +1,75 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.jpamodelgen.test.ormPanache; + +import org.hibernate.jpamodelgen.test.util.CompilationTest; +import org.hibernate.jpamodelgen.test.util.TestUtil; +import org.hibernate.jpamodelgen.test.util.WithClasses; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +import jakarta.persistence.EntityManager; + +import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetamodelClassFor; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +/** + * @author Gavin King + */ +public class QuarkusOrmPanacheTest extends CompilationTest { + @Test + @WithClasses({ PanacheBook.class }) + public void testPanacheEntityMetamodel() throws Exception { + // Panache entity + System.out.println( TestUtil.getMetaModelSourceAsString( PanacheBook.class ) ); + Class entityClass = getMetamodelClassFor( PanacheBook.class ); + Assertions.assertNotNull(entityClass); + + // Make sure it has the proper supertype + Class superclass = entityClass.getSuperclass(); + if(superclass != null) { + Assertions.assertEquals("io.quarkus.hibernate.orm.panache.PanacheEntity_", superclass.getName()); + } + + // Panache static native method generates a static method + Method method = entityClass.getDeclaredMethod("hqlBook", EntityManager.class, String.class); + Assertions.assertNotNull(method); + Assertions.assertTrue(Modifier.isStatic(method.getModifiers())); + + // Panache static native method generates a static method + method = entityClass.getDeclaredMethod("findBook", EntityManager.class, String.class); + Assertions.assertNotNull(method); + Assertions.assertTrue(Modifier.isStatic(method.getModifiers())); + } + + @Test + @WithClasses({ PanacheBook.class, PanacheBookRepository.class }) + public void testPanacheRepositoryMetamodel() throws Exception { + // Panache repository + System.out.println( TestUtil.getMetaModelSourceAsString( PanacheBookRepository.class ) ); + Class repositoryClass = getMetamodelClassFor( PanacheBookRepository.class ); + Assertions.assertNotNull(repositoryClass); + + // Make sure it has the proper supertype + Class superclass = repositoryClass.getSuperclass(); + if(superclass != null) { + Assertions.assertEquals("java.lang.Object", superclass.getName()); + } + + // Panache native method generates a static method + Method method = repositoryClass.getDeclaredMethod("hqlBook", EntityManager.class, String.class); + Assertions.assertNotNull(method); + Assertions.assertTrue(Modifier.isStatic(method.getModifiers())); + + // Panache native method generates a static method + method = repositoryClass.getDeclaredMethod("findBook", EntityManager.class, String.class); + Assertions.assertNotNull(method); + Assertions.assertTrue(Modifier.isStatic(method.getModifiers())); + } +}