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;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
@Mapper(unmappedTargetPolicy = ReportingPolicy.ERROR)
|
||||
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.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
|
||||
@Mapper
|
||||
public interface SimpleBuilderMapper {
|
||||
|
||||
@Mapping(target = "name", source = "fullName")
|
||||
SimpleImmutableTarget toImmutable(SimpleMutableSource source);
|
||||
@Mappings({
|
||||
@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;
|
||||
|
||||
@WithClasses({
|
||||
SimpleMutableSource.class,
|
||||
SimpleImmutableTarget.class
|
||||
SimpleMutablePerson.class,
|
||||
SimpleImmutablePerson.class
|
||||
})
|
||||
@RunWith(AnnotationProcessorTestRunner.class)
|
||||
public class SimpleImmutableBuilderTest {
|
||||
@ -45,14 +45,16 @@ public class SimpleImmutableBuilderTest {
|
||||
@WithClasses({ SimpleBuilderMapper.class })
|
||||
public void testSimpleImmutableBuilderHappyPath() {
|
||||
SimpleBuilderMapper mapper = Mappers.getMapper( SimpleBuilderMapper.class );
|
||||
SimpleMutableSource source = new SimpleMutableSource();
|
||||
SimpleMutablePerson source = new SimpleMutablePerson();
|
||||
source.setAge( 3 );
|
||||
source.setFullName( "Bob" );
|
||||
|
||||
SimpleImmutableTarget targetObject = mapper.toImmutable( source );
|
||||
SimpleImmutablePerson targetObject = mapper.toImmutable( source );
|
||||
|
||||
assertThat( targetObject.getAge() ).isEqualTo( 3 );
|
||||
assertThat( targetObject.getName() ).isEqualTo( "Bob" );
|
||||
assertThat( targetObject.getJob() ).isEqualTo( "programmer" );
|
||||
assertThat( targetObject.getCity() ).isEqualTo( "Bengalore" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -61,7 +63,7 @@ public class SimpleImmutableBuilderTest {
|
||||
diagnostics = @Diagnostic(
|
||||
kind = javax.tools.Diagnostic.Kind.ERROR,
|
||||
type = ErroneousSimpleBuilderMapper.class,
|
||||
line = 27,
|
||||
line = 33,
|
||||
messageRegExp = "Unmapped target property: \"name\"\\."))
|
||||
public void testSimpleImmutableBuilderMissingPropertyFailsToCompile() {
|
||||
}
|
||||
|
@ -18,13 +18,17 @@
|
||||
*/
|
||||
package org.mapstruct.ap.test.builder.simple;
|
||||
|
||||
public class SimpleImmutableTarget {
|
||||
public class SimpleImmutablePerson {
|
||||
private final String name;
|
||||
private final int age;
|
||||
private final String job;
|
||||
private final String city;
|
||||
|
||||
SimpleImmutableTarget(Builder builder) {
|
||||
SimpleImmutablePerson(Builder builder) {
|
||||
this.name = builder.name;
|
||||
this.age = builder.age;
|
||||
this.job = builder.job;
|
||||
this.city = builder.city;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
@ -39,22 +43,42 @@ public class SimpleImmutableTarget {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getJob() {
|
||||
return job;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private String name;
|
||||
private int age;
|
||||
private String job;
|
||||
private String city;
|
||||
|
||||
public Builder age(int age) {
|
||||
this.age = age;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleImmutableTarget build() {
|
||||
return new SimpleImmutableTarget( this );
|
||||
public SimpleImmutablePerson build() {
|
||||
return new SimpleImmutablePerson( this );
|
||||
}
|
||||
|
||||
public Builder name(String name) {
|
||||
this.name = name;
|
||||
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;
|
||||
|
||||
public class SimpleMutableSource {
|
||||
public class SimpleMutablePerson {
|
||||
private String fullName;
|
||||
private int age;
|
||||
|
Loading…
x
Reference in New Issue
Block a user