mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#750 Unit Test of @Named annotation
This commit is contained in:
parent
faedebc119
commit
c1341d4fe0
@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.selection.qualifier.bean;
|
package org.mapstruct.ap.test.selection.qualifier.bean;
|
||||||
|
|
||||||
|
import org.mapstruct.Named;
|
||||||
import org.mapstruct.ap.test.selection.qualifier.annotation.CreateGermanRelease;
|
import org.mapstruct.ap.test.selection.qualifier.annotation.CreateGermanRelease;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,6 +32,7 @@ public class ReleaseFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@CreateGermanRelease
|
@CreateGermanRelease
|
||||||
|
@Named( "CreateGermanRelease" )
|
||||||
public GermanRelease createGermanRelease() {
|
public GermanRelease createGermanRelease() {
|
||||||
return new GermanRelease();
|
return new GermanRelease();
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ import java.util.Map;
|
|||||||
import org.mapstruct.ap.test.selection.qualifier.annotation.EnglishToGerman;
|
import org.mapstruct.ap.test.selection.qualifier.annotation.EnglishToGerman;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import org.mapstruct.Named;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -39,6 +40,7 @@ public class Facts {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
@EnglishToGerman
|
@EnglishToGerman
|
||||||
|
@Named( "EnglishToGerman" )
|
||||||
public String translateFactName( String fact ) {
|
public String translateFactName( String fact ) {
|
||||||
String result = EN_GER.get( fact );
|
String result = EN_GER.get( fact );
|
||||||
return result != null ? result : fact;
|
return result != null ? result : fact;
|
||||||
|
@ -25,6 +25,7 @@ import java.util.Map;
|
|||||||
import org.mapstruct.ap.test.selection.qualifier.annotation.EnglishToGerman;
|
import org.mapstruct.ap.test.selection.qualifier.annotation.EnglishToGerman;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import org.mapstruct.Named;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -39,6 +40,7 @@ public class PlotWords {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
@EnglishToGerman
|
@EnglishToGerman
|
||||||
|
@Named( "EnglishToGerman" )
|
||||||
public List<String> translate( List<String> keywords ) {
|
public List<String> translate( List<String> keywords ) {
|
||||||
List<String> result = new ArrayList<String>();
|
List<String> result = new ArrayList<String>();
|
||||||
for ( String keyword : keywords ) {
|
for ( String keyword : keywords ) {
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.selection.qualifier.handwritten;
|
package org.mapstruct.ap.test.selection.qualifier.handwritten;
|
||||||
|
|
||||||
|
import org.mapstruct.Named;
|
||||||
import org.mapstruct.ap.test.selection.qualifier.annotation.NonQualifierAnnotated;
|
import org.mapstruct.ap.test.selection.qualifier.annotation.NonQualifierAnnotated;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,6 +28,7 @@ import org.mapstruct.ap.test.selection.qualifier.annotation.NonQualifierAnnotate
|
|||||||
public class SomeOtherMapper {
|
public class SomeOtherMapper {
|
||||||
|
|
||||||
@NonQualifierAnnotated
|
@NonQualifierAnnotated
|
||||||
|
@Named( "NonQualifierAnnotated" )
|
||||||
public String methodNotToSelect( String title ) {
|
public String methodNotToSelect( String title ) {
|
||||||
throw new AssertionError( "method should not be called" );
|
throw new AssertionError( "method should not be called" );
|
||||||
}
|
}
|
||||||
|
@ -22,15 +22,16 @@ import java.util.Map;
|
|||||||
|
|
||||||
import org.mapstruct.ap.test.selection.qualifier.annotation.EnglishToGerman;
|
import org.mapstruct.ap.test.selection.qualifier.annotation.EnglishToGerman;
|
||||||
import org.mapstruct.ap.test.selection.qualifier.annotation.TitleTranslator;
|
import org.mapstruct.ap.test.selection.qualifier.annotation.TitleTranslator;
|
||||||
//import org.mapstruct.ap.test.selection.qualifier.annotation.TitleTranslator;
|
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import org.mapstruct.Named;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sjaak Derksen
|
* @author Sjaak Derksen
|
||||||
*/
|
*/
|
||||||
@TitleTranslator
|
@TitleTranslator
|
||||||
|
@Named( "TitleTranslator" )
|
||||||
public class Titles {
|
public class Titles {
|
||||||
|
|
||||||
private static final Map<String, String> EN_GER = ImmutableMap.<String, String>builder()
|
private static final Map<String, String> EN_GER = ImmutableMap.<String, String>builder()
|
||||||
@ -42,6 +43,7 @@ public class Titles {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
@EnglishToGerman
|
@EnglishToGerman
|
||||||
|
@Named( "EnglishToGerman" )
|
||||||
public String translateTitle(String title) {
|
public String translateTitle(String title) {
|
||||||
return EN_GER.get( title );
|
return EN_GER.get( title );
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,63 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2016 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.selection.qualifier.hybrid;
|
||||||
|
|
||||||
|
import static org.fest.assertions.Assertions.assertThat;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.annotation.EnglishToGerman;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.annotation.NonQualifierAnnotated;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.annotation.TitleTranslator;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.SomeOtherMapper;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.Titles;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@IssueKey( "750" )
|
||||||
|
@WithClasses( {
|
||||||
|
SourceRelease.class,
|
||||||
|
TargetRelease.class,
|
||||||
|
ReleaseMapper.class,
|
||||||
|
SomeOtherMapper.class,
|
||||||
|
NonQualifierAnnotated.class,
|
||||||
|
Titles.class,
|
||||||
|
EnglishToGerman.class,
|
||||||
|
TitleTranslator.class
|
||||||
|
} )
|
||||||
|
@RunWith( AnnotationProcessorTestRunner.class )
|
||||||
|
public class HybridTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldMatchClassAndMethod() {
|
||||||
|
|
||||||
|
SourceRelease foreignMovies = new SourceRelease();
|
||||||
|
foreignMovies.setTitle( "Sixth Sense, The" );
|
||||||
|
|
||||||
|
TargetRelease germanMovies = ReleaseMapper.INSTANCE.toGerman( foreignMovies );
|
||||||
|
assertThat( germanMovies ).isNotNull();
|
||||||
|
assertThat( germanMovies.getTitle() ).isEqualTo( "Der sechste Sinn" );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2016 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.selection.qualifier.hybrid;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.Mappings;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.annotation.TitleTranslator;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.SomeOtherMapper;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.Titles;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper( uses = { Titles.class, SomeOtherMapper.class } )
|
||||||
|
public interface ReleaseMapper {
|
||||||
|
|
||||||
|
ReleaseMapper INSTANCE = Mappers.getMapper( ReleaseMapper.class );
|
||||||
|
|
||||||
|
|
||||||
|
@Mappings( {
|
||||||
|
@Mapping( target = "title", qualifiedBy = { TitleTranslator.class }, qualifiedByName = { "EnglishToGerman" } )
|
||||||
|
} )
|
||||||
|
TargetRelease toGerman( SourceRelease movies );
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2016 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.selection.qualifier.hybrid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class SourceRelease {
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2016 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.selection.qualifier.hybrid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class TargetRelease {
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2016 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.selection.qualifier.named;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.Mappings;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.bean.GermanRelease;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.bean.OriginalRelease;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.SomeOtherMapper;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.YetAnotherMapper;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper( uses = { SomeOtherMapper.class, YetAnotherMapper.class } )
|
||||||
|
public interface ErroneousMapper {
|
||||||
|
|
||||||
|
ErroneousMapper INSTANCE = Mappers.getMapper( ErroneousMapper.class );
|
||||||
|
|
||||||
|
@Mappings( {
|
||||||
|
@Mapping( target = "title", qualifiedByName = "NonQualifierAnnotated" )
|
||||||
|
} )
|
||||||
|
GermanRelease toGerman( OriginalRelease movies );
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2016 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.selection.qualifier.named;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.mapstruct.MapMapping;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.Facts;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.PlotWords;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper( uses = { Facts.class, PlotWords.class } )
|
||||||
|
public interface FactMapper {
|
||||||
|
|
||||||
|
FactMapper INSTANCE = Mappers.getMapper( FactMapper.class );
|
||||||
|
|
||||||
|
@MapMapping( keyQualifiedByName = "EnglishToGerman", valueQualifiedByName = "EnglishToGerman" )
|
||||||
|
Map<String, List<String>> mapFacts( Map<String, List<String>> keyWords );
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2016 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.selection.qualifier.named;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.mapstruct.IterableMapping;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.SomeOtherMapper;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import org.mapstruct.Named;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper( uses = { SomeOtherMapper.class } )
|
||||||
|
public abstract class KeyWordMapper {
|
||||||
|
|
||||||
|
private static final Map<String, String> EN_GER = ImmutableMap.<String, String>builder()
|
||||||
|
.put( "magnificent", "Großartig" )
|
||||||
|
.put( "evergreen", "Evergreen" )
|
||||||
|
.put( "classic", "Klassiker" )
|
||||||
|
.put( "box office flop", "Kasse Flop" )
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
public static final KeyWordMapper INSTANCE = Mappers.getMapper( KeyWordMapper.class );
|
||||||
|
|
||||||
|
@IterableMapping( dateFormat = "", qualifiedByName = "EnglishToGerman" )
|
||||||
|
abstract List<String> mapKeyWords( List<String> keyWords );
|
||||||
|
|
||||||
|
@Named( "EnglishToGerman" )
|
||||||
|
public String mapKeyWord( String keyword ) {
|
||||||
|
return EN_GER.get( keyword );
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2016 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.selection.qualifier.named;
|
||||||
|
|
||||||
|
import org.mapstruct.BeanMapping;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.bean.AbstractEntry;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.bean.OriginalRelease;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.bean.ReleaseFactory;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper( uses = ReleaseFactory.class )
|
||||||
|
public interface MovieFactoryMapper {
|
||||||
|
|
||||||
|
MovieFactoryMapper INSTANCE = Mappers.getMapper( MovieFactoryMapper.class );
|
||||||
|
|
||||||
|
@BeanMapping(qualifiedByName = "CreateGermanRelease" )
|
||||||
|
AbstractEntry toGerman( OriginalRelease movies );
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2016 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.selection.qualifier.named;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.Mappings;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.bean.GermanRelease;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.bean.OriginalRelease;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.SomeOtherMapper;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.Titles;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper( uses = { Titles.class, SomeOtherMapper.class, KeyWordMapper.class, FactMapper.class } )
|
||||||
|
public interface MovieMapper {
|
||||||
|
|
||||||
|
MovieMapper INSTANCE = Mappers.getMapper( MovieMapper.class );
|
||||||
|
|
||||||
|
@Mappings( {
|
||||||
|
@Mapping( target = "title", qualifiedByName = { "TitleTranslator", "EnglishToGerman" } )
|
||||||
|
} )
|
||||||
|
GermanRelease toGerman( OriginalRelease movies );
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,135 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2016 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.selection.qualifier.named;
|
||||||
|
|
||||||
|
import static org.fest.assertions.Assertions.assertThat;
|
||||||
|
import static org.fest.assertions.MapAssert.entry;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.annotation.CreateGermanRelease;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.annotation.EnglishToGerman;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.annotation.NonQualifierAnnotated;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.annotation.TitleTranslator;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.bean.AbstractEntry;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.bean.GermanRelease;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.bean.OriginalRelease;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.bean.ReleaseFactory;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.Facts;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.PlotWords;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.SomeOtherMapper;
|
||||||
|
import org.mapstruct.ap.test.selection.qualifier.handwritten.Titles;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@IssueKey( "750" )
|
||||||
|
@WithClasses( {
|
||||||
|
OriginalRelease.class,
|
||||||
|
GermanRelease.class,
|
||||||
|
AbstractEntry.class,
|
||||||
|
SomeOtherMapper.class,
|
||||||
|
NonQualifierAnnotated.class
|
||||||
|
} )
|
||||||
|
@RunWith( AnnotationProcessorTestRunner.class )
|
||||||
|
public class NamedTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses( {
|
||||||
|
Titles.class,
|
||||||
|
Facts.class,
|
||||||
|
PlotWords.class,
|
||||||
|
OriginalRelease.class,
|
||||||
|
EnglishToGerman.class,
|
||||||
|
TitleTranslator.class,
|
||||||
|
MovieMapper.class,
|
||||||
|
KeyWordMapper.class,
|
||||||
|
FactMapper.class } )
|
||||||
|
public void shouldMatchClassAndMethod() {
|
||||||
|
|
||||||
|
OriginalRelease foreignMovies = new OriginalRelease();
|
||||||
|
foreignMovies.setTitle( "Sixth Sense, The" );
|
||||||
|
foreignMovies.setKeyWords( Arrays.asList( "evergreen", "magnificent" ) );
|
||||||
|
Map<String, List<String>> facts = new HashMap<String, List<String>>();
|
||||||
|
facts.put( "director", Arrays.asList( "M. Night Shyamalan" ) );
|
||||||
|
facts.put( "cast", Arrays.asList( "Bruce Willis", "Haley Joel Osment", "Toni Collette" ) );
|
||||||
|
facts.put( "plot keywords", Arrays.asList( "boy", "child psychologist", "I see dead people" ) );
|
||||||
|
foreignMovies.setFacts( facts );
|
||||||
|
|
||||||
|
GermanRelease germanMovies = MovieMapper.INSTANCE.toGerman( foreignMovies );
|
||||||
|
assertThat( germanMovies ).isNotNull();
|
||||||
|
assertThat( germanMovies.getTitle() ).isEqualTo( "Der sechste Sinn" );
|
||||||
|
assertThat( germanMovies.getKeyWords() ).isNotNull();
|
||||||
|
assertThat( germanMovies.getKeyWords().size() ).isEqualTo( 2 );
|
||||||
|
assertThat( germanMovies.getKeyWords() ).containsSequence( "Evergreen", "Großartig" );
|
||||||
|
|
||||||
|
assertThat( germanMovies.getFacts() ).isNotNull();
|
||||||
|
assertThat( germanMovies.getFacts() ).hasSize( 3 );
|
||||||
|
assertThat( germanMovies.getFacts() ).includes(
|
||||||
|
entry( "Regisseur", Arrays.asList( "M. Night Shyamalan" ) ),
|
||||||
|
entry( "Besetzung", Arrays.asList( "Bruce Willis", "Haley Joel Osment", "Toni Collette" ) ),
|
||||||
|
entry( "Handlungstichwörter", Arrays.asList( "Jungen", "Kinderpsychologe", "Ich sehe tote Menschen" ) )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses( {
|
||||||
|
MovieFactoryMapper.class,
|
||||||
|
ReleaseFactory.class,
|
||||||
|
CreateGermanRelease.class
|
||||||
|
})
|
||||||
|
@IssueKey( "342")
|
||||||
|
public void testFactorySelectionWithQualifier() {
|
||||||
|
|
||||||
|
OriginalRelease foreignMovies = new OriginalRelease();
|
||||||
|
foreignMovies.setTitle( "Sixth Sense, The" );
|
||||||
|
foreignMovies.setKeyWords( Arrays.asList( "evergreen", "magnificent" ) );
|
||||||
|
Map<String, List<String>> facts = new HashMap<String, List<String>>();
|
||||||
|
facts.put( "director", Arrays.asList( "M. Night Shyamalan" ) );
|
||||||
|
facts.put( "cast", Arrays.asList( "Bruce Willis", "Haley Joel Osment", "Toni Collette" ) );
|
||||||
|
facts.put( "plot keywords", Arrays.asList( "boy", "child psychologist", "I see dead people" ) );
|
||||||
|
foreignMovies.setFacts( facts );
|
||||||
|
|
||||||
|
AbstractEntry abstractEntry = MovieFactoryMapper.INSTANCE.toGerman( foreignMovies );
|
||||||
|
assertThat( abstractEntry ).isNotNull();
|
||||||
|
assertThat( abstractEntry ).isInstanceOf( GermanRelease.class );
|
||||||
|
assertThat( abstractEntry.getTitle() ).isEqualTo( "Sixth Sense, The" );
|
||||||
|
assertThat( abstractEntry.getKeyWords() ).isNotNull();
|
||||||
|
assertThat( abstractEntry.getKeyWords().size() ).isEqualTo( 2 );
|
||||||
|
assertThat( abstractEntry.getKeyWords() ).containsSequence( "evergreen", "magnificent" );
|
||||||
|
|
||||||
|
assertThat( abstractEntry.getFacts() ).isNotNull();
|
||||||
|
assertThat( abstractEntry.getFacts() ).hasSize( 3 );
|
||||||
|
assertThat( abstractEntry.getFacts() ).includes(
|
||||||
|
entry( "director", Arrays.asList( "M. Night Shyamalan" ) ),
|
||||||
|
entry( "cast", Arrays.asList( "Bruce Willis", "Haley Joel Osment", "Toni Collette" ) ),
|
||||||
|
entry( "plot keywords", Arrays.asList( "boy", "child psychologist", "I see dead people" ) )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user