1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.coding;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryNullCheckWithInstanceOfCheck.MSG_UNNECESSARY_NULLCHECK;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28
29 public class UnnecessaryNullCheckWithInstanceOfCheckTest extends AbstractModuleTestSupport {
30
31 @Override
32 public String getPackageLocation() {
33 return "com/puppycrawl/tools/checkstyle/checks/coding/unnecessarynullcheckwithinstanceof";
34 }
35
36 @Test
37 public void testUnnecessaryNullCheckWithInstanceof() throws Exception {
38
39 final String[] expected = {
40 "12:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
41 "15:39: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
42 "24:14: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
43 "54:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
44 "57:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
45 "60:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
46 "69:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
47 "74:26: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
48 "78:14: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
49 "80:30: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
50 "86:19: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
51 };
52 verifyWithInlineConfigParser(getPath(
53 "InputUnnecessaryNullCheckWithInstanceOfOne.java"), expected);
54 }
55
56 @Test
57 public void testUnnecessaryNullCheckWithInstanceofClass() throws Exception {
58
59 final String[] expected = {
60 "14:21: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
61 "28:24: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
62 };
63 verifyWithInlineConfigParser(getPath(
64 "InputUnnecessaryNullCheckWithInstanceOfAnonymousClass.java"), expected);
65 }
66
67 @Test
68 public void testUnnecessaryNullCheckWithInstanceofConditions() throws Exception {
69
70 final String[] expected = {
71 "12:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
72 "17:31: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
73 "22:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
74 "27:54: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
75 "36:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
76 "36:55: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
77 "50:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
78 };
79 verifyWithInlineConfigParser(getPath(
80 "InputUnnecessaryNullCheckWithInstanceOfMultipleConditions.java"), expected);
81 }
82
83 @Test
84 public void testUnnecessaryNullCheckWithInstanceofNested() throws Exception {
85
86 final String[] expected = {
87 "11:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
88 "18:21: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
89 "26:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
90 };
91 verifyWithInlineConfigParser(getPath(
92 "InputUnnecessaryNullCheckWithInstanceOfNestedIf.java"), expected);
93 }
94
95 @Test
96 public void testUnnecessaryNullCheckWithInstanceofLambda() throws Exception {
97
98 final String[] expected = {
99 "17:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
100 "23:45: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
101 "26:33: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
102 };
103 verifyWithInlineConfigParser(getPath(
104 "InputUnnecessaryNullCheckWithInstanceOfLambda.java"), expected);
105 }
106
107 @Test
108 public void testUnnecessaryNullCheckWithInstanceofSwitch() throws Exception {
109
110 final String[] expected = {
111 "13:21: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
112 "35:21: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
113 };
114 verifyWithInlineConfigParser(getPath(
115 "InputUnnecessaryNullCheckWithInstanceOfSwitch.java"), expected);
116 }
117
118 @Test
119 public void testUnnecessaryNullCheckWithInstanceofTernary() throws Exception {
120
121 final String[] expected = {
122 "11:16: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
123 "18:25: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
124 "23:24: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
125 "26:16: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
126 "27:20: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
127 "33:16: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
128 "34:19: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
129 "43:24: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
130 "43:61: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
131 "49:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
132 };
133 verifyWithInlineConfigParser(getPath(
134 "InputUnnecessaryNullCheckWithInstanceOfTernary.java"), expected);
135 }
136
137 @Test
138 public void testUnnecessaryNullCheckWithInstanceofTryCatch() throws Exception {
139
140 final String[] expected = {
141 "11:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
142 "23:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
143 "33:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
144 };
145 verifyWithInlineConfigParser(getPath(
146 "InputUnnecessaryNullCheckWithInstanceOfTryCatch.java"), expected);
147 }
148
149 @Test
150 public void testUnnecessaryNullCheckWithInstanceofTryVariable() throws Exception {
151
152 final String[] expected = {
153 "12:27: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
154 "19:36: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
155 };
156 verifyWithInlineConfigParser(getPath(
157 "InputUnnecessaryNullCheckWithInstanceOfVariableAssignment.java"), expected);
158 }
159
160 @Test
161 public void testUnnecessaryNullCheckWithInstanceofPattern() throws Exception {
162
163 final String[] expected = {
164 "12:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
165 };
166 verifyWithInlineConfigParser(
167 getPath(
168 "InputUnnecessaryNullCheckWithInstanceOfTwo.java"),
169 expected);
170 }
171
172 @Test
173 public void testUnnecessaryNullCheckWithInstanceofPair() throws Exception {
174
175 final String[] expected = {
176 "15:56: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
177 };
178 verifyWithInlineConfigParser(
179 getPath(
180 "InputUnnecessaryNullCheckWithInstanceOfPair.java"),
181 expected);
182 }
183
184 @Test
185 public void testTokensNotNull() {
186 final UnnecessaryNullCheckWithInstanceOfCheck check =
187 new UnnecessaryNullCheckWithInstanceOfCheck();
188 assertWithMessage("Acceptable tokens should not be null")
189 .that(check.getAcceptableTokens())
190 .isNotNull();
191 assertWithMessage("Default tokens should not be null")
192 .that(check.getDefaultTokens())
193 .isNotNull();
194 assertWithMessage("Required tokens should not be null")
195 .that(check.getRequiredTokens())
196 .isNotNull();
197 }
198
199 @Test
200 public void testUnnecessaryNullCheckWithInstanceofComplexCases() throws Exception {
201
202 final String[] expected = {
203 "14:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
204 "19:27: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
205 "22:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
206 "27:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
207 "34:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
208 "39:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
209 "61:16: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
210 "66:25: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
211 };
212 verifyWithInlineConfigParser(getPath(
213 "InputUnnecessaryNullCheckWithInstanceOfComplexCases.java"), expected);
214 }
215
216 @Test
217 public void testUnnecessaryNullCheckWithInstanceofMutationKiller() throws Exception {
218
219 final String[] expected = {
220 "24:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
221 "28:29: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
222 "35:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
223 "39:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
224 "46:14: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
225 "56:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
226 "75:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
227 "79:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
228 "86:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
229 "90:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
230 "97:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
231 };
232 verifyWithInlineConfigParser(getPath(
233 "InputUnnecessaryNullCheckWithInstanceOfMutationKiller.java"), expected);
234 }
235
236 @Test
237 public void testUnnecessaryNullCheckWithInstanceofMutationKillerTwo() throws Exception {
238
239 final String[] expected = {
240 "12:39: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
241 "16:59: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
242 "23:39: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
243 "27:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
244 };
245 verifyWithInlineConfigParser(getPath(
246 "InputUnnecessaryNullCheckWithInstanceOfMutationKillerTwo.java"), expected);
247 }
248
249 }