use of org.bson.BSONObject in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseEmbeddedArray.
/**
* Test if embedded arrays can be deserialized
* @throws Exception if something goes wrong
*/
@Test
public void parseEmbeddedArray() throws Exception {
List<Integer> i = new ArrayList<Integer>();
i.add(5);
i.add(6);
BSONObject o = new BasicBSONObject();
o.put("Int32", 5);
o.put("Arr", i);
o.put("String", "Hello");
Map<?, ?> data = parseBsonObject(o);
assertEquals(3, data.size());
assertEquals(5, data.get("Int32"));
}
use of org.bson.BSONObject in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseBigStringInThreads.
/**
* Tests reading a very large string using multiple threads. Refers
* issue #19. Does not fail reproducibly, but with very high probability.
* You may have to run unit tests several times though to really rule out
* multi-threading issues.
* @throws Exception if something went wrong
* @author endasb
*/
@Test
public void parseBigStringInThreads() throws Exception {
final BSONObject o = new BasicBSONObject();
final AtomicInteger fails = new AtomicInteger(0);
StringBuilder bigStr = new StringBuilder();
for (int i = 0; i < 80000; i++) {
bigStr.append("abc");
}
o.put("String", bigStr.toString());
ArrayList<Thread> threads = new ArrayList<Thread>();
for (int i = 0; i < 50; i++) {
threads.add(new Thread(new Runnable() {
@Override
public void run() {
try {
Map<?, ?> data = parseBsonObject(o);
data = parseBsonObject(o);
assertNotNull(data);
} catch (Exception e) {
fail("Threading issue " + fails.incrementAndGet());
}
}
}));
}
for (Thread thread : threads) {
thread.start();
}
for (Thread thread : threads) {
thread.join();
}
assertEquals(0, fails.get());
}
use of org.bson.BSONObject in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseObjectId.
/**
* Check if org.bson.types.ObjectId can be serialized and deserialized as
* a byte array. See issue #38
* @throws Exception if something goes wrong
*/
@Test
public void parseObjectId() throws Exception {
class ObjectIdDeserializer extends StdDeserializer<org.bson.types.ObjectId> {
private static final long serialVersionUID = 6934309887169924897L;
protected ObjectIdDeserializer() {
super(org.bson.types.ObjectId.class);
}
@Override
public org.bson.types.ObjectId deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonGenerationException {
return new org.bson.types.ObjectId(jp.getBinaryValue());
}
}
org.bson.types.ObjectId oid = new org.bson.types.ObjectId();
BSONObject o = new BasicBSONObject();
o.put("oid", oid.toByteArray());
SimpleModule mod = new SimpleModule();
mod.addDeserializer(org.bson.types.ObjectId.class, new ObjectIdDeserializer());
ObjectIdClass res = parseBsonObject(o, ObjectIdClass.class, mod);
assertEquals(oid, res.oid);
}
use of org.bson.BSONObject in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseEmbeddedDocumentAsTree.
/**
* Tests reading an embedded document through
* {@link BsonParser#readValueAsTree()}. Refers issue #9
* @throws Exception if something went wrong
* @author audistard
*/
@Test
public void parseEmbeddedDocumentAsTree() throws Exception {
BSONObject o2 = new BasicBSONObject();
o2.put("Int64", 10L);
BSONObject o3 = new BasicBSONObject();
o3.put("Int64", 11L);
BSONObject o1 = new BasicBSONObject();
o1.put("Obj2", o2);
o1.put("Obj3", o3);
BSONEncoder enc = new BasicBSONEncoder();
byte[] b = enc.encode(o1);
ByteArrayInputStream bais = new ByteArrayInputStream(b);
BsonFactory fac = new BsonFactory();
ObjectMapper mapper = new ObjectMapper(fac);
fac.setCodec(mapper);
BsonParser dec = fac.createParser(bais);
assertEquals(JsonToken.START_OBJECT, dec.nextToken());
assertEquals(JsonToken.FIELD_NAME, dec.nextToken());
assertEquals("Obj2", dec.getCurrentName());
assertEquals(JsonToken.START_OBJECT, dec.nextToken());
JsonNode obj2 = dec.readValueAsTree();
assertEquals(1, obj2.size());
assertNotNull(obj2.get("Int64"));
assertEquals(10L, obj2.get("Int64").longValue());
assertEquals(JsonToken.FIELD_NAME, dec.nextToken());
assertEquals("Obj3", dec.getCurrentName());
assertEquals(JsonToken.START_OBJECT, dec.nextToken());
assertEquals(JsonToken.FIELD_NAME, dec.nextToken());
assertEquals("Int64", dec.getCurrentName());
assertEquals(JsonToken.VALUE_NUMBER_INT, dec.nextToken());
assertEquals(11L, dec.getLongValue());
assertEquals(JsonToken.END_OBJECT, dec.nextToken());
assertEquals(JsonToken.END_OBJECT, dec.nextToken());
}
use of org.bson.BSONObject in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseComplex.
/**
* Test if a complex BSON object containing various values can be
* deserialized
* @throws Exception if something goes wrong
*/
@Test
public void parseComplex() throws Exception {
BSONObject o = new BasicBSONObject();
o.put("Timestamp", new BSONTimestamp(0xAABB, 0xCCDD));
o.put("Symbol", new Symbol("Test"));
o.put("ObjectId", org.bson.types.ObjectId.createFromLegacyFormat(Integer.MAX_VALUE, -2, Integer.MIN_VALUE));
Pattern p = Pattern.compile(".*", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE | Pattern.UNICODE_CASE);
o.put("Regex", p);
Map<?, ?> data = parseBsonObject(o);
assertEquals(new Timestamp(0xAABB, 0xCCDD), data.get("Timestamp"));
assertEquals(new de.undercouch.bson4jackson.types.Symbol("Test"), data.get("Symbol"));
ObjectId oid = (ObjectId) data.get("ObjectId");
assertEquals(Integer.MAX_VALUE, oid.getTime());
assertEquals(-2, oid.getMachine());
assertEquals(Integer.MIN_VALUE, oid.getInc());
Pattern p2 = (Pattern) data.get("Regex");
assertEquals(p.flags(), p2.flags());
assertEquals(p.pattern(), p2.pattern());
}
Aggregations