Skip to content

Commit

Permalink
HHH-13884 Order.reverse() contract
Browse files Browse the repository at this point in the history
  • Loading branch information
seregamorph committed Feb 28, 2020
1 parent b856d53 commit 12060d1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.query.criteria.internal;

import java.io.Serializable;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Order;
Expand All @@ -15,8 +16,9 @@
* @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 );
Expand All @@ -28,8 +30,7 @@ public OrderImpl(Expression<?> expression, boolean ascending) {
}

public Order reverse() {
ascending = !ascending;
return this;
return new OrderImpl( expression, !ascending );
}

public boolean isAscending() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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 );
}
}

0 comments on commit 12060d1

Please sign in to comment.