DATADOC-177 - Sort now preserves order of individual sort properties.
Using a LinkedHashMap now to preserve the order of properties to be sorted upon.
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.document.mongodb.query;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
@@ -23,7 +23,7 @@ import com.mongodb.DBObject;
|
||||
|
||||
public class Sort {
|
||||
|
||||
private Map<String, Order> fieldSpec = new HashMap<String, Order>();
|
||||
private Map<String, Order> fieldSpec = new LinkedHashMap<String, Order>();
|
||||
|
||||
public Sort() {
|
||||
}
|
||||
@@ -44,5 +44,4 @@ public class Sort {
|
||||
}
|
||||
return dbo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,21 +15,32 @@
|
||||
*/
|
||||
package org.springframework.data.document.mongodb.query;
|
||||
|
||||
import org.junit.Assert;
|
||||
import static org.springframework.data.document.mongodb.query.Order.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class SortTests {
|
||||
|
||||
@Test
|
||||
public void testWithSortAscending() {
|
||||
Sort s = new Sort().on("name", Order.ASCENDING);
|
||||
Assert.assertEquals("{ \"name\" : 1}", s.getSortObject().toString());
|
||||
Sort s = new Sort().on("name", ASCENDING);
|
||||
assertEquals("{ \"name\" : 1}", s.getSortObject().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithSortDescending() {
|
||||
Sort s = new Sort().on("name", Order.DESCENDING);
|
||||
Assert.assertEquals("{ \"name\" : -1}", s.getSortObject().toString());
|
||||
Sort s = new Sort().on("name", DESCENDING);
|
||||
assertEquals("{ \"name\" : -1}", s.getSortObject().toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATADOC-177
|
||||
*/
|
||||
@Test
|
||||
public void preservesOrderKeysOnMultipleSorts() {
|
||||
Sort sort = new Sort("foo", DESCENDING).on("bar", DESCENDING);
|
||||
assertThat(sort.getSortObject().toString(), is("{ \"foo\" : -1 , \"bar\" : -1}"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user