From 7c2b4c9541697451a93ea575fd29820cd79f7c01 Mon Sep 17 00:00:00 2001 From: Andreas Gudian Date: Wed, 25 Nov 2015 19:56:42 +0100 Subject: [PATCH] #698 Fix missing import for enums in case of default values. --- .../ap/internal/model/PropertyMapping.java | 26 ++++++++++----- .../ap/test/defaultvalue/CountryDts.java | 11 +++++++ .../ap/test/defaultvalue/CountryEntity.java | 10 ++++++ .../ap/test/defaultvalue/CountryMapper.java | 13 ++++---- .../test/defaultvalue/DefaultValueTest.java | 15 ++++++--- .../ap/test/defaultvalue/other/Continent.java | 33 +++++++++++++++++++ 6 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/defaultvalue/other/Continent.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java index 0261a09e5..40cbab29f 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java @@ -18,6 +18,14 @@ */ package org.mapstruct.ap.internal.model; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.type.TypeMirror; + import org.mapstruct.ap.internal.model.assignment.AdderWrapper; import org.mapstruct.ap.internal.model.assignment.ArrayCopyWrapper; import org.mapstruct.ap.internal.model.assignment.Assignment; @@ -36,22 +44,15 @@ import org.mapstruct.ap.internal.model.source.SourceMethod; import org.mapstruct.ap.internal.model.source.SourceReference; import org.mapstruct.ap.internal.model.source.SourceReference.PropertyEntry; import org.mapstruct.ap.internal.util.Executables; +import org.mapstruct.ap.internal.util.MapperConfiguration; import org.mapstruct.ap.internal.util.Message; import org.mapstruct.ap.internal.util.Strings; -import javax.lang.model.element.ExecutableElement; -import javax.lang.model.type.TypeMirror; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Set; - import static org.mapstruct.ap.internal.model.assignment.Assignment.AssignmentType.DIRECT; import static org.mapstruct.ap.internal.model.assignment.Assignment.AssignmentType.MAPPED; import static org.mapstruct.ap.internal.model.assignment.Assignment.AssignmentType.MAPPED_TYPE_CONVERTED; import static org.mapstruct.ap.internal.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED; import static org.mapstruct.ap.internal.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED_MAPPED; -import org.mapstruct.ap.internal.util.MapperConfiguration; /** * Represents the mapping between a source and target property, e.g. from {@code String Source#foo} to @@ -798,8 +799,15 @@ public class PropertyMapping extends ModelElement { } @Override + @SuppressWarnings("unchecked") public Set getImportTypes() { - return assignment.getImportTypes(); + if ( defaultValueAssignment == null ) { + return assignment.getImportTypes(); + } + + return org.mapstruct.ap.internal.util.Collections.asSet( + assignment.getImportTypes(), + defaultValueAssignment.getImportTypes() ); } public List getDependsOn() { diff --git a/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/CountryDts.java b/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/CountryDts.java index 7b7b6b1a2..24d2c7e4b 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/CountryDts.java +++ b/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/CountryDts.java @@ -18,11 +18,14 @@ */ package org.mapstruct.ap.test.defaultvalue; +import org.mapstruct.ap.test.defaultvalue.other.Continent; + public class CountryDts { private String code; private int id; private long zipcode; private String region; + private Continent continent; public String getCode() { return code; @@ -55,4 +58,12 @@ public class CountryDts { public void setRegion(String region) { this.region = region; } + + public Continent getContinent() { + return continent; + } + + public void setContinent(Continent continent) { + this.continent = continent; + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/CountryEntity.java b/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/CountryEntity.java index 90a9b7f3d..02c196ee5 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/CountryEntity.java +++ b/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/CountryEntity.java @@ -18,11 +18,14 @@ */ package org.mapstruct.ap.test.defaultvalue; +import org.mapstruct.ap.test.defaultvalue.other.Continent; + public class CountryEntity { private String code; private Integer id; private long zipcode; private Region region; + private Continent continent; public String getCode() { return code; @@ -56,4 +59,11 @@ public class CountryEntity { this.region = region; } + public Continent getContinent() { + return continent; + } + + public void setContinent(Continent continent) { + this.continent = continent; + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/CountryMapper.java b/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/CountryMapper.java index bea860abe..9d8888ed2 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/CountryMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/CountryMapper.java @@ -33,6 +33,7 @@ public abstract class CountryMapper { @Mapping( target = "id", defaultValue = "42" ), @Mapping( target = "zipcode", defaultValue = "1337" ), @Mapping( target = "region", defaultValue = "someRegion" ), + @Mapping( target = "continent", defaultValue = "EUROPE" ) } ) public abstract CountryDts mapToCountryDts(CountryEntity country); @@ -40,19 +41,19 @@ public abstract class CountryMapper { @Mapping( target = "code", defaultValue = "DE" ), @Mapping( target = "id", defaultValue = "42" ), @Mapping( target = "zipcode", defaultValue = "1337" ), - @Mapping( target = "region", ignore = true ) - + @Mapping( target = "region", ignore = true ), + @Mapping( target = "continent", defaultValue = "EUROPE" ) } ) - public abstract void mapToCountryDts(CountryDts countryDts, @MappingTarget CountryEntity country); + public abstract void mapToCountryEntity(CountryDts countryDts, @MappingTarget CountryEntity country); @Mappings( { @Mapping( target = "code", defaultValue = "DE" ), @Mapping( target = "id", defaultValue = "42" ), @Mapping( target = "zipcode", defaultValue = "1337" ), - @Mapping( target = "region", ignore = true ) - + @Mapping( target = "region", ignore = true ), + @Mapping( target = "continent", defaultValue = "EUROPE" ) } ) - public abstract void mapToCountryDts(CountryEntity source, @MappingTarget CountryEntity target); + public abstract void mapToCountryEntity(CountryEntity source, @MappingTarget CountryEntity target); protected String mapToString(Region region) { return ( region != null ) ? region.getCode() : null; diff --git a/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/DefaultValueTest.java b/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/DefaultValueTest.java index fb4e714e9..4bb0eca2a 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/DefaultValueTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/DefaultValueTest.java @@ -22,6 +22,7 @@ import java.text.ParseException; import org.junit.Test; import org.junit.runner.RunWith; +import org.mapstruct.ap.test.defaultvalue.other.Continent; import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult; @@ -35,7 +36,8 @@ import static org.fest.assertions.Assertions.assertThat; @RunWith( AnnotationProcessorTestRunner.class ) @WithClasses( { CountryEntity.class, - CountryDts.class + CountryDts.class, + Continent.class } ) public class DefaultValueTest { @Test @@ -63,10 +65,9 @@ public class DefaultValueTest { // code is null so it should fall back to the default value assertThat( countryDts.getCode() ).isEqualTo( "DE" ); - assertThat( countryDts.getZipcode() ).isEqualTo( 0 ); - assertThat( countryDts.getRegion() ).isEqualTo( "someRegion" ); + assertThat( countryDts.getContinent() ).isEqualTo( Continent.EUROPE ); } @Test @@ -80,12 +81,14 @@ public class DefaultValueTest { Region region = new Region(); region.setCode( "foobar" ); countryEntity.setRegion( region ); + countryEntity.setContinent( Continent.NORTH_AMERICA ); CountryDts countryDts = CountryMapper.INSTANCE.mapToCountryDts( countryEntity ); // the source entity had a code set, so the default value shouldn't be used assertThat( countryDts.getCode() ).isEqualTo( "US" ); assertThat( countryDts.getRegion() ).isEqualTo( "foobar" ); + assertThat( countryDts.getContinent() ).isEqualTo( Continent.NORTH_AMERICA ); } @Test @@ -97,12 +100,13 @@ public class DefaultValueTest { CountryEntity countryEntity = new CountryEntity(); CountryDts countryDts = new CountryDts(); - CountryMapper.INSTANCE.mapToCountryDts( countryDts, countryEntity ); + CountryMapper.INSTANCE.mapToCountryEntity( countryDts, countryEntity ); assertThat( countryEntity.getId() ).isEqualTo( 0 ); // no code is set, so fall back to default value assertThat( countryEntity.getCode() ).isEqualTo( "DE" ); assertThat( countryEntity.getZipcode() ).isEqualTo( 0 ); + assertThat( countryEntity.getContinent() ).isEqualTo( Continent.EUROPE ); } @Test @@ -114,13 +118,14 @@ public class DefaultValueTest { CountryEntity source = new CountryEntity(); CountryEntity target = new CountryEntity(); - CountryMapper.INSTANCE.mapToCountryDts( source, target ); + CountryMapper.INSTANCE.mapToCountryEntity( source, target ); // no id is set, so fall back to default value assertThat( target.getId() ).isEqualTo( 42 ); // no code is set, so fall back to default value assertThat( target.getCode() ).isEqualTo( "DE" ); assertThat( target.getZipcode() ).isEqualTo( 0 ); + assertThat( target.getContinent() ).isEqualTo( Continent.EUROPE ); } @Test diff --git a/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/other/Continent.java b/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/other/Continent.java new file mode 100644 index 000000000..979582241 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/defaultvalue/other/Continent.java @@ -0,0 +1,33 @@ +/** + * Copyright 2012-2015 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.defaultvalue.other; + +/** + * @author Andreas Gudian + * + */ +public enum Continent { + AFRICA, + ANTARCTICA, + ASIA, + EUROPE, + NORTH_AMERICA, + OCEANIA, + SOUTH_AMERICA +}