Permalink
Browse files

Merge pull request #131 from olim7t/issue130

Issue #130: compatibility issue with GridFS.
  • Loading branch information...
2 parents b71cfe8 + 720d031 commit 0d27ba3ebfd065e7bf4ad323ee21059b762b512e @bguerout committed May 14, 2013
@@ -0,0 +1,19 @@
+package org.jongo;
+
+import com.mongodb.DBObject;
+
+public class RawResultHandler<T extends DBObject> implements ResultHandler<T> {
+ public static <T extends DBObject> RawResultHandler<T> asRaw(Class<T> clazz) {
+ return new RawResultHandler<T>(clazz);
+ }
+
+ private final Class<T> clazz;
+
+ public RawResultHandler(Class<T> clazz) {
+ this.clazz = clazz;
+ }
+
+ public T map(DBObject result) {
+ return clazz.cast(result);
+ }
+}
@@ -17,6 +17,7 @@
package org.jongo.bson;
import com.mongodb.*;
+import com.mongodb.gridfs.GridFSDBFile;
import org.bson.LazyBSONCallback;
import java.util.Iterator;
@@ -42,14 +43,20 @@ public DBDecoder create() {
private static class BsonDBCallback extends LazyDBCallback {
private final DB db;
+ private final DBCollection collection;
public BsonDBCallback(DBCollection collection) {
super(collection);
+ this.collection = collection;
this.db = collection == null ? null : collection.getDB();
}
@Override
public Object createObject(byte[] data, int offset) {
+ if (GridFSDBFile.class.equals(collection.getObjectClass())) {
+ return DefaultDBDecoder.FACTORY.create().decode(data, collection);
+ }
+
DBObject dbo = new RelaxedLazyDBObject(data, new LazyBSONCallback());
Iterator it = dbo.keySet().iterator();
@@ -0,0 +1,75 @@
+package org.jongo.gridfs;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.gridfs.GridFS;
+import com.mongodb.gridfs.GridFSDBFile;
+import com.mongodb.gridfs.GridFSInputFile;
+import org.jongo.RawResultHandler;
+import org.jongo.util.JongoTestCase;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.jongo.RawResultHandler.asRaw;
+
+public class GridFsTest extends JongoTestCase {
+
+ GridFS gridFS;
+
+ @Before
+ public void setUp() throws Exception {
+ gridFS = new GridFS(getDatabase());
+ insertTestFileInGridFS();
+ }
+
+ @Test
+ public void shouldAllowDualAccessToFilesCollection() throws Exception {
+ queryWithJongoAndMapToCustomClass();
+ queryWithJongoAndMapToGridFSDBFile();
+ queryWithGridFS();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ dropGridFsCollections();
+ }
+
+ private void insertTestFileInGridFS() {
+ GridFSInputFile gridFile = gridFS.createFile(new byte[]{
+ (byte) 0xCA,
+ (byte) 0xFE,
+ (byte) 0xBA,
+ (byte) 0xBE});
+ gridFile.setFilename("test.txt");
+ gridFile.save();
+ }
+
+ private void queryWithJongoAndMapToCustomClass() {
+ CustomFileDescriptor descriptor = getJongo().getCollection("fs.files")
+ .findOne()
+ .as(CustomFileDescriptor.class);
+ assertThat(descriptor.filename).isEqualTo("test.txt");
+ }
+
+ private void queryWithJongoAndMapToGridFSDBFile() {
+ GridFSDBFile gridFile = getJongo().getCollection("fs.files")
+ .findOne()
+ .map(asRaw(GridFSDBFile.class));
+ assertThat(gridFile.getFilename()).isEqualTo("test.txt");
+ }
+
+ private void queryWithGridFS() {
+ GridFSDBFile gridFile = gridFS.findOne(new BasicDBObject());
+ assertThat(gridFile.getFilename()).isEqualTo("test.txt");
+ }
+
+ private void dropGridFsCollections() throws Exception {
+ getDatabase().getCollection("fs.files").drop();
+ getDatabase().getCollection("fs.chunks").drop();
+ }
+
+ static class CustomFileDescriptor {
+ String filename;
+ }
+}

0 comments on commit 0d27ba3

Please sign in to comment.