|
5 | 5 | */ |
6 | 6 | package org.mapstruct.ap.test.nullcheck.redundant; |
7 | 7 |
|
| 8 | +import org.apache.commons.io.FileUtils; |
8 | 9 | import org.junit.jupiter.api.extension.RegisterExtension; |
9 | 10 | import org.mapstruct.ap.testutil.IssueKey; |
10 | 11 | import org.mapstruct.ap.testutil.ProcessorTest; |
11 | 12 | import org.mapstruct.ap.testutil.WithClasses; |
12 | 13 | import org.mapstruct.ap.testutil.runner.GeneratedSource; |
13 | 14 |
|
14 | | -import static java.lang.System.lineSeparator; |
15 | | - |
16 | 15 | @WithClasses({ |
17 | 16 | FooMapper.class, |
18 | 17 | FooMapperConfigured.class, |
|
22 | 21 | }) |
23 | 22 | @IssueKey("3133") |
24 | 23 | 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 | | - } |
58 | 24 |
|
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"; |
75 | 27 |
|
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(); |
116 | 30 |
|
117 | 31 | @ProcessorTest |
118 | 32 | @IssueKey("3133") |
119 | | - void shouldNotCreateRedundantNullCheck() { |
| 33 | + void generatedMapperShouldNotContainAnyRedundantNullChecks() { |
120 | 34 | 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 ) ); |
130 | 36 | } |
131 | 37 |
|
132 | | - |
133 | 38 | @ProcessorTest |
134 | 39 | @IssueKey("3133") |
135 | | - void shouldCreateNullCheckIfNoGuardClauseIsPresentWithAdditionalPrimitiveParameters() { |
| 40 | + void generatedMapperConfiguredShouldNotContainAnyRedundantNullChecks() { |
136 | 41 | 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 ) ); |
178 | 43 | } |
179 | 44 |
|
180 | 45 |
|
|
0 commit comments