Skip to content

Commit 82f7835

Browse files
committed
[#3133] change test to assert against expected Files
1 parent 464cc8b commit 82f7835

File tree

3 files changed

+141
-144
lines changed

3 files changed

+141
-144
lines changed

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

Lines changed: 9 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
*/
66
package org.mapstruct.ap.test.nullcheck.redundant;
77

8+
import org.apache.commons.io.FileUtils;
89
import org.junit.jupiter.api.extension.RegisterExtension;
910
import org.mapstruct.ap.testutil.IssueKey;
1011
import org.mapstruct.ap.testutil.ProcessorTest;
1112
import org.mapstruct.ap.testutil.WithClasses;
1213
import org.mapstruct.ap.testutil.runner.GeneratedSource;
1314

14-
import static java.lang.System.lineSeparator;
15-
1615
@WithClasses({
1716
FooMapper.class,
1817
FooMapperConfigured.class,
@@ -22,159 +21,25 @@
2221
})
2322
@IssueKey("3133")
2423
public class RedundantNullCheckTest {
25-
@RegisterExtension
26-
final GeneratedSource generatedSource = new GeneratedSource();
27-
private final String ls = lineSeparator();
28-
29-
@ProcessorTest
30-
@IssueKey("3133")
31-
void shouldNotCreateRedundantNullCheckWithAdditionalPrimitiveParameters() {
32-
generatedSource.forMapper( FooMapper.class )
33-
.content()
34-
.contains( "@Override" + ls +
35-
" public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz) {" + ls +
36-
" if ( input == null ) {" + ls +
37-
" return;" + ls +
38-
" }" + ls +
39-
ls +
40-
" toUpdate.setBar( input.getBar() );" + ls +
41-
" }" );
42-
}
43-
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-
}
5824

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-
}
25+
private static final String EXPECTED_MAPPER_IMPL = "src/test/resources/fixtures/org/mapstruct/ap/test/nullcheck/ExpectedFooMapper.java";
26+
private static final String EXPECTED_MAPPER_CONFIGURED_IMPL = "src/test/resources/fixtures/org/mapstruct/ap/test/nullcheck/ExpectedFooMapperConfigured.java";
7527

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-
}
28+
@RegisterExtension
29+
final GeneratedSource generatedSource = new GeneratedSource();
11630

11731
@ProcessorTest
11832
@IssueKey("3133")
119-
void shouldNotCreateRedundantNullCheck() {
33+
void generatedMapperShouldNotContainAnyRedundantNullChecks() {
12034
generatedSource.forMapper( FooMapper.class )
121-
.content()
122-
.contains( "@Override" + ls +
123-
" public void updateFoo(FooSource input, FooTarget toUpdate) {" + ls +
124-
" if ( input == null ) {" + ls +
125-
" return;" + ls +
126-
" }" + ls +
127-
ls +
128-
" toUpdate.setBar( input.getBar() );" + ls +
129-
" }" );
35+
.hasSameMapperContent( FileUtils.getFile( EXPECTED_MAPPER_IMPL ) );
13036
}
13137

132-
13338
@ProcessorTest
13439
@IssueKey("3133")
135-
void shouldCreateNullCheckIfNoGuardClauseIsPresentWithAdditionalPrimitiveParameters() {
40+
void generatedMapperConfiguredShouldNotContainAnyRedundantNullChecks() {
13641
generatedSource.forMapper( FooMapperConfigured.class )
137-
.content()
138-
.contains( "@Override" + ls +
139-
" public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz) {" + ls +
140-
ls +
141-
" if ( input != null ) {" + ls +
142-
" toUpdate.setBar( input.getBar() );" + ls +
143-
" }" + ls +
144-
" }" );
145-
}
146-
147-
@ProcessorTest
148-
@IssueKey("3133")
149-
void shouldCreateNullCheckIfNoGuardClauseIsPresent() {
150-
generatedSource.forMapper( FooMapperConfigured.class )
151-
.content()
152-
.contains( "@Override" + ls +
153-
" public void updateFoo(FooSource input, FooTarget toUpdate) {" + ls +
154-
ls +
155-
" if ( input != null ) {" + ls +
156-
" toUpdate.setBar( input.getBar() );" + ls +
157-
" }" + ls +
158-
" }" );
159-
}
160-
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-
" }" );
42+
.hasSameMapperContent( FileUtils.getFile( EXPECTED_MAPPER_CONFIGURED_IMPL ) );
17843
}
17944

18045

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package org.mapstruct.ap.test.nullcheck.redundant;
2+
3+
import javax.annotation.processing.Generated;
4+
5+
@Generated(
6+
value = "org.mapstruct.ap.MappingProcessor",
7+
date = "2024-05-07T18:50:27+0200",
8+
comments = "version: , compiler: javac, environment: Java 21.0.2 (Azul Systems, Inc.)"
9+
)
10+
public class FooMapperImpl implements FooMapper {
11+
12+
@Override
13+
public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz) {
14+
if ( input == null ) {
15+
return;
16+
}
17+
18+
toUpdate.setBar( input.getBar() );
19+
}
20+
21+
@Override
22+
public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz, int bay) {
23+
if ( input == null ) {
24+
return;
25+
}
26+
27+
toUpdate.setBar( input.getBar() );
28+
}
29+
30+
@Override
31+
public void updateFoo(FooSource input, FooTarget toUpdate) {
32+
if ( input == null ) {
33+
return;
34+
}
35+
36+
toUpdate.setBar( input.getBar() );
37+
}
38+
39+
@Override
40+
public FooTarget getUpdatedFooTarget(FooSource input, FooTarget toUpdate) {
41+
if ( input == null ) {
42+
return toUpdate;
43+
}
44+
45+
toUpdate.setBar( input.getBar() );
46+
47+
return toUpdate;
48+
}
49+
50+
@Override
51+
public FooTarget getUpdatedFooTarget(FooSource input, FooTarget toUpdate, boolean baz) {
52+
if ( input == null ) {
53+
return toUpdate;
54+
}
55+
56+
toUpdate.setBar( input.getBar() );
57+
58+
return toUpdate;
59+
}
60+
61+
@Override
62+
public FooTarget map(FooSource source) {
63+
if ( source == null ) {
64+
return null;
65+
}
66+
67+
FooTarget fooTarget = new FooTarget();
68+
69+
fooTarget.setBar( source.getBar() );
70+
71+
return fooTarget;
72+
}
73+
74+
@Override
75+
public FooTarget map(FooSourceNested source) {
76+
if ( source == null ) {
77+
return null;
78+
}
79+
80+
FooTarget fooTarget = new FooTarget();
81+
82+
fooTarget.setBar( source.getBar() );
83+
84+
return fooTarget;
85+
}
86+
87+
@Override
88+
public FooTarget map(FooSourceNested input, FooTarget toUpdate, boolean baz) {
89+
if ( input == null ) {
90+
return toUpdate;
91+
}
92+
93+
toUpdate.setBar( inputNestedBar( input ) );
94+
95+
return toUpdate;
96+
}
97+
98+
private String inputNestedBar(FooSourceNested fooSourceNested) {
99+
FooSource nested = fooSourceNested.getNested();
100+
if ( nested == null ) {
101+
return null;
102+
}
103+
return nested.getBar();
104+
}
105+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.mapstruct.ap.test.nullcheck.redundant;
2+
3+
import javax.annotation.processing.Generated;
4+
5+
@Generated(
6+
value = "org.mapstruct.ap.MappingProcessor",
7+
date = "2024-05-07T18:50:27+0200",
8+
comments = "version: , compiler: javac, environment: Java 21.0.2 (Azul Systems, Inc.)"
9+
)
10+
public class FooMapperConfiguredImpl implements FooMapperConfigured {
11+
12+
@Override
13+
public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz) {
14+
15+
if ( input != null ) {
16+
toUpdate.setBar( input.getBar() );
17+
}
18+
}
19+
20+
@Override
21+
public void updateFoo(FooSource input, FooTarget toUpdate) {
22+
23+
if ( input != null ) {
24+
toUpdate.setBar( input.getBar() );
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)