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:
@@ -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;
|
||||
|
||||
|
||||
@@ -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}"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user