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 authored and beikov committed Sep 28, 2020
1 parent 140fbb4 commit eafd262
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
Expand Up @@ -18,7 +18,8 @@
public class SqmSortSpecification implements JpaOrder {
private final SqmExpression sortExpression;
private final String collation;
private SortOrder sortOrder;
private final SortOrder sortOrder;

private NullPrecedence nullPrecedence;

public SqmSortSpecification(
Expand Down Expand Up @@ -73,8 +74,8 @@ public NullPrecedence getNullPrecedence() {

@Override
public JpaOrder reverse() {
this.sortOrder = this.sortOrder == null ? SortOrder.DESCENDING : sortOrder.reverse();
return this;
SortOrder newSortOrder = this.sortOrder == null ? SortOrder.DESCENDING : sortOrder.reverse();
return new SqmSortSpecification( sortExpression, collation, newSortOrder, nullPrecedence );
}

@Override
Expand Down
@@ -0,0 +1,47 @@
/*
* 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.sqm.tree.select;

import org.hibernate.query.criteria.JpaOrder;
import org.hibernate.query.sqm.tree.expression.SqmExpression;

import org.hibernate.testing.TestForIssue;
import org.junit.Test;

import static org.hibernate.NullPrecedence.FIRST;
import static org.hibernate.SortOrder.ASCENDING;
import static org.hibernate.SortOrder.DESCENDING;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.mockito.Mockito.mock;

/**
* @author seregamorph
*/
@TestForIssue(jiraKey = "HHH-13884")
public class HHH13884Test {

@Test
public void testDefaultSqmSortSpecificationReverse() {
SqmExpression sortExpression = mock( SqmExpression.class );
String collation = "collation";

SqmSortSpecification order = new SqmSortSpecification( sortExpression, collation, ASCENDING, FIRST );

assertEquals( sortExpression, order.getSortExpression() );
assertEquals( collation, order.getCollation() );
assertEquals( ASCENDING, order.getSortOrder() );
assertEquals( FIRST, order.getNullPrecedence() );

JpaOrder reversed = order.reverse();

assertEquals( DESCENDING, reversed.getSortOrder() );
assertEquals( FIRST, reversed.getNullPrecedence() );

assertNotSame( "Order.reverse() should create new instance", order, reversed );
}
}

0 comments on commit eafd262

Please sign in to comment.