Skip to content

Commit

Permalink
Configure separate source directories for HR and ORM Quarkus Panache …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
beikov authored and gavinking committed Mar 1, 2024
1 parent 1fe9c66 commit 9e32396
Show file tree
Hide file tree
Showing 12 changed files with 389 additions and 78 deletions.
46 changes: 37 additions & 9 deletions tooling/metamodel-generator/hibernate-jpamodelgen.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,27 @@ ext {
xsdDir = file( "${projectDir}/src/main/xsd" )
}

configurations {
quarkusOrmPanache
quarkusHrPanache
sourceSets {
quarkusOrmPanache {
java {
srcDirs = ['src/quarkusOrmPanache/java']
}
resources {
srcDirs tasks.processTestResources
}
compileClasspath += sourceSets.main.output + sourceSets.test.output
runtimeClasspath += sourceSets.main.output + sourceSets.test.output
}
quarkusHrPanache {
java {
srcDirs = ['src/quarkusHrPanache/java']
}
resources {
srcDirs tasks.processTestResources
}
compileClasspath += sourceSets.main.output + sourceSets.test.output
runtimeClasspath += sourceSets.main.output + sourceSets.test.output
}
}

dependencies {
Expand All @@ -37,16 +55,26 @@ dependencies {
xjc jakartaLibs.jaxb
xjc rootProject.fileTree(dir: 'patched-libs/jaxb2-basics', include: '*.jar')

quarkusOrmPanache "io.quarkus:quarkus-hibernate-orm-panache:3.6.2"
quarkusHrPanache "io.quarkus:quarkus-hibernate-reactive-panache:3.6.2"
quarkusOrmPanacheImplementation "io.quarkus:quarkus-hibernate-orm-panache:3.6.2"
quarkusHrPanacheImplementation "io.quarkus:quarkus-hibernate-reactive-panache:3.6.2"
}

// The source set gets a custom configuration which extends the normal test implementation config
configurations {
quarkusOrmPanacheImplementation.extendsFrom(testImplementation)
quarkusOrmPanacheRuntimeOnly.extendsFrom(testRuntimeOnly)
quarkusOrmPanacheCompileOnly.extendsFrom(testCompileOnly)
quarkusHrPanacheImplementation.extendsFrom(testImplementation)
quarkusHrPanacheRuntimeOnly.extendsFrom(testRuntimeOnly)
quarkusHrPanacheCompileOnly.extendsFrom(testCompileOnly)
}

def quarkusOrmPanacheTestTask = tasks.register( 'quarkusOrmPanacheTest', Test ) {
description = 'Runs the Quarkus ORM Panache tests.'
group = 'verification'

testClassesDirs = sourceSets.test.output.classesDirs
classpath = sourceSets.test.runtimeClasspath + configurations.quarkusOrmPanache
testClassesDirs = sourceSets.quarkusOrmPanache.output.classesDirs
classpath = sourceSets.quarkusOrmPanache.runtimeClasspath
shouldRunAfter test
dependsOn test
}
Expand All @@ -55,8 +83,8 @@ def quarkusHrPanacheTestTask = tasks.register( 'quarkusHrPanacheTest', Test ) {
description = 'Runs the Quarkus HR Panache tests.'
group = 'verification'

testClassesDirs = sourceSets.test.output.classesDirs
classpath = sourceSets.test.runtimeClasspath + configurations.quarkusHrPanache
testClassesDirs = sourceSets.quarkusHrPanache.output.classesDirs
classpath = sourceSets.quarkusHrPanache.runtimeClasspath
shouldRunAfter test
dependsOn test
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.hibernate.jpamodelgen.test.hrPanache;

import java.util.List;

import org.hibernate.annotations.processing.Find;
import org.hibernate.reactive.mutiny.Mutiny;

import io.quarkus.hibernate.reactive.panache.common.runtime.SessionOperations;
import io.smallrye.mutiny.Uni;

public interface BookRepositoryWithSession {

public default Uni<Mutiny.Session> mySession() {
return SessionOperations.getSession();
}

@Find
public Uni<List<PanacheBook>> findBook(String isbn);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.hibernate.jpamodelgen.test.hrPanache;

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.reactive.panache.PanacheEntity;
import io.smallrye.mutiny.Uni;

@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 Uni<List<PanacheBook>> findBook(String isbn);

@HQL("WHERE isbn = :isbn")
public static native Uni<List<PanacheBook>> hqlBook(String isbn);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.hibernate.jpamodelgen.test.hrPanache;

import java.util.List;

import org.hibernate.annotations.processing.Find;
import org.hibernate.annotations.processing.HQL;

import io.quarkus.hibernate.reactive.panache.PanacheRepository;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class PanacheBookRepository implements PanacheRepository<PanacheBook> {
@Find
public native Uni<List<PanacheBook>> findBook(String isbn);

@HQL("WHERE isbn = :isbn")
public native Uni<List<PanacheBook>> hqlBook(String isbn);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.hibernate.jpamodelgen.test.hrPanache;

import java.util.List;

import org.hibernate.annotations.processing.Find;
import org.hibernate.annotations.processing.HQL;

import io.smallrye.mutiny.Uni;

public interface QuarkusBookRepository {
@Find
public Uni<List<PanacheBook>> findBook(String isbn);

@HQL("WHERE isbn = :isbn")
public Uni<List<PanacheBook>> hqlBook(String isbn);

@HQL("DELETE FROM PanacheBook")
public Uni<Void> deleteAllBooksVoid();

@HQL("DELETE FROM PanacheBook")
public Uni<Integer> deleteAllBooksInt();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpamodelgen.test.hrPanache;

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 io.smallrye.mutiny.Uni;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;

import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetamodelClassFor;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
* @author Gavin King
*/
public class QuarkusHrPanacheTest 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.reactive.panache.PanacheEntity_", superclass.getName() );
}

// Panache static native method generates a static method
Method method = entityClass.getDeclaredMethod( "hqlBook", Uni.class, String.class );
Assertions.assertNotNull( method );
checkUni(method);
Assertions.assertTrue( Modifier.isStatic( method.getModifiers()) );

// Panache static native method generates a static method
method = entityClass.getDeclaredMethod( "findBook", Uni.class, String.class );
Assertions.assertNotNull( method );
checkUni(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", Uni.class, String.class );
Assertions.assertNotNull( method );
checkUni(method);
Assertions.assertTrue( Modifier.isStatic(method.getModifiers()) );

// Panache native method generates a static method
method = repositoryClass.getDeclaredMethod( "findBook", Uni.class, String.class );
Assertions.assertNotNull( method );
checkUni(method);
Assertions.assertTrue( Modifier.isStatic( method.getModifiers() ) );
}

private void checkUni(Method method) {
Assertions.assertEquals("io.smallrye.mutiny.Uni<org.hibernate.reactive.mutiny.Mutiny$Session>", method.getGenericParameterTypes()[0].toString());
}

@Test
@WithClasses({ PanacheBook.class, QuarkusBookRepository.class })
public void testQuarkusRepositoryMetamodel() throws Exception {
// Regular repository
System.out.println( TestUtil.getMetaModelSourceAsString( QuarkusBookRepository.class ) );
Class<?> repositoryClass = getMetamodelClassFor( QuarkusBookRepository.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() );
}
Class<?>[] interfaces = repositoryClass.getInterfaces();
Assertions.assertEquals( 1, interfaces.length );
Assertions.assertEquals( QuarkusBookRepository.class.getName(), interfaces[0].getName() );

// Annotated method generates an instance method
Method method = repositoryClass.getDeclaredMethod( "hqlBook", String.class );
Assertions.assertNotNull( method );
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );

// Annotated method generates an instance method
method = repositoryClass.getDeclaredMethod( "findBook", String.class );
Assertions.assertNotNull( method );
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );

// Make sure we have only the default constructor
Constructor<?>[] constructors = repositoryClass.getDeclaredConstructors();
Assertions.assertNotNull( constructors );
Assertions.assertEquals( 1, constructors.length );
Assertions.assertNotNull( repositoryClass.getDeclaredConstructor() );

// Proper return type
method = repositoryClass.getDeclaredMethod( "deleteAllBooksVoid" );
Assertions.assertNotNull( method );
Assertions.assertEquals("io.smallrye.mutiny.Uni<java.lang.Void>", method.getGenericReturnType().toString());
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );

// Proper return type
method = repositoryClass.getDeclaredMethod( "deleteAllBooksInt" );
Assertions.assertNotNull( method );
Assertions.assertEquals("io.smallrye.mutiny.Uni<java.lang.Integer>", method.getGenericReturnType().toString());
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );
}

@Test
@WithClasses({ PanacheBook.class, BookRepositoryWithSession.class })
public void testBookRepositoryWithSessionMetamodel() throws Exception {
// Regular repository with default session method
System.out.println( TestUtil.getMetaModelSourceAsString( BookRepositoryWithSession.class ) );
Class<?> repositoryClass = getMetamodelClassFor( BookRepositoryWithSession.class );
Assertions.assertNotNull( repositoryClass );

// Make sure we have only the default constructor
Constructor<?>[] constructors = repositoryClass.getDeclaredConstructors();
Assertions.assertNotNull( constructors );
Assertions.assertEquals( 1, constructors.length );
Assertions.assertNotNull( repositoryClass.getDeclaredConstructor() );

// Make sure we do not override the default session method
Assertions.assertThrows( NoSuchMethodException.class, () -> repositoryClass.getDeclaredMethod( "mySession" ) );
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.hibernate.jpamodelgen.test.ormPanache;

import java.util.List;

import org.hibernate.annotations.processing.Find;
import org.hibernate.annotations.processing.HQL;

public interface QuarkusBookRepository {
@Find
public List<PanacheBook> findBook(String isbn);

@HQL("WHERE isbn = :isbn")
public List<PanacheBook> hqlBook(String isbn);
}

0 comments on commit 9e32396

Please sign in to comment.