use of org.bson.BSONException in project mongo-java-driver by mongodb.
the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldDecodeDBObjectWithUuidRepresentation.
@Test
public void shouldDecodeDBObjectWithUuidRepresentation() {
bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentationForExplicitEncoding)));
DBObject document;
try {
document = dbObjectCollection.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 shouldDecodePojoWithLegacyUuidRepresentation.
@Test
public void shouldDecodePojoWithLegacyUuidRepresentation() {
bsonDocumentCollection.insertOne(new BsonDocument("_id", new BsonBinary(uuid, uuidRepresentationForExplicitEncoding)));
try {
UuidIdPojo document = uuidIdPojoCollection.find().first();
assertNotNull(document);
assertEquals(uuid, document.getId());
} catch (BSONException e) {
if (uuidCodec.getUuidRepresentation() == uuidRepresentationForExplicitEncoding) {
throw e;
}
}
}
use of org.bson.BSONException in project mongo-java-driver by mongodb.
the class AbstractUuidRepresentationTest method shouldDecodeDocumentWithUuidRepresentation.
@Test
public void shouldDecodeDocumentWithUuidRepresentation() {
bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentation == UuidRepresentation.UNSPECIFIED || uuidRepresentation == UuidRepresentation.STANDARD ? UuidRepresentation.PYTHON_LEGACY : uuidRepresentation)));
Document document;
try {
document = documentCollection.find().first();
assertNotNull(document);
} catch (BSONException e) {
if (uuidRepresentation != STANDARD && uuidRepresentation != JAVA_LEGACY) {
throw e;
}
return;
}
if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
assertEquals(Binary.class, document.get("standard").getClass());
assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
assertEquals(Binary.class, document.get("legacy").getClass());
assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, standardEncodedValue), document.get("legacy"));
} else if (uuidRepresentation == 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, standardEncodedValue), document.get("legacy"));
} 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 AbstractUuidRepresentationTest method shouldDecodeDbObjectWithUuidRepresentation.
@Test
public void shouldDecodeDbObjectWithUuidRepresentation() {
bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentation == UuidRepresentation.UNSPECIFIED || uuidRepresentation == UuidRepresentation.STANDARD ? UuidRepresentation.PYTHON_LEGACY : uuidRepresentation)));
DBObject document;
try {
document = dbObjectCollection.find().first();
assertNotNull(document);
} catch (BSONException e) {
if (uuidRepresentation != STANDARD && uuidRepresentation != JAVA_LEGACY) {
throw e;
}
return;
}
if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
assertEquals(Binary.class, document.get("standard").getClass());
assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
assertEquals(Binary.class, document.get("legacy").getClass());
assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, standardEncodedValue), document.get("legacy"));
} else if (uuidRepresentation == 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, standardEncodedValue), document.get("legacy"));
} 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 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