Search in sources :

Example 1 with ShapeShifter

use of org.mongodb.morphia.testmodel.ShapeShifter in project morphia by mongodb.

the class TestInterfaces method testDynamicInstantiation.

@Test
public void testDynamicInstantiation() throws Exception {
    final DBCollection shapes = getDb().getCollection("shapes");
    final DBCollection shapeshifters = getDb().getCollection("shapeshifters");
    getMorphia().map(Circle.class).map(Rectangle.class).map(ShapeShifter.class);
    final Shape rectangle = new Rectangle(2, 5);
    final DBObject rectangleDbObj = getMorphia().toDBObject(rectangle);
    shapes.save(rectangleDbObj);
    final BasicDBObject rectangleDbObjLoaded = (BasicDBObject) shapes.findOne(new BasicDBObject(Mapper.ID_KEY, rectangleDbObj.get(Mapper.ID_KEY)));
    final Shape rectangleLoaded = getMorphia().fromDBObject(getDs(), Shape.class, rectangleDbObjLoaded, new DefaultEntityCache());
    assertTrue(rectangle.getArea() == rectangleLoaded.getArea());
    assertTrue(rectangleLoaded instanceof Rectangle);
    final ShapeShifter shifter = new ShapeShifter();
    shifter.setReferencedShape(rectangleLoaded);
    shifter.setMainShape(new Circle(2.2));
    shifter.getAvailableShapes().add(new Rectangle(3, 3));
    shifter.getAvailableShapes().add(new Circle(4.4));
    final DBObject shifterDbObj = getMorphia().toDBObject(shifter);
    shapeshifters.save(shifterDbObj);
    final BasicDBObject shifterDbObjLoaded = (BasicDBObject) shapeshifters.findOne(new BasicDBObject(Mapper.ID_KEY, shifterDbObj.get(Mapper.ID_KEY)));
    final ShapeShifter shifterLoaded = getMorphia().fromDBObject(getDs(), ShapeShifter.class, shifterDbObjLoaded, new DefaultEntityCache());
    assertNotNull(shifterLoaded);
    assertNotNull(shifterLoaded.getReferencedShape());
    assertNotNull(shifterLoaded.getReferencedShape().getArea());
    assertNotNull(rectangle);
    assertNotNull(rectangle.getArea());
    assertTrue(rectangle.getArea() == shifterLoaded.getReferencedShape().getArea());
    assertTrue(shifterLoaded.getReferencedShape() instanceof Rectangle);
    assertTrue(shifter.getMainShape().getArea() == shifterLoaded.getMainShape().getArea());
    assertEquals(shifter.getAvailableShapes().size(), shifterLoaded.getAvailableShapes().size());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Circle(org.mongodb.morphia.testmodel.Circle) Shape(org.mongodb.morphia.testmodel.Shape) Rectangle(org.mongodb.morphia.testmodel.Rectangle) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ShapeShifter(org.mongodb.morphia.testmodel.ShapeShifter) Test(org.junit.Test)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)1 DBCollection (com.mongodb.DBCollection)1 DBObject (com.mongodb.DBObject)1 Test (org.junit.Test)1 DefaultEntityCache (org.mongodb.morphia.mapping.cache.DefaultEntityCache)1 Circle (org.mongodb.morphia.testmodel.Circle)1 Rectangle (org.mongodb.morphia.testmodel.Rectangle)1 Shape (org.mongodb.morphia.testmodel.Shape)1 ShapeShifter (org.mongodb.morphia.testmodel.ShapeShifter)1