From c1341d4fe0dfb46fe20a8fa48d314ca5cfd71be2 Mon Sep 17 00:00:00 2001 From: sjaakd Date: Sun, 14 Feb 2016 20:49:18 +0100 Subject: [PATCH] #750 Unit Test of @Named annotation --- .../qualifier/bean/ReleaseFactory.java | 2 + .../qualifier/handwritten/Facts.java | 2 + .../qualifier/handwritten/PlotWords.java | 2 + .../handwritten/SomeOtherMapper.java | 2 + .../qualifier/handwritten/Titles.java | 4 +- .../qualifier/hybrid/HybridTest.java | 63 ++++++++ .../qualifier/hybrid/ReleaseMapper.java | 44 ++++++ .../qualifier/hybrid/SourceRelease.java | 37 +++++ .../qualifier/hybrid/TargetRelease.java | 37 +++++ .../qualifier/named/ErroneousMapper.java | 44 ++++++ .../selection/qualifier/named/FactMapper.java | 42 ++++++ .../qualifier/named/KeyWordMapper.java | 56 ++++++++ .../qualifier/named/MovieFactoryMapper.java | 40 ++++++ .../qualifier/named/MovieMapper.java | 44 ++++++ .../selection/qualifier/named/NamedTest.java | 135 ++++++++++++++++++ 15 files changed, 553 insertions(+), 1 deletion(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/HybridTest.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/ReleaseMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/SourceRelease.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/TargetRelease.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/ErroneousMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/FactMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/KeyWordMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/MovieFactoryMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/MovieMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/NamedTest.java diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/bean/ReleaseFactory.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/bean/ReleaseFactory.java index 52a66e4e8..7cb813025 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/bean/ReleaseFactory.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/bean/ReleaseFactory.java @@ -18,6 +18,7 @@ */ package org.mapstruct.ap.test.selection.qualifier.bean; +import org.mapstruct.Named; import org.mapstruct.ap.test.selection.qualifier.annotation.CreateGermanRelease; /** @@ -31,6 +32,7 @@ public class ReleaseFactory { } @CreateGermanRelease + @Named( "CreateGermanRelease" ) public GermanRelease createGermanRelease() { return new GermanRelease(); } diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/Facts.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/Facts.java index 0ccf3bb2c..334fc97f4 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/Facts.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/Facts.java @@ -23,6 +23,7 @@ import java.util.Map; import org.mapstruct.ap.test.selection.qualifier.annotation.EnglishToGerman; import com.google.common.collect.ImmutableMap; +import org.mapstruct.Named; /** * @@ -39,6 +40,7 @@ public class Facts { .build(); @EnglishToGerman + @Named( "EnglishToGerman" ) public String translateFactName( String fact ) { String result = EN_GER.get( fact ); return result != null ? result : fact; diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/PlotWords.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/PlotWords.java index 225b3d960..b697fa564 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/PlotWords.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/PlotWords.java @@ -25,6 +25,7 @@ import java.util.Map; import org.mapstruct.ap.test.selection.qualifier.annotation.EnglishToGerman; import com.google.common.collect.ImmutableMap; +import org.mapstruct.Named; /** * @@ -39,6 +40,7 @@ public class PlotWords { .build(); @EnglishToGerman + @Named( "EnglishToGerman" ) public List translate( List keywords ) { List result = new ArrayList(); for ( String keyword : keywords ) { diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/SomeOtherMapper.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/SomeOtherMapper.java index 2cdf48643..dbf9d9426 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/SomeOtherMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/SomeOtherMapper.java @@ -18,6 +18,7 @@ */ package org.mapstruct.ap.test.selection.qualifier.handwritten; +import org.mapstruct.Named; 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 { @NonQualifierAnnotated + @Named( "NonQualifierAnnotated" ) public String methodNotToSelect( String title ) { throw new AssertionError( "method should not be called" ); } diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/Titles.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/Titles.java index 1a6693524..a0dd2d2e7 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/Titles.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/handwritten/Titles.java @@ -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.TitleTranslator; -//import org.mapstruct.ap.test.selection.qualifier.annotation.TitleTranslator; import com.google.common.collect.ImmutableMap; +import org.mapstruct.Named; /** * * @author Sjaak Derksen */ @TitleTranslator +@Named( "TitleTranslator" ) public class Titles { private static final Map EN_GER = ImmutableMap.builder() @@ -42,6 +43,7 @@ public class Titles { .build(); @EnglishToGerman + @Named( "EnglishToGerman" ) public String translateTitle(String title) { return EN_GER.get( title ); } diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/HybridTest.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/HybridTest.java new file mode 100644 index 000000000..a6561a3db --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/HybridTest.java @@ -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" ); + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/ReleaseMapper.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/ReleaseMapper.java new file mode 100644 index 000000000..cbc49a4b3 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/ReleaseMapper.java @@ -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 ); + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/SourceRelease.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/SourceRelease.java new file mode 100644 index 000000000..4c80f1f05 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/SourceRelease.java @@ -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; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/TargetRelease.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/TargetRelease.java new file mode 100644 index 000000000..49ad6e296 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/hybrid/TargetRelease.java @@ -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; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/ErroneousMapper.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/ErroneousMapper.java new file mode 100644 index 000000000..5d2405aef --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/ErroneousMapper.java @@ -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 ); + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/FactMapper.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/FactMapper.java new file mode 100644 index 000000000..49da66ca4 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/FactMapper.java @@ -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> mapFacts( Map> keyWords ); + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/KeyWordMapper.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/KeyWordMapper.java new file mode 100644 index 000000000..ea8394c6b --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/KeyWordMapper.java @@ -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 EN_GER = ImmutableMap.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 mapKeyWords( List keyWords ); + + @Named( "EnglishToGerman" ) + public String mapKeyWord( String keyword ) { + return EN_GER.get( keyword ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/MovieFactoryMapper.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/MovieFactoryMapper.java new file mode 100644 index 000000000..3d7289d8a --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/MovieFactoryMapper.java @@ -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 ); + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/MovieMapper.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/MovieMapper.java new file mode 100644 index 000000000..937b23c25 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/MovieMapper.java @@ -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 ); + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/NamedTest.java b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/NamedTest.java new file mode 100644 index 000000000..c5b38f680 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/qualifier/named/NamedTest.java @@ -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> facts = new HashMap>(); + 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> facts = new HashMap>(); + 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" ) ) + ); + } + +}