#136 Add additional test to verify compatibility of @TargetType with nested mapping methods

This commit is contained in:
Andreas Gudian 2014-02-28 23:09:19 +01:00
parent 1db853137c
commit 16112a6156
6 changed files with 64 additions and 2 deletions

View File

@ -1075,7 +1075,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
mappedElement,
mapperReferences,
methods,
methodYCandidate.getParameters().get( 0 ).getType(),
methodYCandidate.getSourceParameters().get( 0 ).getType(),
returnType,
targetPropertyName,
dateFormat

View File

@ -0,0 +1,35 @@
/**
* 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.references;
/**
* @author Andreas Gudian
*
*/
public class GenericWrapper<T> {
private final T wrapped;
public GenericWrapper(T someType) {
this.wrapped = someType;
}
public T getWrapped() {
return wrapped;
}
}

View File

@ -86,17 +86,20 @@ public class ReferencedMapperTest extends MapperTestBase {
@Test
@IssueKey( "136" )
@WithClasses( { SourceTargetMapperWithPrimitives.class, SourceWithWrappers.class, TargetWithPrimitives.class } )
@WithClasses( { SourceTargetMapperWithPrimitives.class, SourceWithWrappers.class, TargetWithPrimitives.class,
GenericWrapper.class } )
public void shouldMapPrimitivesWithCustomMapper() {
SourceWithWrappers source = new SourceWithWrappers();
source.setProp1( new SomeType( "42" ) );
source.setProp2( new SomeType( "1701" ) );
source.setProp3( new SomeType( "true" ) );
source.setProp4( new GenericWrapper<SomeType>( new SomeType( "x" ) ) );
TargetWithPrimitives result = SourceTargetMapperWithPrimitives.INSTANCE.sourceToTarget( source );
assertThat( result.getProp1() ).isEqualTo( 42 );
assertThat( result.getProp2() ).isEqualTo( 1701 );
assertThat( result.isProp3() ).isEqualTo( true );
assertThat( result.getProp4() ).isEqualTo( 'x' );
}
}

View File

@ -44,7 +44,14 @@ public abstract class SourceTargetMapperWithPrimitives {
else if ( clazz == boolean.class ) {
return (T) Boolean.valueOf( wrapper.getValue() );
}
else if ( clazz == char.class ) {
return (T) Character.valueOf( wrapper.getValue().charAt( 0 ) );
}
return null;
}
public <T extends BaseType> T unwrapGenericWrapper(GenericWrapper<T> source, @TargetType Class<T> targetType) {
return source.getWrapped();
}
}

View File

@ -26,6 +26,7 @@ public class SourceWithWrappers {
private SomeType prop1;
private SomeType prop2;
private SomeType prop3;
private GenericWrapper<SomeType> prop4;
public SomeType getProp1() {
return prop1;
@ -51,4 +52,11 @@ public class SourceWithWrappers {
this.prop3 = prop3;
}
public GenericWrapper<SomeType> getProp4() {
return prop4;
}
public void setProp4(GenericWrapper<SomeType> prop4) {
this.prop4 = prop4;
}
}

View File

@ -26,6 +26,7 @@ public class TargetWithPrimitives {
private int prop1;
private long prop2;
private boolean prop3;
private char prop4;
public int getProp1() {
return prop1;
@ -50,4 +51,12 @@ public class TargetWithPrimitives {
public void setProp3(boolean prop3) {
this.prop3 = prop3;
}
public char getProp4() {
return prop4;
}
public void setProp4(char prop4) {
this.prop4 = prop4;
}
}