HHH-18068 tests for case-insensitive Order

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-05-03 16:11:42 +02:00
parent 2932933c43
commit bea5e4cf3d
1 changed files with 205 additions and 0 deletions

View File

@ -14,7 +14,10 @@ import java.util.List;
import static java.util.stream.Collectors.toList;
import static org.hibernate.query.Order.asc;
import static org.hibernate.query.Order.by;
import static org.hibernate.query.Order.desc;
import static org.hibernate.query.SortDirection.ASCENDING;
import static org.hibernate.query.SortDirection.DESCENDING;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SessionFactory
@ -254,6 +257,208 @@ public class OrderTest {
});
}
@Test void testAscendingDescendingCaseInsensitive(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createMutationQuery("delete Book").executeUpdate() );
scope.inTransaction( session -> {
session.persist(new Book("9781932394153", "Hibernate in Action"));
session.persist(new Book("9781617290459", "Java Persistence with Hibernate"));
});
EntityDomainType<Book> bookType = scope.getSessionFactory().getJpaMetamodel().findEntityType(Book.class);
SingularAttribute<? super Book, ?> title = bookType.findSingularAttribute("title");
SingularAttribute<? super Book, ?> isbn = bookType.findSingularAttribute("isbn");
scope.inSession(session -> {
List<String> titlesAsc = session.createSelectionQuery("from Book", Book.class)
.setOrder(asc(title).ignoringCase())
.getResultList()
.stream().map(book -> book.title)
.collect(toList());
assertEquals("Hibernate in Action", titlesAsc.get(0));
assertEquals("Java Persistence with Hibernate", titlesAsc.get(1));
List<String> titlesDesc = session.createSelectionQuery("from Book", Book.class)
.setOrder(desc(title).ignoringCase())
.getResultList()
.stream().map(book -> book.title)
.collect(toList());
assertEquals("Hibernate in Action", titlesDesc.get(1));
assertEquals("Java Persistence with Hibernate", titlesDesc.get(0));
List<String> isbnAsc = session.createSelectionQuery("from Book", Book.class)
.setOrder(List.of(asc(isbn).ignoringCase(), desc(title).ignoringCase()))
.getResultList()
.stream().map(book -> book.title)
.collect(toList());
assertEquals("Hibernate in Action", isbnAsc.get(1));
assertEquals("Java Persistence with Hibernate", isbnAsc.get(0));
List<String> isbnDesc = session.createSelectionQuery("from Book", Book.class)
.setOrder(List.of(desc(isbn).ignoringCase(), desc(title).ignoringCase()))
.getResultList()
.stream().map(book -> book.title)
.collect(toList());
assertEquals("Hibernate in Action", isbnDesc.get(0));
assertEquals("Java Persistence with Hibernate", isbnDesc.get(1));
// titlesAsc = session.createSelectionQuery("from Book order by isbn asc", Book.class)
// .setOrder(asc(title))
// .getResultList()
// .stream().map(book -> book.title)
// .collect(toList());
// assertEquals("Hibernate in Action", titlesAsc.get(1));
// assertEquals("Java Persistence with Hibernate", titlesAsc.get(0));
titlesAsc = session.createSelectionQuery("from Book order by isbn asc", Book.class)
// .setOrder(emptyList())
.setOrder(asc(title).ignoringCase())
.getResultList()
.stream().map(book -> book.title)
.collect(toList());
assertEquals("Hibernate in Action", titlesAsc.get(0));
assertEquals("Java Persistence with Hibernate", titlesAsc.get(1));
});
}
@Test void testAscendingDescendingCaseInsensitiveLongForm(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createMutationQuery("delete Book").executeUpdate() );
scope.inTransaction( session -> {
session.persist(new Book("9781932394153", "Hibernate in Action"));
session.persist(new Book("9781617290459", "Java Persistence with Hibernate"));
});
EntityDomainType<Book> bookType = scope.getSessionFactory().getJpaMetamodel().findEntityType(Book.class);
SingularAttribute<? super Book, ?> title = bookType.findSingularAttribute("title");
SingularAttribute<? super Book, ?> isbn = bookType.findSingularAttribute("isbn");
scope.inSession(session -> {
List<String> titlesAsc = session.createSelectionQuery("from Book", Book.class)
.setOrder(by(title, ASCENDING, true))
.getResultList()
.stream().map(book -> book.title)
.collect(toList());
assertEquals("Hibernate in Action", titlesAsc.get(0));
assertEquals("Java Persistence with Hibernate", titlesAsc.get(1));
List<String> titlesDesc = session.createSelectionQuery("from Book", Book.class)
.setOrder(by(title, DESCENDING, true))
.getResultList()
.stream().map(book -> book.title)
.collect(toList());
assertEquals("Hibernate in Action", titlesDesc.get(1));
assertEquals("Java Persistence with Hibernate", titlesDesc.get(0));
List<String> isbnAsc = session.createSelectionQuery("from Book", Book.class)
.setOrder(List.of(by(isbn, ASCENDING, true), by(title, DESCENDING, true)))
.getResultList()
.stream().map(book -> book.title)
.collect(toList());
assertEquals("Hibernate in Action", isbnAsc.get(1));
assertEquals("Java Persistence with Hibernate", isbnAsc.get(0));
List<String> isbnDesc = session.createSelectionQuery("from Book", Book.class)
.setOrder(List.of(by(isbn, DESCENDING, true), by(title, DESCENDING, true)))
.getResultList()
.stream().map(book -> book.title)
.collect(toList());
assertEquals("Hibernate in Action", isbnDesc.get(0));
assertEquals("Java Persistence with Hibernate", isbnDesc.get(1));
// titlesAsc = session.createSelectionQuery("from Book order by isbn asc", Book.class)
// .setOrder(asc(title))
// .getResultList()
// .stream().map(book -> book.title)
// .collect(toList());
// assertEquals("Hibernate in Action", titlesAsc.get(1));
// assertEquals("Java Persistence with Hibernate", titlesAsc.get(0));
titlesAsc = session.createSelectionQuery("from Book order by isbn asc", Book.class)
// .setOrder(emptyList())
.setOrder(by(title, ASCENDING, true))
.getResultList()
.stream().map(book -> book.title)
.collect(toList());
assertEquals("Hibernate in Action", titlesAsc.get(0));
assertEquals("Java Persistence with Hibernate", titlesAsc.get(1));
});
}
@Test void testAscendingDescendingBySelectElementCaseInsensitive(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createMutationQuery("delete Book").executeUpdate() );
scope.inTransaction( session -> {
session.persist(new Book("9781932394153", "Hibernate in Action"));
session.persist(new Book("9781617290459", "Java Persistence with Hibernate"));
});
scope.inSession(session -> {
List<?> titlesAsc = session.createSelectionQuery("select isbn, title from Book", Object[].class)
.setOrder(asc(2).ignoringCase())
.getResultList()
.stream().map(book -> book[1])
.collect(toList());
assertEquals("Hibernate in Action", titlesAsc.get(0));
assertEquals("Java Persistence with Hibernate", titlesAsc.get(1));
List<?> titlesDesc = session.createSelectionQuery("select isbn, title from Book", Object[].class)
.setOrder(desc(2).ignoringCase())
.getResultList()
.stream().map(book -> book[1])
.collect(toList());
assertEquals("Hibernate in Action", titlesDesc.get(1));
assertEquals("Java Persistence with Hibernate", titlesDesc.get(0));
List<?> isbnAsc = session.createSelectionQuery("select isbn, title from Book", Object[].class)
.setOrder(List.of(asc(1).ignoringCase(), desc(2).ignoringCase()))
.getResultList()
.stream().map(book -> book[1])
.collect(toList());
assertEquals("Hibernate in Action", isbnAsc.get(1));
assertEquals("Java Persistence with Hibernate", isbnAsc.get(0));
List<?> isbnDesc = session.createSelectionQuery("select isbn, title from Book", Object[].class)
.setOrder(List.of(desc(1).ignoringCase(), desc(2).ignoringCase()))
.getResultList()
.stream().map(book -> book[1])
.collect(toList());
assertEquals("Hibernate in Action", isbnDesc.get(0));
assertEquals("Java Persistence with Hibernate", isbnDesc.get(1));
});
}
@Test void testAscendingDescendingBySelectElementCaseInsensitiveLongForm(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createMutationQuery("delete Book").executeUpdate() );
scope.inTransaction( session -> {
session.persist(new Book("9781932394153", "Hibernate in Action"));
session.persist(new Book("9781617290459", "Java Persistence with Hibernate"));
});
scope.inSession(session -> {
List<?> titlesAsc = session.createSelectionQuery("select isbn, title from Book", Object[].class)
.setOrder(by(2, ASCENDING, true))
.getResultList()
.stream().map(book -> book[1])
.collect(toList());
assertEquals("Hibernate in Action", titlesAsc.get(0));
assertEquals("Java Persistence with Hibernate", titlesAsc.get(1));
List<?> titlesDesc = session.createSelectionQuery("select isbn, title from Book", Object[].class)
.setOrder(by(2, DESCENDING, true))
.getResultList()
.stream().map(book -> book[1])
.collect(toList());
assertEquals("Hibernate in Action", titlesDesc.get(1));
assertEquals("Java Persistence with Hibernate", titlesDesc.get(0));
List<?> isbnAsc = session.createSelectionQuery("select isbn, title from Book", Object[].class)
.setOrder(List.of(by(1, ASCENDING, true), by(2, DESCENDING, true)))
.getResultList()
.stream().map(book -> book[1])
.collect(toList());
assertEquals("Hibernate in Action", isbnAsc.get(1));
assertEquals("Java Persistence with Hibernate", isbnAsc.get(0));
List<?> isbnDesc = session.createSelectionQuery("select isbn, title from Book", Object[].class)
.setOrder(List.of(by(1, DESCENDING, true), by(2, DESCENDING, true)))
.getResultList()
.stream().map(book -> book[1])
.collect(toList());
assertEquals("Hibernate in Action", isbnDesc.get(0));
assertEquals("Java Persistence with Hibernate", isbnDesc.get(1));
});
}
@Entity(name="Book")
static class Book {
@Id String isbn;