Skip to content

Commit 443661f

Browse files
authored
Merge pull request #268 from ipavkovic/master
add support for different DuplicateKeyException behavior between mong…
2 parents a7f51b0 + 7825f6c commit 443661f

File tree

6 files changed

+73
-22
lines changed

6 files changed

+73
-22
lines changed

src/main/java/com/github/fakemongo/Fongo.java

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class Fongo implements OperationExecutor {
4747
private final static Logger LOG = LoggerFactory.getLogger(Fongo.class);
4848

4949
public static final ServerVersion V3_2_SERVER_VERSION = new ServerVersion(3, 2);
50+
public static final ServerVersion V3_3_SERVER_VERSION = new ServerVersion(3, 3);
5051
public static final ServerVersion V3_SERVER_VERSION = new ServerVersion(3, 0);
5152
public static final ServerVersion OLD_SERVER_VERSION = new ServerVersion(0, 0);
5253
public static final ServerVersion DEFAULT_SERVER_VERSION = V3_2_SERVER_VERSION;

src/main/java/com/mongodb/FongoDB.java

+23
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.github.fakemongo.impl.Aggregator;
2222
import com.github.fakemongo.impl.ExpressionParser;
2323
import com.github.fakemongo.impl.geo.GeoUtil;
24+
import com.mongodb.connection.ServerVersion;
2425
import com.mongodb.util.JSON;
2526
import com.vividsolutions.jts.geom.Coordinate;
2627

@@ -442,6 +443,28 @@ public WriteConcernException writeConcernException(int code, String err) {
442443
return new WriteConcernException(result, fongo.getServerAddress(), WriteConcernResult.unacknowledged());
443444
}
444445

446+
public WriteConcernException duplicateKeyException(int code, String err, DBObject oldObject) {
447+
if(serverIsAtLeastThreeDotThree(this.fongo)) {
448+
throw duplicateKeyException(code, err);
449+
} else {
450+
if (oldObject == null) {
451+
// insert
452+
throw duplicateKeyException(code, err);
453+
} else {
454+
// update (MongoDB throws a different exception in case of an update, see issue #200)
455+
throw mongoCommandException(code, err);
456+
}
457+
}
458+
}
459+
460+
private boolean serverIsAtLeastThreeDotThree(Fongo fongo) {
461+
return serverIsAtLeastVersion(fongo, Fongo.V3_3_SERVER_VERSION);
462+
}
463+
464+
static boolean serverIsAtLeastVersion(Fongo fongo, final ServerVersion serverVersion) {
465+
return fongo.getServerVersion().compareTo(serverVersion) >= 0;
466+
}
467+
445468
public WriteConcernException duplicateKeyException(int code, String err) {
446469
final BsonDocument result = bsonResultNotOk(code, err);
447470
return new DuplicateKeyException(result, fongo.getServerAddress(), WriteConcernResult.unacknowledged());

src/main/java/com/mongodb/FongoDBCollection.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -1368,14 +1368,7 @@ private void addToIndexes(DBObject object, DBObject oldObject, WriteConcern conc
13681368
if (!error.isEmpty()) {
13691369
// TODO formatting : E11000 duplicate key error index: test.zip.$city_1_state_1_pop_1 dup key: { : "BARRE", : "MA", : 4546.0 }
13701370
if (enforceDuplicates(concern)) {
1371-
String err = "E11000 duplicate key error index: " + this.getFullName() + "." + index.getName() + " dup key : {" + error + " }";
1372-
if (oldObject == null) {
1373-
// insert
1374-
throw fongoDb.duplicateKeyException(11000, err);
1375-
} else {
1376-
// update (MongoDB throws a different exception in case of an update, see issue #200)
1377-
throw fongoDb.mongoCommandException(11000, err);
1378-
}
1371+
throw fongoDb.duplicateKeyException(11000, "E11000 duplicate key error index: " + this.getFullName() + "." + index.getName() + " dup key : {" + error + " }", oldObject);
13791372
}
13801373
return; // silently ignore.
13811374
}

src/test/java/com/github/fakemongo/AbstractFongoV3Test.java

+1-14
Original file line numberDiff line numberDiff line change
@@ -389,19 +389,6 @@ public void deleteOne_remove_must_throw_exception_with_unacknowledged() {
389389
deleteResult.getDeletedCount();
390390
}
391391

392-
@Test
393-
public void insertOneWithDuplicateValueForUniqueColumn_throwsMongoCommandException() {
394-
// Given
395-
MongoCollection collection = newCollection();
396-
collection.createIndex(new Document("a", 1), new IndexOptions().name("a").unique(true));
397-
collection.insertOne(new Document("_id", 1).append("a", 1));
398-
collection.insertOne(new Document("_id", 2).append("a", 2));
399-
400-
// When
401-
exception.expect(MongoCommandException.class);
402-
collection.findOneAndUpdate(docId(2), new Document("$set", new Document("a", 1)));
403-
}
404-
405392
@Test
406393
public void deleteMany_remove_many() {
407394
// Given
@@ -1186,7 +1173,7 @@ public void shouldFindOneDocumentWithoutTheOptionalFieldWithIndex() {
11861173
Assertions.assertThat(collection.count()).isEqualTo(1);
11871174
}
11881175

1189-
private Document docId(final Object value) {
1176+
protected static Document docId(final Object value) {
11901177
return new Document("_id", value);
11911178
}
11921179

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
package com.github.fakemongo;
22

3+
import com.mongodb.MongoCommandException;
4+
import com.mongodb.client.MongoCollection;
5+
import com.mongodb.client.model.IndexOptions;
36
import com.mongodb.connection.ServerVersion;
7+
import org.bson.Document;
8+
import org.junit.Test;
49

510
public class FongoV3ServerVersion3_2Test extends AbstractFongoV3Test {
611

712
@Override
813
public ServerVersion serverVersion() {
914
return Fongo.V3_2_SERVER_VERSION;
1015
}
16+
17+
@Test
18+
public void insertOneWithDuplicateValueForUniqueColumn_throwsMongoCommandException() {
19+
// Given
20+
MongoCollection collection = newCollection();
21+
collection.createIndex(new Document("a", 1), new IndexOptions().name("a").unique(true));
22+
collection.insertOne(new Document("_id", 1).append("a", 1));
23+
collection.insertOne(new Document("_id", 2).append("a", 2));
24+
25+
// When
26+
exception.expect(MongoCommandException.class);
27+
collection.findOneAndUpdate(docId(2), new Document("$set", new Document("a", 1)));
28+
}
1129
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.fakemongo;
2+
3+
import com.mongodb.DuplicateKeyException;
4+
import com.mongodb.client.MongoCollection;
5+
import com.mongodb.client.model.IndexOptions;
6+
import com.mongodb.connection.ServerVersion;
7+
import org.bson.Document;
8+
import org.junit.Test;
9+
10+
public class FongoV3ServerVersion3_3Test extends AbstractFongoV3Test {
11+
12+
@Override
13+
public ServerVersion serverVersion() {
14+
return Fongo.V3_3_SERVER_VERSION;
15+
}
16+
17+
@Test
18+
public void insertOneWithDuplicateValueForUniqueColumn_throwsDuplicateKeyException() {
19+
// Given
20+
MongoCollection collection = newCollection();
21+
collection.createIndex(new Document("a", 1), new IndexOptions().name("a").unique(true));
22+
collection.insertOne(new Document("_id", 1).append("a", 1));
23+
collection.insertOne(new Document("_id", 2).append("a", 2));
24+
25+
// When
26+
exception.expect(DuplicateKeyException.class);
27+
collection.findOneAndUpdate(docId(2), new Document("$set", new Document("a", 1)));
28+
}
29+
}

0 commit comments

Comments
 (0)