Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Java enums, include tests #71

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/com/mongodb/DBCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public WriteResult insert(WriteConcern concern, DBObject ... arr)
* @throws MongoException
* @dochub insert
*/
public WriteResult insert(List<DBObject> list )
public WriteResult insert(List<? extends DBObject> list )
throws MongoException {
return insert( list, getWriteConcern() );
}
Expand All @@ -143,7 +143,7 @@ public WriteResult insert(List<DBObject> list )
* @throws MongoException
* @dochub insert
*/
public WriteResult insert(List<DBObject> list, WriteConcern concern )
public WriteResult insert(List<? extends DBObject> list, WriteConcern concern )
throws MongoException {
return insert( list.toArray( new DBObject[list.size()] ) , concern );
}
Expand Down
14 changes: 4 additions & 10 deletions src/main/com/mongodb/ReflectionDBObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@

package com.mongodb;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.bson.BSONObject;

import java.lang.reflect.Method;
import java.util.*;

/**
* This class enables to map simple Class fields to a BSON object fields
*/
Expand Down Expand Up @@ -197,7 +191,7 @@ public Object set( ReflectionDBObject t , String name , Object val ){
if ( i == null )
throw new IllegalArgumentException( "no field [" + name + "] on [" + _name + "]" );
try {
return i._setter.invoke( t , val );
return i._setter.invoke(t, i._class.isEnum() ? Enum.valueOf(i._class, val.toString()) : val);
}
catch ( Exception e ){
throw new RuntimeException( "could not invoke setter for [" + name + "] on [" + _name + "]" , e );
Expand Down
32 changes: 13 additions & 19 deletions src/main/com/mongodb/util/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,17 @@

package com.mongodb.util;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.Set;
import java.util.SimpleTimeZone;
import java.util.UUID;
import java.util.regex.Pattern;

import org.bson.BSONCallback;
import org.bson.types.BSONTimestamp;
import org.bson.types.Binary;
import org.bson.types.Code;
import org.bson.types.CodeWScope;
import org.bson.types.MaxKey;
import org.bson.types.MinKey;
import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.Bytes;
import com.mongodb.DBObject;
import com.mongodb.DBRefBase;
import org.bson.BSONCallback;
import org.bson.types.*;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
* Helper methods for JSON serialization and de-serialization
Expand Down Expand Up @@ -103,6 +91,12 @@ public static void serialize( Object o , StringBuilder buf ){
return;
}

if (o instanceof Enum)
{
string(buf, ((Enum) o).name());
return;
}

if ( o instanceof Iterable){

boolean first = true;
Expand Down
49 changes: 8 additions & 41 deletions src/main/org/bson/BasicBSONEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,55 +18,20 @@

package org.bson;

import static org.bson.BSON.ARRAY;
import static org.bson.BSON.BINARY;
import static org.bson.BSON.BOOLEAN;
import static org.bson.BSON.B_BINARY;
import static org.bson.BSON.B_GENERAL;
import static org.bson.BSON.B_UUID;
import static org.bson.BSON.CODE;
import static org.bson.BSON.CODE_W_SCOPE;
import static org.bson.BSON.DATE;
import static org.bson.BSON.EOO;
import static org.bson.BSON.MAXKEY;
import static org.bson.BSON.MINKEY;
import static org.bson.BSON.NULL;
import static org.bson.BSON.NUMBER;
import static org.bson.BSON.NUMBER_INT;
import static org.bson.BSON.NUMBER_LONG;
import static org.bson.BSON.OBJECT;
import static org.bson.BSON.OID;
import static org.bson.BSON.REGEX;
import static org.bson.BSON.STRING;
import static org.bson.BSON.SYMBOL;
import static org.bson.BSON.TIMESTAMP;
import static org.bson.BSON.UNDEFINED;
import static org.bson.BSON.regexFlags;
import com.mongodb.DBRefBase;
import org.bson.io.BasicOutputBuffer;
import org.bson.io.OutputBuffer;
import org.bson.types.*;

import java.lang.reflect.Array;
import java.nio.Buffer;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern;

import org.bson.io.BasicOutputBuffer;
import org.bson.io.OutputBuffer;
import org.bson.types.BSONTimestamp;
import org.bson.types.Binary;
import org.bson.types.Code;
import org.bson.types.CodeWScope;
import org.bson.types.MaxKey;
import org.bson.types.MinKey;
import org.bson.types.ObjectId;
import org.bson.types.Symbol;

import com.mongodb.DBRefBase;
import static org.bson.BSON.*;

/**
* this is meant to be pooled or cached
Expand Down Expand Up @@ -218,6 +183,8 @@ else if ( val instanceof Number )
putNumber(name, (Number)val );
else if ( val instanceof Character )
putString(name, val.toString() );
else if (val instanceof Enum)
putString(name, ((Enum) val).name());
else if ( val instanceof String )
putString(name, val.toString() );
else if ( val instanceof ObjectId )
Expand Down
129 changes: 129 additions & 0 deletions src/test/com/mongodb/EnumTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.mongodb;

// TestNg

import com.mongodb.util.JSON;
import com.mongodb.util.TestCase;
import org.bson.BSON;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;

public final class EnumTest extends TestCase
{
private final DB _db;
private final Foo _foo = new Foo(88, Colors.Blue);

public EnumTest() throws IOException, MongoException
{
super();

cleanupMongo = new Mongo("127.0.0.1");
cleanupDB = "com_mongodb_unittest_EnumTest";
_db = cleanupMongo.getDB(cleanupDB);
}

@Test
public void JSONTest()
{
final String result = "{ \"Color\" : \"Blue\" , \"_id\" : 88}";
String json = JSON.serialize(_foo);

Assert.assertNotNull(json);

Object obj = JSON.parse(json);

Assert.assertNotNull(obj);
Assert.assertEquals(json, result);
Assert.assertEquals(obj.toString(), result);
}

@Test
public void BSONTest()
{
byte[] b = BSON.encode(_foo);

Assert.assertNotNull(b);

BSONObject obj = BSON.decode(b);

Assert.assertNotNull(obj);
Assert.assertTrue(obj instanceof BasicBSONObject);
Assert.assertEquals(obj.get("Color").toString(), "Blue");
}

@Test
public void OnlineTest()
{
DBCollection c = _db.getCollection("Foo");
c.setObjectClass(Foo.class);
c.save(_foo);

BasicDBObject dbRef = new BasicDBObject();
dbRef.put("_id", 88);

DBObject dbObject = c.findOne(dbRef);

Assert.assertNotNull(dbObject);
Assert.assertTrue(dbObject instanceof Foo);

Foo foo = (Foo) dbObject;
Assert.assertNotNull(foo);
}

public static final class Foo extends ReflectionDBObject
{
private Colors _color;

public Foo()
{
this(0, Colors.Red);
}

public Foo(int id, Colors colors)
{
this.set_id(id);
this.set_color(colors);
}

public Colors get_color()
{
return _color;
}

public void set_color(Colors colors)
{
this._color = colors;
}
}

public enum Colors
{
Red(0xFF0000, "red one"),
Green(0x00FF00, "green one"),
Blue(0x0000FF, "blue one");

private int _value;
private String _description;

private Colors(int value, String description)
{
this._value = value;
this._description = description;
}

public int get_value()
{
return _value;
}

public String get_description()
{
return _description;
}
}
}