Skip to content

Commit e354867

Browse files
committed
DATAMONGO-1008 - Polishing.
Slightly changed the implementation of the 2dsphere check, Minor refactorings in the test case. Original pull request: #210.
1 parent b80c81f commit e354867

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
import static org.springframework.data.domain.Sort.Direction.*;
1919

2020
import java.util.ArrayList;
21+
import java.util.Arrays;
22+
import java.util.Collection;
2123
import java.util.List;
2224

2325
import org.springframework.dao.DataAccessException;
2426
import org.springframework.data.mongodb.core.index.IndexDefinition;
2527
import org.springframework.data.mongodb.core.index.IndexField;
2628
import org.springframework.data.mongodb.core.index.IndexInfo;
2729
import org.springframework.util.Assert;
28-
import org.springframework.util.ObjectUtils;
2930

3031
import com.mongodb.DBCollection;
3132
import com.mongodb.DBObject;
@@ -42,6 +43,7 @@ public class DefaultIndexOperations implements IndexOperations {
4243

4344
private static final Double ONE = Double.valueOf(1);
4445
private static final Double MINUS_ONE = Double.valueOf(-1);
46+
private static final Collection<String> TWO_D_IDENTIFIERS = Arrays.asList("2d", "2dsphere");
4547

4648
private final MongoOperations mongoOperations;
4749
private final String collectionName;
@@ -141,7 +143,7 @@ private List<IndexInfo> getIndexData(List<DBObject> dbObjectList) {
141143

142144
Object value = keyDbObject.get(key);
143145

144-
if (isGeoIndex(value)) {
146+
if (TWO_D_IDENTIFIERS.contains(value)) {
145147
indexFields.add(IndexField.geo(key));
146148
} else {
147149

@@ -168,8 +170,4 @@ private List<IndexInfo> getIndexData(List<DBObject> dbObjectList) {
168170
}
169171
});
170172
}
171-
172-
private boolean isGeoIndex(Object indexType) {
173-
return ObjectUtils.nullSafeEquals("2d", indexType) || ObjectUtils.nullSafeEquals("2dsphere", indexType);
174-
}
175173
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsIntegrationTests.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import static org.hamcrest.core.Is.*;
18+
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
2020

2121
import org.junit.Before;
@@ -25,34 +25,37 @@
2525
import org.springframework.data.mongodb.core.index.IndexInfo;
2626
import org.springframework.test.context.ContextConfiguration;
2727
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
28-
import org.springframework.util.ClassUtils;
2928
import org.springframework.util.ObjectUtils;
3029

3130
import com.mongodb.BasicDBObject;
3231
import com.mongodb.DBCollection;
3332
import com.mongodb.DBObject;
3433

3534
/**
35+
* Integration tests for {@link DefaultIndexOperations}.
36+
*
3637
* @author Christoph Strobl
38+
* @author Oliver Gierke
3739
*/
3840
@RunWith(SpringJUnit4ClassRunner.class)
3941
@ContextConfiguration("classpath:infrastructure.xml")
40-
public class DefaultIndexOperationTests {
42+
public class DefaultIndexOperationsIntegrationTests {
4143

42-
private static final String COLLECTION_NAME = ClassUtils.getShortNameAsProperty(DefaultIndexOperationTests.class);
43-
private static final DBObject GEO_SPHERE_2D = new BasicDBObject("loaction", "2dsphere");
44+
static final DBObject GEO_SPHERE_2D = new BasicDBObject("loaction", "2dsphere");
4445

4546
@Autowired MongoTemplate template;
46-
private DefaultIndexOperations indexOps;
47-
private DBCollection collection;
47+
DefaultIndexOperations indexOps;
48+
DBCollection collection;
4849

4950
@Before
5051
public void setUp() {
5152

52-
this.collection = this.template.getDb().getCollection(COLLECTION_NAME);
53-
dropAllIndexes();
53+
String collectionName = this.template.getCollectionName(DefaultIndexOperationsIntegrationTestsSample.class);
5454

55-
indexOps = new DefaultIndexOperations(template, COLLECTION_NAME);
55+
this.collection = this.template.getDb().getCollection(collectionName);
56+
this.collection.dropIndexes();
57+
58+
this.indexOps = new DefaultIndexOperations(template, collectionName);
5659
}
5760

5861
/**
@@ -61,7 +64,7 @@ public void setUp() {
6164
@Test
6265
public void getIndexInfoShouldBeAbleToRead2dsphereIndex() {
6366

64-
createIndex(GEO_SPHERE_2D);
67+
collection.createIndex(GEO_SPHERE_2D);
6568

6669
IndexInfo info = findAndReturnIndexInfo(GEO_SPHERE_2D);
6770
assertThat(info.getIndexFields().get(0).isGeo(), is(true));
@@ -72,11 +75,11 @@ private IndexInfo findAndReturnIndexInfo(DBObject keys) {
7275
}
7376

7477
@SuppressWarnings("deprecation")
75-
private IndexInfo findAndReturnIndexInfo(Iterable<IndexInfo> candidates, DBObject keys) {
78+
private static IndexInfo findAndReturnIndexInfo(Iterable<IndexInfo> candidates, DBObject keys) {
7679
return findAndReturnIndexInfo(candidates, DBCollection.genIndexName(keys));
7780
}
7881

79-
private IndexInfo findAndReturnIndexInfo(Iterable<IndexInfo> candidates, String name) {
82+
private static IndexInfo findAndReturnIndexInfo(Iterable<IndexInfo> candidates, String name) {
8083

8184
for (IndexInfo info : candidates) {
8285
if (ObjectUtils.nullSafeEquals(name, info.getName())) {
@@ -86,12 +89,5 @@ private IndexInfo findAndReturnIndexInfo(Iterable<IndexInfo> candidates, String
8689
throw new AssertionError(String.format("Index with %s was not found", name));
8790
}
8891

89-
private void createIndex(DBObject keys) {
90-
template.getDb().getCollection(COLLECTION_NAME).createIndex(keys);
91-
}
92-
93-
private void dropAllIndexes() {
94-
this.collection.dropIndexes();
95-
}
96-
92+
static class DefaultIndexOperationsIntegrationTestsSample {}
9793
}

0 commit comments

Comments
 (0)