-
-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,505 additions
and
126 deletions.
There are no files selected for viewing
281 changes: 281 additions & 0 deletions
281
...essor/src/test/java/org/mapstruct/ap/test/nestedbeans/mixed/AutomappingAndNestedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,281 @@ | ||
/** | ||
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.ap.test.nestedbeans.mixed; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mapstruct.ap.test.nestedbeans.mixed._target.FishDto; | ||
import org.mapstruct.ap.test.nestedbeans.mixed._target.FishTankDto; | ||
import org.mapstruct.ap.test.nestedbeans.mixed._target.FishTankWithNestedDocumentDto; | ||
import org.mapstruct.ap.test.nestedbeans.mixed._target.MaterialDto; | ||
import org.mapstruct.ap.test.nestedbeans.mixed._target.MaterialTypeDto; | ||
import org.mapstruct.ap.test.nestedbeans.mixed._target.OrnamentDto; | ||
import org.mapstruct.ap.test.nestedbeans.mixed._target.WaterPlantDto; | ||
import org.mapstruct.ap.test.nestedbeans.mixed._target.WaterQualityDto; | ||
import org.mapstruct.ap.test.nestedbeans.mixed._target.WaterQualityOrganisationDto; | ||
import org.mapstruct.ap.test.nestedbeans.mixed._target.WaterQualityReportDto; | ||
import org.mapstruct.ap.test.nestedbeans.mixed._target.WaterQualityWithDocumentDto; | ||
import org.mapstruct.ap.test.nestedbeans.mixed.source.Fish; | ||
import org.mapstruct.ap.test.nestedbeans.mixed.source.FishTank; | ||
import org.mapstruct.ap.test.nestedbeans.mixed.source.Interior; | ||
import org.mapstruct.ap.test.nestedbeans.mixed.source.MaterialType; | ||
import org.mapstruct.ap.test.nestedbeans.mixed.source.Ornament; | ||
import org.mapstruct.ap.test.nestedbeans.mixed.source.WaterPlant; | ||
import org.mapstruct.ap.test.nestedbeans.mixed.source.WaterQuality; | ||
import org.mapstruct.ap.test.nestedbeans.mixed.source.WaterQualityReport; | ||
import org.mapstruct.ap.testutil.IssueKey; | ||
import org.mapstruct.ap.testutil.WithClasses; | ||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; | ||
import org.mapstruct.ap.testutil.runner.GeneratedSource; | ||
|
||
/** | ||
* | ||
* @author Sjaak Derksen | ||
*/ | ||
@WithClasses({ | ||
FishDto.class, | ||
FishTankDto.class, | ||
WaterPlantDto.class, | ||
MaterialDto.class, | ||
MaterialTypeDto.class, | ||
OrnamentDto.class, | ||
WaterQualityDto.class, | ||
WaterQualityReportDto.class, | ||
WaterQualityOrganisationDto.class, | ||
Fish.class, | ||
FishTank.class, | ||
WaterPlant.class, | ||
MaterialType.class, | ||
Interior.class, | ||
Ornament.class, | ||
WaterQuality.class, | ||
WaterQualityReport.class, | ||
FishTankWithNestedDocumentDto.class, | ||
WaterQualityWithDocumentDto.class, | ||
FishTankMapper.class, | ||
FishTankMapperConstant.class, | ||
FishTankMapperExpression.class, | ||
FishTankMapperWithDocument.class | ||
}) | ||
@IssueKey("1057") | ||
@RunWith(AnnotationProcessorTestRunner.class) | ||
public class AutomappingAndNestedTest { | ||
|
||
@Rule | ||
public GeneratedSource generatedSource = new GeneratedSource().addComparisonToFixtureFor( | ||
FishTankMapperConstant.class | ||
); | ||
|
||
@Test | ||
public void shouldAutomapAndHandleSourceAndTargetPropertyNesting() { | ||
|
||
// -- prepare | ||
FishTank source = createFishTank(); | ||
|
||
// -- action | ||
FishTankDto target = FishTankMapper.INSTANCE.map( source ); | ||
|
||
// -- result | ||
assertThat( target.getName() ).isEqualTo( source.getName() ); | ||
|
||
// fish and fishDto can be automapped | ||
assertThat( target.getFish() ).isNotNull(); | ||
assertThat( target.getFish().getKind() ).isEqualTo( source.getFish().getType() ); | ||
assertThat( target.getFish().getName() ).isNull(); | ||
|
||
// automapping takes care of mapping property "waterPlant". | ||
assertThat( target.getPlant() ).isNotNull(); | ||
assertThat( target.getPlant().getKind() ).isEqualTo( source.getPlant().getKind() ); | ||
|
||
// ornament (nested asymetric source) | ||
assertThat( target.getOrnament() ).isNotNull(); | ||
assertThat( target.getOrnament().getType() ).isEqualTo( source.getInterior().getOrnament().getType() ); | ||
|
||
// material (nested asymetric target) | ||
assertThat( target.getMaterial() ).isNotNull(); | ||
assertThat( target.getMaterial().getManufacturer() ).isNull(); | ||
assertThat( target.getMaterial().getMaterialType() ).isNotNull(); | ||
assertThat( target.getMaterial().getMaterialType().getType() ).isEqualTo( source.getMaterial().getType() ); | ||
|
||
// first symetric then asymetric | ||
assertThat( target.getQuality() ).isNotNull(); | ||
assertThat( target.getQuality().getReport() ).isNotNull(); | ||
assertThat( target.getQuality().getReport().getVerdict() ) | ||
.isEqualTo( source.getQuality().getReport().getVerdict() ); | ||
assertThat( target.getQuality().getReport().getOrganisation().getApproval() ).isNull(); | ||
assertThat( target.getQuality().getReport().getOrganisation() ).isNotNull(); | ||
assertThat( target.getQuality().getReport().getOrganisation().getName() ) | ||
.isEqualTo( source.getQuality().getReport().getOrganisationName() ); | ||
} | ||
|
||
@Test | ||
public void shouldAutomapAndHandleSourceAndTargetPropertyNestingReverse() { | ||
|
||
// -- prepare | ||
FishTank source = createFishTank(); | ||
|
||
// -- action | ||
FishTankDto target = FishTankMapper.INSTANCE.map( source ); | ||
FishTank source2 = FishTankMapper.INSTANCE.map( target ); | ||
|
||
// -- result | ||
assertThat( source2.getName() ).isEqualTo( source.getName() ); | ||
|
||
// fish | ||
assertThat( source2.getFish() ).isNotNull(); | ||
assertThat( source2.getFish().getType() ).isEqualTo( source.getFish().getType() ); | ||
|
||
// interior, designer will not be mapped (asymetric) to target. Here it shows. | ||
assertThat( source2.getInterior() ).isNotNull(); | ||
assertThat( source2.getInterior().getDesigner() ).isNull(); | ||
assertThat( source2.getInterior().getOrnament() ).isNotNull(); | ||
assertThat( source2.getInterior().getOrnament().getType() ) | ||
.isEqualTo( source.getInterior().getOrnament().getType() ); | ||
|
||
// material | ||
assertThat( source2.getMaterial() ).isNotNull(); | ||
assertThat( source2.getMaterial().getType() ).isEqualTo( source.getMaterial().getType() ); | ||
|
||
// plant | ||
assertThat( source2.getPlant().getKind() ).isEqualTo( source.getPlant().getKind() ); | ||
|
||
// quality | ||
assertThat( source2.getQuality().getReport() ).isNotNull(); | ||
assertThat( source2.getQuality().getReport().getOrganisationName() ) | ||
.isEqualTo( source.getQuality().getReport().getOrganisationName() ); | ||
assertThat( source2.getQuality().getReport().getVerdict() ) | ||
.isEqualTo( source.getQuality().getReport().getVerdict() ); | ||
} | ||
|
||
@Test | ||
public void shouldAutomapAndHandleSourceAndTargetPropertyNestingAndConstant() { | ||
|
||
// -- prepare | ||
FishTank source = createFishTank(); | ||
|
||
// -- action | ||
FishTankDto target = FishTankMapperConstant.INSTANCE.map( source ); | ||
|
||
// -- result | ||
|
||
// fixed value | ||
assertThat( target.getFish().getName() ).isEqualTo( "Nemo" ); | ||
|
||
// automapping takes care of mapping property "waterPlant". | ||
assertThat( target.getPlant() ).isNotNull(); | ||
assertThat( target.getPlant().getKind() ).isEqualTo( source.getPlant().getKind() ); | ||
|
||
// non-nested and constant | ||
assertThat( target.getMaterial() ).isNotNull(); | ||
assertThat( target.getMaterial().getManufacturer() ).isEqualTo( "MMM" ); | ||
assertThat( target.getMaterial().getMaterialType() ).isNotNull(); | ||
assertThat( target.getMaterial().getMaterialType().getType() ).isEqualTo( source.getMaterial().getType() ); | ||
|
||
assertThat( target.getOrnament() ).isNull(); | ||
assertThat( target.getQuality() ).isNull(); | ||
|
||
} | ||
|
||
@Test | ||
public void shouldAutomapAndHandleSourceAndTargetPropertyNestingAndExpresion() { | ||
|
||
// -- prepare | ||
FishTank source = createFishTank(); | ||
|
||
// -- action | ||
FishTankDto target = FishTankMapperExpression.INSTANCE.map( source ); | ||
|
||
// -- result | ||
assertThat( target.getFish().getName() ).isEqualTo( "Jaws" ); | ||
|
||
assertThat( target.getMaterial() ).isNull(); | ||
assertThat( target.getOrnament() ).isNull(); | ||
assertThat( target.getPlant() ).isNull(); | ||
|
||
assertThat( target.getQuality() ).isNotNull(); | ||
assertThat( target.getQuality().getReport() ).isNotNull(); | ||
assertThat( target.getQuality().getReport().getVerdict() ) | ||
.isEqualTo( source.getQuality().getReport().getVerdict() ); | ||
assertThat( target.getQuality().getReport().getOrganisation() ).isNotNull(); | ||
assertThat( target.getQuality().getReport().getOrganisation().getApproval() ).isNull(); | ||
assertThat( target.getQuality().getReport().getOrganisation().getName() ).isEqualTo( "Dunno" ); | ||
} | ||
|
||
@Test | ||
public void shouldAutomapIntermediateLevelAndMapConstant() { | ||
|
||
// -- prepare | ||
FishTank source = createFishTank(); | ||
|
||
// -- action | ||
FishTankWithNestedDocumentDto target = FishTankMapperWithDocument.INSTANCE.map( source ); | ||
|
||
// -- result | ||
assertThat( target.getFish().getName() ).isEqualTo( "Jaws" ); | ||
|
||
assertThat( target.getMaterial() ).isNull(); | ||
assertThat( target.getOrnament() ).isNull(); | ||
assertThat( target.getPlant() ).isNull(); | ||
|
||
assertThat( target.getQuality() ).isNotNull(); | ||
assertThat( target.getQuality().getDocument() ).isNotNull(); | ||
assertThat( target.getQuality().getDocument().getVerdict() ) | ||
.isEqualTo( source.getQuality().getReport().getVerdict() ); | ||
assertThat( target.getQuality().getDocument().getOrganisation() ).isNotNull(); | ||
assertThat( target.getQuality().getDocument().getOrganisation().getApproval() ).isNull(); | ||
assertThat( target.getQuality().getDocument().getOrganisation().getName() ).isEqualTo( "NoIdeaInc" ); | ||
} | ||
|
||
private FishTank createFishTank() { | ||
FishTank fishTank = new FishTank(); | ||
|
||
Fish fish = new Fish(); | ||
fish.setType( "Carp" ); | ||
|
||
WaterPlant waterplant = new WaterPlant(); | ||
waterplant.setKind( "Water Hyacinth" ); | ||
|
||
Interior interior = new Interior(); | ||
interior.setDesigner( "MrVeryFamous" ); | ||
Ornament ornament = new Ornament(); | ||
ornament.setType( "castle" ); | ||
interior.setOrnament( ornament ); | ||
|
||
WaterQuality quality = new WaterQuality(); | ||
WaterQualityReport report = new WaterQualityReport(); | ||
report.setVerdict( "PASSED" ); | ||
report.setOrganisationName( "ACME" ); | ||
quality.setReport( report ); | ||
|
||
MaterialType materialType = new MaterialType(); | ||
materialType.setType( "myMaterialType" ); | ||
|
||
fishTank.setName( "MyLittleFishTank" ); | ||
fishTank.setFish( fish ); | ||
fishTank.setPlant( waterplant ); | ||
fishTank.setInterior( interior ); | ||
fishTank.setMaterial( materialType ); | ||
fishTank.setQuality( quality ); | ||
|
||
return fishTank; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
processor/src/test/java/org/mapstruct/ap/test/nestedbeans/mixed/FishTankMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.ap.test.nestedbeans.mixed; | ||
|
||
import org.mapstruct.InheritInverseConfiguration; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.Mappings; | ||
import org.mapstruct.ap.test.nestedbeans.mixed._target.FishTankDto; | ||
import org.mapstruct.ap.test.nestedbeans.mixed.source.FishTank; | ||
import org.mapstruct.factory.Mappers; | ||
|
||
/** | ||
* | ||
* @author Sjaak Derksen | ||
*/ | ||
@Mapper | ||
public interface FishTankMapper { | ||
|
||
FishTankMapper INSTANCE = Mappers.getMapper( FishTankMapper.class ); | ||
|
||
@Mappings({ | ||
@Mapping(target = "fish.kind", source = "fish.type"), | ||
@Mapping(target = "fish.name", ignore = true), | ||
@Mapping(target = "ornament", source = "interior.ornament"), | ||
@Mapping(target = "material.materialType", source = "material"), | ||
@Mapping(target = "quality.report.organisation.name", source = "quality.report.organisationName") | ||
}) | ||
FishTankDto map( FishTank source ); | ||
|
||
@Mappings({ | ||
@Mapping(target = "fish.kind", source = "source.fish.type"), | ||
@Mapping(target = "fish.name", ignore = true), | ||
@Mapping(target = "ornament", source = "source.interior.ornament"), | ||
@Mapping(target = "material.materialType", source = "source.material"), | ||
@Mapping(target = "quality.report.organisation.name", source = "source.quality.report.organisationName") | ||
}) | ||
FishTankDto mapAsWell( FishTank source ); | ||
|
||
@InheritInverseConfiguration( name = "map" ) | ||
FishTank map( FishTankDto source ); | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
processor/src/test/java/org/mapstruct/ap/test/nestedbeans/mixed/FishTankMapperConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.ap.test.nestedbeans.mixed; | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.Mappings; | ||
import org.mapstruct.ap.test.nestedbeans.mixed._target.FishTankDto; | ||
import org.mapstruct.ap.test.nestedbeans.mixed.source.FishTank; | ||
import org.mapstruct.factory.Mappers; | ||
|
||
/** | ||
* | ||
* @author Sjaak Derksen | ||
*/ | ||
@Mapper | ||
public interface FishTankMapperConstant { | ||
|
||
FishTankMapperConstant INSTANCE = Mappers.getMapper( FishTankMapperConstant.class ); | ||
|
||
@Mappings({ | ||
@Mapping(target = "fish.kind", source = "fish.type"), | ||
@Mapping(target = "fish.name", constant = "Nemo"), | ||
@Mapping(target = "ornament", ignore = true ), | ||
@Mapping(target = "material.materialType", source = "material"), | ||
@Mapping(target = "material.manufacturer", constant = "MMM" ), | ||
@Mapping(target = "quality", ignore = true) | ||
}) | ||
FishTankDto map( FishTank source ); | ||
|
||
} |
Oops, something went wrong.