From 10aeb444f531d3f9fa9344d3b7eae5f4501c1a06 Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Tue, 21 Feb 2017 20:43:19 +0100 Subject: [PATCH] #1091 Map ANY_REMAINING Enum source to null --- .../ap/internal/model/ValueMappingMethod.java | 4 +- .../ap/test/value/EnumToEnumMappingTest.java | 32 ++++++ .../ap/test/value/SpecialOrderMapper.java | 14 ++- .../ap/test/value/DefaultOrderMapperImpl.java | 57 ++++++++++ .../ap/test/value/OrderMapperImpl.java | 90 +++++++++++++++ .../ap/test/value/SpecialOrderMapperImpl.java | 107 ++++++++++++++++++ 6 files changed, 302 insertions(+), 2 deletions(-) create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/value/DefaultOrderMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/value/OrderMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/value/SpecialOrderMapperImpl.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/ValueMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/ValueMappingMethod.java index f4533a311..6831c20fa 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/ValueMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/ValueMappingMethod.java @@ -105,7 +105,9 @@ public class ValueMappingMethod extends MappingMethod { nullTarget = nullTargetValue.getTarget(); } if ( defaultTargetValue != null ) { - defaultTarget = defaultTargetValue.getTarget(); + // If the default target value is NULL then we should map it to null + defaultTarget = MappingConstantsPrism.NULL.equals( defaultTargetValue.getTarget() ) ? null : + defaultTargetValue.getTarget(); } else { throwIllegalArgumentException = true; diff --git a/processor/src/test/java/org/mapstruct/ap/test/value/EnumToEnumMappingTest.java b/processor/src/test/java/org/mapstruct/ap/test/value/EnumToEnumMappingTest.java index 85e03a4ff..96ecef590 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/value/EnumToEnumMappingTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/value/EnumToEnumMappingTest.java @@ -22,6 +22,7 @@ import static org.assertj.core.api.Assertions.assertThat; import javax.tools.Diagnostic.Kind; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mapstruct.ap.testutil.IssueKey; @@ -30,6 +31,7 @@ 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.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; +import org.mapstruct.ap.testutil.runner.GeneratedSource; /** * Test for the generation and invocation of enum mapping methods. @@ -42,6 +44,13 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; @RunWith(AnnotationProcessorTestRunner.class) public class EnumToEnumMappingTest { + @Rule + public final GeneratedSource generatedSource = new GeneratedSource().addComparisonToFixtureFor( + DefaultOrderMapper.class, + OrderMapper.class, + SpecialOrderMapper.class + ); + @Test public void shouldGenerateEnumMappingMethod() { ExternalOrderType target = OrderMapper.INSTANCE.orderTypeToExternalOrderType( OrderType.B2B ); @@ -188,6 +197,29 @@ public class EnumToEnumMappingTest { } + @IssueKey( "1091" ) + @Test + public void shouldMapAnyRemainingToNullCorrectly() throws Exception { + ExternalOrderType externalOrderType = SpecialOrderMapper.INSTANCE.anyRemainingToNull( OrderType.RETAIL ); + assertThat( externalOrderType ) + .isNotNull() + .isEqualTo( ExternalOrderType.RETAIL ); + + externalOrderType = SpecialOrderMapper.INSTANCE.anyRemainingToNull( OrderType.B2B ); + assertThat( externalOrderType ) + .isNotNull() + .isEqualTo( ExternalOrderType.B2B ); + + externalOrderType = SpecialOrderMapper.INSTANCE.anyRemainingToNull( OrderType.EXTRA ); + assertThat( externalOrderType ).isNull(); + + externalOrderType = SpecialOrderMapper.INSTANCE.anyRemainingToNull( OrderType.STANDARD ); + assertThat( externalOrderType ).isNull(); + + externalOrderType = SpecialOrderMapper.INSTANCE.anyRemainingToNull( OrderType.NORMAL ); + assertThat( externalOrderType ).isNull(); + } + @Test @WithClasses(ErroneousOrderMapperMappingSameConstantTwice.class) @ExpectedCompilationOutcome( diff --git a/processor/src/test/java/org/mapstruct/ap/test/value/SpecialOrderMapper.java b/processor/src/test/java/org/mapstruct/ap/test/value/SpecialOrderMapper.java index 3f215b065..396bd82ea 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/value/SpecialOrderMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/value/SpecialOrderMapper.java @@ -20,7 +20,9 @@ package org.mapstruct.ap.test.value; import org.mapstruct.InheritInverseConfiguration; import org.mapstruct.Mapper; +import org.mapstruct.Mapping; import org.mapstruct.MappingConstants; +import org.mapstruct.Named; import org.mapstruct.ValueMapping; import org.mapstruct.ValueMappings; import org.mapstruct.factory.Mappers; @@ -33,8 +35,11 @@ public interface SpecialOrderMapper { SpecialOrderMapper INSTANCE = Mappers.getMapper( SpecialOrderMapper.class ); + + @Mapping(target = "orderType", source = "orderType", qualifiedByName = "orderTypeToExternalOrderType") OrderDto orderEntityToDto(OrderEntity order); + @Named("orderTypeToExternalOrderType") @ValueMappings({ @ValueMapping( source = MappingConstants.NULL, target = "DEFAULT" ), @ValueMapping( source = "STANDARD", target = MappingConstants.NULL ), @@ -42,7 +47,14 @@ public interface SpecialOrderMapper { }) ExternalOrderType orderTypeToExternalOrderType(OrderType orderType); - @InheritInverseConfiguration + @InheritInverseConfiguration(name = "orderTypeToExternalOrderType") @ValueMapping( target = "EXTRA", source = "SPECIAL" ) OrderType externalOrderTypeToOrderType(ExternalOrderType orderType); + + @ValueMappings({ + @ValueMapping(source = MappingConstants.NULL, target = "DEFAULT"), + @ValueMapping(source = "STANDARD", target = MappingConstants.NULL), + @ValueMapping(source = MappingConstants.ANY_REMAINING, target = MappingConstants.NULL) + }) + ExternalOrderType anyRemainingToNull(OrderType orderType); } diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/value/DefaultOrderMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/value/DefaultOrderMapperImpl.java new file mode 100644 index 000000000..270f31168 --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/value/DefaultOrderMapperImpl.java @@ -0,0 +1,57 @@ +/** + * Copyright 2012-2017 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.value; + +import javax.annotation.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2017-02-20T21:25:45+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class DefaultOrderMapperImpl implements DefaultOrderMapper { + + @Override + public OrderDto orderEntityToDto(OrderEntity order) { + if ( order == null ) { + return null; + } + + OrderDto orderDto = new OrderDto(); + + orderDto.setOrderType( orderTypeToExternalOrderType( order.getOrderType() ) ); + + return orderDto; + } + + @Override + public ExternalOrderType orderTypeToExternalOrderType(OrderType orderType) { + if ( orderType == null ) { + return null; + } + + ExternalOrderType externalOrderType; + + switch ( orderType ) { + default: externalOrderType = ExternalOrderType.DEFAULT; + } + + return externalOrderType; + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/value/OrderMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/value/OrderMapperImpl.java new file mode 100644 index 000000000..8fc064706 --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/value/OrderMapperImpl.java @@ -0,0 +1,90 @@ +/** + * Copyright 2012-2017 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.value; + +import javax.annotation.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2017-02-20T21:25:45+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class OrderMapperImpl implements OrderMapper { + + @Override + public OrderDto orderEntityToDto(OrderEntity order) { + if ( order == null ) { + return null; + } + + OrderDto orderDto = new OrderDto(); + + orderDto.setOrderType( orderTypeToExternalOrderType( order.getOrderType() ) ); + + return orderDto; + } + + @Override + public ExternalOrderType orderTypeToExternalOrderType(OrderType orderType) { + if ( orderType == null ) { + return null; + } + + ExternalOrderType externalOrderType; + + switch ( orderType ) { + case EXTRA: externalOrderType = ExternalOrderType.SPECIAL; + break; + case STANDARD: externalOrderType = ExternalOrderType.DEFAULT; + break; + case NORMAL: externalOrderType = ExternalOrderType.DEFAULT; + break; + case RETAIL: externalOrderType = ExternalOrderType.RETAIL; + break; + case B2B: externalOrderType = ExternalOrderType.B2B; + break; + default: throw new IllegalArgumentException( "Unexpected enum constant: " + orderType ); + } + + return externalOrderType; + } + + @Override + public OrderType externalOrderTypeToOrderType(ExternalOrderType orderType) { + if ( orderType == null ) { + return null; + } + + OrderType orderType1; + + switch ( orderType ) { + case SPECIAL: orderType1 = OrderType.EXTRA; + break; + case DEFAULT: orderType1 = OrderType.STANDARD; + break; + case RETAIL: orderType1 = OrderType.RETAIL; + break; + case B2B: orderType1 = OrderType.B2B; + break; + default: throw new IllegalArgumentException( "Unexpected enum constant: " + orderType ); + } + + return orderType1; + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/value/SpecialOrderMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/value/SpecialOrderMapperImpl.java new file mode 100644 index 000000000..489af7dfc --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/value/SpecialOrderMapperImpl.java @@ -0,0 +1,107 @@ +/** + * Copyright 2012-2017 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.value; + +import javax.annotation.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2017-02-20T21:25:45+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class SpecialOrderMapperImpl implements SpecialOrderMapper { + + @Override + public OrderDto orderEntityToDto(OrderEntity order) { + if ( order == null ) { + return null; + } + + OrderDto orderDto = new OrderDto(); + + orderDto.setOrderType( orderTypeToExternalOrderType( order.getOrderType() ) ); + + return orderDto; + } + + @Override + public ExternalOrderType orderTypeToExternalOrderType(OrderType orderType) { + if ( orderType == null ) { + return ExternalOrderType.DEFAULT; + } + + ExternalOrderType externalOrderType; + + switch ( orderType ) { + case STANDARD: externalOrderType = null; + break; + case RETAIL: externalOrderType = ExternalOrderType.RETAIL; + break; + case B2B: externalOrderType = ExternalOrderType.B2B; + break; + default: externalOrderType = ExternalOrderType.SPECIAL; + } + + return externalOrderType; + } + + @Override + public OrderType externalOrderTypeToOrderType(ExternalOrderType orderType) { + if ( orderType == null ) { + return OrderType.STANDARD; + } + + OrderType orderType1; + + switch ( orderType ) { + case SPECIAL: orderType1 = OrderType.EXTRA; + break; + case DEFAULT: orderType1 = null; + break; + case RETAIL: orderType1 = OrderType.RETAIL; + break; + case B2B: orderType1 = OrderType.B2B; + break; + default: throw new IllegalArgumentException( "Unexpected enum constant: " + orderType ); + } + + return orderType1; + } + + @Override + public ExternalOrderType anyRemainingToNull(OrderType orderType) { + if ( orderType == null ) { + return ExternalOrderType.DEFAULT; + } + + ExternalOrderType externalOrderType; + + switch ( orderType ) { + case STANDARD: externalOrderType = null; + break; + case RETAIL: externalOrderType = ExternalOrderType.RETAIL; + break; + case B2B: externalOrderType = ExternalOrderType.B2B; + break; + default: externalOrderType = null; + } + + return externalOrderType; + } +}