From 033def2054eb8532456ca4ceec3309e969191398 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Sun, 23 Feb 2014 16:47:09 +0100 Subject: [PATCH] #128 Raising an error in case an enum constant is mapped more than once --- .../ap/processor/MapperCreationProcessor.java | 16 +++++++- .../ap/test/enums/EnumMappingTest.java | 21 ++++++++++ ...usOrderMapperMappingSameConstantTwice.java | 41 +++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/enums/ErroneousOrderMapperMappingSameConstantTwice.java diff --git a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java index bb1f9c7be..97448470d 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java @@ -26,6 +26,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; + import javax.annotation.processing.Messager; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; @@ -770,7 +771,20 @@ public class MapperCreationProcessor implements ModelElementProcessor targetConstants = new ArrayList( mappedConstants.size() ); + for ( Mapping mapping : mappedConstants ) { + targetConstants.add( mapping.getTargetName() ); + } + messager.printMessage( + Kind.ERROR, + String.format( + "One enum constant must not be mapped to more than one target constant, but constant %s is " + + "mapped to %s.", + enumConstant, + Strings.join( targetConstants, ", " ) + ), + method.getExecutable() + ); } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/enums/EnumMappingTest.java b/processor/src/test/java/org/mapstruct/ap/test/enums/EnumMappingTest.java index 1692593ca..e3591a67f 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/enums/EnumMappingTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/enums/EnumMappingTest.java @@ -18,9 +18,14 @@ */ package org.mapstruct.ap.test.enums; +import javax.tools.Diagnostic.Kind; + import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.testutil.MapperTestBase; import org.mapstruct.ap.testutil.WithClasses; +import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult; +import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic; +import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome; import org.testng.annotations.Test; import static org.fest.assertions.Assertions.assertThat; @@ -64,4 +69,20 @@ public class EnumMappingTest extends MapperTestBase { assertThat( orderDto ).isNotNull(); assertThat( orderDto.getOrderType() ).isEqualTo( ExternalOrderType.SPECIAL ); } + + @Test + @WithClasses(ErroneousOrderMapperMappingSameConstantTwice.class) + @ExpectedCompilationOutcome( + value = CompilationResult.FAILED, + diagnostics = { + @Diagnostic(type = ErroneousOrderMapperMappingSameConstantTwice.class, + kind = Kind.ERROR, + line = 39, + messageRegExp = "One enum constant must not be mapped to more than one target constant, but " + + "constant EXTRA is mapped to SPECIAL, DEFAULT\\.") + } + ) + public void shouldRaiseErrorIfSameSourceEnumConstantIsMappedTwice() { + + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/enums/ErroneousOrderMapperMappingSameConstantTwice.java b/processor/src/test/java/org/mapstruct/ap/test/enums/ErroneousOrderMapperMappingSameConstantTwice.java new file mode 100644 index 000000000..e2725d3ae --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/enums/ErroneousOrderMapperMappingSameConstantTwice.java @@ -0,0 +1,41 @@ +/** + * Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.ap.test.enums; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; +import org.mapstruct.factory.Mappers; + +/** + * @author Gunnar Morling + */ +@Mapper +public interface ErroneousOrderMapperMappingSameConstantTwice { + + ErroneousOrderMapperMappingSameConstantTwice INSTANCE = Mappers.getMapper( + ErroneousOrderMapperMappingSameConstantTwice.class + ); + + @Mappings({ + @Mapping(source = "EXTRA", target = "SPECIAL"), + @Mapping(source = "EXTRA", target = "DEFAULT"), + }) + ExternalOrderType orderTypeToExternalOrderType(OrderType orderType); +}