From 48c20d4dd0acdb26f64975c070e43064d85cdd1d Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Mon, 24 Nov 2014 22:55:18 +0100 Subject: [PATCH] #295 Adding test for bean mapping method with several source parameters --- .../ap/test/nullvaluemapping/CarMapper.java | 7 ++++ .../test/nullvaluemapping/CarMapperTest.java | 26 +++++++++--- .../test/nullvaluemapping/source/Driver.java | 36 ++++++++++++++++ .../target/DriverAndCarDto.java | 41 +++++++++++++++++++ 4 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/source/Driver.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/target/DriverAndCarDto.java diff --git a/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/CarMapper.java b/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/CarMapper.java index dcacc1153..57575055b 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/CarMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/CarMapper.java @@ -27,7 +27,9 @@ import org.mapstruct.Mapping; import org.mapstruct.Mappings; import org.mapstruct.NullValueMapping; import org.mapstruct.ap.test.nullvaluemapping.source.Car; +import org.mapstruct.ap.test.nullvaluemapping.source.Driver; import org.mapstruct.ap.test.nullvaluemapping.target.CarDto; +import org.mapstruct.ap.test.nullvaluemapping.target.DriverAndCarDto; import org.mapstruct.factory.Mappers; import static org.mapstruct.NullValueMappingStrategy.RETURN_DEFAULT; @@ -59,4 +61,9 @@ public interface CarMapper { @NullValueMapping(RETURN_DEFAULT) Map carsToCarDtoMap(Map cars); + + @NullValueMapping(RETURN_DEFAULT) + DriverAndCarDto driverAndCarToDto(Driver driver, Car car); + + DriverAndCarDto driverAndCarToDtoReturningNull(Driver driver, Car car); } diff --git a/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/CarMapperTest.java b/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/CarMapperTest.java index 38de89c07..5667c4fcc 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/CarMapperTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/CarMapperTest.java @@ -26,7 +26,9 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.mapstruct.ap.test.nullvaluemapping.source.Car; +import org.mapstruct.ap.test.nullvaluemapping.source.Driver; import org.mapstruct.ap.test.nullvaluemapping.target.CarDto; +import org.mapstruct.ap.test.nullvaluemapping.target.DriverAndCarDto; import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; @@ -34,7 +36,9 @@ import static org.fest.assertions.Assertions.assertThat; @WithClasses({ Car.class, + Driver.class, CarDto.class, + DriverAndCarDto.class, CarMapper.class, CarMapperSettingOnMapper.class, CentralConfig.class, @@ -118,7 +122,6 @@ public class CarMapperTest { //then assertThat( carDtos2 ).isNotNull(); assertThat( carDtos2.isEmpty() ).isTrue(); - } @Test @@ -143,7 +146,6 @@ public class CarMapperTest { //then assertThat( carDtoMap2 ).isNotNull(); assertThat( carDtoMap2.isEmpty() ).isTrue(); - } @Test @@ -169,7 +171,6 @@ public class CarMapperTest { //then assertThat( carDtos ).isNotNull(); assertThat( carDtos.isEmpty() ).isTrue(); - } @Test @@ -180,7 +181,6 @@ public class CarMapperTest { //then assertThat( carDtoMap ).isNull(); - } @Test @@ -197,7 +197,6 @@ public class CarMapperTest { assertThat( carDto.getCatalogId() ).isNotEmpty(); } - @Test public void shouldMapIterableWithNullArgOnConfig() { @@ -207,7 +206,6 @@ public class CarMapperTest { //then assertThat( carDtos ).isNotNull(); assertThat( carDtos.isEmpty() ).isTrue(); - } @Test @@ -218,6 +216,22 @@ public class CarMapperTest { //then assertThat( carDtoMap ).isNull(); + } + @Test + public void shouldApplyConfiguredStrategyForMethodWithSeveralSourceParams() { + //when + DriverAndCarDto result = CarMapper.INSTANCE.driverAndCarToDto( null, null ); + + //then + assertThat( result ).isNotNull(); + assertThat( result.getMake() ).isNull(); + assertThat( result.getName() ).isNull(); + + //when + result = CarMapper.INSTANCE.driverAndCarToDtoReturningNull( null, null ); + + //then + assertThat( result ).isNull(); } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/source/Driver.java b/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/source/Driver.java new file mode 100644 index 000000000..c5e00a9c9 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/source/Driver.java @@ -0,0 +1,36 @@ +/** + * 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.nullvaluemapping.source; + +public class Driver { + + private String name; + + public Driver(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/target/DriverAndCarDto.java b/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/target/DriverAndCarDto.java new file mode 100644 index 000000000..3b5012f48 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nullvaluemapping/target/DriverAndCarDto.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.nullvaluemapping.target; + +public class DriverAndCarDto { + + private String name; + private String make; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } +}