Skip to content

Commit 77e1016

Browse files
DATAMONGO-367 - Nested @indexed should not trigger creation of separate collection.
The issue has been solved along with DATAMONGO-888 (Pull Request: #162). We have created additional tests to explicitly check it has truly been fixed.
1 parent 8c04b76 commit 77e1016

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

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

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
20+
import static org.mockito.Mockito.*;
2021

2122
import java.util.Collections;
2223
import java.util.Date;
@@ -27,7 +28,6 @@
2728
import org.junit.runner.RunWith;
2829
import org.mockito.ArgumentCaptor;
2930
import org.mockito.Mock;
30-
import org.mockito.Mockito;
3131
import org.mockito.runners.MockitoJUnitRunner;
3232
import org.springframework.context.ApplicationContext;
3333
import org.springframework.data.geo.Point;
@@ -72,10 +72,10 @@ public void setUp() {
7272
optionsCaptor = ArgumentCaptor.forClass(DBObject.class);
7373
collectionCaptor = ArgumentCaptor.forClass(String.class);
7474

75-
Mockito.when(factory.getDb()).thenReturn(db);
76-
Mockito.when(db.getCollection(collectionCaptor.capture())).thenReturn(collection);
75+
when(factory.getDb()).thenReturn(db);
76+
when(db.getCollection(collectionCaptor.capture())).thenReturn(collection);
7777

78-
Mockito.doNothing().when(collection).createIndex(keysCaptor.capture(), optionsCaptor.capture());
78+
doNothing().when(collection).createIndex(keysCaptor.capture(), optionsCaptor.capture());
7979
}
8080

8181
@Test
@@ -106,7 +106,7 @@ public void doesNotCreateIndexForEntityComingFromDifferentMappingContext() {
106106

107107
creator.onApplicationEvent(event);
108108

109-
Mockito.verifyZeroInteractions(collection);
109+
verifyZeroInteractions(collection);
110110
}
111111

112112
/**
@@ -181,6 +181,36 @@ public void autoGeneratedIndexNameShouldGenerateNoName() {
181181
assertThat(optionsCaptor.getValue(), is(new BasicDBObjectBuilder().get()));
182182
}
183183

184+
/**
185+
* @see DATAMONGO-367
186+
*/
187+
@Test
188+
public void indexCreationShouldNotCreateNewCollectionForNestedGeoSpatialIndexStructures() {
189+
190+
MongoMappingContext mappingContext = prepareMappingContext(Wrapper.class);
191+
new MongoPersistentEntityIndexCreator(mappingContext, factory);
192+
193+
ArgumentCaptor<String> collectionNameCapturer = ArgumentCaptor.forClass(String.class);
194+
195+
verify(db, times(1)).getCollection(collectionNameCapturer.capture());
196+
assertThat(collectionNameCapturer.getValue(), equalTo("wrapper"));
197+
}
198+
199+
/**
200+
* @see DATAMONGO-367
201+
*/
202+
@Test
203+
public void indexCreationShouldNotCreateNewCollectionForNestedIndexStructures() {
204+
205+
MongoMappingContext mappingContext = prepareMappingContext(IndexedDocumentWrapper.class);
206+
new MongoPersistentEntityIndexCreator(mappingContext, factory);
207+
208+
ArgumentCaptor<String> collectionNameCapturer = ArgumentCaptor.forClass(String.class);
209+
210+
verify(db, times(1)).getCollection(collectionNameCapturer.capture());
211+
assertThat(collectionNameCapturer.getValue(), equalTo("indexedDocumentWrapper"));
212+
}
213+
184214
private static MongoMappingContext prepareMappingContext(Class<?> type) {
185215

186216
MongoMappingContext mappingContext = new MongoMappingContext();
@@ -233,6 +263,17 @@ static class Address {
233263
@GeoSpatialIndexed Point location;
234264
}
235265

266+
@Document
267+
static class IndexedDocumentWrapper {
268+
269+
IndexedDocument indexedDocument;
270+
}
271+
272+
static class IndexedDocument {
273+
274+
@Indexed String indexedValue;
275+
}
276+
236277
@Document
237278
class EntityWithGeneratedIndexName {
238279

0 commit comments

Comments
 (0)