Search in sources :

Example 6 with DefaultEntityCache

use of org.mongodb.morphia.mapping.cache.DefaultEntityCache 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)

Example 7 with DefaultEntityCache

use of org.mongodb.morphia.mapping.cache.DefaultEntityCache in project morphia by mongodb.

the class MappedFieldTest method nestedCollectionsMapping.

@Test
public void nestedCollectionsMapping() {
    final MappedField field = new MappedField(getField(TestEntity.class, "listOfListOfString"), TestEntity.class, getMorphia().getMapper());
    Assert.assertFalse(field.isSingleValue());
    Assert.assertTrue(field.isMultipleValues());
    Assert.assertFalse(field.isArray());
    Assert.assertTrue(List.class == field.getType());
    final List<MappedField> level1Types = field.getTypeParameters();
    final MappedField typeParameter = level1Types.get(0);
    Assert.assertTrue(List.class == typeParameter.getConcreteType());
    final List<MappedField> level2Types = typeParameter.getTypeParameters();
    final MappedField nested = level2Types.get(0);
    Assert.assertTrue(String.class == nested.getConcreteType());
    Assert.assertEquals("listOfListOfString", field.getJavaFieldName());
    Assert.assertEquals("listOfListOfString", field.getNameToStore());
    final BasicDBList list = new BasicDBList();
    list.add(dbList("a", "b", "c"));
    list.add(dbList("d", "e", "f"));
    final TestEntity entity = getMorphia().getMapper().fromDb(getDs(), new BasicDBObject("listOfListOfString", list), new TestEntity(), new DefaultEntityCache());
    final List<String> strings = asList("a", "b", "c");
    final List<String> strings1 = asList("d", "e", "f");
    final List<List<String>> expected = new ArrayList<List<String>>();
    expected.add(strings);
    expected.add(strings1);
    Assert.assertEquals(expected, entity.listOfListOfString);
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) ArrayList(java.util.ArrayList) BasicDBList(com.mongodb.BasicDBList) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) Test(org.junit.Test)

Example 8 with DefaultEntityCache

use of org.mongodb.morphia.mapping.cache.DefaultEntityCache in project morphia by mongodb.

the class TestMapping method testDbRefMapping.

@Test
public void testDbRefMapping() throws Exception {
    getMorphia().map(ContainsRef.class).map(Rectangle.class);
    final DBCollection stuff = getDb().getCollection("stuff");
    final DBCollection rectangles = getDb().getCollection("rectangles");
    assertTrue("'ne' field should not be persisted!", !getMorphia().getMapper().getMCMap().get(ContainsRef.class.getName()).containsJavaFieldName("ne"));
    final Rectangle r = new Rectangle(1, 1);
    final DBObject rDbObject = getMorphia().toDBObject(r);
    rDbObject.put("_ns", rectangles.getName());
    rectangles.save(rDbObject);
    final ContainsRef cRef = new ContainsRef();
    cRef.rect = new DBRef((String) rDbObject.get("_ns"), rDbObject.get("_id"));
    final DBObject cRefDbObject = getMorphia().toDBObject(cRef);
    stuff.save(cRefDbObject);
    final BasicDBObject cRefDbObjectLoaded = (BasicDBObject) stuff.findOne(BasicDBObjectBuilder.start("_id", cRefDbObject.get("_id")).get());
    final ContainsRef cRefLoaded = getMorphia().fromDBObject(getDs(), ContainsRef.class, cRefDbObjectLoaded, new DefaultEntityCache());
    assertNotNull(cRefLoaded);
    assertNotNull(cRefLoaded.rect);
    assertNotNull(cRefLoaded.rect.getId());
    assertNotNull(cRefLoaded.rect.getCollectionName());
    assertEquals(cRefLoaded.rect.getId(), cRef.rect.getId());
    assertEquals(cRefLoaded.rect.getCollectionName(), cRef.rect.getCollectionName());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Rectangle(org.mongodb.morphia.testmodel.Rectangle) DBRef(com.mongodb.DBRef) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 9 with DefaultEntityCache

use of org.mongodb.morphia.mapping.cache.DefaultEntityCache in project morphia by mongodb.

the class TestMapping method performBasicMappingTest.

private void performBasicMappingTest() {
    final DBCollection hotels = getDb().getCollection("hotels");
    final DBCollection agencies = getDb().getCollection("agencies");
    getMorphia().map(Hotel.class);
    getMorphia().map(TravelAgency.class);
    final Hotel borg = new Hotel();
    borg.setName("Hotel Borg");
    borg.setStars(4);
    borg.setTakesCreditCards(true);
    borg.setStartDate(new Date());
    borg.setType(Hotel.Type.LEISURE);
    borg.getTags().add("Swimming pool");
    borg.getTags().add("Room service");
    borg.setTemp("A temporary transient value");
    borg.getPhoneNumbers().add(new PhoneNumber(354, 5152233, PhoneNumber.Type.PHONE));
    borg.getPhoneNumbers().add(new PhoneNumber(354, 5152244, PhoneNumber.Type.FAX));
    final Address address = new Address();
    address.setStreet("Posthusstraeti 11");
    address.setPostCode("101");
    borg.setAddress(address);
    BasicDBObject hotelDbObj = (BasicDBObject) getMorphia().toDBObject(borg);
    assertTrue(!(((DBObject) ((List) hotelDbObj.get("phoneNumbers")).get(0)).containsField(Mapper.CLASS_NAME_FIELDNAME)));
    hotels.save(hotelDbObj);
    Hotel borgLoaded = getMorphia().fromDBObject(getDs(), Hotel.class, hotelDbObj, new DefaultEntityCache());
    assertEquals(borg.getName(), borgLoaded.getName());
    assertEquals(borg.getStars(), borgLoaded.getStars());
    assertEquals(borg.getStartDate(), borgLoaded.getStartDate());
    assertEquals(borg.getType(), borgLoaded.getType());
    assertEquals(borg.getAddress().getStreet(), borgLoaded.getAddress().getStreet());
    assertEquals(borg.getTags().size(), borgLoaded.getTags().size());
    assertEquals(borg.getTags(), borgLoaded.getTags());
    assertEquals(borg.getPhoneNumbers().size(), borgLoaded.getPhoneNumbers().size());
    assertEquals(borg.getPhoneNumbers().get(1), borgLoaded.getPhoneNumbers().get(1));
    assertNull(borgLoaded.getTemp());
    assertTrue(borgLoaded.getPhoneNumbers() instanceof Vector);
    assertNotNull(borgLoaded.getId());
    final TravelAgency agency = new TravelAgency();
    agency.setName("Lastminute.com");
    agency.getHotels().add(borgLoaded);
    final BasicDBObject agencyDbObj = (BasicDBObject) getMorphia().toDBObject(agency);
    agencies.save(agencyDbObj);
    final TravelAgency agencyLoaded = getMorphia().fromDBObject(getDs(), TravelAgency.class, agencies.findOne(new BasicDBObject(Mapper.ID_KEY, agencyDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    assertEquals(agency.getName(), agencyLoaded.getName());
    assertEquals(1, agency.getHotels().size());
    assertEquals(agency.getHotels().get(0).getName(), borg.getName());
    // try clearing values
    borgLoaded.setAddress(null);
    borgLoaded.getPhoneNumbers().clear();
    borgLoaded.setName(null);
    hotelDbObj = (BasicDBObject) getMorphia().toDBObject(borgLoaded);
    hotels.save(hotelDbObj);
    hotelDbObj = (BasicDBObject) hotels.findOne(new BasicDBObject(Mapper.ID_KEY, hotelDbObj.get(Mapper.ID_KEY)));
    borgLoaded = getMorphia().fromDBObject(getDs(), Hotel.class, hotelDbObj, new DefaultEntityCache());
    assertNull(borgLoaded.getAddress());
    assertEquals(0, borgLoaded.getPhoneNumbers().size());
    assertNull(borgLoaded.getName());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) TravelAgency(org.mongodb.morphia.testmodel.TravelAgency) Address(org.mongodb.morphia.testmodel.Address) PhoneNumber(org.mongodb.morphia.testmodel.PhoneNumber) ArrayList(java.util.ArrayList) List(java.util.List) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) Vector(java.util.Vector) Hotel(org.mongodb.morphia.testmodel.Hotel) Date(java.util.Date)

Aggregations

DefaultEntityCache (org.mongodb.morphia.mapping.cache.DefaultEntityCache)9 BasicDBObject (com.mongodb.BasicDBObject)7 Test (org.junit.Test)7 DBObject (com.mongodb.DBObject)6 DBCollection (com.mongodb.DBCollection)5 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 List (java.util.List)2 Mapper (org.mongodb.morphia.mapping.Mapper)2 Rectangle (org.mongodb.morphia.testmodel.Rectangle)2 BasicDBList (com.mongodb.BasicDBList)1 DBRef (com.mongodb.DBRef)1 Arrays.asList (java.util.Arrays.asList)1 LinkedList (java.util.LinkedList)1 Vector (java.util.Vector)1 ObjectId (org.bson.types.ObjectId)1 BasicDAO (org.mongodb.morphia.dao.BasicDAO)1 EntityCache (org.mongodb.morphia.mapping.cache.EntityCache)1 Address (org.mongodb.morphia.testmodel.Address)1 Article (org.mongodb.morphia.testmodel.Article)1