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));
}
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);
}
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());
}
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());
}
Aggregations