#782 Add tests with nested expanding target

This commit is contained in:
sjaakd 2018-03-18 16:31:20 +01:00 committed by Filip Hrisafov
parent 998d6fc35f
commit 045532fa68
5 changed files with 61 additions and 63 deletions

View File

@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.builder.nestedprop;
package org.mapstruct.ap.test.builder.nestedprop.expanding;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -29,25 +29,22 @@ import static org.assertj.core.api.Assertions.assertThat;
* Verifies that nested property mapping works with an immutable intermediate type.
*/
@WithClasses({
FlattenedSource.class,
ExpandedTarget.class,
ImmutableTargetContainer.class,
FlattenedMapper.class
FlattenedStock.class,
ImmutableExpandedStock.class,
ImmutableArticle.class,
ExpandingMapper.class
})
@RunWith(AnnotationProcessorTestRunner.class)
public class BuilderNestedPropertyTest {
@Test
public void testNestedImmutablePropertyMapper() {
ExpandedTarget expandedTarget = FlattenedMapper.INSTANCE.writeToNestedProperty( new FlattenedSource(
"Foo",
"Bar",
33
) );
FlattenedStock stock = new FlattenedStock( "Sock", "Tie", 33 );
ImmutableExpandedStock expandedTarget = ExpandingMapper.INSTANCE.writeToNestedProperty( stock );
assertThat( expandedTarget ).isNotNull();
assertThat( expandedTarget.getCount() ).isEqualTo( 33 );
assertThat( expandedTarget.getSecond() ).isNull();
assertThat( expandedTarget.getFirst() ).isNotNull();
assertThat( expandedTarget.getFirst().getFoo() ).isEqualTo( "33" );
assertThat( expandedTarget.getFirst().getDescription() ).isEqualTo( "Sock" );
}
}

View File

@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.builder.nestedprop;
package org.mapstruct.ap.test.builder.nestedprop.expanding;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@ -24,13 +24,14 @@ import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
@Mapper
public interface FlattenedMapper {
public interface ExpandingMapper {
FlattenedMapper INSTANCE = Mappers.getMapper( FlattenedMapper.class );
ExpandingMapper INSTANCE = Mappers.getMapper( ExpandingMapper.class );
@Mappings({
@Mapping(target = "first.foo", source = "count"),
@Mapping(target = "articleCount", source = "count"),
@Mapping(target = "first.description", source = "article1"),
@Mapping(target = "second", ignore = true)
})
ExpandedTarget writeToNestedProperty(FlattenedSource source);
ImmutableExpandedStock writeToNestedProperty(FlattenedStock source);
}

View File

@ -16,38 +16,38 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.builder.nestedprop;
package org.mapstruct.ap.test.builder.nestedprop.expanding;
import static com.google.common.base.Preconditions.checkNotNull;
public class FlattenedSource {
private String foo;
private String bar;
public class FlattenedStock {
private String article1;
private String article2;
private int count;
public FlattenedSource() {
public FlattenedStock() {
}
public FlattenedSource(String foo, String bar, int count) {
this.foo = checkNotNull( foo );
this.bar = checkNotNull( bar );
public FlattenedStock(String article1, String article2, int count) {
this.article1 = checkNotNull( article1 );
this.article2 = checkNotNull( article2 );
this.count = count;
}
public String getFoo() {
return foo;
public String getArticle1() {
return article1;
}
public void setFoo(String foo) {
this.foo = foo;
public void setArticle1(String article1) {
this.article1 = article1;
}
public String getBar() {
return bar;
public String getArticle2() {
return article2;
}
public void setBar(String bar) {
this.bar = bar;
public void setArticle2(String article2) {
this.article2 = article2;
}
public int getCount() {

View File

@ -16,32 +16,32 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.builder.nestedprop;
package org.mapstruct.ap.test.builder.nestedprop.expanding;
public class ImmutableTargetContainer {
private final String foo;
public class ImmutableArticle {
private final String description;
ImmutableTargetContainer(ImmutableTargetContainer.Builder builder) {
this.foo = builder.foo;
ImmutableArticle(ImmutableArticle.Builder builder) {
this.description = builder.description;
}
public static ImmutableTargetContainer.Builder builder() {
return new ImmutableTargetContainer.Builder();
public static ImmutableArticle.Builder builder() {
return new ImmutableArticle.Builder();
}
public String getFoo() {
return foo;
public String getDescription() {
return description;
}
public static class Builder {
private String foo;
private String description;
public ImmutableTargetContainer build() {
return new ImmutableTargetContainer( this );
public ImmutableArticle build() {
return new ImmutableArticle( this );
}
public ImmutableTargetContainer.Builder foo(String foo) {
this.foo = foo;
public ImmutableArticle.Builder description(String description) {
this.description = description;
return this;
}
}

View File

@ -16,15 +16,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.builder.nestedprop;
package org.mapstruct.ap.test.builder.nestedprop.expanding;
public class ExpandedTarget {
public class ImmutableExpandedStock {
private final int count;
private final ImmutableTargetContainer first;
private final ImmutableTargetContainer second;
private final ImmutableArticle first;
private final ImmutableArticle second;
ExpandedTarget(Builder builder) {
this.count = builder.count;
ImmutableExpandedStock(Builder builder) {
this.count = builder.articleCount;
this.first = builder.first;
this.second = builder.second;
}
@ -37,36 +37,36 @@ public class ExpandedTarget {
return count;
}
public ImmutableTargetContainer getFirst() {
public ImmutableArticle getFirst() {
return first;
}
public ImmutableTargetContainer getSecond() {
public ImmutableArticle getSecond() {
return second;
}
public static class Builder {
private int count;
private ImmutableTargetContainer first;
private ImmutableTargetContainer second;
private int articleCount;
private ImmutableArticle first;
private ImmutableArticle second;
public Builder count(int count) {
this.count = count;
public Builder articleCount(int articleCount) {
this.articleCount = articleCount;
return this;
}
public Builder first(ImmutableTargetContainer first) {
public Builder first(ImmutableArticle first) {
this.first = first;
return this;
}
public Builder second(ImmutableTargetContainer second) {
public Builder second(ImmutableArticle second) {
this.second = second;
return this;
}
public ExpandedTarget build() {
return new ExpandedTarget( this );
public ImmutableExpandedStock build() {
return new ImmutableExpandedStock( this );
}
}
}