use of com.mongodb.BasicDBObject in project mongo-java-driver by mongodb.
the class GridFSTest method testOutStream.
void testOutStream(final String s) throws Exception {
int[] start = getCurrentCollectionCounts();
GridFSInputFile in = gridFS.createFile();
OutputStream writeStream = in.getOutputStream();
writeStream.write(s.getBytes(defaultCharset()), 0, s.length());
writeStream.close();
GridFSDBFile out = gridFS.findOne(new BasicDBObject("_id", in.getId()));
assert (out.getId().equals(in.getId()));
assert (out.getChunkSize() == (long) GridFS.DEFAULT_CHUNKSIZE);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
out.writeTo(bout);
String outString = new String(bout.toByteArray(), defaultCharset());
assert (outString.equals(s));
out.remove();
int[] end = getCurrentCollectionCounts();
assertEquals(start[0], end[0]);
assertEquals(start[1], end[1]);
}
use of com.mongodb.BasicDBObject in project mongo-java-driver by mongodb.
the class GridFSTest method testMetadata.
@Test
public void testMetadata() throws Exception {
GridFSInputFile in = gridFS.createFile("foo".getBytes(defaultCharset()));
in.put("meta", 5);
in.save();
GridFSDBFile out = gridFS.findOne(new BasicDBObject("_id", in.getId()));
assertTrue(out.get("meta").equals(5));
}
use of com.mongodb.BasicDBObject in project mongo-java-driver by mongodb.
the class JSONTest method testNumbers2.
@Test
public void testNumbers2() {
DBObject x = new BasicDBObject("x", 123);
assertEquals(JSON.parse(x.toString()), x);
x = new BasicDBObject("x", 123123123123L);
assertEquals(JSON.parse(x.toString()), x);
x = new BasicDBObject("x", 123123123);
assertEquals(JSON.parse(x.toString()), x);
}
use of com.mongodb.BasicDBObject in project mongo-java-driver by mongodb.
the class JSONTest method testJSONEncoding.
@Test
public void testJSONEncoding() throws ParseException {
String json = "{ 'str' : 'asdfasd' , 'long' : 123123123123 , 'int' : 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, " + "'uuid' : { '$uuid' : '60f65152-6d4a-4f11-9c9b-590b575da7b5' }}";
BasicDBObject a = (BasicDBObject) JSON.parse(json);
assertEquals("asdfasd", a.get("str"));
assertEquals(5, a.get("int"));
assertEquals(123123123123L, a.get("long"));
assertEquals(0.4d, a.get("float"));
assertEquals(false, a.get("bool"));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
assertEquals(format.parse("2011-05-18T18:56:00Z"), a.get("date"));
Pattern pat = (Pattern) a.get("pat");
Pattern pat2 = Pattern.compile(".*", BSON.regexFlags(""));
assertEquals(pat.pattern(), pat2.pattern());
assertEquals(pat.flags(), pat2.flags());
ObjectId oid = (ObjectId) a.get("oid");
assertEquals(new ObjectId("4d83ab3ea39562db9c1ae2ae"), oid);
// DBRef ref = (DBRef) a.get("ref");
// assertEquals(new DBRef(null, "test.test", new ObjectId("4d83ab59a39562db9c1ae2af")), ref);
assertEquals(new Code("asdfdsa"), a.get("code"));
assertEquals(new CodeWScope("ggggg", new BasicBSONObject()), a.get("codews"));
assertEquals(new BSONTimestamp(1300474885, 10), a.get("ts"));
assertEquals(UUID.fromString("60f65152-6d4a-4f11-9c9b-590b575da7b5"), a.get("uuid"));
String json2 = JSON.serialize(a);
BasicDBObject b = (BasicDBObject) JSON.parse(json2);
assertEquals(JSON.serialize(b), JSON.serialize(b));
}
use of com.mongodb.BasicDBObject in project mongo-java-driver by mongodb.
the class JSONTest method testEscape1.
@Test
public void testEscape1() {
String raw = "a\nb";
DBObject x = new BasicDBObject("x", raw);
assertEquals("\"a\\nb\"", JSON.serialize(raw));
assertEquals(x, JSON.parse(x.toString()));
assertEquals(raw, ((DBObject) JSON.parse(x.toString())).get("x"));
x = new BasicDBObject("x", "a\nb\bc\td\re");
assertEquals(x, JSON.parse(x.toString()));
String thingy = "va\"lue";
x = new BasicDBObject("name", thingy);
x = (DBObject) JSON.parse(x.toString());
assertEquals(thingy, x.get("name"));
thingy = "va\\lue";
x = new BasicDBObject("name", thingy);
x = (DBObject) JSON.parse(x.toString());
assertEquals(thingy, x.get("name"));
assertEquals("va/lue", JSON.parse("\"va\\/lue\""));
assertEquals("value", JSON.parse("\"va\\lue\""));
assertEquals("va\\lue", JSON.parse("\"va\\\\lue\""));
escapeChar("\t");
escapeChar("\b");
escapeChar("\n");
escapeChar("\r");
escapeChar("\'");
escapeChar("\"");
escapeChar("\\");
}
Aggregations