Skip to content

Commit 0b95442

Browse files
committed
Adds more verifying test
1 parent 7016d48 commit 0b95442

3 files changed

Lines changed: 133 additions & 5 deletions

File tree

processor/src/test/java/org/mapstruct/ap/test/nullcheck/redundant/FooMapper.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.mapstruct.ap.test.nullcheck.redundant;
77

88
import org.mapstruct.Mapper;
9+
import org.mapstruct.Mapping;
910
import org.mapstruct.MappingTarget;
1011
import org.mapstruct.factory.Mappers;
1112

@@ -16,5 +17,18 @@ public interface FooMapper {
1617

1718
void updateFoo(FooSource input, @MappingTarget FooTarget toUpdate, boolean baz);
1819

20+
void updateFoo(FooSource input, @MappingTarget FooTarget toUpdate, boolean baz, int bay);
21+
1922
void updateFoo(FooSource input, @MappingTarget FooTarget toUpdate);
23+
24+
FooTarget getUpdatedFooTarget(FooSource input, @MappingTarget FooTarget toUpdate);
25+
26+
FooTarget getUpdatedFooTarget(FooSource input, @MappingTarget FooTarget toUpdate, boolean baz);
27+
28+
FooTarget map(FooSource source);
29+
30+
FooTarget map(FooSourceNested source);
31+
32+
@Mapping(source = "input.nested.bar", target = "bar")
33+
FooTarget map(FooSourceNested input, @MappingTarget FooTarget toUpdate, boolean baz);
2034
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.mapstruct.ap.test.nullcheck.redundant;
2+
3+
public class FooSourceNested {
4+
private String bar;
5+
private FooSource nested;
6+
7+
public FooSource getNested() {
8+
return nested;
9+
}
10+
11+
public void setNested(FooSource nested) {
12+
this.nested = nested;
13+
}
14+
15+
public String getBar() {
16+
return bar;
17+
}
18+
19+
public void setBar(String bar) {
20+
this.bar = bar;
21+
}
22+
}

processor/src/test/java/org/mapstruct/ap/test/nullcheck/redundant/RedundantNullCheckTest.java

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
FooMapper.class,
1818
FooMapperConfigured.class,
1919
FooSource.class,
20-
FooTarget.class
20+
FooTarget.class,
21+
FooSourceNested.class
2122
})
2223
@IssueKey("3133")
2324
public class RedundantNullCheckTest {
2425
@RegisterExtension
2526
final GeneratedSource generatedSource = new GeneratedSource();
27+
private final String ls = lineSeparator();
2628

2729
@ProcessorTest
2830
@IssueKey("3133")
2931
void shouldNotCreateRedundantNullCheckWithAdditionalPrimitiveParameters() {
30-
String ls = lineSeparator();
3132
generatedSource.forMapper( FooMapper.class )
3233
.content()
3334
.contains( "@Override" + ls +
@@ -40,10 +41,82 @@ void shouldNotCreateRedundantNullCheckWithAdditionalPrimitiveParameters() {
4041
" }" );
4142
}
4243

44+
@ProcessorTest
45+
@IssueKey("3133")
46+
void shouldNotCreateRedundantNullCheckWithMultipleAdditionalPrimitiveParameters() {
47+
generatedSource.forMapper( FooMapper.class )
48+
.content()
49+
.contains( "@Override" + ls +
50+
" public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz, int bay) {" + ls +
51+
" if ( input == null ) {" + ls +
52+
" return;" + ls +
53+
" }" + ls +
54+
ls +
55+
" toUpdate.setBar( input.getBar() );" + ls +
56+
" }" );
57+
}
58+
59+
@ProcessorTest
60+
@IssueKey("3133")
61+
void shouldNotCreateRedundantNullCheckWithReturnValue() {
62+
generatedSource.forMapper( FooMapper.class )
63+
.content()
64+
.contains( "@Override" + ls +
65+
" public FooTarget getUpdatedFooTarget(FooSource input, FooTarget toUpdate) {" + ls +
66+
" if ( input == null ) {" + ls +
67+
" return toUpdate;" + ls +
68+
" }" + ls +
69+
ls +
70+
" toUpdate.setBar( input.getBar() );" + ls +
71+
ls +
72+
" return toUpdate;" + ls +
73+
" }" );
74+
}
75+
76+
@ProcessorTest
77+
@IssueKey("3133")
78+
void shouldNotCreateRedundantNullCheckWithReturnValueWithAdditionalPrimitiveParameters() {
79+
generatedSource.forMapper( FooMapper.class )
80+
.content()
81+
.contains( "@Override" + ls +
82+
" public FooTarget getUpdatedFooTarget(FooSource input, FooTarget toUpdate, boolean baz) {" + ls +
83+
" if ( input == null ) {" + ls +
84+
" return toUpdate;" + ls +
85+
" }" + ls +
86+
ls +
87+
" toUpdate.setBar( input.getBar() );" + ls +
88+
ls +
89+
" return toUpdate;" + ls +
90+
" }" );
91+
}
92+
93+
@ProcessorTest
94+
@IssueKey("3133")
95+
void shouldNotCreateRedundantNullCheckForNestedSourceWithReturnValueWithAdditionalPrimitiveParameters() {
96+
generatedSource.forMapper( FooMapper.class )
97+
.content()
98+
.containsIgnoringWhitespaces( "@Override" + ls +
99+
" public FooTarget map(FooSourceNested input, FooTarget toUpdate, boolean baz) {" + ls +
100+
" if ( input == null ) {" + ls +
101+
" return toUpdate;" + ls +
102+
" }" + ls +
103+
ls +
104+
" toUpdate.setBar( inputNestedBar( input ) );" + ls +
105+
ls +
106+
" return toUpdate;" + ls +
107+
" }" + ls +
108+
" private String inputNestedBar(FooSourceNested fooSourceNested) {" + ls +
109+
" FooSource nested = fooSourceNested.getNested();" + ls +
110+
" if ( nested == null ) {" + ls +
111+
" return null;" + ls +
112+
" }" + ls +
113+
" return nested.getBar();" + ls +
114+
" }" );
115+
}
116+
43117
@ProcessorTest
44118
@IssueKey("3133")
45119
void shouldNotCreateRedundantNullCheck() {
46-
String ls = lineSeparator();
47120
generatedSource.forMapper( FooMapper.class )
48121
.content()
49122
.contains( "@Override" + ls +
@@ -56,10 +129,10 @@ void shouldNotCreateRedundantNullCheck() {
56129
" }" );
57130
}
58131

132+
59133
@ProcessorTest
60134
@IssueKey("3133")
61135
void shouldCreateNullCheckIfNoGuardClauseIsPresentWithAdditionalPrimitiveParameters() {
62-
String ls = lineSeparator();
63136
generatedSource.forMapper( FooMapperConfigured.class )
64137
.content()
65138
.contains( "@Override" + ls +
@@ -74,7 +147,6 @@ void shouldCreateNullCheckIfNoGuardClauseIsPresentWithAdditionalPrimitiveParamet
74147
@ProcessorTest
75148
@IssueKey("3133")
76149
void shouldCreateNullCheckIfNoGuardClauseIsPresent() {
77-
String ls = lineSeparator();
78150
generatedSource.forMapper( FooMapperConfigured.class )
79151
.content()
80152
.contains( "@Override" + ls +
@@ -86,4 +158,24 @@ void shouldCreateNullCheckIfNoGuardClauseIsPresent() {
86158
" }" );
87159
}
88160

161+
@ProcessorTest
162+
@IssueKey("3133")
163+
void shouldCreateNullCheckIfNoGuardClauseIsPresentForNestedTargetClass() {
164+
generatedSource.forMapper( FooMapper.class )
165+
.content()
166+
.contains( "@Override" + ls +
167+
" public FooTarget map(FooSourceNested source) {" + ls +
168+
" if ( source == null ) {" + ls +
169+
" return null;" + ls +
170+
" }" + ls +
171+
ls +
172+
" FooTarget fooTarget = new FooTarget();" + ls +
173+
ls +
174+
" fooTarget.setBar( source.getBar() );" + ls +
175+
ls +
176+
" return fooTarget;" + ls +
177+
" }" );
178+
}
179+
180+
89181
}

0 commit comments

Comments
 (0)