Skip to content

Commit 4c5a82a

Browse files
committed
fixed a bug due to improper ordering of instanceof conditionals. Since CodeWScope extends Code, it has to be checked before Code
1 parent fabd379 commit 4c5a82a

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

driver-compat/src/main/com/mongodb/Bytes.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,15 @@ public static byte getType(final Object o) {
233233
return OBJECT;
234234
}
235235

236-
if (o instanceof Code) {
237-
return CODE;
238-
}
239-
240236
if (o instanceof CodeWScope) {
241237
return CODE_W_SCOPE;
242238
}
243239

240+
if (o instanceof Code) {
241+
return CODE;
242+
}
243+
244244
return -1;
245245
}
246246

247-
static final ObjectId COLLECTION_REF_ID = new ObjectId(-1, -1, -1);
248247
}

driver/src/test/org/mongodb/MongoCursorTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ public void setUp() {
4040
}
4141
}
4242

43+
@After
44+
public void tearDown() {
45+
super.tearDown();
46+
if (cursor != null) {
47+
cursor.close();
48+
}
49+
}
50+
4351
@Test
4452
public void testNormalLoopWithGetMore() {
4553
cursor = collection.sort(new SortCriteriaDocument("_id", 1)).batchSize(2).all();
@@ -92,11 +100,4 @@ public void shouldNotBeAbleToCallHasNextAfterClose() {
92100
cursor.close();
93101
cursor.hasNext();
94102
}
95-
96-
@After
97-
public void tearDown() {
98-
if (cursor != null) {
99-
cursor.close();
100-
}
101-
}
102103
}

driver/src/test/org/mongodb/MongoSaveTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.mongodb;
1818

1919
import org.bson.types.Document;
20+
import org.bson.types.ObjectId;
2021
import org.junit.Test;
2122

2223
import static org.hamcrest.core.Is.is;
@@ -45,4 +46,15 @@ public void shouldUpsertIfAbsent() {
4546
collection.save(document);
4647
assertThat("Did not upsert the document", collection.one(), is(document));
4748
}
49+
50+
@Test
51+
public void shouldUpsertWithNewObjectId() {
52+
final Document document = new Document("_id", new ObjectId()).append("x", 1);
53+
collection.save(document);
54+
assertThat("Did not replace the document", collection.one(), is(document));
55+
56+
document.put("y", 2);
57+
collection.save(document);
58+
assertThat("Did not replace the document", collection.one(), is(document));
59+
}
4860
}

0 commit comments

Comments
 (0)