/** * 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 testUserData() { getMorphia().map(MapOfListString.class); MapOfListString ud = new MapOfListString(); ud.id = "123"; ArrayList<String> d = new ArrayList<String>(); d.add("1"); d.add("2"); d.add("3"); ud.data.put("123123", d); getDs().save(ud); }
@Before @Override public void setUp() { super.setUp(); getMorphia().map(ContainsByte.class, ContainsDouble.class, ContainsFloat.class, ContainsShort.class); }
@Test public void validationDocuments() { Document validator = Document.parse("{ \"jelly\" : { \"$ne\" : \"rhubarb\" } }"); getMorphia().map(DocumentValidation.class); MappedClass mappedClass = getMorphia().getMapper().getMappedClass(DocumentValidation.class); for (ValidationLevel level : EnumSet.allOf(ValidationLevel.class)) { for (ValidationAction action : EnumSet.allOf(ValidationAction.class)) { checkValidation(validator, mappedClass, level, action); } } }
@Test(expected = ConstraintViolationException.class) public void testCheck() { final E e = new E(); getDs().save(e); Assert.assertTrue(e.document.contains("myFunkyR")); getMorphia().map(E2.class); }
@Test public void testMapAsId() { getMorphia().map(MapAsId.class); final MapAsId mai = new MapAsId(); mai.id.put("test", "string"); final Key<MapAsId> maiKey = getDs().save(mai); final MapAsId maiLoaded = getDs().get(MapAsId.class, new BasicDBObject("test", "string")); assertNotNull(maiLoaded); assertNotNull(maiKey); }
@Test public void testCanCreate2dSphereIndexes() { // given getMorphia().map(Place.class); // when getDs().ensureIndexes(); // then List<DBObject> indexInfo = getDs().getCollection(Place.class).getIndexInfo(); assertThat(indexInfo.size(), is(2)); assertThat(indexInfo, hasIndexNamed("location_2dsphere")); }
@Test public void testTimestampMapping() { getMorphia().map(ContainsTimestamp.class); final ContainsTimestamp cts = new ContainsTimestamp(); final Key<ContainsTimestamp> savedKey = getDs().save(cts); final ContainsTimestamp loaded = getDs().get(ContainsTimestamp.class, savedKey.getId()); assertNotNull(loaded.ts); assertEquals(loaded.ts.getTime(), cts.ts.getTime()); }
@Test public void testListOfListOfPerson() { getMorphia().map(ListListPerson.class); ListListPerson list = new ListListPerson(); list.list.add(asList(new Person("Peter"), new Person("Paul"), new Person("Mary"))); list.list.add(asList(new Person("Crosby"), new Person("Stills"), new Person("Nash"))); getDs().save(list); ListListPerson result = getDs().find(ListListPerson.class).find(new FindOptions().limit(1)).tryNext(); Assert.assertEquals(list, result); }
@Test public void testListOfMapOfEntity() { getMorphia().map(ListMapPerson.class); ListMapPerson listMap = new ListMapPerson(); listMap.list.add(map("Rick", new Person("Richard"))); listMap.list.add(map("Bill", new Person("William"))); getDs().save(listMap); Assert.assertEquals(listMap, getDs().find(ListMapPerson.class).find(new FindOptions().limit(1)).tryNext()); }
@Test public void testGlobalEntityInterceptorWorksAfterEntityCallback() { getMorphia().map(E.class); getMorphia().getMapper().addInterceptor(new Interceptor()); getDs().save(new E()); }
@Test public void testCollectionMapping() { getMorphia().map(ContainsCollection.class); final Key<ContainsCollection> savedKey = getDs().save(new ContainsCollection()); final ContainsCollection loaded = getDs().get(ContainsCollection.class, savedKey.getId()); assertEquals(loaded.coll, (new ContainsCollection()).coll); assertNotNull(loaded.id); }
@Test public void testEmbeddedDBObject() { getMorphia().map(ContainsDBObject.class); getDs().save(new ContainsDBObject()); assertNotNull(getDs().find(ContainsDBObject.class) .find(new FindOptions().limit(1)) .next()); }
@Test public void testSerializedMapping() { getMorphia().map(ContainsSerializedData.class); final Key<ContainsSerializedData> savedKey = getDs().save(new ContainsSerializedData()); final ContainsSerializedData loaded = getDs().get(ContainsSerializedData.class, savedKey.getId()); assertNotNull(loaded.data); assertEquals(loaded.data.someString, (new ContainsSerializedData()).data.someString); assertNotNull(loaded.id); }
@Test(expected = ConcurrentModificationException.class) public void testConcurrentModDetection() throws Exception { getMorphia().map(ALongPrimitive.class); final ALongPrimitive a = new ALongPrimitive(); Assert.assertEquals(0, a.version); getDs().save(a); getDs().save(getDs().get(a)); getDs().save(a); }
@Test public void interfaces() { getMorphia().map(WithNested.class, Nested.class, NestedImpl.class, AnotherNested.class); Mapper mapper = getMorphia().getMapper(); MappedClass mappedClass = mapper.getMappedClass(WithNested.class); PathTarget pathTarget = new PathTarget(mapper, mappedClass, "nested.value"); Assert.assertEquals("nested.value", pathTarget.translatedPath()); Assert.assertEquals(mapper.getMappedClass(AnotherNested.class).getMappedFieldByJavaField("value"), pathTarget.getTarget()); pathTarget = new PathTarget(mapper, mappedClass, "nested.field"); Assert.assertEquals("nested.field", pathTarget.translatedPath()); Assert.assertEquals(mapper.getMappedClass(NestedImpl.class).getMappedFieldByJavaField("field"), pathTarget.getTarget()); }
@Test @SuppressWarnings("deprecation") public void testMultipleIndexedFields() { final MappedClass mc = getMorphia().getMapper().getMappedClass(Ad.class); getMorphia().map(Ad.class); assertThat(getDb().getCollection(mc.getCollectionName()).getIndexInfo(), doesNotHaveIndexNamed("lastMod_1_active_-1")); getDs().ensureIndex(Ad.class, "lastMod, -active"); assertThat(getDb().getCollection(mc.getCollectionName()).getIndexInfo(), hasIndexNamed("lastMod_1_active_-1")); }