Skip to content

Commit 0b856f5

Browse files
sebersolegavinking
authored andcommitted
add better test for TypedQueryReference in metamodel
1 parent ef3250c commit 0b856f5

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/namedquery/AuxiliaryTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55
package org.hibernate.processor.test.namedquery;
66

77
import jakarta.persistence.EntityManager;
8+
import jakarta.persistence.TypedQueryReference;
89
import org.hibernate.processor.test.util.CompilationTest;
910
import org.hibernate.processor.test.util.TestUtil;
1011
import org.hibernate.processor.test.util.WithClasses;
1112
import org.junit.Test;
1213

14+
import java.lang.reflect.ParameterizedType;
15+
1316
import static org.hibernate.processor.test.util.TestUtil.assertMetamodelClassGeneratedFor;
17+
import static org.hibernate.processor.test.util.TestUtil.assertPresenceOfFieldInMetamodelFor;
1418
import static org.hibernate.processor.test.util.TestUtil.assertPresenceOfMethodInMetamodelFor;
1519
import static org.hibernate.processor.test.util.TestUtil.assertPresenceOfNameFieldInMetamodelFor;
20+
import static org.hibernate.processor.test.util.TestUtil.getFieldFromMetamodelFor;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
1622

1723
/**
1824
* @author Gavin King
@@ -129,5 +135,31 @@ public void test() {
129135
Object.class,
130136
Object.class
131137
);
138+
139+
checkTypedQueryReference( "QUERY_TITLES_WITH_ISBNS", "_titlesWithIsbns_", Object[].class );
140+
checkTypedQueryReference( "QUERY_TITLES_AND_ISBNS_AS_RECORD", "_titlesAndIsbnsAsRecord_", TitleAndIsbn.class );
141+
checkTypedQueryReference( "QUERY_TYPE_OF_BOOK", "__typeOfBook_", Type.class );
142+
checkTypedQueryReference( "QUERY_GET_TITLES", "__getTitles_", String.class );
143+
checkTypedQueryReference( "QUERY_GET_UPPER_LOWER_TITLES", "__getUpperLowerTitles_", Object[].class );
144+
checkTypedQueryReference( "QUERY_BOOKS_BY_TITLE", "_booksByTitle_", Book.class );
145+
checkTypedQueryReference( "QUERY_BOOKS_BY_TITLE_VERBOSE", "_booksByTitleVerbose_", Book.class );
146+
}
147+
148+
private static void checkTypedQueryReference(String NAME, String name, Class<?> type) {
149+
assertPresenceOfNameFieldInMetamodelFor(
150+
Book.class,
151+
NAME,
152+
"Missing named query attribute."
153+
);
154+
assertPresenceOfFieldInMetamodelFor(
155+
Book.class,
156+
name,
157+
"Missing typed query reference."
158+
);
159+
ParameterizedType fieldType = (ParameterizedType)
160+
getFieldFromMetamodelFor( Book.class, name )
161+
.getGenericType();
162+
assertEquals(TypedQueryReference.class, fieldType.getRawType());
163+
assertEquals( type, fieldType.getActualTypeArguments()[0]);
132164
}
133165
}

tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/namedquery/Book.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
query = "select book from Book book where book.title like :titlePattern and book.type = :type")
2424
@NamedQuery(name = "#getTitles",
2525
query = "select title from Book")
26+
@NamedQuery(name = "titlesAndIsbnsAsRecord",
27+
query = "select new org.hibernate.processor.test.namedquery.TitleAndIsbn(title,isbn) from Book")
2628
@NamedQuery(name = "#getUpperLowerTitles",
2729
query = "select upper(title), lower(title), length(title) from Book")
2830
@NamedQuery(name = "#typeOfBook",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.namedquery;
6+
7+
public final class TitleAndIsbn {
8+
private final String title;
9+
private final String isbn;
10+
11+
public TitleAndIsbn(String title, String isbn) {
12+
this.title = title;
13+
this.isbn = isbn;
14+
}
15+
16+
public String title() {
17+
return title;
18+
}
19+
20+
public String isbn() {
21+
return isbn;
22+
}
23+
}

0 commit comments

Comments
 (0)