From 0e0fd313e560dd17097f730f7d759d95d84eb1ee Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Fri, 12 Oct 2018 23:57:24 +0200 Subject: [PATCH] #1594 Add test case to show that it has been fixed --- .../ap/test/bugs/_1594/Issue1594Mapper.java | 84 +++++++++++++++++++ .../ap/test/bugs/_1594/Issue1594Test.java | 41 +++++++++ 2 files changed, 125 insertions(+) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1594/Issue1594Mapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1594/Issue1594Test.java diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1594/Issue1594Mapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1594/Issue1594Mapper.java new file mode 100644 index 000000000..fbb99ed52 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1594/Issue1594Mapper.java @@ -0,0 +1,84 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.ap.test.bugs._1594; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; +import org.mapstruct.factory.Mappers; + +/** + * @author Filip Hrisafov + */ +@Mapper +public interface Issue1594Mapper { + + Issue1594Mapper INSTANCE = Mappers.getMapper( Issue1594Mapper.class ); + + @Mappings({ + @Mapping(target = "address.country.oid", expression = "java(dto.getFullAddress().split( \"-\" )[0])"), + @Mapping(target = "address.city.oid", expression = "java(dto.getFullAddress().split( \"-\" )[1])"), + }) + Client toClient(Dto dto); + + class Client { + private Address address; + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + } + + class Address { + + private Id country; + private Id city; + + public Id getCountry() { + return country; + } + + public void setCountry(Id country) { + this.country = country; + } + + public Id getCity() { + return city; + } + + public void setCity(Id city) { + this.city = city; + } + } + + class Id { + private String oid; + + public String getOid() { + return oid; + } + + public void setOid(String oid) { + this.oid = oid; + } + } + + class Dto { + private String fullAddress; + + public String getFullAddress() { + return fullAddress; + } + + public void setFullAddress(String fullAddress) { + this.fullAddress = fullAddress; + } + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1594/Issue1594Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1594/Issue1594Test.java new file mode 100644 index 000000000..b498884b2 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1594/Issue1594Test.java @@ -0,0 +1,41 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.ap.test.bugs._1594; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.WithClasses; +import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Filip Hrisafov + */ +@RunWith(AnnotationProcessorTestRunner.class) +@IssueKey("1594") +@WithClasses({ + Issue1594Mapper.class +}) +public class Issue1594Test { + + @Test + public void shouldGenerateCorrectMapping() { + Issue1594Mapper.Dto dto = new Issue1594Mapper.Dto(); + dto.setFullAddress( "Switzerland-Zurich" ); + + Issue1594Mapper.Client client = Issue1594Mapper.INSTANCE.toClient( dto ); + + assertThat( client ).isNotNull(); + assertThat( client.getAddress() ).isNotNull(); + assertThat( client.getAddress().getCountry() ).isNotNull(); + assertThat( client.getAddress().getCountry().getOid() ).isEqualTo( "Switzerland" ); + assertThat( client.getAddress().getCity() ).isNotNull(); + assertThat( client.getAddress().getCity().getOid() ).isEqualTo( "Zurich" ); + + } +}