Skip to content

Commit 04237c7

Browse files
author
Justin Lee
committed
resolves JAVA-1141 DBCollection.createIndex(DBObject, DBObject) does not generate the index name if none is provided
1 parent 422ce22 commit 04237c7

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

src/main/com/mongodb/DBCollectionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public void createIndex(final DBObject keys, final DBObject options, DBEncoder e
342342
DBPort port = db.getConnector().getPrimaryPort();
343343

344344
try {
345-
DBObject index = new BasicDBObject();
345+
DBObject index = defaultOptions(keys);
346346
index.putAll(options);
347347
index.put("key", keys);
348348

src/test/com/mongodb/DBCollectionTest.java

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ public void testDropIndex(){
181181
c.dropIndexes();
182182
assertEquals( 1 , c.getIndexInfo().size() );
183183

184+
c.createIndex( new BasicDBObject( "x" , 1 ) );
185+
assertEquals( 2 , c.getIndexInfo().size() );
186+
187+
c.dropIndexes();
188+
assertEquals( 1 , c.getIndexInfo().size() );
189+
184190
c.ensureIndex( new BasicDBObject( "x" , 1 ) );
185191
assertEquals( 2 , c.getIndexInfo().size() );
186192

@@ -190,6 +196,18 @@ public void testDropIndex(){
190196
c.dropIndex( new BasicDBObject( "x" , 1 ) );
191197
assertEquals( 2 , c.getIndexInfo().size() );
192198

199+
c.dropIndexes();
200+
assertEquals( 1 , c.getIndexInfo().size() );
201+
202+
c.createIndex( new BasicDBObject( "x" , 1 ) );
203+
assertEquals( 2 , c.getIndexInfo().size() );
204+
205+
c.createIndex( new BasicDBObject( "y" , 1 ) );
206+
assertEquals( 3 , c.getIndexInfo().size() );
207+
208+
c.dropIndex( new BasicDBObject( "x" , 1 ) );
209+
assertEquals( 2 , c.getIndexInfo().size() );
210+
c.dropIndexes();
193211
}
194212

195213
@Test
@@ -246,7 +264,15 @@ public void testEnsureIndex(){
246264

247265
c.ensureIndex( new BasicDBObject( "x" , 1 ) , new BasicDBObject( "unique" , true ) );
248266
assertEquals( 2 , c.getIndexInfo().size() );
249-
assertEquals( Boolean.TRUE , c.getIndexInfo().get(1).get( "unique" ) );
267+
DBObject indexInfo = c.getIndexInfo().get(1);
268+
assertEquals("x_1", indexInfo.get("name"));
269+
270+
c.drop();
271+
272+
c.createIndex(new BasicDBObject("x", 1), new BasicDBObject("unique", true));
273+
indexInfo = c.getIndexInfo().get(1);
274+
assertEquals( Boolean.TRUE , indexInfo.get("unique") );
275+
assertEquals("x_1", indexInfo.get("name"));
250276
}
251277

252278
@Test
@@ -259,22 +285,47 @@ public void testEnsureNestedIndex(){
259285
assertEquals( 1 , c.getIndexInfo().size() );
260286
c.ensureIndex( new BasicDBObject("x.y", 1), "nestedIdx1", false);
261287
assertEquals( 2 , c.getIndexInfo().size() );
288+
assertEquals( "nestedIdx1" , c.getIndexInfo().get(1).get("name") );
289+
290+
c.drop();
291+
c.createIndex(new BasicDBObject("x.y", 1), new BasicDBObject("name", "nestedIdx1").append("unique", false));
292+
assertEquals(2, c.getIndexInfo().size());
293+
assertEquals( "nestedIdx1" , c.getIndexInfo().get(1).get("name") );
262294
}
263295

264296

265-
@Test(expected = MongoException.DuplicateKey.class)
266-
public void testIndexExceptions(){
297+
@Test(expected = DuplicateKeyException.class)
298+
public void testEnsureIndexExceptions(){
267299
collection.insert(new BasicDBObject("x", 1));
268300
collection.insert(new BasicDBObject("x", 1));
269301

270302
collection.ensureIndex(new BasicDBObject("y", 1));
271303
collection.resetIndexCache();
272-
collection.ensureIndex(new BasicDBObject("y", 1)); // make sure this doesn't throw
304+
try {
305+
collection.ensureIndex(new BasicDBObject("y", 1)); // make sure this doesn't throw
306+
} catch (Exception e) {
307+
fail("Trying to create an existing index should not fail.");
308+
}
273309
collection.resetIndexCache();
274310

275311
collection.ensureIndex(new BasicDBObject("x", 1), new BasicDBObject("unique", true));
276312
}
277313

314+
@Test(expected = DuplicateKeyException.class)
315+
public void testCreateIndexExceptions(){
316+
collection.insert(new BasicDBObject("x", 1));
317+
collection.insert(new BasicDBObject("x", 1));
318+
319+
collection.createIndex(new BasicDBObject("y", 1));
320+
try {
321+
collection.createIndex(new BasicDBObject("y", 1)); // make sure this doesn't throw
322+
} catch (Exception e) {
323+
fail("Trying to create an existing index should not fail.");
324+
}
325+
326+
collection.createIndex(new BasicDBObject("x", 1), new BasicDBObject("unique", true));
327+
}
328+
278329
@Test
279330
public void testMultiInsertNoContinue() {
280331
collection.setWriteConcern(WriteConcern.NORMAL);

0 commit comments

Comments
 (0)