From e815e3cb1eabfef2930accf7d5f2c442ff2a456e Mon Sep 17 00:00:00 2001 From: Oliver Erhart <8238759+thunderhook@users.noreply.github.com> Date: Sun, 10 Mar 2024 09:15:37 +0100 Subject: [PATCH] #3524 Provide tests with Lombok style super builders --- .../ap/test/superbuilder/AbstractVehicle.java | 46 +++++ .../test/superbuilder/AbstractVehicleDto.java | 20 +++ .../mapstruct/ap/test/superbuilder/Car.java | 63 +++++++ .../ap/test/superbuilder/CarDto.java | 21 +++ .../ap/test/superbuilder/CarMapper.java | 43 +++++ .../superbuilder/ChainedAccessorsCar.java | 58 +++++++ .../superbuilder/ChainedAccessorsVehicle.java | 72 ++++++++ .../superbuilder/InheritedAbstractCar.java | 54 ++++++ .../superbuilder/InheritedAbstractCarDto.java | 21 +++ .../ap/test/superbuilder/MuscleCar.java | 52 ++++++ .../ap/test/superbuilder/MuscleCarDto.java | 21 +++ .../ap/test/superbuilder/Passenger.java | 50 ++++++ .../ap/test/superbuilder/PassengerDto.java | 20 +++ .../test/superbuilder/SuperBuilderTest.java | 160 ++++++++++++++++++ .../ap/test/superbuilder/Vehicle.java | 50 ++++++ .../ap/test/superbuilder/VehicleDto.java | 25 +++ 16 files changed, 776 insertions(+) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/AbstractVehicle.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/AbstractVehicleDto.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/Car.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/CarDto.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/CarMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/ChainedAccessorsCar.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/ChainedAccessorsVehicle.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/InheritedAbstractCar.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/InheritedAbstractCarDto.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/MuscleCar.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/MuscleCarDto.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/Passenger.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/PassengerDto.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/SuperBuilderTest.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/Vehicle.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/superbuilder/VehicleDto.java diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/AbstractVehicle.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/AbstractVehicle.java new file mode 100644 index 0000000000..d9cf159888 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/AbstractVehicle.java @@ -0,0 +1,46 @@ +/* + * 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.superbuilder; + +public abstract class AbstractVehicle { + + private final int amountOfTires; + private final Passenger passenger; + + protected AbstractVehicle(AbstractVehicleBuilder b) { + this.amountOfTires = b.amountOfTires; + this.passenger = b.passenger; + } + + public int getAmountOfTires() { + return this.amountOfTires; + } + + public Passenger getPassenger() { + return this.passenger; + } + + public abstract static class AbstractVehicleBuilder> { + + private int amountOfTires; + private Passenger passenger; + + public B amountOfTires(int amountOfTires) { + this.amountOfTires = amountOfTires; + return self(); + } + + public B passenger(Passenger passenger) { + this.passenger = passenger; + return self(); + } + + protected abstract B self(); + + public abstract C build(); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/AbstractVehicleDto.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/AbstractVehicleDto.java new file mode 100644 index 0000000000..0b97398ae0 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/AbstractVehicleDto.java @@ -0,0 +1,20 @@ +/* + * 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.superbuilder; + +public abstract class AbstractVehicleDto { + + private final int amountOfTires; + + public AbstractVehicleDto(int amountOfTires) { + this.amountOfTires = amountOfTires; + } + + public int getAmountOfTires() { + return amountOfTires; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/Car.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/Car.java new file mode 100644 index 0000000000..af15ee6631 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/Car.java @@ -0,0 +1,63 @@ +/* + * 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.superbuilder; + +public class Car extends Vehicle { + + private final String manufacturer; + private final Passenger passenger; + + protected Car(CarBuilder b) { + super( b ); + this.manufacturer = b.manufacturer; + this.passenger = b.passenger; + } + + public static CarBuilder builder() { + return new CarBuilderImpl(); + } + + public String getManufacturer() { + return this.manufacturer; + } + + public Passenger getPassenger() { + return this.passenger; + } + + public abstract static class CarBuilder> extends VehicleBuilder { + + private String manufacturer; + private Passenger passenger; + + public B manufacturer(String manufacturer) { + this.manufacturer = manufacturer; + return self(); + } + + public B passenger(Passenger passenger) { + this.passenger = passenger; + return self(); + } + + protected abstract B self(); + + public abstract C build(); + } + + private static final class CarBuilderImpl extends CarBuilder { + private CarBuilderImpl() { + } + + protected CarBuilderImpl self() { + return this; + } + + public Car build() { + return new Car( this ); + } + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/CarDto.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/CarDto.java new file mode 100644 index 0000000000..9877e27d71 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/CarDto.java @@ -0,0 +1,21 @@ +/* + * 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.superbuilder; + +public class CarDto extends VehicleDto { + + private final String manufacturer; + + public CarDto(int amountOfTires, String manufacturer, PassengerDto passenger) { + super( amountOfTires, passenger ); + this.manufacturer = manufacturer; + } + + public String getManufacturer() { + return manufacturer; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/CarMapper.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/CarMapper.java new file mode 100644 index 0000000000..a3def64ead --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/CarMapper.java @@ -0,0 +1,43 @@ +/* + * 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.superbuilder; + +import org.mapstruct.InheritInverseConfiguration; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface CarMapper { + + CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); + + @Mapping(target = "amountOfTires", source = "tireCount") + Car carDtoToCar(CarDto source); + + @InheritInverseConfiguration(name = "carDtoToCar") + CarDto carToCarDto(Car source); + + @Mapping(target = "amountOfTires", source = "tireCount") + ChainedAccessorsCar carDtoToChainedAccessorsCar(CarDto source); + + @InheritInverseConfiguration(name = "carDtoToChainedAccessorsCar") + CarDto chainedAccessorsCarToCarDto(ChainedAccessorsCar source); + + @Mapping(target = "amountOfTires", source = "tireCount") + InheritedAbstractCar carDtoToInheritedAbstractCar(CarDto source); + + @InheritInverseConfiguration(name = "carDtoToInheritedAbstractCar") + CarDto inheritedAbstractCarToCarDto(InheritedAbstractCar source); + + @Mapping(target = "amountOfTires", source = "tireCount") + @Mapping(target = "horsePower", constant = "140.5f") + MuscleCar carDtoToMuscleCar(CarDto source); + + @InheritInverseConfiguration(name = "carDtoToMuscleCar") + CarDto muscleCarToCarDto(MuscleCar source); + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/ChainedAccessorsCar.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/ChainedAccessorsCar.java new file mode 100644 index 0000000000..48faa101c3 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/ChainedAccessorsCar.java @@ -0,0 +1,58 @@ +/* + * 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.superbuilder; + +public class ChainedAccessorsCar extends ChainedAccessorsVehicle { + + private String manufacturer; + + protected ChainedAccessorsCar(ChainedAccessorsCarBuilder b) { + super( b ); + this.manufacturer = b.manufacturer; + } + + public static ChainedAccessorsCarBuilder builder() { + return new ChainedAccessorsCarBuilderImpl(); + } + + public String getManufacturer() { + return this.manufacturer; + } + + public ChainedAccessorsCar setManufacturer(String manufacturer) { + this.manufacturer = manufacturer; + return this; + } + + public abstract static class ChainedAccessorsCarBuilder> extends ChainedAccessorsVehicleBuilder { + + private String manufacturer; + + public B manufacturer(String manufacturer) { + this.manufacturer = manufacturer; + return self(); + } + + protected abstract B self(); + + public abstract C build(); + } + + private static final class ChainedAccessorsCarBuilderImpl + extends ChainedAccessorsCarBuilder { + private ChainedAccessorsCarBuilderImpl() { + } + + protected ChainedAccessorsCarBuilderImpl self() { + return this; + } + + public ChainedAccessorsCar build() { + return new ChainedAccessorsCar( this ); + } + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/ChainedAccessorsVehicle.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/ChainedAccessorsVehicle.java new file mode 100644 index 0000000000..3344fb26f9 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/ChainedAccessorsVehicle.java @@ -0,0 +1,72 @@ +/* + * 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.superbuilder; + +public class ChainedAccessorsVehicle { + private int amountOfTires; + private Passenger passenger; + + protected ChainedAccessorsVehicle(ChainedAccessorsVehicleBuilder b) { + this.amountOfTires = b.amountOfTires; + this.passenger = b.passenger; + } + + public static ChainedAccessorsVehicleBuilder builder() { + return new ChainedAccessorsVehicleBuilderImpl(); + } + + public int getAmountOfTires() { + return this.amountOfTires; + } + + public Passenger getPassenger() { + return this.passenger; + } + + public ChainedAccessorsVehicle setAmountOfTires(int amountOfTires) { + this.amountOfTires = amountOfTires; + return this; + } + + public ChainedAccessorsVehicle setPassenger(Passenger passenger) { + this.passenger = passenger; + return this; + } + + public abstract static class ChainedAccessorsVehicleBuilder> { + private int amountOfTires; + private Passenger passenger; + + public B amountOfTires(int amountOfTires) { + this.amountOfTires = amountOfTires; + return self(); + } + + public B passenger(Passenger passenger) { + this.passenger = passenger; + return self(); + } + + protected abstract B self(); + + public abstract C build(); + } + + private static final class ChainedAccessorsVehicleBuilderImpl + extends ChainedAccessorsVehicleBuilder { + private ChainedAccessorsVehicleBuilderImpl() { + } + + protected ChainedAccessorsVehicleBuilderImpl self() { + return this; + } + + public ChainedAccessorsVehicle build() { + return new ChainedAccessorsVehicle( this ); + } + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/InheritedAbstractCar.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/InheritedAbstractCar.java new file mode 100644 index 0000000000..1d07106af8 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/InheritedAbstractCar.java @@ -0,0 +1,54 @@ +/* + * 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.superbuilder; + +public class InheritedAbstractCar extends AbstractVehicle { + + private final String manufacturer; + + protected InheritedAbstractCar(InheritedAbstractCarBuilder b) { + super( b ); + this.manufacturer = b.manufacturer; + } + + public static InheritedAbstractCarBuilder builder() { + return new InheritedAbstractCarBuilderImpl(); + } + + public String getManufacturer() { + return this.manufacturer; + } + + public abstract static class InheritedAbstractCarBuilder> + + extends AbstractVehicleBuilder { + private String manufacturer; + + public B manufacturer(String manufacturer) { + this.manufacturer = manufacturer; + return self(); + } + + protected abstract B self(); + + public abstract C build(); + } + + private static final class InheritedAbstractCarBuilderImpl + extends InheritedAbstractCarBuilder { + private InheritedAbstractCarBuilderImpl() { + } + + protected InheritedAbstractCarBuilderImpl self() { + return this; + } + + public InheritedAbstractCar build() { + return new InheritedAbstractCar( this ); + } + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/InheritedAbstractCarDto.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/InheritedAbstractCarDto.java new file mode 100644 index 0000000000..a9cc6bbdbb --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/InheritedAbstractCarDto.java @@ -0,0 +1,21 @@ +/* + * 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.superbuilder; + +public class InheritedAbstractCarDto extends AbstractVehicleDto { + + private final String manufacturer; + + public InheritedAbstractCarDto(int amountOfTires, String manufacturer) { + super( amountOfTires ); + this.manufacturer = manufacturer; + } + + public String getManufacturer() { + return manufacturer; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/MuscleCar.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/MuscleCar.java new file mode 100644 index 0000000000..ec330536ff --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/MuscleCar.java @@ -0,0 +1,52 @@ +/* + * 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.superbuilder; + +public class MuscleCar extends Car { + + private final float horsePower; + + protected MuscleCar(MuscleCarBuilder b) { + super( b ); + this.horsePower = b.horsePower; + } + + public static MuscleCarBuilder builder() { + return new MuscleCarBuilderImpl(); + } + + public float getHorsePower() { + return this.horsePower; + } + + public abstract static class MuscleCarBuilder> + extends CarBuilder { + + private float horsePower; + + public B horsePower(float horsePower) { + this.horsePower = horsePower; + return self(); + } + + protected abstract B self(); + + public abstract C build(); + } + + private static final class MuscleCarBuilderImpl extends MuscleCarBuilder { + private MuscleCarBuilderImpl() { + } + + protected MuscleCarBuilderImpl self() { + return this; + } + + public MuscleCar build() { + return new MuscleCar( this ); + } + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/MuscleCarDto.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/MuscleCarDto.java new file mode 100644 index 0000000000..7ffe150ecf --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/MuscleCarDto.java @@ -0,0 +1,21 @@ +/* + * 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.superbuilder; + +public class MuscleCarDto extends CarDto { + + private final float horsePower; + + public MuscleCarDto(int amountOfTires, String manufacturer, PassengerDto passenger, float horsePower) { + super( amountOfTires, manufacturer, passenger ); + this.horsePower = horsePower; + } + + public float getHorsePower() { + return horsePower; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/Passenger.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/Passenger.java new file mode 100644 index 0000000000..2b6740af78 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/Passenger.java @@ -0,0 +1,50 @@ +/* + * 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.superbuilder; + +public class Passenger { + + private final String name; + + protected Passenger(PassengerBuilder b) { + this.name = b.name; + } + + public static PassengerBuilder builder() { + return new PassengerBuilderImpl(); + } + + public String getName() { + return this.name; + } + + public abstract static class PassengerBuilder> { + + private String name; + + public B name(String name) { + this.name = name; + return self(); + } + + protected abstract B self(); + + public abstract C build(); + } + + private static final class PassengerBuilderImpl extends PassengerBuilder { + private PassengerBuilderImpl() { + } + + protected PassengerBuilderImpl self() { + return this; + } + + public Passenger build() { + return new Passenger( this ); + } + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/PassengerDto.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/PassengerDto.java new file mode 100644 index 0000000000..f63f6e0683 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/PassengerDto.java @@ -0,0 +1,20 @@ +/* + * 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.superbuilder; + +public class PassengerDto { + + private final String name; + + public PassengerDto(String name) { + this.name = name; + } + + public String getName() { + return name; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/SuperBuilderTest.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/SuperBuilderTest.java new file mode 100644 index 0000000000..93dfb746b0 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/SuperBuilderTest.java @@ -0,0 +1,160 @@ +/* + * 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.superbuilder; + +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.ProcessorTest; +import org.mapstruct.ap.testutil.WithClasses; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests the mapping of models annotated with @SuperBuilder as source and target. + * + * @author Oliver Erhart + */ +@WithClasses({ + AbstractVehicleDto.class, + CarDto.class, + InheritedAbstractCarDto.class, + MuscleCarDto.class, + PassengerDto.class, + VehicleDto.class, + AbstractVehicle.class, + Car.class, + ChainedAccessorsCar.class, + ChainedAccessorsVehicle.class, + InheritedAbstractCar.class, + MuscleCar.class, + Passenger.class, + Vehicle.class, + CarMapper.class +}) +@IssueKey("3524") +public class SuperBuilderTest { + + @ProcessorTest + public void simpleMapping() { + + PassengerDto passenger = new PassengerDto( "Tom" ); + CarDto carDto = new CarDto( 4, "BMW", passenger ); + + Car car = CarMapper.INSTANCE.carDtoToCar( carDto ); + + assertThat( car.getManufacturer() ).isEqualTo( "BMW" ); + assertThat( car.getAmountOfTires() ).isEqualTo( 4 ); + assertThat( car.getPassenger().getName() ).isEqualTo( "Tom" ); + } + + @ProcessorTest + public void simpleMappingInverse() { + + Passenger passenger = Passenger.builder().name( "Tom" ).build(); + Car car = Car.builder() + .manufacturer( "BMW" ) + .amountOfTires( 4 ) + .passenger( passenger ) + .build(); + + CarDto carDto = CarMapper.INSTANCE.carToCarDto( car ); + + assertThat( carDto.getManufacturer() ).isEqualTo( "BMW" ); + assertThat( carDto.getTireCount() ).isEqualTo( 4 ); + assertThat( carDto.getPassenger().getName() ).isEqualTo( "Tom" ); + } + + @ProcessorTest + public void chainedMapping() { + + PassengerDto passenger = new PassengerDto( "Tom" ); + CarDto carDto = new CarDto( 4, "BMW", passenger ); + + ChainedAccessorsCar car = CarMapper.INSTANCE.carDtoToChainedAccessorsCar( carDto ); + + assertThat( car.getManufacturer() ).isEqualTo( "BMW" ); + assertThat( car.getAmountOfTires() ).isEqualTo( 4 ); + assertThat( car.getPassenger().getName() ).isEqualTo( "Tom" ); + } + + @ProcessorTest + public void chainedMappingInverse() { + + Passenger passenger = Passenger.builder().name( "Tom" ).build(); + ChainedAccessorsCar chainedAccessorsCar = ChainedAccessorsCar.builder() + .manufacturer( "BMW" ) + .amountOfTires( 4 ) + .passenger( passenger ) + .build(); + + CarDto carDto = CarMapper.INSTANCE.chainedAccessorsCarToCarDto( chainedAccessorsCar ); + + assertThat( carDto.getManufacturer() ).isEqualTo( "BMW" ); + assertThat( carDto.getTireCount() ).isEqualTo( 4 ); + assertThat( carDto.getPassenger().getName() ).isEqualTo( "Tom" ); + } + + @ProcessorTest + public void inheritedAbstractMapping() { + + PassengerDto passenger = new PassengerDto( "Tom" ); + CarDto carDto = new CarDto( 4, "BMW", passenger ); + + InheritedAbstractCar car = CarMapper.INSTANCE.carDtoToInheritedAbstractCar( carDto ); + + assertThat( car.getManufacturer() ).isEqualTo( "BMW" ); + assertThat( car.getAmountOfTires() ).isEqualTo( 4 ); + assertThat( car.getPassenger().getName() ).isEqualTo( "Tom" ); + } + + @ProcessorTest + public void inheritedAbstractMappingInverse() { + + Passenger passenger = Passenger.builder().name( "Tom" ).build(); + InheritedAbstractCar inheritedAbstractCar = InheritedAbstractCar.builder() + .manufacturer( "BMW" ) + .amountOfTires( 4 ) + .passenger( passenger ) + .build(); + + CarDto carDto = CarMapper.INSTANCE.inheritedAbstractCarToCarDto( inheritedAbstractCar ); + + assertThat( carDto.getManufacturer() ).isEqualTo( "BMW" ); + assertThat( carDto.getTireCount() ).isEqualTo( 4 ); + assertThat( carDto.getPassenger().getName() ).isEqualTo( "Tom" ); + } + + @ProcessorTest + public void secondLevelMapping() { + + PassengerDto passenger = new PassengerDto( "Tom" ); + CarDto carDto = new CarDto( 4, "BMW", passenger ); + + MuscleCar car = CarMapper.INSTANCE.carDtoToMuscleCar( carDto ); + + assertThat( car.getManufacturer() ).isEqualTo( "BMW" ); + assertThat( car.getAmountOfTires() ).isEqualTo( 4 ); + assertThat( car.getHorsePower() ).isEqualTo( 140.5f ); + assertThat( car.getPassenger().getName() ).isEqualTo( "Tom" ); + } + + @ProcessorTest + public void secondLevelMappingInverse() { + + Passenger passenger = Passenger.builder().name( "Tom" ).build(); + MuscleCar muscleCar = MuscleCar.builder() + .manufacturer( "BMW" ) + .amountOfTires( 4 ) + .passenger( passenger ) + .build(); + + CarDto carDto = CarMapper.INSTANCE.muscleCarToCarDto( muscleCar ); + + assertThat( carDto.getManufacturer() ).isEqualTo( "BMW" ); + assertThat( carDto.getTireCount() ).isEqualTo( 4 ); + assertThat( carDto.getPassenger().getName() ).isEqualTo( "Tom" ); + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/Vehicle.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/Vehicle.java new file mode 100644 index 0000000000..2f4a19d842 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/Vehicle.java @@ -0,0 +1,50 @@ +/* + * 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.superbuilder; + +public class Vehicle { + + private final int amountOfTires; + + protected Vehicle(VehicleBuilder b) { + this.amountOfTires = b.amountOfTires; + } + + public static VehicleBuilder builder() { + return new VehicleBuilderImpl(); + } + + public int getAmountOfTires() { + return this.amountOfTires; + } + + public abstract static class VehicleBuilder> { + + private int amountOfTires; + + public B amountOfTires(int amountOfTires) { + this.amountOfTires = amountOfTires; + return self(); + } + + protected abstract B self(); + + public abstract C build(); + } + + private static final class VehicleBuilderImpl extends VehicleBuilder { + private VehicleBuilderImpl() { + } + + protected VehicleBuilderImpl self() { + return this; + } + + public Vehicle build() { + return new Vehicle( this ); + } + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/superbuilder/VehicleDto.java b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/VehicleDto.java new file mode 100644 index 0000000000..04dfefb205 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/superbuilder/VehicleDto.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.superbuilder; + +public class VehicleDto { + + private final int tireCount; + private final PassengerDto passenger; + + public VehicleDto(int tireCount, PassengerDto passenger) { + this.tireCount = tireCount; + this.passenger = passenger; + } + + public int getTireCount() { + return tireCount; + } + + public PassengerDto getPassenger() { + return passenger; + } +}