mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#782 Add tests with expressions and constants
This commit is contained in:
parent
6291631af7
commit
998d6fc35f
@ -19,10 +19,16 @@
|
|||||||
package org.mapstruct.ap.test.builder.simple;
|
package org.mapstruct.ap.test.builder.simple;
|
||||||
|
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.Mappings;
|
||||||
import org.mapstruct.ReportingPolicy;
|
import org.mapstruct.ReportingPolicy;
|
||||||
|
|
||||||
@Mapper(unmappedTargetPolicy = ReportingPolicy.ERROR)
|
@Mapper(unmappedTargetPolicy = ReportingPolicy.ERROR)
|
||||||
public interface ErroneousSimpleBuilderMapper {
|
public interface ErroneousSimpleBuilderMapper {
|
||||||
|
|
||||||
SimpleImmutableTarget toImmutable(SimpleMutableSource source);
|
@Mappings({
|
||||||
|
@Mapping(target = "job", ignore = true ),
|
||||||
|
@Mapping(target = "city", ignore = true )
|
||||||
|
})
|
||||||
|
SimpleImmutablePerson toImmutable(SimpleMutablePerson source);
|
||||||
}
|
}
|
||||||
|
@ -20,10 +20,15 @@ package org.mapstruct.ap.test.builder.simple;
|
|||||||
|
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
import org.mapstruct.Mapping;
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.Mappings;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface SimpleBuilderMapper {
|
public interface SimpleBuilderMapper {
|
||||||
|
|
||||||
@Mapping(target = "name", source = "fullName")
|
@Mappings({
|
||||||
SimpleImmutableTarget toImmutable(SimpleMutableSource source);
|
@Mapping(target = "name", source = "fullName"),
|
||||||
|
@Mapping(target = "job", constant = "programmer"),
|
||||||
|
@Mapping(target = "city", expression = "java(\"Bengalore\")")
|
||||||
|
})
|
||||||
|
SimpleImmutablePerson toImmutable(SimpleMutablePerson source);
|
||||||
}
|
}
|
||||||
|
@ -32,8 +32,8 @@ import org.mapstruct.factory.Mappers;
|
|||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@WithClasses({
|
@WithClasses({
|
||||||
SimpleMutableSource.class,
|
SimpleMutablePerson.class,
|
||||||
SimpleImmutableTarget.class
|
SimpleImmutablePerson.class
|
||||||
})
|
})
|
||||||
@RunWith(AnnotationProcessorTestRunner.class)
|
@RunWith(AnnotationProcessorTestRunner.class)
|
||||||
public class SimpleImmutableBuilderTest {
|
public class SimpleImmutableBuilderTest {
|
||||||
@ -45,14 +45,16 @@ public class SimpleImmutableBuilderTest {
|
|||||||
@WithClasses({ SimpleBuilderMapper.class })
|
@WithClasses({ SimpleBuilderMapper.class })
|
||||||
public void testSimpleImmutableBuilderHappyPath() {
|
public void testSimpleImmutableBuilderHappyPath() {
|
||||||
SimpleBuilderMapper mapper = Mappers.getMapper( SimpleBuilderMapper.class );
|
SimpleBuilderMapper mapper = Mappers.getMapper( SimpleBuilderMapper.class );
|
||||||
SimpleMutableSource source = new SimpleMutableSource();
|
SimpleMutablePerson source = new SimpleMutablePerson();
|
||||||
source.setAge( 3 );
|
source.setAge( 3 );
|
||||||
source.setFullName( "Bob" );
|
source.setFullName( "Bob" );
|
||||||
|
|
||||||
SimpleImmutableTarget targetObject = mapper.toImmutable( source );
|
SimpleImmutablePerson targetObject = mapper.toImmutable( source );
|
||||||
|
|
||||||
assertThat( targetObject.getAge() ).isEqualTo( 3 );
|
assertThat( targetObject.getAge() ).isEqualTo( 3 );
|
||||||
assertThat( targetObject.getName() ).isEqualTo( "Bob" );
|
assertThat( targetObject.getName() ).isEqualTo( "Bob" );
|
||||||
|
assertThat( targetObject.getJob() ).isEqualTo( "programmer" );
|
||||||
|
assertThat( targetObject.getCity() ).isEqualTo( "Bengalore" );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -61,7 +63,7 @@ public class SimpleImmutableBuilderTest {
|
|||||||
diagnostics = @Diagnostic(
|
diagnostics = @Diagnostic(
|
||||||
kind = javax.tools.Diagnostic.Kind.ERROR,
|
kind = javax.tools.Diagnostic.Kind.ERROR,
|
||||||
type = ErroneousSimpleBuilderMapper.class,
|
type = ErroneousSimpleBuilderMapper.class,
|
||||||
line = 27,
|
line = 33,
|
||||||
messageRegExp = "Unmapped target property: \"name\"\\."))
|
messageRegExp = "Unmapped target property: \"name\"\\."))
|
||||||
public void testSimpleImmutableBuilderMissingPropertyFailsToCompile() {
|
public void testSimpleImmutableBuilderMissingPropertyFailsToCompile() {
|
||||||
}
|
}
|
||||||
|
@ -18,13 +18,17 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.builder.simple;
|
package org.mapstruct.ap.test.builder.simple;
|
||||||
|
|
||||||
public class SimpleImmutableTarget {
|
public class SimpleImmutablePerson {
|
||||||
private final String name;
|
private final String name;
|
||||||
private final int age;
|
private final int age;
|
||||||
|
private final String job;
|
||||||
|
private final String city;
|
||||||
|
|
||||||
SimpleImmutableTarget(Builder builder) {
|
SimpleImmutablePerson(Builder builder) {
|
||||||
this.name = builder.name;
|
this.name = builder.name;
|
||||||
this.age = builder.age;
|
this.age = builder.age;
|
||||||
|
this.job = builder.job;
|
||||||
|
this.city = builder.city;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Builder builder() {
|
public static Builder builder() {
|
||||||
@ -39,22 +43,42 @@ public class SimpleImmutableTarget {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getJob() {
|
||||||
|
return job;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCity() {
|
||||||
|
return city;
|
||||||
|
}
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
private String name;
|
private String name;
|
||||||
private int age;
|
private int age;
|
||||||
|
private String job;
|
||||||
|
private String city;
|
||||||
|
|
||||||
public Builder age(int age) {
|
public Builder age(int age) {
|
||||||
this.age = age;
|
this.age = age;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleImmutableTarget build() {
|
public SimpleImmutablePerson build() {
|
||||||
return new SimpleImmutableTarget( this );
|
return new SimpleImmutablePerson( this );
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder name(String name) {
|
public Builder name(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Builder job(String job) {
|
||||||
|
this.job = job;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder city(String city) {
|
||||||
|
this.city = city;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -18,7 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.builder.simple;
|
package org.mapstruct.ap.test.builder.simple;
|
||||||
|
|
||||||
public class SimpleMutableSource {
|
public class SimpleMutablePerson {
|
||||||
private String fullName;
|
private String fullName;
|
||||||
private int age;
|
private int age;
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user