diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java index 7aa674720..7fe6343cd 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java @@ -18,6 +18,8 @@ package org.springframework.data.mongodb.core; import static org.springframework.data.domain.Sort.Direction.*; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; import java.util.List; import org.springframework.dao.DataAccessException; @@ -25,7 +27,6 @@ import org.springframework.data.mongodb.core.index.IndexDefinition; import org.springframework.data.mongodb.core.index.IndexField; import org.springframework.data.mongodb.core.index.IndexInfo; import org.springframework.util.Assert; -import org.springframework.util.ObjectUtils; import com.mongodb.DBCollection; import com.mongodb.DBObject; @@ -42,6 +43,7 @@ public class DefaultIndexOperations implements IndexOperations { private static final Double ONE = Double.valueOf(1); private static final Double MINUS_ONE = Double.valueOf(-1); + private static final Collection TWO_D_IDENTIFIERS = Arrays.asList("2d", "2dsphere"); private final MongoOperations mongoOperations; private final String collectionName; @@ -141,7 +143,7 @@ public class DefaultIndexOperations implements IndexOperations { Object value = keyDbObject.get(key); - if (isGeoIndex(value)) { + if (TWO_D_IDENTIFIERS.contains(value)) { indexFields.add(IndexField.geo(key)); } else { @@ -168,8 +170,4 @@ public class DefaultIndexOperations implements IndexOperations { } }); } - - private boolean isGeoIndex(Object indexType) { - return ObjectUtils.nullSafeEquals("2d", indexType) || ObjectUtils.nullSafeEquals("2dsphere", indexType); - } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsIntegrationTests.java index e364d54f7..f71bd09d1 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsIntegrationTests.java @@ -15,7 +15,7 @@ */ package org.springframework.data.mongodb.core; -import static org.hamcrest.core.Is.*; +import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import org.junit.Before; @@ -25,7 +25,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.index.IndexInfo; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; import com.mongodb.BasicDBObject; @@ -33,26 +32,30 @@ import com.mongodb.DBCollection; import com.mongodb.DBObject; /** + * Integration tests for {@link DefaultIndexOperations}. + * * @author Christoph Strobl + * @author Oliver Gierke */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:infrastructure.xml") -public class DefaultIndexOperationTests { +public class DefaultIndexOperationsIntegrationTests { - private static final String COLLECTION_NAME = ClassUtils.getShortNameAsProperty(DefaultIndexOperationTests.class); - private static final DBObject GEO_SPHERE_2D = new BasicDBObject("loaction", "2dsphere"); + static final DBObject GEO_SPHERE_2D = new BasicDBObject("loaction", "2dsphere"); @Autowired MongoTemplate template; - private DefaultIndexOperations indexOps; - private DBCollection collection; + DefaultIndexOperations indexOps; + DBCollection collection; @Before public void setUp() { - this.collection = this.template.getDb().getCollection(COLLECTION_NAME); - dropAllIndexes(); + String collectionName = this.template.getCollectionName(DefaultIndexOperationsIntegrationTestsSample.class); - indexOps = new DefaultIndexOperations(template, COLLECTION_NAME); + this.collection = this.template.getDb().getCollection(collectionName); + this.collection.dropIndexes(); + + this.indexOps = new DefaultIndexOperations(template, collectionName); } /** @@ -61,7 +64,7 @@ public class DefaultIndexOperationTests { @Test public void getIndexInfoShouldBeAbleToRead2dsphereIndex() { - createIndex(GEO_SPHERE_2D); + collection.createIndex(GEO_SPHERE_2D); IndexInfo info = findAndReturnIndexInfo(GEO_SPHERE_2D); assertThat(info.getIndexFields().get(0).isGeo(), is(true)); @@ -72,11 +75,11 @@ public class DefaultIndexOperationTests { } @SuppressWarnings("deprecation") - private IndexInfo findAndReturnIndexInfo(Iterable candidates, DBObject keys) { + private static IndexInfo findAndReturnIndexInfo(Iterable candidates, DBObject keys) { return findAndReturnIndexInfo(candidates, DBCollection.genIndexName(keys)); } - private IndexInfo findAndReturnIndexInfo(Iterable candidates, String name) { + private static IndexInfo findAndReturnIndexInfo(Iterable candidates, String name) { for (IndexInfo info : candidates) { if (ObjectUtils.nullSafeEquals(name, info.getName())) { @@ -86,12 +89,5 @@ public class DefaultIndexOperationTests { throw new AssertionError(String.format("Index with %s was not found", name)); } - private void createIndex(DBObject keys) { - template.getDb().getCollection(COLLECTION_NAME).createIndex(keys); - } - - private void dropAllIndexes() { - this.collection.dropIndexes(); - } - + static class DefaultIndexOperationsIntegrationTestsSample {} }