Package com.github.jmkgreen.morphia.testmodel

Examples of com.github.jmkgreen.morphia.testmodel.Rectangle


    Random rnd = new Random();
   
    //create 100 circles and rectangles
    for(int i = 0; i < 100; i++){
      ads.insert("shapes", new Circle(rnd.nextDouble()));
      ads.insert("shapes", new Rectangle(rnd.nextDouble(), rnd.nextDouble()));
    }
    String map = "function () { if(this['radius']) { emit('circle', {count:1}); return; } emit('rect', {count:1}); }";
    String reduce = "function (key, values) { var total = 0; for ( var i=0; i<values.length; i++ ) {total += values[i].count;} return { count : total }; }";
    MapreduceResults<ResultEntity> mrRes = ds.mapReduce(MapreduceType.REPLACE, ads.createQuery(Shape.class), map, reduce, null, null, ResultEntity.class);
    Assert.assertEquals(2, mrRes.createQuery().countAll());
View Full Code Here


        assertEquals(0, ds.getCount(FacebookUser.class));
        assertNull(ds.exists(facebookUser));
    }
   
  public void testIdUpdatedOnSave() throws Exception {
    Rectangle rect = new Rectangle(10, 10);
    ds.save(rect);
    assertNotNull(rect.getId());
  }
View Full Code Here

    assertNotNull(rect.getId());
  }

  @Test
    public void testSaveAndDelete() throws Exception {
    Rectangle rect = new Rectangle(10, 10);
    rect.setId("1");
   
   
    //test delete(entity)
    ds.save(rect);
    assertEquals(1, ds.getCount(rect));
    ds.delete(rect);
    assertEquals(0, ds.getCount(rect));

    //test delete(entity, id)
    ds.save(rect);
    assertEquals(1, ds.getCount(rect));
    ds.delete(rect.getClass(), 1);
    assertEquals(1, ds.getCount(rect));
    ds.delete(rect.getClass(), "1");
    assertEquals(0, ds.getCount(rect));

    //test delete(entity, {id})
    ds.save(rect);
    assertEquals(1, ds.getCount(rect));
    List<String> ids = new ArrayList<String>();
    ids.add("1");
    ds.delete(rect.getClass(), ids);
    assertEquals(0, ds.getCount(rect));

    //test delete(entity, {id,id})
    rect.setId("1");
    ds.save(rect);
    rect.setId("2");
    ds.save(rect);
    assertEquals(2, ds.getCount(rect));
    ids.clear(); ids.add("1"); ids.add("2");
    ds.delete(rect.getClass(), ids);
    assertEquals(0, ds.getCount(rect));
   
    //test delete(entity, {id,id}) with autogenerated ids
    ids.clear();
    rect.setId(new ObjectId().toString()); // rect1
    ds.save(rect);
    ids.add(rect.getId());
    rect.setId(new ObjectId().toString()); // rect2
    ds.save(rect);
    ids.add(rect.getId());
    assertEquals("datastore should have saved two entities with autogenerated ids", 2, ds.getCount(rect));
    ds.delete(rect.getClass(), ids);
    assertEquals("datastore should have deleted two entities with autogenerated ids", 0, ds.getCount(rect));

    //test delete(entity, {id}) with one left
    rect.setId("1");
    ds.save(rect);
    rect.setId("2");
    ds.save(rect);
    assertEquals(2, ds.getCount(rect));
    ids.clear(); ids.add("1");
    ds.delete(rect.getClass(), ids);
    assertEquals(1, ds.getCount(rect));

    //test delete(Class, {id}) with one left
    rect.setId("1");
    ds.save(rect);
    rect.setId("2");
    ds.save(rect);
    assertEquals(2, ds.getCount(rect));
    ids.clear(); ids.add("1");
    ds.delete(Rectangle.class, ids);
    assertEquals(1, ds.getCount(rect));
View Full Code Here

*/
@SuppressWarnings({"unused"})
public class TestKeyType extends TestBase {
  @Test
    public void testKeyComparisons() throws Exception {
    Rectangle r = new Rectangle(2,1);
    Key<Rectangle> k1 = new Key<Rectangle>(Rectangle.class, r.getId());
    Key<Rectangle> k2 = this.ds.getMapper().getKey(r);

    Assert.assertTrue(k1.equals(k2));
    Assert.assertTrue(k2.equals(k1));

View Full Code Here

        morphia.map(Circle.class)
                .map(Rectangle.class)
                .map(ShapeShifter.class);

        Shape rectangle = new Rectangle(2,5);
       
        DBObject rectangleDbObj = morphia.toDBObject(rectangle);
        shapes.save(rectangleDbObj);

        BasicDBObject rectangleDbObjLoaded = (BasicDBObject) shapes.findOne(new BasicDBObject(Mapper.ID_KEY, rectangleDbObj.get(Mapper.ID_KEY)));
    Shape rectangleLoaded = morphia.fromDBObject(Shape.class, rectangleDbObjLoaded, new DefaultEntityCache());

        assertTrue(rectangle.getArea() == rectangleLoaded.getArea());
        assertTrue(rectangleLoaded instanceof Rectangle);

        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));

        DBObject shifterDbObj = morphia.toDBObject(shifter);
        shapeshifters.save(shifterDbObj);

        BasicDBObject shifterDbObjLoaded = (BasicDBObject) shapeshifters.findOne(new BasicDBObject(Mapper.ID_KEY, shifterDbObj.get(Mapper.ID_KEY)));
    ShapeShifter shifterLoaded = morphia.fromDBObject(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());
    }
View Full Code Here

  }

  @Test
    public void testSaveAndDelete() throws Exception {
    String ns = "hotels";
    Rectangle rect = new Rectangle(10, 10);
    rect.setId("1");

    db.getCollection(ns).remove(new BasicDBObject());

    //test delete(entity, id)
    ads.save(ns, rect);
View Full Code Here

  }

  @Test
    public void testGet() throws Exception {
    String ns = "hotels";
    Rectangle rect = new Rectangle(10, 10);
    rect.setId("1");

    db.getCollection(ns).remove(new BasicDBObject());

    //test delete(entity, id)
    ads.save(ns, rect);
    assertEquals(1, ads.getCount(ns));
    Rectangle rectLoaded = ads.get(ns, Rectangle.class, rect.getId());
    assertEquals(rect.getId(), rectLoaded.getId());
    assertEquals(rect.getArea(), rectLoaded.getArea(), 0);
  }
View Full Code Here

  }

  @Test
    public void testFind() throws Exception {
    String ns = "hotels";
    Rectangle rect = new Rectangle(10, 10);
    rect.setId("1");

    db.getCollection(ns).remove(new BasicDBObject());

    //test delete(entity, id)
    ads.save(ns, rect);
    assertEquals(1, ads.getCount(ns));
    Rectangle rectLoaded = ads.find(ns, Rectangle.class).get();
    assertEquals(rect.getId(), rectLoaded.getId());
    assertEquals(rect.getArea(), rectLoaded.getArea(), 0);

    rect = new Rectangle(2, 1);
    rect.setId("2");
    ads.save(rect); //saved to default collection name (kind)
    assertEquals(1, ads.getCount(rect));

    rect.setId("3");
    ads.save(rect); //saved to default collection name (kind)
    assertEquals(2, ads.getCount(rect));

    rect = new Rectangle(4, 3);
    rect.setId("3");
    ads.save(ns, rect);
    assertEquals(2, ads.getCount(ns));
    List<Rectangle> rects = ads.find(ns, Rectangle.class).asList();

    rectLoaded = rects.get(1);
    assertEquals(rect.getId(), rectLoaded.getId());
    assertEquals(rect.getArea(), rectLoaded.getArea(), 0);

    rectLoaded = ads.find(ns, Rectangle.class, "_id !=", "-1", 1, 1).get();

  }
View Full Code Here

    Key<Pic> pic;
    }

  @Test @Category(StandardTests.class)
    public void testIncDec() throws Exception {
    Rectangle[] rects = new Rectangle(1, 10),
                new Rectangle(1, 10),
                new Rectangle(1, 10),
                new Rectangle(10, 10),
                new Rectangle(10, 10),
                };
    for(Rectangle rect: rects)
      ds.save(rect);
   
    Query<Rectangle> q1 = ds.find(Rectangle.class, "height", 1D);
View Full Code Here

  @Test @Ignore("need to set the _db in the dbref for this to work... see issue 90, ")
    public void testReferenceAsId() throws Exception {
        morphia.map(ReferenceAsId.class);

        Rectangle r = new Rectangle(1,1);
        Key<Rectangle> rKey = ds.save(r);

        ReferenceAsId rai = new ReferenceAsId(r);
        Key<ReferenceAsId> raiKey = ds.save(rai);
        ReferenceAsId raiLoaded = ds.get(ReferenceAsId.class, rKey);
        assertNotNull(raiLoaded);
        assertEquals(raiLoaded.id.getArea(), r.getArea(), 0);

        assertNotNull(raiKey);
  }
View Full Code Here

TOP

Related Classes of com.github.jmkgreen.morphia.testmodel.Rectangle

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.