Skip to content

Commit

Permalink
mapstruct#1057 additional testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaakd authored and filiphr committed Feb 13, 2017
1 parent fb02e7c commit d560144
Show file tree
Hide file tree
Showing 16 changed files with 596 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,21 @@ public interface FishTankMapper {

@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 = "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 );

// how about reversing?
// ignores?
// constants, expressions?
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.mapstruct.ap.test.nestedtargetproperties;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -188,6 +189,7 @@ public void shouldMapNestedTargetWitUpdate() {
}

@Test
@Ignore // for the time being
public void automappingAndTargetNestingDemonstrator() {

FishTank source = new FishTank();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,21 @@
import org.junit.runner.RunWith;
import org.mapstruct.ap.test.nestedtargetproperties._target.FishDto;
import org.mapstruct.ap.test.nestedtargetproperties._target.FishTankDto;
import org.mapstruct.ap.test.nestedtargetproperties._target.MaterialDto;
import org.mapstruct.ap.test.nestedtargetproperties._target.MaterialTypeDto;
import org.mapstruct.ap.test.nestedtargetproperties._target.OrnamentDto;
import org.mapstruct.ap.test.nestedtargetproperties._target.WaterPlantDto;
import org.mapstruct.ap.test.nestedtargetproperties._target.WaterQualityDto;
import org.mapstruct.ap.test.nestedtargetproperties._target.WaterQualityOrganisationDto;
import org.mapstruct.ap.test.nestedtargetproperties._target.WaterQualityReportDto;
import org.mapstruct.ap.test.nestedtargetproperties.source.Fish;
import org.mapstruct.ap.test.nestedtargetproperties.source.FishTank;
import org.mapstruct.ap.test.nestedtargetproperties.source.Interior;
import org.mapstruct.ap.test.nestedtargetproperties.source.MaterialType;
import org.mapstruct.ap.test.nestedtargetproperties.source.Ornament;
import org.mapstruct.ap.test.nestedtargetproperties.source.WaterPlant;
import org.mapstruct.ap.test.nestedtargetproperties.source.WaterQuality;
import org.mapstruct.ap.test.nestedtargetproperties.source.WaterQualityReport;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import org.mapstruct.ap.testutil.runner.WithSingleCompiler;
Expand All @@ -39,9 +50,20 @@
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,
FishTankMapper.class
})
@RunWith(AnnotationProcessorTestRunner.class)
Expand All @@ -52,26 +74,63 @@ public class NestedTargetPropertiesTest1 {
public void automappingAndTargetNestingDemonstrator() {

FishTank source = new FishTank();
source.setName( "MyLittleFishTank" );

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 );

source.setName( "MyLittleFishTank" );
source.setFish( fish );
source.setPlant( waterplant );
source.setInterior( interior );


FishTankDto target = FishTankMapper.INSTANCE.map( source );

// the nested property generates a method fishTankToFishDto(FishTank fishTank, FishDto mappingTarget)
// when name based mapping continues MapStruct searches for a property called `name` in fishTank (type
// 'FishTank'. If it is there, it should most cerntainly not be mapped to a mappingTarget of type 'FishDto'
assertThat( target.getName() ).isEqualTo( source.getName() );

assertThat( target.getFish() ).isNotNull();
assertThat( target.getFish().getKind() ).isEqualTo( "Carp" );
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( "Water Hyacinth" );
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() ).isNotNull();
assertThat( target.getQuality().getReport().getOrganisation().getName() )
.isEqualTo( source.getQuality().getReport().getOrganisationName() );
assertThat( target.getQuality().getReport().getOrganisation().getName() ).isNull();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public class FishTankDto {

private FishDto fish;
private WaterPlantDto plant;
private String name;
private MaterialDto material;
private OrnamentDto ornament;
private WaterQualityDto quality;

public FishDto getFish() {
return fish;
Expand All @@ -43,4 +47,36 @@ public void setPlant(WaterPlantDto plant) {
this.plant = plant;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public MaterialDto getMaterial() {
return material;
}

public void setMaterial(MaterialDto material) {
this.material = material;
}

public OrnamentDto getOrnament() {
return ornament;
}

public void setOrnament(OrnamentDto ornament) {
this.ornament = ornament;
}

public WaterQualityDto getQuality() {
return quality;
}

public void setQuality(WaterQualityDto quality) {
this.quality = quality;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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.nestedtargetproperties._target;

/**
*
* @author Sjaak Derksen
*/
public class MaterialDto {

private String manufacturer;
private MaterialTypeDto materialType;

public String getManufacturer() {
return manufacturer;
}

public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}

public MaterialTypeDto getMaterialType() {
return materialType;
}

public void setMaterialType(MaterialTypeDto materialType) {
this.materialType = materialType;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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.nestedtargetproperties._target;

/**
*
* @author Sjaak Derksen
*/
public class MaterialTypeDto {

private String type;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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.nestedtargetproperties._target;

/**
*
* @author Sjaak Derksen
*/
public class OrnamentDto {

private String type;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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.nestedtargetproperties._target;

/**
*
* @author Sjaak Derksen
*/
public class WaterQualityDto {

private WaterQualityReportDto report;

public WaterQualityReportDto getReport() {
return report;
}

public void setReport(WaterQualityReportDto report) {
this.report = report;
}

}

0 comments on commit d560144

Please sign in to comment.