From 9934baf90ec9859c7ab4c59633c5881f8067d53c Mon Sep 17 00:00:00 2001 From: seregamorph Date: Fri, 28 Feb 2020 16:21:27 +0300 Subject: [PATCH] HHH-13884 Order.reverse() contract --- .../query/criteria/internal/OrderImpl.java | 7 +-- .../query/criteria/internal/HHH13884Test.java | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/query/criteria/internal/HHH13884Test.java diff --git a/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/OrderImpl.java b/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/OrderImpl.java index 6f8b4759c0..2078da4166 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/OrderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/OrderImpl.java @@ -5,6 +5,7 @@ * See the lgpl.txt file in the root directory or . */ package org.hibernate.query.criteria.internal; + import java.io.Serializable; import javax.persistence.criteria.Expression; import javax.persistence.criteria.Order; @@ -15,8 +16,9 @@ import javax.persistence.criteria.Order; * @author Steve Ebersole */ public class OrderImpl implements Order, Serializable { + private final Expression expression; - private boolean ascending; + private final boolean ascending; public OrderImpl(Expression expression) { this( expression, true ); @@ -28,8 +30,7 @@ public class OrderImpl implements Order, Serializable { } public Order reverse() { - ascending = !ascending; - return this; + return new OrderImpl( expression, !ascending ); } public boolean isAscending() { diff --git a/hibernate-core/src/test/java/org/hibernate/query/criteria/internal/HHH13884Test.java b/hibernate-core/src/test/java/org/hibernate/query/criteria/internal/HHH13884Test.java new file mode 100644 index 0000000000..b56c244a51 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/query/criteria/internal/HHH13884Test.java @@ -0,0 +1,43 @@ +/* + * 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.query.criteria.internal; + +import javax.persistence.criteria.Expression; +import javax.persistence.criteria.Order; + +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +/** + * @author seregamorph + */ +@TestForIssue(jiraKey = "HHH-13884") +public class HHH13884Test { + + @Test + public void testDefaultReversedOrderImpl() { + Expression expression = mock( Expression.class ); + + OrderImpl order = new OrderImpl( expression ); + + assertEquals( expression, order.getExpression() ); + assertTrue( "Order should be ascending by default", order.isAscending() ); + + Order reversed = order.reverse(); + + assertEquals( expression, reversed.getExpression() ); + assertFalse( "Reversed Order should be descending", reversed.isAscending() ); + + assertNotSame( "Order.reverse() should create new instance by the contract", order, reversed ); + } +}