Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mongodb/mongo-java-driver
Browse files Browse the repository at this point in the history
  • Loading branch information
jyemin committed Apr 24, 2012
2 parents 0cca6bf + 88aaf55 commit 8849ef2
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 13 deletions.
64 changes: 64 additions & 0 deletions src/main/org/bson/BasicBSONObject.java
Expand Up @@ -144,6 +144,20 @@ public long getLong( String key){
return ((Number)foo).longValue();
}

/**
* Returns the value of a field as an <code>long</code>.
* @param key the field to look for
* @param def the default to return
* @return the field value (or default)
*/
public long getLong( String key , long def ) {
Object foo = get( key );
if ( foo == null )
return def;

return ((Number)foo).longValue();
}

/**
* Returns the value of a field as a <code>double</code>.
*
Expand All @@ -155,6 +169,20 @@ public double getDouble( String key){
return ((Number)foo).doubleValue();
}

/**
* Returns the value of a field as an <code>double</code>.
* @param key the field to look for
* @param def the default to return
* @return the field value (or default)
*/
public double getDouble( String key , double def ) {
Object foo = get( key );
if ( foo == null )
return def;

return ((Number)foo).doubleValue();
}

/** Returns the value of a field as a string
* @param key the field to look up
* @return the value of the field, converted to a string
Expand All @@ -166,6 +194,20 @@ public String getString( String key ){
return foo.toString();
}

/**
* Returns the value of a field as a string
* @param key the field to look up
* @param def the default to return
* @return the value of the field, converted to a string
*/
public String getString( String key, final String def ) {
Object foo = get( key );
if ( foo == null )
return def;

return foo.toString();
}

/** Returns the value of a field as a boolean.
* @param key the field to look up
* @return the value of the field, or false if field does not exist
Expand Down Expand Up @@ -199,6 +241,17 @@ public ObjectId getObjectId( final String field ) {
return (ObjectId) get( field );
}

/**
* Returns the object id or def if not set.
* @param field The field to return
* @param def the default value in case the field is not found
* @return The field object value or def if not set.
*/
public ObjectId getObjectId( final String field, final ObjectId def ) {
final Object foo = get( field );
return (foo != null) ? (ObjectId)foo : def;
}

/**
* Returns the date or null if not set.
* @param field The field to return
Expand All @@ -208,6 +261,17 @@ public Date getDate( final String field ) {
return (Date) get( field );
}

/**
* Returns the date or def if not set.
* @param field The field to return
* @param def the default value in case the field is not found
* @return The field object value or def if not set.
*/
public Date getDate( final String field, final Date def ) {
final Object foo = get( field );
return (foo != null) ? (Date)foo : def;
}

/** Add a key/value pair to this object
* @param key the field name
* @param val the field value
Expand Down
48 changes: 47 additions & 1 deletion src/test/com/mongodb/BasicDBObjectTest.java
Expand Up @@ -23,15 +23,61 @@

import org.testng.annotations.*;

// Java
import java.util.Date;

public class BasicDBObjectTest extends TestCase {

@Test(groups = {"basic"})
public void testGetDate() {
final Date date = new Date();
BasicDBObject doc = new BasicDBObject( "foo" , date);
assert( doc.getDate( "foo" ).equals( date ) );
}

@Test(groups = {"basic"})
public void testGetDateWithDefault() {
final Date date = new Date();
BasicDBObject doc = new BasicDBObject( "foo" , date);
assert( doc.getDate( "foo", new Date() ).equals( date ) );
assert( doc.getDate( "bar", date ).equals( date ) );
}

@Test(groups = {"basic"})
public void testGetObjectId() {
final ObjectId objId = ObjectId.get();

BasicDBObject doc = new BasicDBObject( "foo" , objId);
assert( doc.getObjectId( "foo" ).equals( objId ) );
}

@Test(groups = {"basic"})
public void testGetObjectIdWithDefault() {
final ObjectId objId = ObjectId.get();
BasicDBObject doc = new BasicDBObject( "foo" , objId);
assert( doc.getObjectId( "foo", ObjectId.get() ).equals( objId ) );
assert( doc.getObjectId( "bar", objId ).equals( objId ) );
}

@Test(groups = {"basic"})
public void testGetLongWithDefault() {
final long test = 100;
BasicDBObject doc = new BasicDBObject( "foo" , test);
assert( doc.getLong( "foo", 0l ) == test );
assert( doc.getLong( "bar", 0l ) == 0l );
}

@Test(groups = {"basic"})
public void testGetDoubleWithDefault() {
BasicDBObject doc = new BasicDBObject( "foo" , Double.MAX_VALUE);
assert( doc.getDouble( "foo", (double)0 ) == Double.MAX_VALUE);
assert( doc.getDouble( "bar", Double.MIN_VALUE ) == Double.MIN_VALUE);
}

@Test(groups = {"basic"})
public void testGetStringWithDefault() {
BasicDBObject doc = new BasicDBObject( "foo" , "badmf");
assert( doc.getString( "foo", "ID" ).equals("badmf"));
assert( doc.getString( "bar", "DEFAULT" ).equals("DEFAULT") );
}

@Test(groups = {"basic"})
Expand Down
22 changes: 10 additions & 12 deletions src/test/com/mongodb/QueryBuilderTest.java
Expand Up @@ -28,7 +28,6 @@
import org.testng.annotations.*;

import com.mongodb.QueryBuilder.QueryBuilderException;
import com.mongodb.util.JSON;
import com.mongodb.util.TestCase;

/**
Expand Down Expand Up @@ -262,15 +261,13 @@ public void arrayChainTest() {
@Test
public void nearTest() {
String key = "loc";
DBCollection collection = _testDB.getCollection("geoSpacial-test");
DBCollection collection = _testDB.getCollection("geoSpatial-test");
BasicDBObject geoSpatialIndex = new BasicDBObject();
geoSpatialIndex.put(key, "2d");
collection.ensureIndex(geoSpatialIndex);

BasicDBObject location = new BasicDBObject();
Double[] coordinates = {(double) 50, (double) 30};
location.put(key, coordinates );
saveTestDocument(collection, key, location);
saveTestDocument(collection, key, coordinates);

DBObject queryTrue = QueryBuilder.start(key).near(45, 45).get();
assertTrue(testQuery(collection, queryTrue));
Expand All @@ -287,12 +284,14 @@ public void nearTest() {
queryTrue = QueryBuilder.start(key).withinCenterSphere(50, 30, 0.5).get();
assertTrue(testQuery(collection, queryTrue));

ArrayList<Double[]> points = new ArrayList<Double[]>();
points.add( new Double[] { (double)30, (double)30 });
points.add( new Double[] { (double)70, (double)30 });
points.add( new Double[] { (double)70, (double)30 });
queryTrue = QueryBuilder.start(key).withinPolygon(points).get();
assertTrue(testQuery(collection, queryTrue));
if (serverIsAtLeastVersion(1.9)) {
ArrayList<Double[]> points = new ArrayList<Double[]>();
points.add( new Double[] { (double)30, (double)30 });
points.add( new Double[] { (double)70, (double)30 });
points.add( new Double[] { (double)70, (double)30 });
queryTrue = QueryBuilder.start(key).withinPolygon(points).get();
assertTrue(testQuery(collection, queryTrue));
}

try{
QueryBuilder.start(key).withinPolygon(null);
Expand Down Expand Up @@ -390,5 +389,4 @@ private boolean testQuery(DBCollection collection, DBObject query) {
DBCursor cursor = collection.find(query);
return cursor.hasNext();
}

}

0 comments on commit 8849ef2

Please sign in to comment.