Search in sources :

Example 1 with Mapper

use of org.mongodb.morphia.mapping.Mapper in project morphia by mongodb.

the class PathTargetTest method dottedPath.

@Test
public void dottedPath() {
    getMorphia().map(ParentType.class, EmbeddedType.class);
    Mapper mapper = getMorphia().getMapper();
    PathTarget pathTarget = new PathTarget(mapper, mapper.getMappedClass(ParentType.class), "embedded.number");
    Assert.assertEquals("embedded.number", pathTarget.translatedPath());
    Assert.assertEquals(mapper.getMappedClass(EmbeddedType.class).getMappedFieldByJavaField("number"), pathTarget.getTarget());
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) ParentType(org.mongodb.morphia.entities.ParentType) Test(org.junit.Test)

Example 2 with Mapper

use of org.mongodb.morphia.mapping.Mapper in project morphia by mongodb.

the class PathTargetTest method disableValidation.

@Test
public void disableValidation() {
    getMorphia().map(WithNested.class, Nested.class, NestedImpl.class, AnotherNested.class);
    Mapper mapper = getMorphia().getMapper();
    MappedClass mappedClass = mapper.getMappedClass(WithNested.class);
    final PathTarget pathTarget = new PathTarget(mapper, mappedClass, "nested.field.fail");
    pathTarget.disableValidation();
    Assert.assertEquals("nested.field.fail", pathTarget.translatedPath());
    Assert.assertNull(pathTarget.getTarget());
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 3 with Mapper

use of org.mongodb.morphia.mapping.Mapper in project morphia by mongodb.

the class PathTargetTest method simpleResolution.

@Test
public void simpleResolution() {
    getMorphia().map(ParentType.class, EmbeddedType.class);
    Mapper mapper = getMorphia().getMapper();
    MappedClass mappedClass = mapper.getMappedClass(ParentType.class);
    PathTarget pathTarget = new PathTarget(mapper, mappedClass, "name");
    Assert.assertEquals("n", pathTarget.translatedPath());
    Assert.assertEquals(mappedClass.getMappedFieldByJavaField("name"), pathTarget.getTarget());
    pathTarget = new PathTarget(mapper, mappedClass, "n");
    Assert.assertEquals("n", pathTarget.translatedPath());
    Assert.assertEquals(mappedClass.getMappedField("n"), pathTarget.getTarget());
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 4 with Mapper

use of org.mongodb.morphia.mapping.Mapper in project morphia by mongodb.

the class TestMapping method testMapping.

@Test
public void testMapping() {
    final BasicDAO<User, ObjectId> messageDAO = new BasicDAO<User, ObjectId>(User.class, getDs());
    Assert.assertNotNull(messageDAO);
    Mapper mapper = new Mapper();
    User user = new User();
    user.id = 1;
    user.userObject = "just a String";
    DBObject dbObject = mapper.toDBObject(user);
    Object object = mapper.fromDBObject(getDs(), User.class, dbObject, new DefaultEntityCache());
    Assert.assertEquals(user.userObject, ((User) object).userObject);
    user.userObject = 33;
    dbObject = mapper.toDBObject(user);
    object = mapper.fromDBObject(getDs(), User.class, dbObject, new DefaultEntityCache());
    Assert.assertEquals(user.userObject, ((User) object).userObject);
    user.userObject = 33.3;
    dbObject = mapper.toDBObject(user);
    object = mapper.fromDBObject(getDs(), User.class, dbObject, new DefaultEntityCache());
    Assert.assertEquals(user.userObject, ((User) object).userObject);
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) ObjectId(org.bson.types.ObjectId) BasicDAO(org.mongodb.morphia.dao.BasicDAO) DBObject(com.mongodb.DBObject) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 5 with Mapper

use of org.mongodb.morphia.mapping.Mapper in project morphia by mongodb.

the class TestMapping method testCanMapSerializableObject.

@Test
public void testCanMapSerializableObject() {
    // given
    Mapper mapper = new Mapper();
    User user = new User();
    user.id = 1;
    user.userObject = new SerializableObject();
    // when
    DBObject dbObject = mapper.toDBObject(user);
    User object = mapper.fromDBObject(getDs(), User.class, dbObject, new DefaultEntityCache());
    // then
    assertThat(object.userObject, is(user.userObject));
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Aggregations

Mapper (org.mongodb.morphia.mapping.Mapper)62 Test (org.junit.Test)61 MappedClass (org.mongodb.morphia.mapping.MappedClass)53 MappedField (org.mongodb.morphia.mapping.MappedField)46 ArrayList (java.util.ArrayList)41 BasicDBObject (com.mongodb.BasicDBObject)9 DBObject (com.mongodb.DBObject)5 List (java.util.List)5 ObjectId (org.bson.types.ObjectId)5 SimpleEntity (org.mongodb.morphia.entities.SimpleEntity)5 Key (org.mongodb.morphia.Key)4 ParentType (org.mongodb.morphia.entities.ParentType)2 DefaultEntityCache (org.mongodb.morphia.mapping.cache.DefaultEntityCache)2 CollectionIdConverter (edu.stanford.bmir.protege.web.server.collection.CollectionIdConverter)1 CollectionItemConverter (edu.stanford.bmir.protege.web.server.collection.CollectionItemConverter)1 FormDataConverter (edu.stanford.bmir.protege.web.server.form.FormDataConverter)1 FormDataValueConverter (edu.stanford.bmir.protege.web.server.form.FormDataValueConverter)1 BsonDocument (org.bson.BsonDocument)1 BsonString (org.bson.BsonString)1 Document (org.bson.Document)1