use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.
the class IndexHelperTest method findField.
@Test
public void findField() {
MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class);
assertEquals("indexName", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), singletonList("indexName")));
assertEquals("nest.name", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), asList("nested", "name")));
assertEquals("nest.name", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), asList("nest", "name")));
try {
assertEquals("nest.whatsit", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), asList("nest", "whatsit")));
fail("Should have failed on the bad index path");
} catch (MappingException e) {
// alles ist gut
}
assertEquals("nest.whatsit.nested.more.deeply.than.the.object.model", indexHelper.findField(mappedClass, new IndexOptionsBuilder().disableValidation(true), asList("nest", "whatsit", "nested", "more", "deeply", "than", "the", "object", "model")));
}
use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.
the class ReflectionUtils method getParameterizedType.
/**
* Returns the parameterized type for a field
*
* @param field the field to examine
* @param index the location of the parameter to return
* @return the type
*/
public static Type getParameterizedType(final Field field, final int index) {
if (field != null) {
if (field.getGenericType() instanceof ParameterizedType) {
final ParameterizedType type = (ParameterizedType) field.getGenericType();
if ((type.getActualTypeArguments() != null) && (type.getActualTypeArguments().length <= index)) {
return null;
}
final Type paramType = type.getActualTypeArguments()[index];
if (paramType instanceof GenericArrayType) {
//((GenericArrayType) paramType).getGenericComponentType();
return paramType;
} else {
if (paramType instanceof ParameterizedType) {
return paramType;
} else {
if (paramType instanceof TypeVariable) {
// paramType).getName() + "> = " + ((TypeVariable) paramType).getBounds()[0]);
return paramType;
} else if (paramType instanceof WildcardType) {
return paramType;
} else if (paramType instanceof Class) {
return paramType;
} else {
throw new MappingException("Unknown type... pretty bad... call for help, wave your hands... yeah!");
}
}
}
}
// Not defined on field, but may be on class or super class...
return getParameterizedClass(field.getType());
}
return null;
}
use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.
the class IndexHelperTest method createIndex.
@Test
public void createIndex() {
checkMinServerVersion(3.4);
String collectionName = getDs().getCollection(IndexedClass.class).getName();
MongoCollection<Document> collection = getDatabase().getCollection(collectionName);
Mapper mapper = getMorphia().getMapper();
indexHelper.createIndex(collection, mapper.getMappedClass(IndexedClass.class), false);
List<DBObject> indexInfo = getDs().getCollection(IndexedClass.class).getIndexInfo();
assertEquals("Should have 6 indexes", 6, indexInfo.size());
for (DBObject dbObject : indexInfo) {
String name = dbObject.get("name").toString();
if (name.equals("latitude_1")) {
assertEquals(parse("{ 'latitude' : 1 }"), dbObject.get("key"));
} else if (name.equals("behind_interface")) {
assertEquals(parse("{ 'nest.name' : -1} "), dbObject.get("key"));
assertEquals(parse("{ 'locale' : 'en' , 'caseLevel' : false , 'caseFirst' : 'off' , 'strength' : 2 , 'numericOrdering' :" + " false , 'alternate' : 'non-ignorable' , 'maxVariable' : 'punct' , 'normalization' : false , " + "'backwards' : false , 'version' : '57.1'}"), dbObject.get("collation"));
} else if (name.equals("nest.name_1")) {
assertEquals(parse("{ 'nest.name' : 1} "), dbObject.get("key"));
} else if (name.equals("searchme")) {
assertEquals(parse("{ 'text' : 10 }"), dbObject.get("weights"));
} else if (name.equals("indexName_1")) {
assertEquals(parse("{'indexName': 1 }"), dbObject.get("key"));
} else {
if (!"_id_".equals(dbObject.get("name"))) {
throw new MappingException("Found an index I wasn't expecting: " + dbObject);
}
}
}
collection = getDatabase().getCollection(getDs().getCollection(AbstractParent.class).getName());
indexHelper.createIndex(collection, mapper.getMappedClass(AbstractParent.class), false);
indexInfo = getDs().getCollection(AbstractParent.class).getIndexInfo();
assertTrue("Shouldn't find any indexes: " + indexInfo, indexInfo.isEmpty());
}
use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.
the class IndexHelperTest method calculateBadKeys.
@Test
public void calculateBadKeys() {
MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class);
IndexBuilder index = new IndexBuilder().fields(new FieldBuilder().value("texting").type(IndexType.TEXT).weight(1), new FieldBuilder().value("nest").type(IndexType.DESC));
try {
indexHelper.calculateKeys(mappedClass, index);
fail("Validation should have failed on the bad key");
} catch (MappingException e) {
// all good
}
index.options(new IndexOptionsBuilder().disableValidation(true));
indexHelper.calculateKeys(mappedClass, index);
}
use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.
the class DatastoreImpl method toDbObject.
private <T> DBObject toDbObject(final T ent, final Map<Object, DBObject> involvedObjects) {
final MappedClass mc = mapper.getMappedClass(ent);
if (mc.getAnnotation(NotSaved.class) != null) {
throw new MappingException(format("Entity type: %s is marked as NotSaved which means you should not try to save it!", mc.getClazz().getName()));
}
DBObject dbObject = entityToDBObj(ent, involvedObjects);
List<MappedField> versionFields = mc.getFieldsAnnotatedWith(Version.class);
for (MappedField mappedField : versionFields) {
String name = mappedField.getNameToStore();
if (dbObject.get(name) == null) {
dbObject.put(name, 1);
mappedField.setFieldValue(ent, 1L);
}
}
return dbObject;
}
Aggregations