use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class JsonWriterTest method testBinaryRelaxed.
@Test
public void testBinaryRelaxed() {
List<TestData<BsonBinary>> tests = asList(new TestData<BsonBinary>(new BsonBinary(new byte[0]), "{\"$binary\": {\"base64\": \"\", " + "\"subType\": \"00\"}}"), new TestData<BsonBinary>(new BsonBinary(new byte[] { 1 }), "{\"$binary\": {\"base64\": \"AQ==\", " + "\"subType\": \"00\"}}"), new TestData<BsonBinary>(new BsonBinary(new byte[] { 1, 2 }), "{\"$binary\": {\"base64\": \"AQI=\", " + "\"subType\": \"00\"}}"), new TestData<BsonBinary>(new BsonBinary(new byte[] { 1, 2, 3 }), "{\"$binary\": {\"base64\": \"AQID\", " + "\"subType\": \"00\"}}"), new TestData<BsonBinary>(new BsonBinary((byte) 0x80, new byte[] { 1, 2, 3 }), "{\"$binary\": {\"base64\": \"AQID\", " + "\"subType\": \"80\"}}"));
for (final TestData<BsonBinary> cur : tests) {
stringWriter = new StringWriter();
writer = new JsonWriter(stringWriter, JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build());
writer.writeStartDocument();
writer.writeBinaryData("binary", cur.value);
writer.writeEndDocument();
String expected = "{\"binary\": " + cur.expected + "}";
assertEquals(expected, stringWriter.toString());
}
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class JsonWriterTest method shouldThrowAnErrorIfTryingToWriteValueIntoAJavascriptScope.
@Test(expected = BsonInvalidOperationException.class)
public void shouldThrowAnErrorIfTryingToWriteValueIntoAJavascriptScope() {
writer.writeStartDocument();
writer.writeJavaScriptWithScope("js1", "var i = 1");
writer.writeBinaryData(new BsonBinary(new byte[] { 0, 0, 1, 0 }));
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class JsonReaderTest method testLegacyUserDefinedBinaryWithNumericType.
@Test
public void testLegacyUserDefinedBinaryWithNumericType() {
String json = "{ \"$binary\" : \"AQID\", \"$type\" : 128 }";
testStringAndStream(json, bsonReader -> {
assertEquals(BsonType.BINARY, bsonReader.readBsonType());
BsonBinary binary = bsonReader.readBinaryData();
assertEquals(BsonBinarySubType.USER_DEFINED.getValue(), binary.getType());
assertArrayEquals(new byte[] { 1, 2, 3 }, binary.getData());
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
return null;
});
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class JsonReaderTest method testUuid.
@Test
public void testUuid() {
String json = "{ \"$uuid\" : \"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\"}}";
testStringAndStream(json, bsonReader -> {
assertEquals(BsonType.BINARY, bsonReader.readBsonType());
BsonBinary binary = bsonReader.readBinaryData();
assertEquals(BsonBinarySubType.UUID_STANDARD.getValue(), binary.getType());
assertArrayEquals(new byte[] { -75, -14, 30, 12, 42, 13, 66, -42, -83, 3, -40, 39, 0, -115, -118, -74 }, binary.getData());
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
return null;
});
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class JsonReaderTest method testBinDataWithNew.
@Test
public void testBinDataWithNew() {
String json = "{ \"a\" : new BinData(3, AQID) }";
testStringAndStream(json, bsonReader -> {
bsonReader.readStartDocument();
assertEquals(BsonType.BINARY, bsonReader.readBsonType());
BsonBinary binary = bsonReader.readBinaryData();
assertEquals(3, binary.getType());
assertArrayEquals(new byte[] { 1, 2, 3 }, binary.getData());
bsonReader.readEndDocument();
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
return null;
});
}
Aggregations