15
15
*/
16
16
package org .springframework .data .mongodb .core ;
17
17
18
- import static org .hamcrest .core . Is .*;
18
+ import static org .hamcrest .CoreMatchers .*;
19
19
import static org .junit .Assert .*;
20
20
21
21
import org .junit .Before ;
25
25
import org .springframework .data .mongodb .core .index .IndexInfo ;
26
26
import org .springframework .test .context .ContextConfiguration ;
27
27
import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
28
- import org .springframework .util .ClassUtils ;
29
28
import org .springframework .util .ObjectUtils ;
30
29
31
30
import com .mongodb .BasicDBObject ;
32
31
import com .mongodb .DBCollection ;
33
32
import com .mongodb .DBObject ;
34
33
35
34
/**
35
+ * Integration tests for {@link DefaultIndexOperations}.
36
+ *
36
37
* @author Christoph Strobl
38
+ * @author Oliver Gierke
37
39
*/
38
40
@ RunWith (SpringJUnit4ClassRunner .class )
39
41
@ ContextConfiguration ("classpath:infrastructure.xml" )
40
- public class DefaultIndexOperationTests {
42
+ public class DefaultIndexOperationsIntegrationTests {
41
43
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" );
44
45
45
46
@ Autowired MongoTemplate template ;
46
- private DefaultIndexOperations indexOps ;
47
- private DBCollection collection ;
47
+ DefaultIndexOperations indexOps ;
48
+ DBCollection collection ;
48
49
49
50
@ Before
50
51
public void setUp () {
51
52
52
- this .collection = this .template .getDb ().getCollection (COLLECTION_NAME );
53
- dropAllIndexes ();
53
+ String collectionName = this .template .getCollectionName (DefaultIndexOperationsIntegrationTestsSample .class );
54
54
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 );
56
59
}
57
60
58
61
/**
@@ -61,7 +64,7 @@ public void setUp() {
61
64
@ Test
62
65
public void getIndexInfoShouldBeAbleToRead2dsphereIndex () {
63
66
64
- createIndex (GEO_SPHERE_2D );
67
+ collection . createIndex (GEO_SPHERE_2D );
65
68
66
69
IndexInfo info = findAndReturnIndexInfo (GEO_SPHERE_2D );
67
70
assertThat (info .getIndexFields ().get (0 ).isGeo (), is (true ));
@@ -72,11 +75,11 @@ private IndexInfo findAndReturnIndexInfo(DBObject keys) {
72
75
}
73
76
74
77
@ SuppressWarnings ("deprecation" )
75
- private IndexInfo findAndReturnIndexInfo (Iterable <IndexInfo > candidates , DBObject keys ) {
78
+ private static IndexInfo findAndReturnIndexInfo (Iterable <IndexInfo > candidates , DBObject keys ) {
76
79
return findAndReturnIndexInfo (candidates , DBCollection .genIndexName (keys ));
77
80
}
78
81
79
- private IndexInfo findAndReturnIndexInfo (Iterable <IndexInfo > candidates , String name ) {
82
+ private static IndexInfo findAndReturnIndexInfo (Iterable <IndexInfo > candidates , String name ) {
80
83
81
84
for (IndexInfo info : candidates ) {
82
85
if (ObjectUtils .nullSafeEquals (name , info .getName ())) {
@@ -86,12 +89,5 @@ private IndexInfo findAndReturnIndexInfo(Iterable<IndexInfo> candidates, String
86
89
throw new AssertionError (String .format ("Index with %s was not found" , name ));
87
90
}
88
91
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 {}
97
93
}
0 commit comments