DATADOC-186 - Ensure index property order.

Use LinkedHashMap inside Index to make sure properties added are kept in the order of the addition.
This commit is contained in:
Oliver Gierke
2011-07-07 07:45:15 +02:00
parent dd06973f50
commit 2d09b8b7d1
2 changed files with 21 additions and 13 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 org.springframework.data.document.mongodb.index.IndexDefinition;
@@ -29,7 +29,7 @@ public class Index implements IndexDefinition {
RETAIN, DROP
}
private Map<String, Order> fieldSpec = new HashMap<String, Order>();
private final Map<String, Order> fieldSpec = new LinkedHashMap<String, Order>();
private String name;

View File

@@ -15,7 +15,9 @@
*/
package org.springframework.data.document.mongodb.query;
import org.junit.Assert;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.junit.Test;
import org.springframework.data.document.mongodb.query.Index.Duplicates;
@@ -24,44 +26,50 @@ public class IndexTests {
@Test
public void testWithAscendingIndex() {
Index i = new Index().on("name", Order.ASCENDING);
Assert.assertEquals("{ \"name\" : 1}", i.getIndexKeys().toString());
assertEquals("{ \"name\" : 1}", i.getIndexKeys().toString());
}
@Test
public void testWithDescendingIndex() {
Index i = new Index().on("name", Order.DESCENDING);
Assert.assertEquals("{ \"name\" : -1}", i.getIndexKeys().toString());
assertEquals("{ \"name\" : -1}", i.getIndexKeys().toString());
}
@Test
public void testNamedMultiFieldUniqueIndex() {
Index i = new Index().on("name", Order.ASCENDING).on("age", Order.DESCENDING);
i.named("test").unique();
Assert.assertEquals("{ \"age\" : -1 , \"name\" : 1}", i.getIndexKeys().toString());
Assert.assertEquals("{ \"name\" : \"test\" , \"unique\" : true}", i.getIndexOptions().toString());
assertEquals("{ \"name\" : 1 , \"age\" : -1}", i.getIndexKeys().toString());
assertEquals("{ \"name\" : \"test\" , \"unique\" : true}", i.getIndexOptions().toString());
}
@Test
public void testWithDropDuplicates() {
Index i = new Index().on("name", Order.ASCENDING);
i.unique(Duplicates.DROP);
Assert.assertEquals("{ \"name\" : 1}", i.getIndexKeys().toString());
Assert.assertEquals("{ \"unique\" : true , \"dropDups\" : true}", i.getIndexOptions().toString());
assertEquals("{ \"name\" : 1}", i.getIndexKeys().toString());
assertEquals("{ \"unique\" : true , \"dropDups\" : true}", i.getIndexOptions().toString());
}
@Test
public void testWithSparse() {
Index i = new Index().on("name", Order.ASCENDING);
i.sparse().unique();
Assert.assertEquals("{ \"name\" : 1}", i.getIndexKeys().toString());
Assert.assertEquals("{ \"unique\" : true , \"sparse\" : true}", i.getIndexOptions().toString());
assertEquals("{ \"name\" : 1}", i.getIndexKeys().toString());
assertEquals("{ \"unique\" : true , \"sparse\" : true}", i.getIndexOptions().toString());
}
@Test
public void testGeospatialIndex() {
GeospatialIndex i = new GeospatialIndex("location").withMin(0);
Assert.assertEquals("{ \"location\" : \"2d\"}", i.getIndexKeys().toString());
Assert.assertEquals("{ \"min\" : 0}", i.getIndexOptions().toString());
assertEquals("{ \"location\" : \"2d\"}", i.getIndexKeys().toString());
assertEquals("{ \"min\" : 0}", i.getIndexOptions().toString());
}
@Test
public void ensuresPropertyOrder() {
Index on = new Index("foo", Order.ASCENDING).on("bar", Order.ASCENDING);
assertThat(on.getIndexKeys().toString(), is("{ \"foo\" : 1 , \"bar\" : 1}"));
}
}