use of org.bson.BSONObject in project bson4jackson by michel-kraemer.
the class BsonGeneratorTest method objectIds.
/**
* Test if {@link ObjectId}s can be serialized
* @throws Exception if something goes wrong
*/
@Test
public void objectIds() throws Exception {
Map<String, Object> data = new LinkedHashMap<String, Object>();
ObjectId objectId = new ObjectId((int) (System.currentTimeMillis() / 1000), new Random().nextInt(), 100);
data.put("_id", objectId);
BSONObject obj = generateAndParse(data);
org.bson.types.ObjectId result = (org.bson.types.ObjectId) obj.get("_id");
assertNotNull(result);
assertEquals(org.bson.types.ObjectId.createFromLegacyFormat(objectId.getTime(), objectId.getMachine(), objectId.getInc()), result);
}
use of org.bson.BSONObject in project bson4jackson by michel-kraemer.
the class BsonGeneratorTest method patterns.
/**
* Test if {@link Pattern}s can be serialized
* @throws Exception if something goes wrong
*/
@Test
public void patterns() throws Exception {
Pattern pattern = Pattern.compile("a.*a", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Map<String, Object> data = new LinkedHashMap<String, Object>();
data.put("pattern", pattern);
BSONObject obj = generateAndParse(data);
Pattern result = (Pattern) obj.get("pattern");
assertNotNull(result);
assertEquals(pattern.pattern(), result.pattern());
assertEquals(pattern.flags(), result.flags());
}
use of org.bson.BSONObject in project bson4jackson by michel-kraemer.
the class BsonGeneratorTest method dates.
/**
* Test if {@link Date} objects can be serialized
* @throws Exception if something goes wrong
*/
@Test
public void dates() throws Exception {
Map<String, Object> data = new LinkedHashMap<String, Object>();
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
data.put("date", date);
data.put("calendar", calendar);
BSONObject obj = generateAndParse(data);
assertEquals(date, obj.get("date"));
assertEquals(date, obj.get("calendar"));
}
use of org.bson.BSONObject in project bson4jackson by michel-kraemer.
the class BsonGeneratorTest method writeBigDecimalsAsStrings.
/**
* Test if {@link BigDecimal} objects can be serialized as {@link String}s
* @throws Exception if something goes wrong
*/
@Test
public void writeBigDecimalsAsStrings() throws Exception {
Map<String, Object> data = new LinkedHashMap<String, Object>();
data.put("big", new BigDecimal("0.3"));
BSONObject obj = generateAndParse(data);
Double result = (Double) obj.get("big");
// BigDecimal("0.3") does not equal 0.3!
assertEquals(Double.valueOf(0.3), result, 0.000001);
assertFalse(Double.valueOf(0.3).equals(result));
data = new LinkedHashMap<String, Object>();
data.put("big", new BigDecimal("0.3"));
obj = generateAndParse(data, BsonGenerator.Feature.WRITE_BIGDECIMALS_AS_STRINGS);
String strResult = (String) obj.get("big");
assertEquals("0.3", strResult);
}
use of org.bson.BSONObject in project bson4jackson by michel-kraemer.
the class BsonGeneratorTest method assertRaw.
private void assertRaw(byte[] r) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(r);
BSONDecoder decoder = new BasicBSONDecoder();
BSONObject obj = decoder.readObject(bais);
byte[] o = (byte[]) obj.get("Test");
CharBuffer buf = ByteBuffer.wrap(o).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer();
assertEquals(2, buf.remaining());
char a = buf.get();
char b = buf.get();
assertEquals('a', a);
assertEquals('b', b);
}
Aggregations