Skip to content

Commit bf8f037

Browse files
committed
mapstruct#782 Add integration tests for builders with immutables
1 parent 3d45d07 commit bf8f037

9 files changed

Lines changed: 317 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.itest.tests;
20+
21+
import org.junit.runner.RunWith;
22+
import org.mapstruct.itest.testutil.runner.ProcessorSuite;
23+
import org.mapstruct.itest.testutil.runner.ProcessorSuiteRunner;
24+
25+
/**
26+
* @author Filip Hrisafov
27+
*/
28+
@RunWith( ProcessorSuiteRunner.class )
29+
@ProcessorSuite( baseDir = "immutablesBuilderTest" )
30+
public class ImmutablesBuilderTest {
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
5+
and/or other contributors as indicated by the @authors tag. See the
6+
copyright.txt file in the distribution for a full listing of all
7+
contributors.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
21+
-->
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<parent>
26+
<groupId>org.mapstruct</groupId>
27+
<artifactId>mapstruct-it-parent</artifactId>
28+
<version>1.0.0</version>
29+
<relativePath>../pom.xml</relativePath>
30+
</parent>
31+
32+
<artifactId>immutablesIntegrationTest</artifactId>
33+
<packaging>jar</packaging>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.immutables</groupId>
38+
<artifactId>value</artifactId>
39+
<scope>provided</scope>
40+
</dependency>
41+
</dependencies>
42+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.itest.immutables;
20+
21+
import org.immutables.value.Value;
22+
23+
@Value.Immutable
24+
public interface Address {
25+
String getAddressLine();
26+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.itest.immutables;
20+
21+
public class AddressDto {
22+
23+
private String addressLine;
24+
25+
public AddressDto() {
26+
27+
}
28+
29+
public AddressDto(String addressLine) {
30+
this.addressLine = addressLine;
31+
}
32+
33+
public String getAddressLine() {
34+
return addressLine;
35+
}
36+
37+
public void setAddressLine(String addressLine) {
38+
this.addressLine = addressLine;
39+
}
40+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.itest.immutables;
20+
21+
import org.immutables.value.Value;
22+
23+
@Value.Immutable
24+
public interface Person {
25+
String getName();
26+
int getAge();
27+
Address getAddress();
28+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.itest.immutables;
20+
21+
public class PersonDto {
22+
private String name;
23+
private int age;
24+
private AddressDto address;
25+
26+
public PersonDto() {
27+
}
28+
29+
public PersonDto(String name, int age, AddressDto address) {
30+
this.name = name;
31+
this.age = age;
32+
this.address = address;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public int getAge() {
44+
return age;
45+
}
46+
47+
public void setAge(int age) {
48+
this.age = age;
49+
}
50+
51+
public AddressDto getAddress() {
52+
return address;
53+
}
54+
55+
public void setAddress(AddressDto address) {
56+
this.address = address;
57+
}
58+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.itest.immutables;
20+
21+
import org.mapstruct.Mapper;
22+
import org.mapstruct.ReportingPolicy;
23+
import org.mapstruct.factory.Mappers;
24+
25+
@Mapper(unmappedTargetPolicy = ReportingPolicy.ERROR)
26+
public interface PersonMapper {
27+
28+
PersonMapper INSTANCE = Mappers.getMapper( PersonMapper.class );
29+
30+
Person fromDto(PersonDto personDto);
31+
PersonDto toDto(Person personDto);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.itest.immutables;
20+
21+
import org.junit.Test;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Test for generation of Lombok Builder Mapper implementations
27+
*
28+
* @author Eric Martineau
29+
*/
30+
public class ImmutablesMapperTest {
31+
32+
@Test
33+
public void testSimpleImmutableBuilderHappyPath() {
34+
PersonDto personDto = PersonMapper.INSTANCE.toDto( ImmutablePerson.builder()
35+
.age( 33 )
36+
.name( "Bob" )
37+
.address( ImmutableAddress.builder()
38+
.addressLine( "Wild Drive" )
39+
.build() )
40+
.build() );
41+
assertThat( personDto.getAge() ).isEqualTo( 33 );
42+
assertThat( personDto.getName() ).isEqualTo( "Bob" );
43+
assertThat( personDto.getAddress() ).isNotNull();
44+
assertThat( personDto.getAddress().getAddressLine() ).isEqualTo( "Wild Drive" );
45+
}
46+
47+
@Test
48+
public void testLombokToImmutable() {
49+
Person person = PersonMapper.INSTANCE.fromDto( new PersonDto( "Bob", 33, new AddressDto( "Wild Drive" ) ) );
50+
assertThat( person.getAge() ).isEqualTo( 33 );
51+
assertThat( person.getName() ).isEqualTo( "Bob" );
52+
assertThat( person.getAddress() ).isNotNull();
53+
assertThat( person.getAddress().getAddressLine() ).isEqualTo( "Wild Drive" );
54+
}
55+
}

parent/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@
199199
<artifactId>lombok</artifactId>
200200
<version>1.16.18</version>
201201
</dependency>
202+
<dependency>
203+
<groupId>org.immutables</groupId>
204+
<artifactId>value</artifactId>
205+
<version>2.5.6</version>
206+
</dependency>
202207

203208
<!-- Joda-Time -->
204209
<dependency>

0 commit comments

Comments
 (0)