mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#782 Add tests with nested expanding target
This commit is contained in:
parent
998d6fc35f
commit
045532fa68
@ -16,7 +16,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* 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.Test;
|
||||||
import org.junit.runner.RunWith;
|
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.
|
* Verifies that nested property mapping works with an immutable intermediate type.
|
||||||
*/
|
*/
|
||||||
@WithClasses({
|
@WithClasses({
|
||||||
FlattenedSource.class,
|
FlattenedStock.class,
|
||||||
ExpandedTarget.class,
|
ImmutableExpandedStock.class,
|
||||||
ImmutableTargetContainer.class,
|
ImmutableArticle.class,
|
||||||
FlattenedMapper.class
|
ExpandingMapper.class
|
||||||
})
|
})
|
||||||
@RunWith(AnnotationProcessorTestRunner.class)
|
@RunWith(AnnotationProcessorTestRunner.class)
|
||||||
public class BuilderNestedPropertyTest {
|
public class BuilderNestedPropertyTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNestedImmutablePropertyMapper() {
|
public void testNestedImmutablePropertyMapper() {
|
||||||
ExpandedTarget expandedTarget = FlattenedMapper.INSTANCE.writeToNestedProperty( new FlattenedSource(
|
FlattenedStock stock = new FlattenedStock( "Sock", "Tie", 33 );
|
||||||
"Foo",
|
ImmutableExpandedStock expandedTarget = ExpandingMapper.INSTANCE.writeToNestedProperty( stock );
|
||||||
"Bar",
|
|
||||||
33
|
|
||||||
) );
|
|
||||||
assertThat( expandedTarget ).isNotNull();
|
assertThat( expandedTarget ).isNotNull();
|
||||||
assertThat( expandedTarget.getCount() ).isEqualTo( 33 );
|
assertThat( expandedTarget.getCount() ).isEqualTo( 33 );
|
||||||
assertThat( expandedTarget.getSecond() ).isNull();
|
assertThat( expandedTarget.getSecond() ).isNull();
|
||||||
assertThat( expandedTarget.getFirst() ).isNotNull();
|
assertThat( expandedTarget.getFirst() ).isNotNull();
|
||||||
assertThat( expandedTarget.getFirst().getFoo() ).isEqualTo( "33" );
|
assertThat( expandedTarget.getFirst().getDescription() ).isEqualTo( "Sock" );
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,7 +16,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* 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.Mapper;
|
||||||
import org.mapstruct.Mapping;
|
import org.mapstruct.Mapping;
|
||||||
@ -24,13 +24,14 @@ import org.mapstruct.Mappings;
|
|||||||
import org.mapstruct.factory.Mappers;
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface FlattenedMapper {
|
public interface ExpandingMapper {
|
||||||
|
|
||||||
FlattenedMapper INSTANCE = Mappers.getMapper( FlattenedMapper.class );
|
ExpandingMapper INSTANCE = Mappers.getMapper( ExpandingMapper.class );
|
||||||
|
|
||||||
@Mappings({
|
@Mappings({
|
||||||
@Mapping(target = "first.foo", source = "count"),
|
@Mapping(target = "articleCount", source = "count"),
|
||||||
|
@Mapping(target = "first.description", source = "article1"),
|
||||||
@Mapping(target = "second", ignore = true)
|
@Mapping(target = "second", ignore = true)
|
||||||
})
|
})
|
||||||
ExpandedTarget writeToNestedProperty(FlattenedSource source);
|
ImmutableExpandedStock writeToNestedProperty(FlattenedStock source);
|
||||||
}
|
}
|
@ -16,38 +16,38 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* 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;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
public class FlattenedSource {
|
public class FlattenedStock {
|
||||||
private String foo;
|
private String article1;
|
||||||
private String bar;
|
private String article2;
|
||||||
private int count;
|
private int count;
|
||||||
|
|
||||||
public FlattenedSource() {
|
public FlattenedStock() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public FlattenedSource(String foo, String bar, int count) {
|
public FlattenedStock(String article1, String article2, int count) {
|
||||||
this.foo = checkNotNull( foo );
|
this.article1 = checkNotNull( article1 );
|
||||||
this.bar = checkNotNull( bar );
|
this.article2 = checkNotNull( article2 );
|
||||||
this.count = count;
|
this.count = count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFoo() {
|
public String getArticle1() {
|
||||||
return foo;
|
return article1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFoo(String foo) {
|
public void setArticle1(String article1) {
|
||||||
this.foo = foo;
|
this.article1 = article1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBar() {
|
public String getArticle2() {
|
||||||
return bar;
|
return article2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBar(String bar) {
|
public void setArticle2(String article2) {
|
||||||
this.bar = bar;
|
this.article2 = article2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCount() {
|
public int getCount() {
|
@ -16,32 +16,32 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.builder.nestedprop;
|
package org.mapstruct.ap.test.builder.nestedprop.expanding;
|
||||||
|
|
||||||
public class ImmutableTargetContainer {
|
public class ImmutableArticle {
|
||||||
private final String foo;
|
private final String description;
|
||||||
|
|
||||||
ImmutableTargetContainer(ImmutableTargetContainer.Builder builder) {
|
ImmutableArticle(ImmutableArticle.Builder builder) {
|
||||||
this.foo = builder.foo;
|
this.description = builder.description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ImmutableTargetContainer.Builder builder() {
|
public static ImmutableArticle.Builder builder() {
|
||||||
return new ImmutableTargetContainer.Builder();
|
return new ImmutableArticle.Builder();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFoo() {
|
public String getDescription() {
|
||||||
return foo;
|
return description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
private String foo;
|
private String description;
|
||||||
|
|
||||||
public ImmutableTargetContainer build() {
|
public ImmutableArticle build() {
|
||||||
return new ImmutableTargetContainer( this );
|
return new ImmutableArticle( this );
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImmutableTargetContainer.Builder foo(String foo) {
|
public ImmutableArticle.Builder description(String description) {
|
||||||
this.foo = foo;
|
this.description = description;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,15 +16,15 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* 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 int count;
|
||||||
private final ImmutableTargetContainer first;
|
private final ImmutableArticle first;
|
||||||
private final ImmutableTargetContainer second;
|
private final ImmutableArticle second;
|
||||||
|
|
||||||
ExpandedTarget(Builder builder) {
|
ImmutableExpandedStock(Builder builder) {
|
||||||
this.count = builder.count;
|
this.count = builder.articleCount;
|
||||||
this.first = builder.first;
|
this.first = builder.first;
|
||||||
this.second = builder.second;
|
this.second = builder.second;
|
||||||
}
|
}
|
||||||
@ -37,36 +37,36 @@ public class ExpandedTarget {
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImmutableTargetContainer getFirst() {
|
public ImmutableArticle getFirst() {
|
||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImmutableTargetContainer getSecond() {
|
public ImmutableArticle getSecond() {
|
||||||
return second;
|
return second;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
private int count;
|
private int articleCount;
|
||||||
private ImmutableTargetContainer first;
|
private ImmutableArticle first;
|
||||||
private ImmutableTargetContainer second;
|
private ImmutableArticle second;
|
||||||
|
|
||||||
public Builder count(int count) {
|
public Builder articleCount(int articleCount) {
|
||||||
this.count = count;
|
this.articleCount = articleCount;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder first(ImmutableTargetContainer first) {
|
public Builder first(ImmutableArticle first) {
|
||||||
this.first = first;
|
this.first = first;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder second(ImmutableTargetContainer second) {
|
public Builder second(ImmutableArticle second) {
|
||||||
this.second = second;
|
this.second = second;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExpandedTarget build() {
|
public ImmutableExpandedStock build() {
|
||||||
return new ExpandedTarget( this );
|
return new ImmutableExpandedStock( this );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user