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:
Oliver Gierke
2011-07-06 15:39:22 +02:00
parent af0cd9049a
commit f4373957b3
2 changed files with 19 additions and 9 deletions

View File

@@ -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;
}
}

View File

@@ -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}"));
}
}