use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.
the class AbstractUuidRepresentationTest method shouldEncodeDbObjectWithUuidRepresentation.
@Test
public void shouldEncodeDbObjectWithUuidRepresentation() {
if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
try {
dbObjectCollection.insertOne(new BasicDBObject("_id", uuid));
fail();
} catch (CodecConfigurationException e) {
// all good
}
} else {
dbObjectCollection.insertOne(new BasicDBObject("_id", uuid));
BsonDocument document = bsonDocumentCollection.find().first();
assertNotNull(document);
BsonBinary uuidAsBinary = document.getBinary("_id");
assertEquals(subType.getValue(), uuidAsBinary.getType());
assertEquals(subType.getValue(), uuidAsBinary.getType());
assertArrayEquals(encodedValue, uuidAsBinary.getData());
}
}
use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.
the class AbstractUuidRepresentationTest method shouldEncodePojoWithUuidRepresentation.
@Test
public void shouldEncodePojoWithUuidRepresentation() {
if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
try {
uuidIdPojoCollection.insertOne(new UuidIdPojo(uuid));
fail();
} catch (CodecConfigurationException e) {
// all good
}
} else {
uuidIdPojoCollection.insertOne(new UuidIdPojo(uuid));
BsonDocument document = bsonDocumentCollection.find().first();
assertNotNull(document);
BsonBinary uuidAsBinary = document.getBinary("_id");
assertEquals(subType.getValue(), uuidAsBinary.getType());
assertEquals(subType.getValue(), uuidAsBinary.getType());
assertArrayEquals(encodedValue, uuidAsBinary.getData());
}
}
use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.
the class AbstractUuidRepresentationTest method shouldEncodeDocumentWithUuidRepresentation.
@Test
public void shouldEncodeDocumentWithUuidRepresentation() {
if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
try {
documentCollection.insertOne(new Document("_id", uuid));
fail();
} catch (CodecConfigurationException e) {
// all good
}
} else {
documentCollection.insertOne(new Document("_id", uuid));
BsonDocument document = bsonDocumentCollection.find().first();
assertNotNull(document);
BsonBinary uuidAsBinary = document.getBinary("_id");
assertEquals(subType.getValue(), uuidAsBinary.getType());
assertArrayEquals(encodedValue, uuidAsBinary.getData());
}
}
use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.
the class LazyPropertyModelCodec method getCodecFromPropertyRegistry.
@SuppressWarnings("unchecked")
private Codec<T> getCodecFromPropertyRegistry(final PropertyModel<T> propertyModel) {
Codec<T> localCodec;
try {
localCodec = propertyCodecRegistry.get(propertyModel.getTypeData());
} catch (CodecConfigurationException e) {
return new LazyMissingCodec<>(propertyModel.getTypeData().getType(), e);
}
if (localCodec == null) {
localCodec = new LazyMissingCodec<>(propertyModel.getTypeData().getType(), new CodecConfigurationException("Unexpected missing codec for: " + propertyModel.getName()));
}
BsonType representation = propertyModel.getBsonRepresentation();
if (representation != null) {
if (localCodec instanceof RepresentationConfigurable) {
return ((RepresentationConfigurable<T>) localCodec).withRepresentation(representation);
}
throw new CodecConfigurationException("Codec must implement RepresentationConfigurable to support BsonRepresentation");
}
return localCodec;
}
use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.
the class PojoCodecImpl method getCodecFromDocument.
@SuppressWarnings("unchecked")
private Codec<T> getCodecFromDocument(final BsonReader reader, final boolean useDiscriminator, final String discriminatorKey, final CodecRegistry registry, final DiscriminatorLookup discriminatorLookup, final Codec<T> defaultCodec) {
Codec<T> codec = defaultCodec;
if (useDiscriminator) {
BsonReaderMark mark = reader.getMark();
reader.readStartDocument();
boolean discriminatorKeyFound = false;
while (!discriminatorKeyFound && reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String name = reader.readName();
if (discriminatorKey.equals(name)) {
discriminatorKeyFound = true;
try {
Class<?> discriminatorClass = discriminatorLookup.lookup(reader.readString());
if (!codec.getEncoderClass().equals(discriminatorClass)) {
codec = (Codec<T>) registry.get(discriminatorClass);
}
} catch (Exception e) {
throw new CodecConfigurationException(format("Failed to decode '%s'. Decoding errored with: %s", classModel.getName(), e.getMessage()), e);
}
} else {
reader.skipValue();
}
}
mark.reset();
}
return codec;
}
Aggregations