|
| 1 | +/* |
| 2 | + * Copyright 2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.core; |
| 17 | + |
| 18 | +import static org.hamcrest.core.Is.*; |
| 19 | +import static org.junit.Assert.*; |
| 20 | + |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.data.mongodb.core.index.IndexInfo; |
| 26 | +import org.springframework.test.context.ContextConfiguration; |
| 27 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 28 | +import org.springframework.util.ClassUtils; |
| 29 | +import org.springframework.util.ObjectUtils; |
| 30 | + |
| 31 | +import com.mongodb.BasicDBObject; |
| 32 | +import com.mongodb.DBCollection; |
| 33 | +import com.mongodb.DBObject; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author Christoph Strobl |
| 37 | + */ |
| 38 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 39 | +@ContextConfiguration("classpath:infrastructure.xml") |
| 40 | +public class DefaultIndexOperationTests { |
| 41 | + |
| 42 | + private static final String COLLECTION_NAME = ClassUtils.getShortNameAsProperty(DefaultIndexOperationTests.class); |
| 43 | + private static final DBObject GEO_SPHERE_2D = new BasicDBObject("loaction", "2dsphere"); |
| 44 | + |
| 45 | + @Autowired MongoTemplate template; |
| 46 | + private DefaultIndexOperations indexOps; |
| 47 | + private DBCollection collection; |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setUp() { |
| 51 | + |
| 52 | + this.collection = this.template.getDb().getCollection(COLLECTION_NAME); |
| 53 | + dropAllIndexes(); |
| 54 | + |
| 55 | + indexOps = new DefaultIndexOperations(template, COLLECTION_NAME); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @see DATAMONGO-1008 |
| 60 | + */ |
| 61 | + @Test |
| 62 | + public void getIndexInfoShouldBeAbleToRead2dsphereIndex() { |
| 63 | + |
| 64 | + createIndex(GEO_SPHERE_2D); |
| 65 | + |
| 66 | + IndexInfo info = findAndReturnIndexInfo(GEO_SPHERE_2D); |
| 67 | + assertThat(info.getIndexFields().get(0).isGeo(), is(true)); |
| 68 | + } |
| 69 | + |
| 70 | + private IndexInfo findAndReturnIndexInfo(DBObject keys) { |
| 71 | + return findAndReturnIndexInfo(indexOps.getIndexInfo(), keys); |
| 72 | + } |
| 73 | + |
| 74 | + @SuppressWarnings("deprecation") |
| 75 | + private IndexInfo findAndReturnIndexInfo(Iterable<IndexInfo> candidates, DBObject keys) { |
| 76 | + return findAndReturnIndexInfo(candidates, DBCollection.genIndexName(keys)); |
| 77 | + } |
| 78 | + |
| 79 | + private IndexInfo findAndReturnIndexInfo(Iterable<IndexInfo> candidates, String name) { |
| 80 | + |
| 81 | + for (IndexInfo info : candidates) { |
| 82 | + if (ObjectUtils.nullSafeEquals(name, info.getName())) { |
| 83 | + return info; |
| 84 | + } |
| 85 | + } |
| 86 | + throw new AssertionError(String.format("Index with %s was not found", name)); |
| 87 | + } |
| 88 | + |
| 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 | + |
| 97 | +} |
0 commit comments