use of org.bson.BSONException in project mongo-java-driver by mongodb.
the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldDecodeDocumentWithUuidRepresentation.
@Test
public void shouldDecodeDocumentWithUuidRepresentation() {
bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentationForExplicitEncoding)));
Document document;
try {
document = documentCollection.find().first();
assertNotNull(document);
} catch (BSONException e) {
if (uuidCodec.getUuidRepresentation() != STANDARD) {
throw e;
}
return;
}
if (uuidRepresentationForClient == UuidRepresentation.STANDARD) {
assertEquals(UUID.class, document.get("standard").getClass());
assertEquals(uuid, document.get("standard"));
assertEquals(Binary.class, document.get("legacy").getClass());
assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, encodedValue), document.get("legacy"));
} else {
if (uuidRepresentationForClient == UuidRepresentation.JAVA_LEGACY) {
assertEquals(UUID.class, document.get("standard").getClass());
assertEquals(uuid, document.get("standard"));
} else {
assertEquals(Binary.class, document.get("standard").getClass());
assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
}
assertEquals(UUID.class, document.get("legacy").getClass());
assertEquals(uuid, document.get("legacy"));
}
}
use of org.bson.BSONException in project mongo-java-driver by mongodb.
the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldDecodePojoWithStandardUuidRepresentation.
@Test
public void shouldDecodePojoWithStandardUuidRepresentation() {
bsonDocumentCollection.insertOne(new BsonDocument("_id", new BsonBinary(uuid, STANDARD)));
try {
UuidIdPojo document = uuidIdPojoCollection.find().first();
assertNotNull(document);
assertEquals(uuid, document.getId());
} catch (BSONException e) {
if (uuidCodec.getUuidRepresentation() == uuidRepresentationForClient) {
throw e;
}
}
}
use of org.bson.BSONException in project mongo-java-driver by mongodb.
the class AbstractUuidRepresentationTest method shouldDecodePojoWithLegacyUuidRepresentation.
@Test
public void shouldDecodePojoWithLegacyUuidRepresentation() {
bsonDocumentCollection.insertOne(new BsonDocument("_id", new BsonBinary(uuid, uuidRepresentation == UuidRepresentation.UNSPECIFIED || uuidRepresentation == UuidRepresentation.STANDARD ? UuidRepresentation.PYTHON_LEGACY : uuidRepresentation)));
try {
UuidIdPojo document = uuidIdPojoCollection.find().first();
assertNotNull(document);
assertEquals(uuid, document.getId());
} catch (BSONException e) {
assertNotEquals(UuidRepresentation.C_SHARP_LEGACY, uuidRepresentation);
assertNotEquals(UuidRepresentation.PYTHON_LEGACY, uuidRepresentation);
}
}
use of org.bson.BSONException in project mongo-java-driver by mongodb.
the class AbstractUuidRepresentationTest method shouldDecodePojoWithStandardUuidRepresentation.
@Test
public void shouldDecodePojoWithStandardUuidRepresentation() {
bsonDocumentCollection.insertOne(new BsonDocument("_id", new BsonBinary(uuid, UuidRepresentation.STANDARD)));
try {
UuidIdPojo document = uuidIdPojoCollection.find().first();
assertNotNull(document);
assertEquals(uuid, document.getId());
} catch (BSONException e) {
assertNotEquals(UuidRepresentation.STANDARD, uuidRepresentation);
}
}
use of org.bson.BSONException in project mongo-java-driver by mongodb.
the class UuidCodec method encode.
@Override
public void encode(final BsonWriter writer, final UUID value, final EncoderContext encoderContext) {
byte[] binaryData = new byte[16];
writeLongToArrayBigEndian(binaryData, 0, value.getMostSignificantBits());
writeLongToArrayBigEndian(binaryData, 8, value.getLeastSignificantBits());
switch(encoderUuidRepresentation) {
case C_SHARP_LEGACY:
UuidCodecHelper.reverseByteArray(binaryData, 0, 4);
UuidCodecHelper.reverseByteArray(binaryData, 4, 2);
UuidCodecHelper.reverseByteArray(binaryData, 6, 2);
break;
case JAVA_LEGACY:
UuidCodecHelper.reverseByteArray(binaryData, 0, 8);
UuidCodecHelper.reverseByteArray(binaryData, 8, 8);
break;
case PYTHON_LEGACY:
case STANDARD:
break;
default:
throw new BSONException("Unexpected UUID representation");
}
// changed the default subtype to STANDARD since 3.0
if (encoderUuidRepresentation == UuidRepresentation.STANDARD) {
writer.writeBinaryData(new BsonBinary(BsonBinarySubType.UUID_STANDARD, binaryData));
} else {
writer.writeBinaryData(new BsonBinary(BsonBinarySubType.UUID_LEGACY, binaryData));
}
}
Aggregations