Skip to content

Commit

Permalink
#1011 reproducer, not stopping name based mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaakd committed Jan 28, 2017
1 parent 280a438 commit 23fd42a
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 1 deletion.
@@ -0,0 +1,38 @@
/**
* 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;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ap.test.nestedtargetproperties._target.FishTankDto;
import org.mapstruct.ap.test.nestedtargetproperties.source.FishTank;
import org.mapstruct.factory.Mappers;

/**
*
* @author Sjaak Derksen
*/
@Mapper
public interface FishTankMapper {

FishTankMapper INSTANCE = Mappers.getMapper( FishTankMapper.class );

@Mapping(target = "fish.kind", source = "fish.type")
FishTankDto map( FishTank source );
}
Expand Up @@ -27,6 +27,12 @@
import org.mapstruct.ap.test.nestedsourceproperties.source.Label;
import org.mapstruct.ap.test.nestedsourceproperties.source.Song;
import org.mapstruct.ap.test.nestedsourceproperties.source.Studio;
import org.mapstruct.ap.test.nestedtargetproperties._target.FishDto;
import org.mapstruct.ap.test.nestedtargetproperties._target.FishTankDto;
import org.mapstruct.ap.test.nestedtargetproperties._target.WaterPlantDto;
import org.mapstruct.ap.test.nestedtargetproperties.source.Fish;
import org.mapstruct.ap.test.nestedtargetproperties.source.FishTank;
import org.mapstruct.ap.test.nestedtargetproperties.source.WaterPlant;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
Expand All @@ -35,7 +41,19 @@
*
* @author Sjaak Derksen
*/
@WithClasses({Song.class, Artist.class, Chart.class, Label.class, Studio.class, ChartEntry.class})
@WithClasses({
Song.class,
Artist.class,
Chart.class,
Label.class,
Studio.class,
ChartEntry.class,
FishDto.class,
FishTankDto.class,
WaterPlantDto.class,
Fish.class,
FishTank.class,
WaterPlant.class})
@IssueKey("389")
@RunWith(AnnotationProcessorTestRunner.class)
public class NestedTargetPropertiesTest {
Expand Down Expand Up @@ -159,4 +177,32 @@ public void shouldMapNestedTargetWitUpdate() {
assertThat( result.getSong().getPositions().get( 0 ) ).isEqualTo( 1 );

}

@Test
@WithClasses({FishTankMapper.class})
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" );
source.setFish( fish );
source.setPlant( waterplant );

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.getFish() ).isNotNull();
assertThat( target.getFish().getKind() ).isEqualTo( "Carp" );
assertThat( target.getFish().getName() ).isNull();

// automapping takes care of mapping property "waterPlant".
assertThat( target.getPlant() ).isNotNull();
assertThat( target.getPlant().getKind() ).isEqualTo( "Water Hyacinth" );
}

}
@@ -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.nestedtargetproperties._target;

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

private String kind;

// make sure that mapping on name does not happen based on name mapping
private String name;

public String getKind() {
return kind;
}

public void setKind(String kind) {
this.kind = kind;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
@@ -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 FishTankDto {

private FishDto fish;
private WaterPlantDto plant;

public FishDto getFish() {
return fish;
}

public void setFish(FishDto fish) {
this.fish = fish;
}

public WaterPlantDto getPlant() {
return plant;
}

public void setPlant(WaterPlantDto plant) {
this.plant = plant;
}

}
@@ -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 WaterPlantDto {

private String kind;

public String getKind() {
return kind;
}

public void setKind(String kind) {
this.kind = kind;
}

}
@@ -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.source;

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

private String type;

public String getType() {
return type;
}

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

}
@@ -0,0 +1,55 @@
/**
* 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.source;

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

private Fish fish;
private WaterPlant plant;
private String name;

public Fish getFish() {
return fish;
}

public void setFish(Fish fish) {
this.fish = fish;
}

public WaterPlant getPlant() {
return plant;
}

public void setPlant(WaterPlant plant) {
this.plant = plant;
}

public String getName() {
return name;
}

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

}
@@ -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.source;

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

private String kind;

public String getKind() {
return kind;
}

public void setKind(String kind) {
this.kind = kind;
}

}

0 comments on commit 23fd42a

Please sign in to comment.