mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
Adding test for reverse mapping with custom binding
This commit is contained in:
parent
1cf7e4275e
commit
02006a90b7
@ -22,29 +22,33 @@ import org.dozer.loader.api.BeanMappingBuilder;
|
||||
|
||||
public class ${implementationType} implements ${interfaceType} {
|
||||
|
||||
private final DozerBeanMapper mapper;
|
||||
private final DozerBeanMapper mapper;
|
||||
|
||||
public ${implementationType}() {
|
||||
mapper = new DozerBeanMapper();
|
||||
public ${implementationType}() {
|
||||
mapper = new DozerBeanMapper();
|
||||
|
||||
<#list mapperMethods as oneMethod>
|
||||
BeanMappingBuilder builder = new BeanMappingBuilder() {
|
||||
protected void configure() {
|
||||
mapping( ${oneMethod.parameter.type.name}.class, ${oneMethod.returnType.name}.class )
|
||||
<#list oneMethod.bindings as oneBinding>
|
||||
.fields("${oneBinding.sourceProperty}", "${oneBinding.targetProperty}")
|
||||
BeanMappingBuilder builder = null;
|
||||
|
||||
<#list mapperMethods as oneMethod>
|
||||
<#if oneMethod.bindings?has_content>
|
||||
builder = new BeanMappingBuilder() {
|
||||
protected void configure() {
|
||||
mapping( ${oneMethod.parameter.type.name}.class, ${oneMethod.returnType.name}.class )
|
||||
<#list oneMethod.bindings as oneBinding>
|
||||
.fields("${oneBinding.sourceProperty}", "${oneBinding.targetProperty}")
|
||||
</#list>
|
||||
;
|
||||
}
|
||||
};
|
||||
mapper.addMapping( builder );
|
||||
</#if>
|
||||
|
||||
</#list>
|
||||
}
|
||||
|
||||
<#list mapperMethods as oneMethod>
|
||||
public ${oneMethod.returnType.name} ${oneMethod.name}(${oneMethod.parameter.type.name} ${oneMethod.parameter.name}) {
|
||||
return mapper.map(${oneMethod.parameter.name}, ${oneMethod.returnType.name}.class);
|
||||
}
|
||||
</#list>
|
||||
;
|
||||
}
|
||||
};
|
||||
|
||||
mapper.addMapping( builder );
|
||||
</#list>
|
||||
}
|
||||
|
||||
<#list mapperMethods as oneMethod>
|
||||
public ${oneMethod.returnType.name} ${oneMethod.name}(${oneMethod.parameter.type.name} ${oneMethod.parameter.name}) {
|
||||
return mapper.map(${oneMethod.parameter.name}, ${oneMethod.returnType.name}.class);
|
||||
}
|
||||
</#list>
|
||||
}
|
||||
|
@ -125,6 +125,20 @@ public class CarMapperTest {
|
||||
assertThat( carDto.getSeatCount() ).isEqualTo( car.getNumberOfSeats() );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConsiderCustomMappingForReverseMapping() {
|
||||
|
||||
//given
|
||||
CarDto carDto = new CarDto( "Morris", 2 );
|
||||
|
||||
//when
|
||||
Car car = CarMapper.INSTANCE.carDtoToCar( carDto );
|
||||
|
||||
//then
|
||||
assertThat( car ).isNotNull();
|
||||
assertThat( car.getNumberOfSeats() ).isEqualTo( carDto.getSeatCount() );
|
||||
}
|
||||
|
||||
private File[] getSourceFiles(Class<?>... clazz) {
|
||||
|
||||
File[] sourceFiles = new File[clazz.length];
|
||||
|
@ -21,6 +21,9 @@ public class Car {
|
||||
|
||||
private int numberOfSeats;
|
||||
|
||||
public Car() {
|
||||
}
|
||||
|
||||
public Car(String make, int numberOfSeats) {
|
||||
this.make = make;
|
||||
this.numberOfSeats = numberOfSeats;
|
||||
|
@ -24,6 +24,11 @@ public class CarDto {
|
||||
public CarDto() {
|
||||
}
|
||||
|
||||
public CarDto(String make, int seatCount) {
|
||||
this.make = make;
|
||||
this.seatCount = seatCount;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
@ -26,4 +26,6 @@ public interface CarMapper {
|
||||
|
||||
@Mapping(source = "numberOfSeats", target = "seatCount")
|
||||
CarDto carToCarDto(Car car);
|
||||
|
||||
Car carDtoToCar(CarDto carDto);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user