From 83595ea461115dc7411e1ea0492a15f1d2f83c44 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 23 Feb 2024 21:36:06 +0100 Subject: [PATCH] HHH-17772 add tests --- settings.gradle | 2 + .../hibernate-jpamodelgen.gradle | 1 + .../jpamodelgen/test/data/Author.java | 21 +++++++ .../hibernate/jpamodelgen/test/data/Book.java | 39 ++++++++++++ .../test/data/BookAuthorRepository.java | 60 +++++++++++++++++++ .../jpamodelgen/test/data/DataTest.java | 30 ++++++++++ 6 files changed, 153 insertions(+) create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/Author.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/Book.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/BookAuthorRepository.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/DataTest.java diff --git a/settings.gradle b/settings.gradle index ba8007072a..b800e12801 100644 --- a/settings.gradle +++ b/settings.gradle @@ -140,6 +140,7 @@ dependencyResolutionManagement { def elVersion = version "el", "5.0.0" def injectVersion = version "inject", "2.0.1" def interceptorsVersion = version "interceptors", "2.1.0" + def dataVersion = version "data", "1.0.0-M3" def jaccVersion = version "jacc", "2.1.0" def jaxbApiVersion = version "jaxbApi", "4.0.0" def jaxbRuntimeVersion = version "jaxbRuntime", "4.0.2" @@ -157,6 +158,7 @@ dependencyResolutionManagement { library( "cdi", "jakarta.enterprise", "jakarta.enterprise.cdi-api" ).versionRef( cdiVersion ) library( "annotation", "jakarta.annotation", "jakarta.annotation-api" ).versionRef( annotationVersion ) library( "interceptors", "jakarta.interceptor", "jakarta.interceptor-api" ).versionRef( interceptorsVersion ) + library( "data", "jakarta.data", "jakarta.data-api" ).versionRef( dataVersion ) library( "jsonbApi", "jakarta.json.bind", "jakarta.json.bind-api" ).versionRef( jsonbApiVersion ) library( "jsonb", "org.eclipse", "yasson" ).versionRef( jsonbRuntimeVersion ) library( "inject", "jakarta.inject", "jakarta.inject-api" ).versionRef( injectVersion ) diff --git a/tooling/metamodel-generator/hibernate-jpamodelgen.gradle b/tooling/metamodel-generator/hibernate-jpamodelgen.gradle index b41da6d2a6..7c55256e42 100644 --- a/tooling/metamodel-generator/hibernate-jpamodelgen.gradle +++ b/tooling/metamodel-generator/hibernate-jpamodelgen.gradle @@ -25,6 +25,7 @@ dependencies { api jakartaLibs.jaxb api jakartaLibs.validation api jakartaLibs.annotation + api jakartaLibs.data api libs.antlrRuntime api libs.byteBuddy diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/Author.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/Author.java new file mode 100644 index 0000000000..89274ccba9 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/Author.java @@ -0,0 +1,21 @@ +package org.hibernate.jpamodelgen.test.data; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToMany; + +import java.util.Set; + +@Entity +public class Author { + @Id + String ssn; + String name; + +// @Embedded +// Address address; + + @ManyToMany + Set books; +} + diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/Book.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/Book.java new file mode 100644 index 0000000000..61b9d982c2 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/Book.java @@ -0,0 +1,39 @@ +package org.hibernate.jpamodelgen.test.data; + +import jakarta.persistence.*; +import org.hibernate.annotations.NaturalId; + +import java.math.BigDecimal; +import java.time.LocalDate; +import java.util.Set; + +@Entity +@Table(name = "books") +public class Book { + @Id + String isbn; + + @NaturalId + @Basic(optional = false) + String title; + + @Basic(optional = false) + String text; + + @NaturalId + LocalDate publicationDate; + + @ManyToMany(mappedBy = "books") + Set authors; + + BigDecimal price; + + int pages; + + public Book(String isbn, String title, String text) { + this.isbn = isbn; + this.title = title; + this.text = text; + } + Book() {} +} \ No newline at end of file diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/BookAuthorRepository.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/BookAuthorRepository.java new file mode 100644 index 0000000000..b6714b2528 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/BookAuthorRepository.java @@ -0,0 +1,60 @@ +package org.hibernate.jpamodelgen.test.data; + +import jakarta.data.Limit; +import jakarta.data.Order; +import jakarta.data.Sort; +import jakarta.data.repository.By; +import jakarta.data.repository.Delete; +import jakarta.data.repository.Find; +import jakarta.data.repository.Insert; +import jakarta.data.repository.OrderBy; +import jakarta.data.repository.Param; +import jakarta.data.repository.Query; +import jakarta.data.repository.Repository; +import jakarta.data.repository.Update; +import org.hibernate.StatelessSession; + +import java.time.LocalDate; +import java.util.List; + +@Repository(dataStore = "myds") +public interface BookAuthorRepository { + + StatelessSession session(); + + @Find + Book book(String isbn); + + @Find + Book bookById(@By("isbn") String id); + + @Find + @OrderBy(value = "title", ignoreCase = true) + List byPubDate0(LocalDate publicationDate); + + @Find + @OrderBy(value = "title", ignoreCase = true) + @OrderBy(value = "isbn", descending = true) + List byPubDate1(LocalDate publicationDate, Limit limit, Sort order); + + @Find + List byPubDate2(LocalDate publicationDate, Order order); + + @Insert + void create(Book book); + + @Update + void update(Book book); + + @Delete + void delete(Book book); + + @Query("from Book where title like :title") + List books0(String title); + + @Query("from Book where title like :title") + List books1(@Param("title") String titlePattern, Order order); + + @Query("from Book where title like :title") + List books2(@Param("title") String titlePattern, Limit limit); +} diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/DataTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/DataTest.java new file mode 100644 index 0000000000..aaedb905ba --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/data/DataTest.java @@ -0,0 +1,30 @@ +/* + * 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.data; + +import org.hibernate.jpamodelgen.test.util.CompilationTest; +import org.hibernate.jpamodelgen.test.util.WithClasses; +import org.junit.Test; + +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor; +import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetaModelSourceAsString; + +/** + * @author Gavin King + */ +public class DataTest extends CompilationTest { + @Test + @WithClasses({ Author.class, Book.class, BookAuthorRepository.class }) + public void test() { + System.out.println( getMetaModelSourceAsString( Author.class ) ); + System.out.println( getMetaModelSourceAsString( Book.class ) ); + System.out.println( getMetaModelSourceAsString( BookAuthorRepository.class ) ); + assertMetamodelClassGeneratedFor( Author.class ); + assertMetamodelClassGeneratedFor( Book.class ); + assertMetamodelClassGeneratedFor( BookAuthorRepository.class ); + } +}