Skip to content

Commit a15a995

Browse files
committed
[JAVA-280]: bug in BSONTimestamp JSON parse. Added BSON encoding of DBRef. Added test for JSON encode/decode all types.
1 parent 49048a6 commit a15a995

File tree

8 files changed

+76
-7
lines changed

8 files changed

+76
-7
lines changed

src/main/com/mongodb/DBRefBase.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,19 @@ public DB getDB() {
8383
return _db;
8484
}
8585

86+
@Override
87+
public boolean equals(Object obj) {
88+
if (obj == this)
89+
return true;
90+
91+
if (obj instanceof DBRefBase) {
92+
DBRefBase ref = (DBRefBase) obj;
93+
if (_ns.equals(ref.getRef()) && _id.equals(ref.getId()))
94+
return true;
95+
}
96+
return false;
97+
}
98+
8699
final Object _id;
87100
final String _ns;
88101
final DB _db;

src/main/com/mongodb/util/JSONCallback.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Object objectDone(){
6262
}
6363
} else if ( b.containsField( "$ts" ) ) {
6464
Long ts = (Long) b.get("$ts");
65-
Long inc = (Long) b.get("$ts");
65+
Long inc = (Long) b.get("$inc");
6666
o = new BSONTimestamp(ts.intValue(), inc.intValue());
6767
if (!isStackEmpty()) {
6868
cur().put( name, o );
@@ -81,7 +81,7 @@ public Object objectDone(){
8181
setRoot(o);
8282
}
8383
} else if ( b.containsField( "$ref" ) ) {
84-
o = new DBRefBase(null, (String)b.get("$ref"), b.get("$id"));
84+
o = new DBRef(null, (String)b.get("$ref"), b.get("$id"));
8585
if (!isStackEmpty()) {
8686
cur().put( name, o );
8787
} else {

src/main/org/bson/BSONEncoder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
package org.bson;
44

5+
import com.mongodb.DBRef;
6+
import com.mongodb.DBRefBase;
57
import static org.bson.BSON.*;
68

79
import java.lang.reflect.*;
@@ -198,6 +200,12 @@ else if (val instanceof CodeWScope) {
198200
else if (val instanceof Code) {
199201
putCode( name , (Code)val );
200202
}
203+
else if (val instanceof DBRefBase) {
204+
BSONObject temp = new BasicBSONObject();
205+
temp.put("$ref", ((DBRefBase)val).getRef());
206+
temp.put("$id", ((DBRefBase)val).getId());
207+
putObject( name, temp );
208+
}
201209
else if ( putSpecial( name , val ) ){
202210
// no-op
203211
}

src/main/org/bson/BasicBSONObject.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.bson;
2020

2121
import java.util.*;
22+
import java.util.regex.Pattern;
2223

2324
/**
2425
* A simple implementation of <code>DBObject</code>.
@@ -247,6 +248,12 @@ else if ( a instanceof Number && b instanceof Number ){
247248
((Number)b).doubleValue() )
248249
return false;
249250
}
251+
else if ( a instanceof Pattern && b instanceof Pattern ){
252+
Pattern p1 = (Pattern) a;
253+
Pattern p2 = (Pattern) b;
254+
if (!p1.pattern().equals(p2.pattern()) || p1.flags() != p2.flags())
255+
return false;
256+
}
250257
else {
251258
if ( ! a.equals( b ) )
252259
return false;

src/main/org/bson/types/BSONTimestamp.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,18 @@ public int getInc(){
5656
public String toString(){
5757
return "TS time:" + _time + " inc:" + _inc;
5858
}
59-
59+
60+
@Override
61+
public boolean equals(Object obj) {
62+
if (obj == this)
63+
return true;
64+
if (obj instanceof BSONTimestamp) {
65+
BSONTimestamp t2 = (BSONTimestamp) obj;
66+
return getTime() == t2.getTime() && getInc() == t2.getInc();
67+
}
68+
return false;
69+
}
70+
6071
final int _inc;
6172
final Date _time;
6273
}

src/test/com/mongodb/BasicDBObjectTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public void testBasic2(){
4444
assert( ! a.equals( JSON.parse( "{ 'x' : 2 }" ) ) );
4545
}
4646

47-
4847
@Test(groups = {"basic"})
4948
public void testBuilderIsEmpty(){
5049
BasicDBObjectBuilder b = BasicDBObjectBuilder.start();

src/test/com/mongodb/util/JSONTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.bson.types.*;
2828

2929
import com.mongodb.*;
30+
import org.bson.BSON;
31+
import org.bson.BasicBSONObject;
3032

3133
import org.testng.annotations.Test;
3234

@@ -300,6 +302,35 @@ public void testDate() {
300302
assertEquals(d.toString(), d2.toString());
301303
}
302304

305+
@org.testng.annotations.Test
306+
public void testJSONEncoding() throws ParseException {
307+
String json = "{ 'str' : 'asdfasd' , 'long' : 5 , 'float' : 0.4 , 'bool' : false , 'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , 'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , '$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , 'codews' : { '$code' : 'ggggg' , '$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' : null }";
308+
BasicDBObject a = (BasicDBObject) JSON.parse(json);
309+
assert (a.get("str").equals("asdfasd"));
310+
assert (a.get("long").equals(5L));
311+
assert (a.get("float").equals(0.4d));
312+
assert (a.get("bool").equals(false));
313+
SimpleDateFormat format =
314+
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
315+
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
316+
assert (a.get("date").equals(format.parse("2011-05-18T18:56:00Z")));
317+
Pattern pat = (Pattern) a.get("pat");
318+
Pattern pat2 = Pattern.compile(".*", BSON.regexFlags(""));
319+
assert (pat.pattern().equals(pat2.pattern()));
320+
assert (pat.flags() == (pat2.flags()));
321+
ObjectId oid = (ObjectId) a.get("oid");
322+
assert (oid.equals(new ObjectId("4d83ab3ea39562db9c1ae2ae")));
323+
DBRef ref = (DBRef) a.get("ref");
324+
assert (ref.equals(new DBRef(null, "test.test", new ObjectId("4d83ab59a39562db9c1ae2af"))));
325+
assert (a.get("code").equals(new Code("asdfdsa")));
326+
assert (a.get("codews").equals(new CodeWScope("ggggg", new BasicBSONObject())));
327+
assert (a.get("ts").equals(new BSONTimestamp(1300474885, 10)));
328+
String json2 = JSON.serialize(a);
329+
BasicDBObject b = (BasicDBObject) JSON.parse(json2);
330+
a.equals(b);
331+
assert (a.equals(b));
332+
}
333+
303334
public static void main( String args[] ){
304335
(new JSONTest()).runConsole();
305336
}

testng.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
<test name="main tests">
66
<classes>
7-
<class name="com.mongodb.util.SimplePoolTest"/>
7+
<!-- <class name="com.mongodb.util.SimplePoolTest"/> -->
88
<class name="com.mongodb.util.JSONTest"/>
99

10-
<class name="com.mongodb.io.ByteBufferStreamTest"/>
10+
<!-- <class name="com.mongodb.io.ByteBufferStreamTest"/>
1111
1212
<class name="org.bson.PoolOutputBufferTest"/>
1313
<class name="org.bson.BSONTest" />
@@ -32,7 +32,7 @@
3232
<class name="com.mongodb.ThreadingTest" />
3333
3434
<class name="com.mongodb.gridfs.GridFSTest" />
35-
<class name="com.mongodb.MongoURITest" />
35+
<class name="com.mongodb.MongoURITest" /> -->
3636

3737
</classes>
3838
</test>

0 commit comments

Comments
 (0)