### Question:
MongoMetadataServiceImpl implements IMetadataService { @Override public MetaClass createMetaClass(MetaClass metaClass, MetadataContext context) { try { metadataLock.lock(); if (metaClass == null) { throw new IllegalMetaClassException("meta class can not be null"); } if (org.apache.commons.lang.StringUtils.isEmpty(metaClass.getName())) { throw new IllegalMetaClassException("meta class must have a name"); } metaClass.setMetadataService(this); Collection<MetaClass> mergedMetaClasses = new ArrayList<MetaClass>(); for (MetaClass mc : graph.getMetaClasses()) { mergedMetaClasses.add(mc); } mergedMetaClasses.add(metaClass); MetaClassGraph tempGraph = new MetaClassGraph(mergedMetaClasses); for (MetaClass mc : mergedMetaClasses) { mc.setMetadataService(this); tempGraph.updateMetaClass(mc); } validator.validateForCreation(metaClass, Collections.<String, MetaClass> emptyMap(), tempGraph); if (!repo.getRepositoryName().equals(metaClass.getRepository())) { throw new IllegalMetaClassException("inconsistency repo name: should be " + repo.getRepositoryName() + " , but was " + metaClass.getRepository()); } return innerCreateMetaClass(metaClass, true, context); } catch (InterruptedException e) { logger.info("lock interrupted for createMetaClass {}", metaClass.getName()); throw new MetaDataException(MetaErrCodeEnum.LOCK_INTERRUPTED, "lock interrupted for createMetaClass " + metaClass.getName(), e); } finally { metadataLock.unlock(); } } MongoMetadataServiceImpl(Mongo mongo, int maxCacheSize,
            int cacheExpiredTime, int collectionCountCacheSize, int collectionCountCacheExpiredTime,
            Repository repo, ICMSLock metadataLock); @Override Repository getRepository(); @Override MetaClass getMetaClass(String className); @Override MetaClass getMetaClass(String className, int version, MetadataContext context); @Override MetaClass createMetaClass(MetaClass metaClass, MetadataContext context); @Override void deleteMetaClass(String className, MetadataContext metaContext); @Override MetaClass deleteMetaField(String className, String fieldName, MetadataContext metaContext); @Override MetaClass updateMetaClass(MetaClass metaClass, MetadataContext context); @Override List<MetaClass> batchUpsert(List<MetaClass> metaClasses, MetadataContext context); @Override final List<MetaClass> getMetaClasses(MetadataContext ctx); @Override void updateMetaOption(String className, MetaOption options, MetadataContext context); @Override int getCollectionCount(String dbCollectionName); @Override MetaClass updateMetaField(MetaClass metaClass, String fieldName, MetadataContext context); @Override void validateMetaClass(String className); @Override void setMetaHistoryService(IMetadataHistoryService historyService); static final Logger logger; }### Answer:
@Test(expected=IllegalMetaClassException.class) public void testInternalFields() { MetaClass metaClass = createMetaClass(); MetaAttribute attribute = new MetaAttribute(); attribute.setName("_type"); attribute.setDataType(MetaField.DataTypeEnum.INTEGER); metaClass.addField(attribute); }
@Test(expected = IllegalMetaClassException.class) public void testCreateMetaclassNull() { repo.getMetadataService().createMetaClass(null, new MetadataContext()); }
@Test public void testCreateMetaclassWithReservedNames() { for (String name : MetaClass.RESERVED_METACLASS_NAME) { try { MetaClass m = createMetaClass(); m.setName(name); repo.getMetadataService().createMetaClass(m, new MetadataContext()); Assert.fail("should have exception"); } catch (Throwable e) { Assert.assertTrue(e instanceof IllegalMetaClassException); } } MetaClass m = createMetaClass(); m.setName("reservedFieldName"); MetaAttribute attribute = new MetaAttribute(); attribute.setName("_length"); attribute.setDataType(DataTypeEnum.STRING); m.addField(attribute); try { repo.getMetadataService().createMetaClass(m, new MetadataContext()); Assert.fail(); } catch (Throwable e) { Assert.assertTrue(e instanceof IllegalMetaClassException); } }