@Test public void testMorphiaDS() { new Morphia().createDatastore(getMongoClient(), "test"); }
@Test public void testCanMapAnEntityWithAnAbstractVersionedParent() { Morphia morphia = getMorphia().map(VersionedChildEntity.class); Collection<MappedClass> mappedClasses = morphia.getMapper().getMappedClasses(); assertThat(mappedClasses.size(), is(2)); List<Class<?>> list = new ArrayList<Class<?>>(); for (MappedClass mappedClass : mappedClasses) { list.add(mappedClass.getClazz()); } assertTrue(list.contains(VersionedChildEntity.class)); assertTrue(list.contains(AbstractVersionedBase.class)); }
@Test public void shouldOnlyMapEntitiesInTheGivenPackage() { // when final Morphia morphia = new Morphia(); morphia.mapPackage("xyz.morphia.testmappackage"); // then Collection<MappedClass> mappedClasses = morphia.getMapper().getMappedClasses(); assertThat(mappedClasses.size(), is(1)); assertEquals(mappedClasses.iterator().next().getClazz(), SimpleEntity.class); }
/** * Creates a Morphia instance with the given Mapper and class set. * * @param mapper the Mapper to use * @param classesToMap the classes to map */ public Morphia(final Mapper mapper, final Set<Class> classesToMap) { this.mapper = mapper; for (final Class c : classesToMap) { map(c); } }
@Test public void testBasicMappingWithCachedClasses() { getMorphia().getMapper().getOptions().setCacheClassLookups(true); try { performBasicMappingTest(); final DefaultCreator objectFactory = (DefaultCreator) getMorphia().getMapper().getOptions().getObjectFactory(); assertTrue(objectFactory.getClassNameCache().containsKey(Hotel.class.getName())); assertTrue(objectFactory.getClassNameCache().containsKey(TravelAgency.class.getName())); } finally { getMorphia().getMapper().getOptions().setCacheClassLookups(false); } }
@Test public void testDbRefMapping() { 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()); }
final DBCollection stuff = getDb().getCollection("stuff"); getMorphia().map(RecursiveParent.class).map(RecursiveChild.class); final DBObject parentDbObj = getMorphia().toDBObject(parent); stuff.save(parentDbObj); final DBObject childDbObj = getMorphia().toDBObject(child); stuff.save(childDbObj); final RecursiveParent parentLoaded = getMorphia().fromDBObject(getDs(), RecursiveParent.class, stuff.findOne(new BasicDBObject("_id", parentDbObj.get("_id"))), new DefaultEntityCache()); final RecursiveChild childLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject("_id", childDbObj.get("_id"))), stuff.save(getMorphia().toDBObject(parentLoaded)); stuff.save(getMorphia().toDBObject(childLoaded)); final RecursiveParent finalParentLoaded = getMorphia().fromDBObject(getDs(), RecursiveParent.class, stuff.findOne(new BasicDBObject("_id", parentDbObj.get("_id"))), new DefaultEntityCache()); final RecursiveChild finalChildLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject("_id", childDbObj.get("_id"))),
@Test public void testEmbeddedEntityDBObjectHasNoClassname() { getMorphia().map(ContainsEmbeddedEntity.class); final ContainsEmbeddedEntity cee = new ContainsEmbeddedEntity(); cee.cil = new ContainsIntegerList(); cee.cil.intList = Collections.singletonList(1); final DBObject dbObj = getMorphia().toDBObject(cee); assertTrue(!((DBObject) dbObj.get("cil")).containsField( getMorphia().getMapper().getOptions().getDiscriminatorField())); }
public void testMapperPerformance() { Morphia morphia = getMorphia(); Mapper mapper = morphia.getMapper(); mapper.addMappedClass(Container.class); Datastore ds = morphia.createDatastore(getMongoClient(), "testDB"); for (int i = 0; i < iterations; i++) { long start = System.nanoTime(); morphia.fromDBObject(ds, Container.class, p, entityCache); long stop = System.nanoTime();
private void morphify() { String packageName = this.config.getString(MORPHIA_PACKAGE, DEFAULT_MORPHIA_PACKAGE); String dbName = this.config.getString(MONGODB_DBNAME, DEFAULT_MONGODB_NAME); this.morphia = new Morphia().mapPackage(packageName); this.datastore = this.morphia.createDatastore(this.mongoClient, dbName); LOG.info("Mapped Morphia models of package '" + packageName + "' and created Morphia Datastore with database '" + dbName + "'"); }
@Test public void testCanMapAPackageContainingAVersionedAbstractBaseClass() { Morphia morphia = getMorphia().mapPackage("xyz.morphia.entities.version"); Collection<MappedClass> mappedClasses = morphia.getMapper().getMappedClasses(); assertThat(mappedClasses.size(), is(2)); List<Class<?>> list = new ArrayList<Class<?>>(); for (MappedClass mappedClass : mappedClasses) { list.add(mappedClass.getClazz()); } assertTrue(list.contains(VersionedChildEntity.class)); assertTrue(list.contains(AbstractVersionedBase.class)); }
@Test public void testMapping() { getMorphia().map(ClassLevelThree.class); final ClassLevelThree sp = new ClassLevelThree(); //Old way final DBObject wrapObj = getMorphia().toDBObject(sp); //the error points here from the user getDs().getDB().getCollection("testColl").save(wrapObj); //better way getDs().save(sp); }
@Test public void testMultipleDatabasesSingleThreaded() { getMorphia().map(FacebookUser.class); final Datastore ds1 = getMorphia().createDatastore(getMongoClient(), "db1"); final Datastore ds2 = getMorphia().createDatastore(getMongoClient(), "db2"); final FacebookUser db1Friend = new FacebookUser(3, "DB1 FaceBook Friend"); ds1.save(db1Friend); final FacebookUser db1User = new FacebookUser(1, "DB1 FaceBook User"); db1User.friends.add(db1Friend); ds1.save(db1User); final FacebookUser db2Friend = new FacebookUser(4, "DB2 FaceBook Friend"); ds2.save(db2Friend); final FacebookUser db2User = new FacebookUser(2, "DB2 FaceBook User"); db2User.friends.add(db2Friend); ds2.save(db2User); testFirstDatastore(ds1); testSecondDatastore(ds2); testFirstDatastore(ds1); testSecondDatastore(ds2); testFirstDatastore(ds1); testSecondDatastore(ds2); testFirstDatastore(ds1); testSecondDatastore(ds2); testStandardDatastore(); }
/** * Tries to map all classes in the package specified. Fails if one of the classes is not valid for mapping. * * @param packageName the name of the package to process * @return the Morphia instance */ public synchronized Morphia mapPackage(final String packageName) { return mapPackage(packageName, false); }
protected void initDS(final MongoClient mongoClient, final Morphia mor, final String db) { ds = (xyz.morphia.DatastoreImpl) mor.createDatastore(mongoClient, db); }
@Test public void shouldCorrectlySerialiseLineStringsInGeometryCollection() { // given LineString lineString = lineString(point(1, 2), point(3, 5), point(19, 13)); GeometryCollection geometryCollection = GeoJson.geometryCollection(lineString); getMorphia().getMapper().addMappedClass(Point.class); // when DBObject dbObject = getMorphia().toDBObject(geometryCollection); assertThat(dbObject, is(notNullValue())); assertThat(dbObject.toString(), JSONMatcher.jsonEqual(" {" + " type: 'GeometryCollection', " + " geometries: " + " [" + " {" + " type: 'LineString', " + " coordinates: [ [ 2.0, 1.0]," + " [ 5.0, 3.0]," + " [13.0, 19.0] ]" + " }," + " ]" + "}")); }
/** * This test is green when {@link MyEntity#valueObject} is annotated with {@code @Property}, as in this case the field is not * serialized * at all. However, the bson encoder would fail to encode the object of type ValueObject (as shown by {@link * #testFullBSONSerialization()}). */ @Test public void testDBObjectSerialization() { final MyEntity entity = new MyEntity(1L, new ValueObject(2L)); final DBObject dbObject = getMorphia().toDBObject(entity); assertEquals(new BasicDBObject("_id", 1L).append("valueObject", 2L), dbObject); assertEquals(entity, getMorphia().fromDBObject(getDs(), MyEntity.class, dbObject)); }
/** * Creates an entity and populates its state based on the dbObject given. This method is primarily an internal method. Reliance on * this method may break your application in future releases. * * @param <T> type of the entity * @param datastore the Datastore to use when fetching this reference * @param entityClass type to create * @param dbObject the object state to use * @return the newly created and populated entity */ public <T> T fromDBObject(final Datastore datastore, final Class<T> entityClass, final DBObject dbObject) { return fromDBObject(datastore, entityClass, dbObject, mapper.createEntityCache()); }
@Test public void shouldConvertPointCorrectlyToDBObject() { // given City city = new City("New City", point(3.0, 7.0)); // when DBObject dbObject = getMorphia().toDBObject(city); assertThat(dbObject, is(notNullValue())); assertThat(dbObject.toString(), JSONMatcher.jsonEqual(" {" + " name: 'New City'," + " className: 'xyz.morphia.geo.City'," + " location: " + " {" + " type: 'Point', " + " coordinates: [7.0, 3.0]" + " }" + "}")); }