use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testWriteDBPointer.
@Test
public void testWriteDBPointer() {
writer.writeStartDocument();
BsonDbPointer dbPointer = new BsonDbPointer("my.test", new ObjectId("50d3332018c6a1d8d1662b61"));
writer.writeDBPointer("pt", dbPointer);
writer.writeEndDocument();
byte[] expectedValues = { 33, 0, 0, 0, 12, 112, 116, 0, 8, 0, 0, 0, 109, 121, 46, 116, 101, 115, 116, 0, 80, -45, 51, 32, 24, -58, -95, -40, -47, 102, 43, 97, 0 };
assertArrayEquals(expectedValues, buffer.toByteArray());
BsonReader reader = createReaderForBytes(expectedValues);
reader.readStartDocument();
assertThat(reader.readBsonType(), is(BsonType.DB_POINTER));
assertEquals("pt", reader.readName());
assertEquals(dbPointer, reader.readDBPointer());
reader.readEndDocument();
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class DocumentCodecTest method testPrimitiveBSONTypeCodecs.
@Test
public void testPrimitiveBSONTypeCodecs() throws IOException {
DocumentCodec documentCodec = new DocumentCodec();
Document doc = new Document();
doc.put("oid", new ObjectId());
doc.put("integer", 1);
doc.put("long", 2L);
doc.put("string", "hello");
doc.put("double", 3.2);
doc.put("decimal", Decimal128.parse("0.100"));
doc.put("binary", new Binary(BsonBinarySubType.USER_DEFINED, new byte[] { 0, 1, 2, 3 }));
doc.put("date", new Date(1000));
doc.put("boolean", true);
doc.put("code", new Code("var i = 0"));
doc.put("minkey", new MinKey());
doc.put("maxkey", new MaxKey());
// doc.put("pattern", Pattern.compile("^hello")); // TODO: Pattern doesn't override equals method!
doc.put("null", null);
documentCodec.encode(writer, doc, EncoderContext.builder().build());
BsonInput bsonInput = createInputBuffer();
Document decodedDocument = documentCodec.decode(new BsonBinaryReader(bsonInput), DecoderContext.builder().build());
assertEquals(doc, decodedDocument);
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class JsonReader method visitDBPointerConstructor.
private BsonDbPointer visitDBPointerConstructor() {
verifyToken(JsonTokenType.LEFT_PAREN);
JsonToken namespaceToken = popToken();
if (namespaceToken.getType() != JsonTokenType.STRING) {
throw new JsonParseException("JSON reader expected a string but found '%s'.", namespaceToken.getValue());
}
verifyToken(JsonTokenType.COMMA);
JsonToken idToken = popToken();
if (namespaceToken.getType() != JsonTokenType.STRING) {
throw new JsonParseException("JSON reader expected a string but found '%s'.", idToken.getValue());
}
verifyToken(JsonTokenType.RIGHT_PAREN);
return new BsonDbPointer(namespaceToken.getValue(String.class), new ObjectId(idToken.getValue(String.class)));
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class JsonWriterTest method testObjectIdShell.
@Test
public void testObjectIdShell() {
writer = new JsonWriter(stringWriter, JsonWriterSettings.builder().outputMode(JsonMode.SHELL).build());
ObjectId objectId = new ObjectId("4d0ce088e447ad08b4721a37");
writer.writeStartDocument();
writer.writeObjectId("_id", objectId);
writer.writeEndDocument();
String expected = "{ \"_id\" : ObjectId(\"4d0ce088e447ad08b4721a37\") }";
assertEquals(expected, stringWriter.toString());
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class JsonReaderTest method testObjectIdTenGen.
@Test
public void testObjectIdTenGen() {
String json = "ObjectId(\"4d0ce088e447ad08b4721a37\")";
bsonReader = new JsonReader(json);
assertEquals(BsonType.OBJECT_ID, bsonReader.readBsonType());
ObjectId objectId = bsonReader.readObjectId();
assertEquals("4d0ce088e447ad08b4721a37", objectId.toString());
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
}
Aggregations