Search in sources :

Example 81 with MappedClass

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

the class TestIndexed method testIndexedRecursiveEntity.

@Test
public void testIndexedRecursiveEntity() throws Exception {
    final MappedClass mc = getMorphia().getMapper().getMappedClass(CircularEmbeddedEntity.class);
    getDs().ensureIndexes();
    assertThat(getDb().getCollection(mc.getCollectionName()).getIndexInfo(), hasIndexNamed("a_1"));
}
Also used : MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 82 with MappedClass

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

the class PathTargetTest method arrays.

@Test
public void arrays() {
    getMorphia().map(EntityWithListsAndArrays.class, EmbeddedType.class, Student.class);
    Mapper mapper = getMorphia().getMapper();
    MappedClass mappedClass = mapper.getMappedClass(EntityWithListsAndArrays.class);
    PathTarget pathTarget = new PathTarget(mapper, mappedClass, "listEmbeddedType.1.number");
    Assert.assertEquals("listEmbeddedType.1.number", pathTarget.translatedPath());
    Assert.assertEquals(mapper.getMappedClass(EmbeddedType.class).getMappedFieldByJavaField("number"), pathTarget.getTarget());
    assertEquals("listEmbeddedType.$", new PathTarget(mapper, mappedClass, "listEmbeddedType.$").translatedPath());
    assertEquals("listEmbeddedType.1", new PathTarget(mapper, mappedClass, "listEmbeddedType.1").translatedPath());
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 83 with MappedClass

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

the class PathTargetTest method interfaces.

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

Example 84 with MappedClass

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

the class PathTargetTest method maps.

@Test
public void maps() {
    getMorphia().map(Student.class, Article.class);
    Mapper mapper = getMorphia().getMapper();
    MappedClass mappedClass = mapper.getMappedClass(Student.class);
    PathTarget pathTarget = new PathTarget(mapper, mappedClass, "grades.$.data.name");
    Assert.assertEquals("grades.$.d.name", pathTarget.translatedPath());
    Assert.assertEquals(mapper.getMappedClass(Grade.class).getMappedFieldByJavaField("data"), pathTarget.getTarget());
    pathTarget = new PathTarget(mapper, mappedClass, "grades.$.d.name");
    Assert.assertEquals("grades.$.d.name", pathTarget.translatedPath());
    Assert.assertEquals(mapper.getMappedClass(Grade.class).getMappedField("d"), pathTarget.getTarget());
    pathTarget = new PathTarget(mapper, mapper.getMappedClass(Article.class), "translations");
    Assert.assertEquals("translations", pathTarget.translatedPath());
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 85 with MappedClass

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

the class TestMapping method testToMongoObjectCorrectlyMapsSerializableFieldForIssue591.

@Test
public void testToMongoObjectCorrectlyMapsSerializableFieldForIssue591() {
    // given
    Mapper mapper = new Mapper();
    User user = new User();
    user.id = 1;
    user.userObject = new SerializableObject();
    MappedClass mc = new MappedClass(User.class, mapper);
    MappedField mf = mc.getMappedField("userObject");
    // when
    Object dbValue = mapper.toMongoObject(mf, null, user.userObject);
    Class<byte[]> byteArrayClass = byte[].class;
    // then
    assertThat(dbValue, is(instanceOf(byteArrayClass)));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Mapper(org.mongodb.morphia.mapping.Mapper) DBObject(com.mongodb.DBObject) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Aggregations

MappedClass (org.mongodb.morphia.mapping.MappedClass)92 Test (org.junit.Test)73 MappedField (org.mongodb.morphia.mapping.MappedField)56 Mapper (org.mongodb.morphia.mapping.Mapper)53 ArrayList (java.util.ArrayList)48 BasicDBObject (com.mongodb.BasicDBObject)18 DBObject (com.mongodb.DBObject)12 BsonDocument (org.bson.BsonDocument)8 Document (org.bson.Document)8 MappingException (org.mongodb.morphia.mapping.MappingException)6 DBCollection (com.mongodb.DBCollection)5 List (java.util.List)5 SimpleEntity (org.mongodb.morphia.entities.SimpleEntity)5 ObjectId (org.bson.types.ObjectId)4 Key (org.mongodb.morphia.Key)4 Index (org.mongodb.morphia.annotations.Index)4 LinkedHashMap (java.util.LinkedHashMap)3 NotSaved (org.mongodb.morphia.annotations.NotSaved)3 UpdateResults (org.mongodb.morphia.query.UpdateResults)3 WriteResult (com.mongodb.WriteResult)2