From ac0a532fb4f3e6f27b76c5c755399c8ccaa110c5 Mon Sep 17 00:00:00 2001 From: sjaakd Date: Fri, 14 Nov 2014 22:54:12 +0100 Subject: [PATCH] #295 NullValueMapping inheritance mechanism from MapperConfig to Mapper to Method --- .../java/org/mapstruct/MapNullToDefault.java | 21 ++--- .../mapstruct/MapNullToDefaultStrategy.java | 56 +++++++++++++ .../src/main/java/org/mapstruct/Mapper.java | 6 ++ .../main/java/org/mapstruct/MapperConfig.java | 7 ++ .../mapstruct/ap/model/BeanMappingMethod.java | 3 +- .../ap/model/IterableMappingMethod.java | 4 +- .../mapstruct/ap/model/MapMappingMethod.java | 4 +- .../prism/MapNullToDefaultStrategyPrism.java | 31 +++++++ .../org/mapstruct/ap/util/MapperConfig.java | 43 +++++++++- .../CarMapperSettingOnConfig.java | 53 ++++++++++++ .../CarMapperSettingOnMapper.java | 53 ++++++++++++ .../test/mapnulltodefault/CarMapperTest.java | 80 ++++++++++++++++++- .../test/mapnulltodefault/CentralConfig.java | 31 +++++++ .../ap/test/prism/EnumPrismsTest.java | 8 ++ 14 files changed, 383 insertions(+), 17 deletions(-) create mode 100644 core-common/src/main/java/org/mapstruct/MapNullToDefaultStrategy.java create mode 100644 processor/src/main/java/org/mapstruct/ap/prism/MapNullToDefaultStrategyPrism.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CarMapperSettingOnConfig.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CarMapperSettingOnMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CentralConfig.java diff --git a/core-common/src/main/java/org/mapstruct/MapNullToDefault.java b/core-common/src/main/java/org/mapstruct/MapNullToDefault.java index a61fc41e7..ed95c0020 100644 --- a/core-common/src/main/java/org/mapstruct/MapNullToDefault.java +++ b/core-common/src/main/java/org/mapstruct/MapNullToDefault.java @@ -24,16 +24,17 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * Advises the code generator to apply all the {@link Mapping}s from an inverse mapping method to the annotated method - * as well. An inverse mapping method is a method which has the annotated method's source type as target type (return - * type or indicated through a parameter annotated with {@link MappingTarget}) and the annotated method's target type as - * source type. + * Determines what kind to return in case of a null source argument. *

- * Any mappings given on the annotated method itself are added to those mappings inherited from the inverse method. In - * case of a conflict local mappings take precedence over inherited mappings. - *

- * If more than one matching inverse method exists, the name of the method to inherit the configuration from must be - * specified via {@link #name()} + * For: + *

    + *
  1. Bean Mapping: an 'empty' target bean, except for expressions and constants
  2. + *
  3. Iterable Mapping: an 'empty' list
  4. + *
  5. Map Mapping: an 'empty' map
  6. + *
+ * + * The user has a choice to use this annotation. When its used, it is used to either override a more global + * setting, or in the most common case, to set the specific behavior to map null to default * * @author Sjaak Derksen */ @@ -41,5 +42,5 @@ import java.lang.annotation.Target; @Retention( RetentionPolicy.SOURCE ) public @interface MapNullToDefault { - boolean value() default true; + MapNullToDefaultStrategy value() default MapNullToDefaultStrategy.MAP_NULL_TO_DEFAULT; } diff --git a/core-common/src/main/java/org/mapstruct/MapNullToDefaultStrategy.java b/core-common/src/main/java/org/mapstruct/MapNullToDefaultStrategy.java new file mode 100644 index 000000000..f266359b1 --- /dev/null +++ b/core-common/src/main/java/org/mapstruct/MapNullToDefaultStrategy.java @@ -0,0 +1,56 @@ +/** + * 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; + +/** + * Strategy for propagating the value of collection-typed properties from source to target. + * + * @author Sjaak Derksen + */ +public enum MapNullToDefaultStrategy { + + /** + * A null source argument of a mapping method will be mapped to a null target result + */ + MAP_NULL_TO_NULL, + + /** + * A null source argument of a mapping method will be mapped to a default target result + *

+ *

    + *
  1. For a bean mapping this means a target object will be returned. {@link Mapping#expression()} and + * {@link Mapping#constant()} will be added to the target<\li> + *
  2. For an iterable mapping this means a {@link java.util.Collections#emptyList() } will be returned<\li> + *
  3. For an map mapping this means a {@link java.util.Collections#emptyMap() } will be returned<\li> + *
+ */ + MAP_NULL_TO_DEFAULT, + + /** + * When given via {@link Mapper#mapNullToDefaultStrategy()}, causes the setting specified via + * {@link MapperConfig#mapNullToDefaultStrategy()} to be applied, if present. + *

+ * When given via {@link MapNullToDefault#mapNullToDefaultStrategy()}, causes the setting specified via + * {@link Mapper#mapNullToDefaultStrategy()} to be applied, if present. + *

+ * Otherwise causes + * {@link #MAP_NULL_TO_NULL} to be applied. + */ + DEFAULT; +} diff --git a/core-common/src/main/java/org/mapstruct/Mapper.java b/core-common/src/main/java/org/mapstruct/Mapper.java index 031126752..741483aa9 100644 --- a/core-common/src/main/java/org/mapstruct/Mapper.java +++ b/core-common/src/main/java/org/mapstruct/Mapper.java @@ -104,4 +104,10 @@ public @interface Mapper { */ CollectionMappingStrategy collectionMappingStrategy() default CollectionMappingStrategy.DEFAULT; + /** + * The strategy to be applied when for returning a target when the source equals null. + * + * @return The strategy applied when determining whether to return null or an empty object, list or map. + */ + MapNullToDefaultStrategy mapNullToDefaultStrategy() default MapNullToDefaultStrategy.DEFAULT; } diff --git a/core-common/src/main/java/org/mapstruct/MapperConfig.java b/core-common/src/main/java/org/mapstruct/MapperConfig.java index a84f52cdc..336d30d42 100644 --- a/core-common/src/main/java/org/mapstruct/MapperConfig.java +++ b/core-common/src/main/java/org/mapstruct/MapperConfig.java @@ -81,4 +81,11 @@ public @interface MapperConfig { * @return The strategy applied when propagating the value of collection-typed properties. */ CollectionMappingStrategy collectionMappingStrategy() default CollectionMappingStrategy.DEFAULT; + + /** + * The strategy to be applied when for returning a target when the source equals null. + * + * @return The strategy applied when determining whether to return null or an empty object, list or map. + */ + MapNullToDefaultStrategy mapNullToDefaultStrategy() default MapNullToDefaultStrategy.DEFAULT; } diff --git a/processor/src/main/java/org/mapstruct/ap/model/BeanMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/BeanMappingMethod.java index b7c605146..c2575d26d 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/BeanMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/BeanMappingMethod.java @@ -101,7 +101,8 @@ public class BeanMappingMethod extends MappingMethod { // mapNullToDefault MapNullToDefaultPrism prism = MapNullToDefaultPrism.getInstanceOn( method.getExecutable() ); - boolean mapNullToDefault = ( prism != null ) && prism.value(); + boolean mapNullToDefault = + MapperConfig.getInstanceOn( ctx.getMapperTypeElement() ).isMapToDefault( prism ); MethodReference factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx ); return new BeanMappingMethod( method, propertyMappings, factoryMethod, mapNullToDefault ); diff --git a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java index 738000505..08b935e83 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java @@ -32,6 +32,7 @@ import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.TypeFactory; import org.mapstruct.ap.model.source.Method; import org.mapstruct.ap.prism.MapNullToDefaultPrism; +import org.mapstruct.ap.util.MapperConfig; import org.mapstruct.ap.util.Strings; /** @@ -109,7 +110,8 @@ public class IterableMappingMethod extends MappingMethod { // mapNullToDefault MapNullToDefaultPrism prism = MapNullToDefaultPrism.getInstanceOn( method.getExecutable() ); - boolean mapNullToDefault = ( prism != null ) && prism.value(); + boolean mapNullToDefault + = MapperConfig.getInstanceOn( ctx.getMapperTypeElement() ).isMapToDefault( prism ); MethodReference factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx ); return new IterableMappingMethod( diff --git a/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java index d1a87658a..73fc211a0 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java @@ -32,6 +32,7 @@ import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.TypeFactory; import org.mapstruct.ap.model.source.Method; import org.mapstruct.ap.prism.MapNullToDefaultPrism; +import org.mapstruct.ap.util.MapperConfig; import org.mapstruct.ap.util.Strings; /** @@ -141,7 +142,8 @@ public class MapMappingMethod extends MappingMethod { // mapNullToDefault MapNullToDefaultPrism prism = MapNullToDefaultPrism.getInstanceOn( method.getExecutable() ); - boolean mapNullToDefault = ( prism != null ) && prism.value(); + boolean mapNullToDefault = + MapperConfig.getInstanceOn( ctx.getMapperTypeElement() ).isMapToDefault( prism ); MethodReference factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx ); diff --git a/processor/src/main/java/org/mapstruct/ap/prism/MapNullToDefaultStrategyPrism.java b/processor/src/main/java/org/mapstruct/ap/prism/MapNullToDefaultStrategyPrism.java new file mode 100644 index 000000000..56584c7c3 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/prism/MapNullToDefaultStrategyPrism.java @@ -0,0 +1,31 @@ +/** + * 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.prism; + +/** + * Prism for the enum {@link org.mapstruct.MapNullToDefaultStrategy} + * + * @author Sjaak Derksen + */ +public enum MapNullToDefaultStrategyPrism { + + MAP_NULL_TO_NULL, + MAP_NULL_TO_DEFAULT, + DEFAULT; +} diff --git a/processor/src/main/java/org/mapstruct/ap/util/MapperConfig.java b/processor/src/main/java/org/mapstruct/ap/util/MapperConfig.java index b249f7e1c..b9671fe32 100644 --- a/processor/src/main/java/org/mapstruct/ap/util/MapperConfig.java +++ b/processor/src/main/java/org/mapstruct/ap/util/MapperConfig.java @@ -29,6 +29,9 @@ import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; +import org.mapstruct.ap.prism.MapNullToDefaultStrategyPrism; +import org.mapstruct.ap.prism.MapNullToDefaultPrism; + import org.mapstruct.ap.option.ReportingPolicy; import org.mapstruct.ap.prism.CollectionMappingStrategyPrism; import org.mapstruct.ap.prism.MapperConfigPrism; @@ -84,13 +87,12 @@ public class MapperConfig { } public String unmappedTargetPolicy() { - if ( !ReportingPolicy.valueOf( mapperPrism.unmappedTargetPolicy() ).equals( ReportingPolicy.DEFAULT ) ) { + if ( ReportingPolicy.valueOf( mapperPrism.unmappedTargetPolicy() ) != ReportingPolicy.DEFAULT ) { // it is not the default configuration return mapperPrism.unmappedTargetPolicy(); } else if ( mapperConfigPrism != null && - !ReportingPolicy.valueOf( mapperConfigPrism.unmappedTargetPolicy() ) - .equals( ReportingPolicy.DEFAULT ) ) { + ReportingPolicy.valueOf( mapperConfigPrism.unmappedTargetPolicy() ) != ReportingPolicy.DEFAULT ) { return mapperConfigPrism.unmappedTargetPolicy(); } else { @@ -117,6 +119,41 @@ public class MapperConfig { return CollectionMappingStrategyPrism.ACCESSOR_ONLY; } + public boolean isMapToDefault(MapNullToDefaultPrism mapNullToDefault) { + + // check on method level + if ( mapNullToDefault != null ) { + MapNullToDefaultStrategyPrism methodPolicy + = MapNullToDefaultStrategyPrism.valueOf( mapNullToDefault.value() ); + if ( methodPolicy != MapNullToDefaultStrategyPrism.DEFAULT ) { + return methodPolicy == MapNullToDefaultStrategyPrism.MAP_NULL_TO_DEFAULT; + } + } + + // check on mapper level + MapNullToDefaultStrategyPrism mapperPolicy = + MapNullToDefaultStrategyPrism.valueOf( mapperPrism.mapNullToDefaultStrategy() ); + + if ( mapperPolicy != MapNullToDefaultStrategyPrism.DEFAULT ) { + // it is not the default mapper configuration, so return the mapper configured value + return mapperPolicy == MapNullToDefaultStrategyPrism.MAP_NULL_TO_DEFAULT; + } + + // check on mapping config level + else if ( mapperConfigPrism != null ) { + // try the config mapper configuration + MapNullToDefaultStrategyPrism configPolicy = + MapNullToDefaultStrategyPrism.valueOf( mapperConfigPrism.mapNullToDefaultStrategy() ); + if ( configPolicy != MapNullToDefaultStrategyPrism.DEFAULT ) { + // its not the default configuration, so return the mapper config configured value + return configPolicy == MapNullToDefaultStrategyPrism.MAP_NULL_TO_DEFAULT; + } + } + // when nothing specified, return MAP_NULL_TO_NULL (default option) + return false; + } + + public String componentModel() { if ( !mapperPrism.componentModel().equals( "default" ) ) { return mapperPrism.componentModel(); diff --git a/processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CarMapperSettingOnConfig.java b/processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CarMapperSettingOnConfig.java new file mode 100644 index 000000000..463c91520 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CarMapperSettingOnConfig.java @@ -0,0 +1,53 @@ +/** + * 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.mapnulltodefault; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.mapstruct.Mapper; +import org.mapstruct.MapNullToDefault; +import org.mapstruct.MapNullToDefaultStrategy; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; +import org.mapstruct.ap.test.mapnulltodefault.source.Car; +import org.mapstruct.ap.test.mapnulltodefault.target.CarDto; +import org.mapstruct.factory.Mappers; + +@Mapper(imports = UUID.class, config = CentralConfig.class ) +public interface CarMapperSettingOnConfig { + + CarMapperSettingOnConfig INSTANCE = Mappers.getMapper( CarMapperSettingOnConfig.class ); + + @Mappings({ + @Mapping(target = "seatCount", source = "numberOfSeats"), + @Mapping(target = "model", constant = "ModelT"), + @Mapping(target = "catalogId", expression = "java( UUID.randomUUID().toString() )") + }) + CarDto carToCarDto(Car car); + + + @MapNullToDefault(MapNullToDefaultStrategy.DEFAULT) + List carsToCarDtos(List cars); + + + @MapNullToDefault(MapNullToDefaultStrategy.MAP_NULL_TO_NULL) + Map carsToCarDtoMap(Map cars); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CarMapperSettingOnMapper.java b/processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CarMapperSettingOnMapper.java new file mode 100644 index 000000000..8c1f0d363 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CarMapperSettingOnMapper.java @@ -0,0 +1,53 @@ +/** + * 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.mapnulltodefault; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.mapstruct.Mapper; +import org.mapstruct.MapNullToDefault; +import org.mapstruct.MapNullToDefaultStrategy; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; +import org.mapstruct.ap.test.mapnulltodefault.source.Car; +import org.mapstruct.ap.test.mapnulltodefault.target.CarDto; +import org.mapstruct.factory.Mappers; + +@Mapper(imports = UUID.class, mapNullToDefaultStrategy = MapNullToDefaultStrategy.MAP_NULL_TO_DEFAULT ) +public interface CarMapperSettingOnMapper { + + CarMapperSettingOnMapper INSTANCE = Mappers.getMapper( CarMapperSettingOnMapper.class ); + + @Mappings({ + @Mapping(target = "seatCount", source = "numberOfSeats"), + @Mapping(target = "model", constant = "ModelT"), + @Mapping(target = "catalogId", expression = "java( UUID.randomUUID().toString() )") + }) + CarDto carToCarDto(Car car); + + + @MapNullToDefault(MapNullToDefaultStrategy.DEFAULT) + List carsToCarDtos(List cars); + + + @MapNullToDefault(MapNullToDefaultStrategy.MAP_NULL_TO_NULL) + Map carsToCarDtoMap(Map cars); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CarMapperTest.java b/processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CarMapperTest.java index 40199ebe3..09cbd184f 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CarMapperTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CarMapperTest.java @@ -35,7 +35,10 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; @WithClasses({ Car.class, CarDto.class, - CarMapper.class + CarMapper.class, + CarMapperSettingOnMapper.class, + CentralConfig.class, + CarMapperSettingOnConfig.class }) @RunWith(AnnotationProcessorTestRunner.class) public class CarMapperTest { @@ -141,4 +144,79 @@ public class CarMapperTest { assertThat( carDtoMap2.isEmpty() ).isTrue(); } + + @Test + public void shouldMapExpressionAndConstantRegardlessNullArgOnMapper() { + + //when + CarDto carDto = CarMapperSettingOnMapper.INSTANCE.carToCarDto( null ); + + //then + assertThat( carDto ).isNotNull(); + assertThat( carDto.getMake() ).isNull(); + assertThat( carDto.getSeatCount() ).isEqualTo( 0 ); + assertThat( carDto.getModel() ).isEqualTo( "ModelT" ); + assertThat( carDto.getCatalogId() ).isNotEmpty(); + } + + @Test + public void shouldMapIterableWithNullArgOnMapper() { + + //when + List carDtos = CarMapperSettingOnMapper.INSTANCE.carsToCarDtos( null ); + + //then + assertThat( carDtos ).isNotNull(); + assertThat( carDtos.isEmpty() ).isTrue(); + + } + + @Test + public void shouldMapMapWithNullArgOnMapper() { + + //when + Map carDtoMap = CarMapperSettingOnMapper.INSTANCE.carsToCarDtoMap( null ); + + //then + assertThat( carDtoMap ).isNull(); + + } + + @Test + public void shouldMapExpressionAndConstantRegardlessNullArgOnConfig() { + + //when + CarDto carDto = CarMapperSettingOnConfig.INSTANCE.carToCarDto( null ); + + //then + assertThat( carDto ).isNotNull(); + assertThat( carDto.getMake() ).isNull(); + assertThat( carDto.getSeatCount() ).isEqualTo( 0 ); + assertThat( carDto.getModel() ).isEqualTo( "ModelT" ); + assertThat( carDto.getCatalogId() ).isNotEmpty(); + } + + + @Test + public void shouldMapIterableWithNullArgOnConfig() { + + //when + List carDtos = CarMapperSettingOnConfig.INSTANCE.carsToCarDtos( null ); + + //then + assertThat( carDtos ).isNotNull(); + assertThat( carDtos.isEmpty() ).isTrue(); + + } + + @Test + public void shouldMapMapWithNullArgOnConfig() { + + //when + Map carDtoMap = CarMapperSettingOnConfig.INSTANCE.carsToCarDtoMap( null ); + + //then + assertThat( carDtoMap ).isNull(); + + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CentralConfig.java b/processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CentralConfig.java new file mode 100644 index 000000000..dbdaaed31 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/mapnulltodefault/CentralConfig.java @@ -0,0 +1,31 @@ +/** + * 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.mapnulltodefault; + +import org.mapstruct.MapNullToDefaultStrategy; +import org.mapstruct.MapperConfig; + +/** + * + * @author Sjaak Derksen + */ +@MapperConfig( mapNullToDefaultStrategy = MapNullToDefaultStrategy.MAP_NULL_TO_DEFAULT ) +public class CentralConfig { + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/prism/EnumPrismsTest.java b/processor/src/test/java/org/mapstruct/ap/test/prism/EnumPrismsTest.java index 129291301..84b5c3789 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/prism/EnumPrismsTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/prism/EnumPrismsTest.java @@ -26,6 +26,8 @@ import org.mapstruct.CollectionMappingStrategy; import org.mapstruct.ap.prism.CollectionMappingStrategyPrism; import static org.fest.assertions.Assertions.assertThat; +import org.mapstruct.MapNullToDefaultStrategy; +import org.mapstruct.ap.prism.MapNullToDefaultStrategyPrism; /** * Test for manually created prisms on enumeration types @@ -39,6 +41,12 @@ public class EnumPrismsTest { namesOf( CollectionMappingStrategyPrism.values() ) ); } + @Test + public void mapNullToDefaultStrategyPrismIsCorrect() { + assertThat( namesOf( MapNullToDefaultStrategy.values() ) ).isEqualTo( + namesOf( MapNullToDefaultStrategyPrism.values() ) ); + } + private static List namesOf(Enum[] values) { List names = new ArrayList( values.length );