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} {
|
public class ${implementationType} implements ${interfaceType} {
|
||||||
|
|
||||||
private final DozerBeanMapper mapper;
|
private final DozerBeanMapper mapper;
|
||||||
|
|
||||||
public ${implementationType}() {
|
public ${implementationType}() {
|
||||||
mapper = new DozerBeanMapper();
|
mapper = new DozerBeanMapper();
|
||||||
|
|
||||||
<#list mapperMethods as oneMethod>
|
BeanMappingBuilder builder = null;
|
||||||
BeanMappingBuilder builder = new BeanMappingBuilder() {
|
|
||||||
protected void configure() {
|
<#list mapperMethods as oneMethod>
|
||||||
mapping( ${oneMethod.parameter.type.name}.class, ${oneMethod.returnType.name}.class )
|
<#if oneMethod.bindings?has_content>
|
||||||
<#list oneMethod.bindings as oneBinding>
|
builder = new BeanMappingBuilder() {
|
||||||
.fields("${oneBinding.sourceProperty}", "${oneBinding.targetProperty}")
|
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>
|
</#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() );
|
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) {
|
private File[] getSourceFiles(Class<?>... clazz) {
|
||||||
|
|
||||||
File[] sourceFiles = new File[clazz.length];
|
File[] sourceFiles = new File[clazz.length];
|
||||||
|
@ -21,6 +21,9 @@ public class Car {
|
|||||||
|
|
||||||
private int numberOfSeats;
|
private int numberOfSeats;
|
||||||
|
|
||||||
|
public Car() {
|
||||||
|
}
|
||||||
|
|
||||||
public Car(String make, int numberOfSeats) {
|
public Car(String make, int numberOfSeats) {
|
||||||
this.make = make;
|
this.make = make;
|
||||||
this.numberOfSeats = numberOfSeats;
|
this.numberOfSeats = numberOfSeats;
|
||||||
|
@ -24,6 +24,11 @@ public class CarDto {
|
|||||||
public CarDto() {
|
public CarDto() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CarDto(String make, int seatCount) {
|
||||||
|
this.make = make;
|
||||||
|
this.seatCount = seatCount;
|
||||||
|
}
|
||||||
|
|
||||||
public String getMake() {
|
public String getMake() {
|
||||||
return make;
|
return make;
|
||||||
}
|
}
|
||||||
|
@ -26,4 +26,6 @@ public interface CarMapper {
|
|||||||
|
|
||||||
@Mapping(source = "numberOfSeats", target = "seatCount")
|
@Mapping(source = "numberOfSeats", target = "seatCount")
|
||||||
CarDto carToCarDto(Car car);
|
CarDto carToCarDto(Car car);
|
||||||
|
|
||||||
|
Car carDtoToCar(CarDto carDto);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user