Skip to content

Loading…

Add support for Java enums, include tests #71

Closed
wants to merge 5 commits into from

3 participants

@dgottlieb
mongodb member

I like the idea that Enum support is only exposed via the ReflectionDBObject. This patch looks good to me, though I wish the OnlineTest didn't stop short on actually testing the values that were read back out from the DB.

@ymind

I'm so sorry! This is my mistake, I had fixed these problems, please check it.
I'm a newbie for java, thanks for your guidance!

ymind added some commits
@ymind ymind Added generic type covariant support 94c4ac3
@ymind ymind Revert "add more assert in Online test"
This reverts commit 739712b24f303f9d416db48b00a3d589d639de22.
2058014
@trishagee

Hi, thanks for the pull request. Unfortunately, supporting Java enums is not as straightforward as it might at first seem, see https://jira.mongodb.org/browse/JAVA-268 for more details. We're going to look into implementing it after the 3.0 release - feel free to check out the 3.0 branch to see how you might implement it there.

Trisha

@trishagee trishagee closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 18, 2012
  1. @ymind
Commits on Apr 19, 2012
  1. @ymind
  2. @ymind

    add more assert in Online test

    ymind committed
  3. @ymind
Commits on Dec 23, 2012
  1. @ymind

    Revert "add more assert in Online test"

    ymind committed
    This reverts commit 739712b24f303f9d416db48b00a3d589d639de22.
This page is out of date. Refresh to see the latest.
View
4 src/main/com/mongodb/DBCollection.java
@@ -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() );
}
@@ -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 );
}
View
14 src/main/com/mongodb/ReflectionDBObject.java
@@ -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
*/
@@ -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 );
View
32 src/main/com/mongodb/util/JSON.java
@@ -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
@@ -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;
View
49 src/main/org/bson/BasicBSONEncoder.java
@@ -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
@@ -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 )
View
129 src/test/com/mongodb/EnumTest.java
@@ -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;
+ }
+ }
+}
+
Something went wrong with that request. Please try again.