MappingsParser { public ClassMappings processMappings(List<ClassMap> classMaps, Configuration globalConfiguration) { if (globalConfiguration == null) { throw new IllegalArgumentException("Global configuration parameter cannot be null"); } ClassMappings result = new ClassMappings(beanContainer); if (classMaps == null || classMaps.size() == 0) { return result; } FieldMap fieldMapPrime; ClassMap classMapPrime; Set<String> mapIds = new HashSet<>(); for (ClassMap classMap : classMaps) { classMap.setGlobalConfiguration(globalConfiguration); ReflectionUtils.findPropertyDescriptor(classMap.getSrcClassToMap(), "", null); ReflectionUtils.findPropertyDescriptor(classMap.getDestClassToMap(), "", null); if (!MappingUtils.isBlankOrNull(classMap.getMapId())) { if (mapIds.contains(classMap.getMapId())) { throw new IllegalArgumentException("Duplicate Map Id's Found. Map Id: " + classMap.getMapId()); } mapIds.add(classMap.getMapId()); } result.add(classMap.getSrcClassToMap(), classMap.getDestClassToMap(), classMap.getMapId(), classMap); classMapPrime = new ClassMap(globalConfiguration); MappingUtils.reverseFields(classMap, classMapPrime, beanContainer); if (classMap.getFieldMaps() != null) { List<FieldMap> fms = classMap.getFieldMaps(); if (!MappingDirection.ONE_WAY.equals(classMap.getType())) { for (FieldMap fieldMap : fms.toArray(new FieldMap[] {})) { fieldMap.validate(); if (!(fieldMap instanceof ExcludeFieldMap)) { if ((MappingUtils.isSupportedMap(classMap.getDestClassToMap()) ^ MappingUtils.isSupportedMap(classMap.getSrcClassToMap())) || (MappingUtils.isSupportedMap(fieldMap.getDestFieldType(classMap.getDestClassToMap())) ^ MappingUtils.isSupportedMap(fieldMap.getSrcFieldType(classMap.getSrcClassToMap())))) { FieldMap fm = new MapFieldMap(fieldMap, beanContainer, destBeanCreator, propertyDescriptorFactory); classMap.removeFieldMapping(fieldMap); classMap.addFieldMapping(fm); fieldMap = fm; } } if (MappingUtils.isSupportedMap(classMap.getSrcClassToMap()) && fieldMap.getSrcFieldKey() == null) { DozerField newSrcField = fieldMap.getSrcFieldCopy(); newSrcField.setName(DozerConstants.SELF_KEYWORD); newSrcField.setKey(fieldMap.getSrcFieldName()); fieldMap.setSrcField(newSrcField); } if (MappingUtils.isSupportedMap(classMap.getDestClassToMap()) && fieldMap.getDestFieldKey() == null) { DozerField newDestField = fieldMap.getDestFieldCopy(); newDestField.setName(DozerConstants.SELF_KEYWORD); newDestField.setKey(fieldMap.getDestFieldName()); fieldMap.setDestField(newDestField); } if (!(MappingDirection.ONE_WAY.equals(fieldMap.getType()) && !(fieldMap instanceof ExcludeFieldMap))) { fieldMapPrime = (FieldMap)fieldMap.clone(); fieldMapPrime.setClassMap(classMapPrime); if (fieldMapPrime instanceof ExcludeFieldMap && MappingDirection.ONE_WAY.equals(fieldMap.getType())) { fieldMapPrime = new GenericFieldMap(classMapPrime, beanContainer, destBeanCreator, propertyDescriptorFactory); } MappingUtils.reverseFields(fieldMap, fieldMapPrime); if (!(fieldMap instanceof ExcludeFieldMap)) { MappingUtils.applyGlobalCopyByReference(globalConfiguration, fieldMap, classMap); } if (!(fieldMapPrime instanceof ExcludeFieldMap)) { MappingUtils.applyGlobalCopyByReference(globalConfiguration, fieldMapPrime, classMapPrime); } } else { fieldMapPrime = new ExcludeFieldMap(classMapPrime, beanContainer, destBeanCreator, propertyDescriptorFactory); MappingUtils.reverseFields(fieldMap, fieldMapPrime); MappingUtils.applyGlobalCopyByReference(globalConfiguration, fieldMap, classMap); } classMapPrime.addFieldMapping(fieldMapPrime); } } else { for (FieldMap oneWayFieldMap : fms.toArray(new FieldMap[] {})) { oneWayFieldMap.validate(); MappingUtils.applyGlobalCopyByReference(globalConfiguration, oneWayFieldMap, classMap); if (MappingDirection.ONE_WAY.equals(oneWayFieldMap.getType())) { fieldMapPrime = new ExcludeFieldMap(classMapPrime, beanContainer, destBeanCreator, propertyDescriptorFactory); MappingUtils.reverseFields(oneWayFieldMap, fieldMapPrime); classMapPrime.addFieldMapping(fieldMapPrime); } } } } if (!MappingDirection.ONE_WAY.equals(classMap.getType())) { result.add(classMap.getDestClassToMap(), classMap.getSrcClassToMap(), classMap.getMapId(), classMapPrime); } } return result; } MappingsParser(BeanContainer beanContainer, DestBeanCreator destBeanCreator, PropertyDescriptorFactory propertyDescriptorFactory); ClassMappings processMappings(List<ClassMap> classMaps, Configuration globalConfiguration);  }
@Test(expected = IllegalArgumentException.class) public void testDuplicateMapIds() { MappingFileReader fileReader = new MappingFileReader(new XMLParserFactory(beanContainer), new XMLParser(beanContainer, destBeanCreator, propertyDescriptorFactory), beanContainer); MappingFileData mappingFileData = fileReader.read("mappings/duplicateMapIdsMapping.xml"); parser.processMappings(mappingFileData.getClassMaps(), new Configuration()); fail("should have thrown exception"); }
@Test(expected = IllegalArgumentException.class) public void testDetectDuplicateMapping() { MappingFileReader fileReader = new MappingFileReader(new XMLParserFactory(beanContainer), new XMLParser(beanContainer, destBeanCreator, propertyDescriptorFactory), beanContainer); MappingFileData mappingFileData = fileReader.read("mappings/duplicateMapping.xml"); parser.processMappings(mappingFileData.getClassMaps(), new Configuration()); fail("should have thrown exception"); }
@Test public void testEmptyMappings() { MappingFileData mappingFileData = new MappingFileData(); ClassMappings result = parser.processMappings(mappingFileData.getClassMaps(), new Configuration()); assertNotNull("result should not be null", result); assertEquals("result should be empty", 0, result.size()); }