Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ private Assignment resolveViaMethodAndMethod(Type sourceType, Type targetType) {
* <li>there is a method from B to C, methodY</li>
* </ul>
* 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) {

Expand Down Expand Up @@ -421,7 +425,11 @@ private Assignment resolveViaConversionAndMethod(Type sourceType, Type targetTyp
* <li>there is a conversion from A to B, conversionX</li>
* <li>there is a method from B to C, methodY</li>
* </ul>
* 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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +18,6 @@
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;
Expand All @@ -34,10 +32,13 @@
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;
Expand All @@ -64,6 +65,8 @@
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.
*
Expand All @@ -78,24 +81,11 @@
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
})
Expand All @@ -117,8 +107,8 @@ public static void restoreOriginalTimeZone() {
}

@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" );
Expand All @@ -130,6 +120,30 @@ public void shouldApplyBuiltInOnJAXBElement() throws ParseException, DatatypeCon
}

@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 {

Expand All @@ -144,6 +158,7 @@ public void shouldApplyBuiltInOnJAXBElementList() throws ParseException, Datatyp
}

@Test
@WithClasses( DateToXmlGregCalMapper.class )
public void shouldApplyBuiltInOnDateToXmlGregCal() throws ParseException, DatatypeConfigurationException {

DateProperty source = new DateProperty();
Expand All @@ -159,6 +174,7 @@ public void shouldApplyBuiltInOnDateToXmlGregCal() throws ParseException, Dataty
}

@Test
@WithClasses( XmlGregCalToDateMapper.class )
public void shouldApplyBuiltInOnXmlGregCalToDate() throws ParseException, DatatypeConfigurationException {

XmlGregorianCalendarProperty source = new XmlGregorianCalendarProperty();
Expand All @@ -175,6 +191,7 @@ public void shouldApplyBuiltInOnXmlGregCalToDate() throws ParseException, Dataty
}

@Test
@WithClasses( StringToXmlGregCalMapper.class )
public void shouldApplyBuiltInStringToXmlGregCal() throws ParseException, DatatypeConfigurationException {

StringProperty source = new StringProperty();
Expand Down Expand Up @@ -209,6 +226,7 @@ public void shouldApplyBuiltInStringToXmlGregCal() throws ParseException, Dataty
}

@Test
@WithClasses( XmlGregCalToStringMapper.class )
public void shouldApplyBuiltInXmlGregCalToString() throws ParseException, DatatypeConfigurationException {

XmlGregorianCalendarProperty source = new XmlGregorianCalendarProperty();
Expand All @@ -235,6 +253,7 @@ public void shouldApplyBuiltInXmlGregCalToString() throws ParseException, Dataty
}

@Test
@WithClasses( CalendarToXmlGregCalMapper.class )
public void shouldApplyBuiltInOnCalendarToXmlGregCal() throws ParseException, DatatypeConfigurationException {

CalendarProperty source = new CalendarProperty();
Expand All @@ -250,6 +269,7 @@ public void shouldApplyBuiltInOnCalendarToXmlGregCal() throws ParseException, Da
}

@Test
@WithClasses( XmlGregCalToCalendarMapper.class )
public void shouldApplyBuiltInOnXmlGregCalToCalendar() throws ParseException, DatatypeConfigurationException {

XmlGregorianCalendarProperty source = new XmlGregorianCalendarProperty();
Expand All @@ -267,6 +287,7 @@ public void shouldApplyBuiltInOnXmlGregCalToCalendar() throws ParseException, Da
}

@Test
@WithClasses( CalendarToDateMapper.class )
public void shouldApplyBuiltInOnCalendarToDate() throws ParseException, DatatypeConfigurationException {

CalendarProperty source = new CalendarProperty();
Expand All @@ -282,6 +303,7 @@ public void shouldApplyBuiltInOnCalendarToDate() throws ParseException, Datatype
}

@Test
@WithClasses( DateToCalendarMapper.class )
public void shouldApplyBuiltInOnDateToCalendar() throws ParseException, DatatypeConfigurationException {

DateProperty source = new DateProperty();
Expand All @@ -298,6 +320,7 @@ public void shouldApplyBuiltInOnDateToCalendar() throws ParseException, Datatype
}

@Test
@WithClasses( CalendarToStringMapper.class )
public void shouldApplyBuiltInOnCalendarToString() throws ParseException, DatatypeConfigurationException {

CalendarProperty source = new CalendarProperty();
Expand All @@ -313,6 +336,7 @@ public void shouldApplyBuiltInOnCalendarToString() throws ParseException, Dataty
}

@Test
@WithClasses( StringToCalendarMapper.class )
public void shouldApplyBuiltInOnStringToCalendar() throws ParseException, DatatypeConfigurationException {

StringProperty source = new StringProperty();
Expand All @@ -329,6 +353,7 @@ public void shouldApplyBuiltInOnStringToCalendar() throws ParseException, Dataty
}

@Test
@WithClasses( IterableSourceTargetMapper.class )
public void shouldApplyBuiltInOnIterable() throws ParseException, DatatypeConfigurationException {

IterableSource source = new IterableSource();
Expand All @@ -342,6 +367,7 @@ public void shouldApplyBuiltInOnIterable() throws ParseException, DatatypeConfig
}

@Test
@WithClasses( MapSourceTargetMapper.class )
public void shouldApplyBuiltInOnMap() throws ParseException, DatatypeConfigurationException {

MapSource source = new MapSource();
Expand All @@ -357,6 +383,7 @@ public void shouldApplyBuiltInOnMap() throws ParseException, DatatypeConfigurati
}

@Test
@WithClasses( CalendarToZonedDateTimeMapper.class )
public void shouldApplyBuiltInOnCalendarToZonedDateTime() throws ParseException {
assertThat( CalendarToZonedDateTimeMapper.INSTANCE.map( null ) ).isNull();

Expand All @@ -373,6 +400,7 @@ public void shouldApplyBuiltInOnCalendarToZonedDateTime() throws ParseException
}

@Test
@WithClasses( ZonedDateTimeToCalendarMapper.class )
public void shouldApplyBuiltInOnZonedDateTimeToCalendar() throws ParseException {
assertThat( ZonedDateTimeToCalendarMapper.INSTANCE.map( null ) ).isNull();

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}