diff --git a/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/Article.java b/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/Article.java new file mode 100644 index 000000000..5f5020519 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/Article.java @@ -0,0 +1,32 @@ +/** + * 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.builder.nestedprop.flattening; + +public class Article { + private String description; + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/BuilderNestedPropertyTest.java b/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/BuilderNestedPropertyTest.java new file mode 100644 index 000000000..fbf1b3233 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/BuilderNestedPropertyTest.java @@ -0,0 +1,59 @@ +/** + * 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.builder.nestedprop.flattening; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mapstruct.ap.testutil.WithClasses; +import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Verifies that nested property mapping works with an immutable intermediate type. + */ +@WithClasses({ + ImmutableFlattenedStock.class, + Stock.class, + Article.class, + FlatteningMapper.class +}) +@RunWith(AnnotationProcessorTestRunner.class) +public class BuilderNestedPropertyTest { + + @Test + public void testNestedImmutablePropertyMapper() { + + Stock stock = new Stock(); + Article article1 = new Article(); + article1.setDescription( "shavingfoam" ); + Article article2 = new Article(); + article2.setDescription( "soap" ); + stock.setCount( 2 ); + stock.setFirst( article1 ); + stock.setSecond( article2 ); + + ImmutableFlattenedStock flattenedTarget = FlatteningMapper.INSTANCE.writeToFlatProperty( stock ); + + assertThat( flattenedTarget ).isNotNull(); + assertThat( flattenedTarget.getArticleCount() ).isEqualTo( 2 ); + assertThat( flattenedTarget.getArticle1() ).isEqualTo( "shavingfoam" ); + assertThat( flattenedTarget.getArticle2() ).isNull(); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/FlatteningMapper.java b/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/FlatteningMapper.java new file mode 100644 index 000000000..75a339b53 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/FlatteningMapper.java @@ -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.builder.nestedprop.flattening; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface FlatteningMapper { + + FlatteningMapper INSTANCE = Mappers.getMapper( FlatteningMapper.class ); + + @Mappings({ + @Mapping(target = "articleCount", source = "count"), + @Mapping(target = "article1", source = "first.description"), + @Mapping(target = "article2", ignore = true) + }) + ImmutableFlattenedStock writeToFlatProperty(Stock source); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/ImmutableFlattenedStock.java b/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/ImmutableFlattenedStock.java new file mode 100644 index 000000000..461bd3e7d --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/ImmutableFlattenedStock.java @@ -0,0 +1,78 @@ +/** + * 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.builder.nestedprop.flattening; + +public class ImmutableFlattenedStock { + + private final String article1; + private final String article2; + private final int articleCount; + + public ImmutableFlattenedStock(String article1, String article2, int count) { + this.article1 = article1; + this.article2 = article2; + this.articleCount = count; + } + + public String getArticle1() { + return article1; + } + + public String getArticle2() { + return article2; + } + + public int getArticleCount() { + return articleCount; + } + + public static ImmutableFlattenedStock.Builder builder() { + return new ImmutableFlattenedStock.Builder(); + } + + public static class Builder { + + private String article1; + private String article2; + private int articleCount; + + public Builder() { + } + + public Builder setArticle1(String article1) { + this.article1 = article1; + return this; + } + + public Builder setArticle2(String article2) { + this.article2 = article2; + return this; + } + + public Builder setArticleCount(int articleCount) { + this.articleCount = articleCount; + return this; + } + + public ImmutableFlattenedStock build() { + return new ImmutableFlattenedStock( article1, article2, articleCount ); + } + + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/Stock.java b/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/Stock.java new file mode 100644 index 000000000..ab10144e1 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builder/nestedprop/flattening/Stock.java @@ -0,0 +1,50 @@ +/** + * 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.builder.nestedprop.flattening; + +public class Stock { + private int count; + private Article first; + private Article second; + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public Article getFirst() { + return first; + } + + public void setFirst(Article first) { + this.first = first; + } + + public Article getSecond() { + return second; + } + + public void setSecond(Article second) { + this.second = second; + } + +}