#173 solution and added unit test with added test checks

This commit is contained in:
sjaakd 2014-03-21 23:37:46 +01:00
parent 51fb6ad20b
commit 82c41416a2
11 changed files with 420 additions and 3 deletions

View File

@ -192,9 +192,11 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
break;
}
}
Type declaringMapper = mappingMethod.getDeclaringMapper();
if ( implementationRequired ) {
mappingMethods.add( new DelegatingMethod( mappingMethod ) );
if ( ( declaringMapper == null ) || declaringMapper.equals( typeFactory.getType( element ) ) ) {
mappingMethods.add( new DelegatingMethod( mappingMethod ) );
}
}
}

View File

@ -113,7 +113,8 @@ public class DecoratorTest extends MapperTestBase {
//given
Calendar birthday = Calendar.getInstance();
birthday.set( 1928, 4, 23 );
Person person = new Person( "Gary", "Crant", birthday.getTime(), new Address( "42 Ocean View Drive" ) );
Person person = new Person2( "Gary", "Crant", birthday.getTime(), new Address( "42 Ocean View Drive" ) );
//when
PersonDto personDto = YetAnotherPersonMapper.INSTANCE.personToPersonDto( person );
@ -125,6 +126,43 @@ public class DecoratorTest extends MapperTestBase {
assertThat( personDto.getAddress().getAddressLine() ).isEqualTo( "42 Ocean View Drive" );
}
@IssueKey("173")
@Test
@WithClasses({
Person2Mapper.class,
Person2MapperDecorator.class,
Person2.class,
PersonDto2.class,
Employer.class,
EmployerDto.class,
EmployerMapper.class,
SportsClub.class,
SportsClubDto.class,
})
public void shouldApplyCustomMappers() {
//given
Calendar birthday = Calendar.getInstance();
birthday.set( 1928, 4, 23 );
Person2 person = new Person2( "Gary", "Crant", birthday.getTime(), new Address( "42 Ocean View Drive" ) );
person.setEmployer( new Employer( "ACME" ) );
person.setSportsClub( new SportsClub( "SC Duckburg" ) );
//when
PersonDto2 personDto = Person2Mapper.INSTANCE.personToPersonDto( person );
//then
assertThat( personDto ).isNotNull();
assertThat( personDto.getName() ).isEqualTo( "Gary Crant" );
assertThat( personDto.getAddress() ).isNotNull();
assertThat( personDto.getAddress().getAddressLine() ).isEqualTo( "42 Ocean View Drive" );
assertThat( personDto.getEmployer() ).isNotNull();
assertThat( personDto.getEmployer().getName() ).isNotNull();
assertThat( personDto.getEmployer().getName() ).isEqualTo( "ACME" );
assertThat( personDto.getSportsClub() ).isNotNull();
assertThat( personDto.getSportsClub().getName() ).isNotNull();
assertThat( personDto.getSportsClub().getName() ).isEqualTo( "SC Duckburg" );
}
@Test
@WithClasses({
ErroneousPersonMapper.class,

View File

@ -0,0 +1,42 @@
/**
* Copyright 2012-2014 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.decorator;
/**
*
* @author Sjaak Derksen
*/
public class Employer {
private String name;
public Employer() { }
public Employer( String name ) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,36 @@
/**
* Copyright 2012-2014 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.decorator;
/**
*
* @author Sjaak Derksen
*/
public class EmployerDto {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,38 @@
/**
* Copyright 2012-2014 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.decorator;
/**
*
* @author Sjaak Derksen
*/
public class EmployerMapper {
public Employer fromDto(EmployerDto dto) {
Employer employer = new Employer();
employer.setName( dto.getName() );
return employer;
}
public EmployerDto toDto(Employer employer) {
EmployerDto dto = new EmployerDto();
dto.setName( employer.getName() );
return dto;
}
}

View File

@ -0,0 +1,52 @@
/**
* Copyright 2012-2014 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.decorator;
import java.util.Date;
/**
*
* @author Sjaak Derksen
*/
public class Person2 extends Person {
private SportsClub sportsClub;
private Employer employer;
public Person2(String firstName, String lastName, Date dateOfBirth, Address address) {
super( firstName, lastName, dateOfBirth, address );
}
public SportsClub getSportsClub() {
return sportsClub;
}
public void setSportsClub(SportsClub sportsClub) {
this.sportsClub = sportsClub;
}
public Employer getEmployer() {
return employer;
}
public void setEmployer(Employer employer) {
this.employer = employer;
}
}

View File

@ -0,0 +1,47 @@
/**
* Copyright 2012-2014 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.decorator;
import org.mapstruct.DecoratedWith;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, uses = { EmployerMapper.class } )
@DecoratedWith(Person2MapperDecorator.class)
public abstract class Person2Mapper {
public static final Person2Mapper INSTANCE = Mappers.getMapper( Person2Mapper.class );
public abstract PersonDto2 personToPersonDto(Person2 person);
public abstract AddressDto addressToAddressDto(Address address);
public SportsClub fromDto(SportsClubDto dto) {
SportsClub sc = new SportsClub();
sc.setName( dto.getName() );
return sc;
}
public SportsClubDto toDto(SportsClub sc) {
SportsClubDto dto = new SportsClubDto();
dto.setName( sc.getName() );
return dto;
}
}

View File

@ -0,0 +1,37 @@
/**
* Copyright 2012-2014 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.decorator;
public abstract class Person2MapperDecorator extends Person2Mapper {
private final Person2Mapper delegate;
public Person2MapperDecorator(Person2Mapper delegate) {
this.delegate = delegate;
}
@Override
public PersonDto2 personToPersonDto(Person2 person) {
PersonDto2 dto = delegate.personToPersonDto( person );
dto.setName( person.getFirstName() + " " + person.getLastName() );
return dto;
}
}

View File

@ -0,0 +1,46 @@
/**
* Copyright 2012-2014 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.decorator;
/**
*
* @author Sjaak Derksen
*/
public class PersonDto2 extends PersonDto {
private SportsClubDto sportsClub;
private EmployerDto employer;
public SportsClubDto getSportsClub() {
return sportsClub;
}
public void setSportsClub(SportsClubDto sportsClub) {
this.sportsClub = sportsClub;
}
public EmployerDto getEmployer() {
return employer;
}
public void setEmployer(EmployerDto employer) {
this.employer = employer;
}
}

View File

@ -0,0 +1,42 @@
/**
* Copyright 2012-2014 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.decorator;
/**
*
* @author Sjaak Derksen
*/
public class SportsClub {
private String name;
public SportsClub() { }
public SportsClub( String name ) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,37 @@
/**
* Copyright 2012-2014 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.decorator;
/**
*
* @author Sjaak Derksen
*/
public class SportsClubDto {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}