From 6c1108d5bb8cf84dad84ff401c1911ff095452e7 Mon Sep 17 00:00:00 2001 From: Sjaak Derksen Date: Mon, 25 Feb 2019 20:56:28 +0100 Subject: [PATCH] #1698 Skip "java.lang.Object" as intermediate result additional unit test (#1737) --- .../creation/MappingResolverImpl.java | 10 ++- .../ap/test/builtin/BuiltInTest.java | 70 +++++++++++++------ .../test/builtin/bean/BigDecimalProperty.java | 25 +++++++ .../ap/test/builtin/bean/SomeType.java | 9 +++ .../test/builtin/bean/SomeTypeProperty.java | 23 ++++++ .../ap/test/builtin/mapper/JaxbMapper.java | 11 +++ 6 files changed, 126 insertions(+), 22 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/bean/BigDecimalProperty.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/bean/SomeType.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/bean/SomeTypeProperty.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java index b45235420..9938beff1 100755 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java @@ -378,6 +378,10 @@ public class MappingResolverImpl implements MappingResolver { *
  • there is a method from B to C, methodY
  • * * then this method tries to resolve this combination and make a mapping methodY( conversionX ( parameter ) ) + * + * In stead of directly using a built in method candidate all the return types as 'B' of all available built-in + * methods are used to resolve a mapping (assignment) from result type to 'B'. If a match is found, an attempt + * is done to find a matching type conversion. */ private Assignment resolveViaConversionAndMethod(Type sourceType, Type targetType) { @@ -421,7 +425,11 @@ public class MappingResolverImpl implements MappingResolver { *
  • there is a conversion from A to B, conversionX
  • *
  • there is a method from B to C, methodY
  • * - * then this method tries to resolve this combination and make a mapping methodY( conversionX ( parameter ) ) + * then this method tries to resolve this combination and make a mapping conversionY( methodX ( parameter ) ) + * + * In stead of directly using a built in method candidate all the return types as 'B' of all available built-in + * methods are used to resolve a mapping (assignment) from source type to 'B'. If a match is found, an attempt + * is done to find a matching type conversion. */ private ConversionAssignment resolveViaMethodAndConversion(Type sourceType, Type targetType) { diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/BuiltInTest.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/BuiltInTest.java index 42e691966..d5e2de5b6 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/builtin/BuiltInTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/BuiltInTest.java @@ -5,8 +5,7 @@ */ package org.mapstruct.ap.test.builtin; -import static org.assertj.core.api.Assertions.assertThat; - +import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.ZoneId; @@ -19,7 +18,6 @@ import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.TimeZone; - import javax.xml.bind.JAXBElement; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; @@ -34,10 +32,13 @@ import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import org.mapstruct.ap.test.builtin._target.IterableTarget; import org.mapstruct.ap.test.builtin._target.MapTarget; +import org.mapstruct.ap.test.builtin.bean.BigDecimalProperty; import org.mapstruct.ap.test.builtin.bean.CalendarProperty; import org.mapstruct.ap.test.builtin.bean.DateProperty; import org.mapstruct.ap.test.builtin.bean.JaxbElementListProperty; import org.mapstruct.ap.test.builtin.bean.JaxbElementProperty; +import org.mapstruct.ap.test.builtin.bean.SomeType; +import org.mapstruct.ap.test.builtin.bean.SomeTypeProperty; import org.mapstruct.ap.test.builtin.bean.StringListProperty; import org.mapstruct.ap.test.builtin.bean.StringProperty; import org.mapstruct.ap.test.builtin.bean.XmlGregorianCalendarProperty; @@ -64,6 +65,8 @@ import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; +import static org.assertj.core.api.Assertions.assertThat; + /** * Test for the generation of built-in mapping methods. * @@ -78,24 +81,11 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; JaxbElementProperty.class, StringListProperty.class, StringProperty.class, + BigDecimalProperty.class, + SomeTypeProperty.class, + SomeType.class, XmlGregorianCalendarProperty.class, ZonedDateTimeProperty.class, - CalendarToZonedDateTimeMapper.class, - ZonedDateTimeToCalendarMapper.class, - CalendarToDateMapper.class, - CalendarToStringMapper.class, - CalendarToXmlGregCalMapper.class, - DateToCalendarMapper.class, - DateToXmlGregCalMapper.class, - IterableSourceTargetMapper.class, - JaxbListMapper.class, - JaxbMapper.class, - MapSourceTargetMapper.class, - StringToCalendarMapper.class, - StringToXmlGregCalMapper.class, - XmlGregCalToCalendarMapper.class, - XmlGregCalToDateMapper.class, - XmlGregCalToStringMapper.class, IterableSource.class, MapSource.class }) @@ -117,8 +107,8 @@ public class BuiltInTest { } @Test - public void shouldApplyBuiltInOnJAXBElement() throws ParseException, DatatypeConfigurationException { - + @WithClasses( JaxbMapper.class ) + public void shouldApplyBuiltInOnJAXBElement() { JaxbElementProperty source = new JaxbElementProperty(); source.setProp( createJaxb( "TEST" ) ); source.publicProp = createJaxb( "PUBLIC TEST" ); @@ -130,6 +120,30 @@ public class BuiltInTest { } @Test + @WithClasses( JaxbMapper.class ) + @IssueKey( "1698" ) + public void shouldApplyBuiltInOnJAXBElementExtra() { + JaxbElementProperty source = new JaxbElementProperty(); + source.setProp( createJaxb( "5" ) ); + source.publicProp = createJaxb( "5" ); + + BigDecimalProperty target = JaxbMapper.INSTANCE.mapBD( source ); + assertThat( target ).isNotNull(); + assertThat( target.getProp() ).isEqualTo( new BigDecimal( "5" ) ); + assertThat( target.publicProp ).isEqualTo( new BigDecimal( "5" ) ); + + JaxbElementProperty source2 = new JaxbElementProperty(); + source2.setProp( createJaxb( "5" ) ); + source2.publicProp = createJaxb( "5" ); + + SomeTypeProperty target2 = JaxbMapper.INSTANCE.mapSomeType( source2 ); + assertThat( target2 ).isNotNull(); + assertThat( target2.publicProp ).isNotNull(); + assertThat( target2.getProp() ).isNotNull(); + } + + @Test + @WithClasses( JaxbListMapper.class ) @IssueKey( "141" ) public void shouldApplyBuiltInOnJAXBElementList() throws ParseException, DatatypeConfigurationException { @@ -144,6 +158,7 @@ public class BuiltInTest { } @Test + @WithClasses( DateToXmlGregCalMapper.class ) public void shouldApplyBuiltInOnDateToXmlGregCal() throws ParseException, DatatypeConfigurationException { DateProperty source = new DateProperty(); @@ -159,6 +174,7 @@ public class BuiltInTest { } @Test + @WithClasses( XmlGregCalToDateMapper.class ) public void shouldApplyBuiltInOnXmlGregCalToDate() throws ParseException, DatatypeConfigurationException { XmlGregorianCalendarProperty source = new XmlGregorianCalendarProperty(); @@ -175,6 +191,7 @@ public class BuiltInTest { } @Test + @WithClasses( StringToXmlGregCalMapper.class ) public void shouldApplyBuiltInStringToXmlGregCal() throws ParseException, DatatypeConfigurationException { StringProperty source = new StringProperty(); @@ -209,6 +226,7 @@ public class BuiltInTest { } @Test + @WithClasses( XmlGregCalToStringMapper.class ) public void shouldApplyBuiltInXmlGregCalToString() throws ParseException, DatatypeConfigurationException { XmlGregorianCalendarProperty source = new XmlGregorianCalendarProperty(); @@ -235,6 +253,7 @@ public class BuiltInTest { } @Test + @WithClasses( CalendarToXmlGregCalMapper.class ) public void shouldApplyBuiltInOnCalendarToXmlGregCal() throws ParseException, DatatypeConfigurationException { CalendarProperty source = new CalendarProperty(); @@ -250,6 +269,7 @@ public class BuiltInTest { } @Test + @WithClasses( XmlGregCalToCalendarMapper.class ) public void shouldApplyBuiltInOnXmlGregCalToCalendar() throws ParseException, DatatypeConfigurationException { XmlGregorianCalendarProperty source = new XmlGregorianCalendarProperty(); @@ -267,6 +287,7 @@ public class BuiltInTest { } @Test + @WithClasses( CalendarToDateMapper.class ) public void shouldApplyBuiltInOnCalendarToDate() throws ParseException, DatatypeConfigurationException { CalendarProperty source = new CalendarProperty(); @@ -282,6 +303,7 @@ public class BuiltInTest { } @Test + @WithClasses( DateToCalendarMapper.class ) public void shouldApplyBuiltInOnDateToCalendar() throws ParseException, DatatypeConfigurationException { DateProperty source = new DateProperty(); @@ -298,6 +320,7 @@ public class BuiltInTest { } @Test + @WithClasses( CalendarToStringMapper.class ) public void shouldApplyBuiltInOnCalendarToString() throws ParseException, DatatypeConfigurationException { CalendarProperty source = new CalendarProperty(); @@ -313,6 +336,7 @@ public class BuiltInTest { } @Test + @WithClasses( StringToCalendarMapper.class ) public void shouldApplyBuiltInOnStringToCalendar() throws ParseException, DatatypeConfigurationException { StringProperty source = new StringProperty(); @@ -329,6 +353,7 @@ public class BuiltInTest { } @Test + @WithClasses( IterableSourceTargetMapper.class ) public void shouldApplyBuiltInOnIterable() throws ParseException, DatatypeConfigurationException { IterableSource source = new IterableSource(); @@ -342,6 +367,7 @@ public class BuiltInTest { } @Test + @WithClasses( MapSourceTargetMapper.class ) public void shouldApplyBuiltInOnMap() throws ParseException, DatatypeConfigurationException { MapSource source = new MapSource(); @@ -357,6 +383,7 @@ public class BuiltInTest { } @Test + @WithClasses( CalendarToZonedDateTimeMapper.class ) public void shouldApplyBuiltInOnCalendarToZonedDateTime() throws ParseException { assertThat( CalendarToZonedDateTimeMapper.INSTANCE.map( null ) ).isNull(); @@ -373,6 +400,7 @@ public class BuiltInTest { } @Test + @WithClasses( ZonedDateTimeToCalendarMapper.class ) public void shouldApplyBuiltInOnZonedDateTimeToCalendar() throws ParseException { assertThat( ZonedDateTimeToCalendarMapper.INSTANCE.map( null ) ).isNull(); diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/BigDecimalProperty.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/BigDecimalProperty.java new file mode 100644 index 000000000..85e92f7c4 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/BigDecimalProperty.java @@ -0,0 +1,25 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.ap.test.builtin.bean; + +import java.math.BigDecimal; + +public class BigDecimalProperty { + + // CHECKSTYLE:OFF + public BigDecimal publicProp; + // CHECKSTYLE:ON + + private BigDecimal prop; + + public BigDecimal getProp() { + return prop; + } + + public void setProp( BigDecimal prop ) { + this.prop = prop; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/SomeType.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/SomeType.java new file mode 100644 index 000000000..9ba2a3816 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/SomeType.java @@ -0,0 +1,9 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.ap.test.builtin.bean; + +public class SomeType { +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/SomeTypeProperty.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/SomeTypeProperty.java new file mode 100644 index 000000000..fa40da83a --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/SomeTypeProperty.java @@ -0,0 +1,23 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.ap.test.builtin.bean; + +public class SomeTypeProperty { + + // CHECKSTYLE:OFF + public SomeType publicProp; + // CHECKSTYLE:ON + + private SomeType prop; + + public SomeType getProp() { + return prop; + } + + public void setProp( SomeType prop ) { + this.prop = prop; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/mapper/JaxbMapper.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/mapper/JaxbMapper.java index ae49b8d17..5b407e19d 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/builtin/mapper/JaxbMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/mapper/JaxbMapper.java @@ -6,6 +6,9 @@ package org.mapstruct.ap.test.builtin.mapper; import org.mapstruct.Mapper; +import org.mapstruct.ap.test.builtin.bean.BigDecimalProperty; +import org.mapstruct.ap.test.builtin.bean.SomeType; +import org.mapstruct.ap.test.builtin.bean.SomeTypeProperty; import org.mapstruct.ap.test.builtin.bean.JaxbElementProperty; import org.mapstruct.ap.test.builtin.bean.StringProperty; import org.mapstruct.factory.Mappers; @@ -16,4 +19,12 @@ public interface JaxbMapper { JaxbMapper INSTANCE = Mappers.getMapper( JaxbMapper.class ); StringProperty map(JaxbElementProperty source); + + BigDecimalProperty mapBD(JaxbElementProperty source); + + SomeTypeProperty mapSomeType(JaxbElementProperty source); + + default SomeType map( String in ) { + return new SomeType(); + } }