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.design;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.design.VisibilityModifierCheck.MSG_KEY;
24
25 import java.io.File;
26
27 import org.antlr.v4.runtime.CommonToken;
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31 import com.puppycrawl.tools.checkstyle.DetailAstImpl;
32 import com.puppycrawl.tools.checkstyle.JavaParser;
33 import com.puppycrawl.tools.checkstyle.api.DetailAST;
34 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
36 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
37
38 public class VisibilityModifierCheckTest
39 extends AbstractModuleTestSupport {
40
41 @Override
42 public String getPackageLocation() {
43 return "com/puppycrawl/tools/checkstyle/checks/design/visibilitymodifier";
44 }
45
46 @Test
47 public void testGetRequiredTokens() {
48 final VisibilityModifierCheck checkObj = new VisibilityModifierCheck();
49 final int[] expected = {
50 TokenTypes.VARIABLE_DEF,
51 TokenTypes.IMPORT,
52 };
53 assertWithMessage("Default required tokens are invalid")
54 .that(checkObj.getRequiredTokens())
55 .isEqualTo(expected);
56 }
57
58 @Test
59 public void testInner()
60 throws Exception {
61 final String[] expected = {
62 "47:24: " + getCheckMessage(MSG_KEY, "rData"),
63 "51:27: " + getCheckMessage(MSG_KEY, "safeField"),
64 "55:17: " + getCheckMessage(MSG_KEY, "localField"),
65 "61:29: " + getCheckMessage(MSG_KEY, "sWeird"),
66 "64:19: " + getCheckMessage(MSG_KEY, "sWeird2"),
67 "99:20: " + getCheckMessage(MSG_KEY, "someValue"),
68 "103:11: " + getCheckMessage(MSG_KEY, "fSerialVersionUID"),
69 };
70 verifyWithInlineConfigParser(
71 getPath("InputVisibilityModifierInner.java"), expected);
72 }
73
74 @Test
75 public void testIgnoreAccess()
76 throws Exception {
77 final String[] expected = {
78 "34:20: " + getCheckMessage(MSG_KEY, "fData"),
79 "95:20: " + getCheckMessage(MSG_KEY, "someValue"),
80 };
81 verifyWithInlineConfigParser(
82 getPath("InputVisibilityModifierInner1.java"), expected);
83 }
84
85 @Test
86 public void testSimple1() throws Exception {
87 final String[] expected = {
88 "49:19: " + getCheckMessage(MSG_KEY, "mNumCreated2"),
89 "60:23: " + getCheckMessage(MSG_KEY, "sTest1"),
90 "63:26: " + getCheckMessage(MSG_KEY, "sTest3"),
91 "66:16: " + getCheckMessage(MSG_KEY, "sTest2"),
92 "70:9: " + getCheckMessage(MSG_KEY, "mTest1"),
93 "73:16: " + getCheckMessage(MSG_KEY, "mTest2"),
94 };
95 verifyWithInlineConfigParser(
96 getPath("InputVisibilityModifierSimple.java"), expected);
97 }
98
99 @Test
100 public void testSimple2() throws Exception {
101 final String[] expected = {
102 };
103 verifyWithInlineConfigParser(
104 getPath("InputVisibilityModifierSimple2.java"), expected);
105 }
106
107 @Test
108 public void testStrictJavadoc() throws Exception {
109 final String[] expected = {
110 "49:9: " + getCheckMessage(MSG_KEY, "mLen"),
111 "51:19: " + getCheckMessage(MSG_KEY, "mDeer"),
112 "53:16: " + getCheckMessage(MSG_KEY, "aFreddo"),
113 };
114 verifyWithInlineConfigParser(
115 getPath("InputVisibilityModifierPublicOnly.java"), expected);
116 }
117
118 @Test
119 public void testAllowPublicFinalFieldsInImmutableClass() throws Exception {
120 final String[] expected = {
121 "33:39: " + getCheckMessage(MSG_KEY, "includes"),
122 "35:39: " + getCheckMessage(MSG_KEY, "excludes"),
123 "39:23: " + getCheckMessage(MSG_KEY, "list"),
124 "58:20: " + getCheckMessage(MSG_KEY, "value"),
125 "61:24: " + getCheckMessage(MSG_KEY, "rate"),
126 "63:31: " + getCheckMessage(MSG_KEY, "id"),
127 "68:19: " + getCheckMessage(MSG_KEY, "C_D_E"),
128 };
129 verifyWithInlineConfigParser(
130 getPath("InputVisibilityModifierImmutable.java"), expected);
131 }
132
133 @Test
134 public void testAllowPublicFinalFieldsInImmutableClassWithNonCanonicalClasses()
135 throws Exception {
136 final String[] expected = {
137 "28:39: " + getCheckMessage(MSG_KEY, "includes"),
138 "30:39: " + getCheckMessage(MSG_KEY, "excludes"),
139 "33:29: " + getCheckMessage(MSG_KEY, "money"),
140 "35:23: " + getCheckMessage(MSG_KEY, "list"),
141 "52:35: " + getCheckMessage(MSG_KEY, "uri"),
142 "54:35: " + getCheckMessage(MSG_KEY, "file"),
143 "56:20: " + getCheckMessage(MSG_KEY, "value"),
144 "58:35: " + getCheckMessage(MSG_KEY, "url"),
145 "60:24: " + getCheckMessage(MSG_KEY, "rate"),
146 "62:21: " + getCheckMessage(MSG_KEY, "id"),
147 "67:19: " + getCheckMessage(MSG_KEY, "C_D_E"),
148 };
149 verifyWithInlineConfigParser(
150 getPath("InputVisibilityModifierImmutable2.java"), expected);
151 }
152
153 @Test
154 public void testDisAllowPublicFinalAndImmutableFieldsInImmutableClass() throws Exception {
155 final String[] expected = {
156 "32:22: " + getCheckMessage(MSG_KEY, "someIntValue"),
157 "34:39: " + getCheckMessage(MSG_KEY, "includes"),
158 "36:39: " + getCheckMessage(MSG_KEY, "excludes"),
159 "38:25: " + getCheckMessage(MSG_KEY, "notes"),
160 "40:29: " + getCheckMessage(MSG_KEY, "money"),
161 "42:23: " + getCheckMessage(MSG_KEY, "list"),
162 "57:28: " + getCheckMessage(MSG_KEY, "f"),
163 "59:30: " + getCheckMessage(MSG_KEY, "bool"),
164 "61:35: " + getCheckMessage(MSG_KEY, "uri"),
165 "63:35: " + getCheckMessage(MSG_KEY, "file"),
166 "65:20: " + getCheckMessage(MSG_KEY, "value"),
167 "67:35: " + getCheckMessage(MSG_KEY, "url"),
168 "69:24: " + getCheckMessage(MSG_KEY, "rate"),
169 "71:21: " + getCheckMessage(MSG_KEY, "id"),
170 "76:19: " + getCheckMessage(MSG_KEY, "C_D_E"),
171 };
172 verifyWithInlineConfigParser(
173 getPath("InputVisibilityModifierImmutable3.java"), expected);
174 }
175
176 @Test
177 public void testAllowPublicFinalFieldsInNonFinalClass() throws Exception {
178 final String[] expected = {
179 "55:20: " + getCheckMessage(MSG_KEY, "value"),
180 "58:24: " + getCheckMessage(MSG_KEY, "rate"),
181 "60:21: " + getCheckMessage(MSG_KEY, "id"),
182 };
183 verifyWithInlineConfigParser(
184 getPath("InputVisibilityModifierImmutable4.java"), expected);
185 }
186
187 @Test
188 public void testUserSpecifiedImmutableClassesList() throws Exception {
189 final String[] expected = {
190 "30:29: " + getCheckMessage(MSG_KEY, "money"),
191 "48:35: " + getCheckMessage(MSG_KEY, "uri"),
192 "50:35: " + getCheckMessage(MSG_KEY, "file"),
193 "52:20: " + getCheckMessage(MSG_KEY, "value"),
194 "54:35: " + getCheckMessage(MSG_KEY, "url"),
195 "56:24: " + getCheckMessage(MSG_KEY, "rate"),
196 "58:21: " + getCheckMessage(MSG_KEY, "id"),
197 "63:19: " + getCheckMessage(MSG_KEY, "C_D_E"),
198 };
199 verifyWithInlineConfigParser(
200 getPath("InputVisibilityModifierImmutable5.java"), expected);
201 }
202
203 @Test
204 public void testImmutableSpecifiedSameTypeName() throws Exception {
205 final String[] expected = {
206 "23:46: " + getCheckMessage(MSG_KEY, "calendar"),
207 "27:36: " + getCheckMessage(MSG_KEY, "address"),
208 "29:36: " + getCheckMessage(MSG_KEY, "adr"),
209 };
210 verifyWithInlineConfigParser(
211 getPath("InputVisibilityModifierImmutableSameTypeName.java"),
212 expected);
213 }
214
215 @Test
216 public void testImmutableValueSameTypeName() throws Exception {
217 final String[] expected = {
218 "28:46: " + getCheckMessage(MSG_KEY, "calendar"),
219 "30:59: " + getCheckMessage(MSG_KEY, "calendar2"),
220 "32:59: " + getCheckMessage(MSG_KEY, "calendar3"),
221 };
222 verifyWithInlineConfigParser(
223 getPath("InputVisibilityModifierImmutableSameTypeName2.java"),
224 expected);
225 }
226
227 @Test
228 public void testImmutableStarImportFalseNegative() throws Exception {
229 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
230 verifyWithInlineConfigParser(
231 getPath("InputVisibilityModifierImmutableStarImport.java"), expected);
232 }
233
234 @Test
235 public void testImmutableStarImportNoWarn() throws Exception {
236 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
237 verifyWithInlineConfigParser(
238 getPath("InputVisibilityModifierImmutableStarImport2.java"),
239 expected);
240 }
241
242 @Test
243 public void testDefaultAnnotationPatterns() throws Exception {
244 final String[] expected = {
245 "61:19: " + getCheckMessage(MSG_KEY, "publicCustom"),
246 "65:12: " + getCheckMessage(MSG_KEY, "packageCustom"),
247 "69:22: " + getCheckMessage(MSG_KEY, "protectedCustom"),
248 "72:19: " + getCheckMessage(MSG_KEY, "unannotatedPublic"),
249 "74:12: " + getCheckMessage(MSG_KEY, "unannotatedPackage"),
250 "76:22: " + getCheckMessage(MSG_KEY, "unannotatedProtected"),
251 };
252 verifyWithInlineConfigParser(
253 getPath("InputVisibilityModifierAnnotated.java"), expected);
254 }
255
256 @Test
257 public void testCustomAnnotationPatterns() throws Exception {
258 final String[] expected = {
259 "37:28: " + getCheckMessage(MSG_KEY, "rule"),
260 "41:28: " + getCheckMessage(MSG_KEY, "ruleFull"),
261 "45:19: " + getCheckMessage(MSG_KEY, "publicGoogle"),
262 "49:12: " + getCheckMessage(MSG_KEY, "packageGoogle"),
263 "53:22: " + getCheckMessage(MSG_KEY, "protectedGoogle"),
264 "57:19: " + getCheckMessage(MSG_KEY, "publicGoogleFull"),
265 "61:12: " + getCheckMessage(MSG_KEY, "packageGoogleFull"),
266 "65:22: " + getCheckMessage(MSG_KEY, "protectedGoogleFull"),
267 "77:19: " + getCheckMessage(MSG_KEY, "unannotatedPublic"),
268 "79:12: " + getCheckMessage(MSG_KEY, "unannotatedPackage"),
269 "81:22: " + getCheckMessage(MSG_KEY, "unannotatedProtected"),
270 "91:35: " + getCheckMessage(MSG_KEY, "classRule"),
271 "95:35: " + getCheckMessage(MSG_KEY, "classRuleFull"),
272 };
273 verifyWithInlineConfigParser(
274 getPath("InputVisibilityModifierAnnotated2.java"), expected);
275 }
276
277 @Test
278 public void testIgnoreAnnotationNoPattern() throws Exception {
279 final String[] expected = {
280 "36:28: " + getCheckMessage(MSG_KEY, "rule"),
281 "40:28: " + getCheckMessage(MSG_KEY, "ruleFull"),
282 "44:19: " + getCheckMessage(MSG_KEY, "publicGoogle"),
283 "48:12: " + getCheckMessage(MSG_KEY, "packageGoogle"),
284 "52:22: " + getCheckMessage(MSG_KEY, "protectedGoogle"),
285 "56:19: " + getCheckMessage(MSG_KEY, "publicGoogleFull"),
286 "60:12: " + getCheckMessage(MSG_KEY, "packageGoogleFull"),
287 "64:22: " + getCheckMessage(MSG_KEY, "protectedGoogleFull"),
288 "68:19: " + getCheckMessage(MSG_KEY, "publicCustom"),
289 "72:12: " + getCheckMessage(MSG_KEY, "packageCustom"),
290 "76:22: " + getCheckMessage(MSG_KEY, "protectedCustom"),
291 "79:19: " + getCheckMessage(MSG_KEY, "unannotatedPublic"),
292 "81:12: " + getCheckMessage(MSG_KEY, "unannotatedPackage"),
293 "83:22: " + getCheckMessage(MSG_KEY, "unannotatedProtected"),
294 "93:35: " + getCheckMessage(MSG_KEY, "classRule"),
295 "97:35: " + getCheckMessage(MSG_KEY, "classRuleFull"),
296 };
297 verifyWithInlineConfigParser(
298 getPath("InputVisibilityModifierAnnotated3.java"), expected);
299 }
300
301 @Test
302 public void testIgnoreAnnotationSameName() throws Exception {
303 final String[] expected = {
304 "33:28: " + getCheckMessage(MSG_KEY, "rule"),
305 "37:28: " + getCheckMessage(MSG_KEY, "classRule"),
306 };
307 verifyWithInlineConfigParser(
308 getPath("InputVisibilityModifierAnnotatedSameTypeName.java"),
309 expected);
310 }
311
312 @Test
313 public void testGetAcceptableTokens() {
314 final VisibilityModifierCheck obj = new VisibilityModifierCheck();
315 final int[] expected = {
316 TokenTypes.VARIABLE_DEF,
317 TokenTypes.IMPORT,
318 };
319 assertWithMessage("Default acceptable tokens are invalid")
320 .that(obj.getAcceptableTokens())
321 .isEqualTo(expected);
322 }
323
324 @Test
325 public void testPublicImmutableFieldsNotAllowed() throws Exception {
326 final String[] expected = {
327 "31:22: " + getCheckMessage(MSG_KEY, "someIntValue"),
328 "33:39: " + getCheckMessage(MSG_KEY, "includes"),
329 "35:35: " + getCheckMessage(MSG_KEY, "notes"),
330 "37:29: " + getCheckMessage(MSG_KEY, "value"),
331 "39:23: " + getCheckMessage(MSG_KEY, "list"),
332 };
333 verifyWithInlineConfigParser(
334 getPath("InputVisibilityModifiersPublicImmutable.java"), expected);
335 }
336
337 @Test
338 public void testPublicFinalFieldsNotAllowed() throws Exception {
339 final String[] expected = {
340 "31:22: " + getCheckMessage(MSG_KEY, "someIntValue"),
341 "33:39: " + getCheckMessage(MSG_KEY, "includes"),
342 "35:25: " + getCheckMessage(MSG_KEY, "notes"),
343 "37:29: " + getCheckMessage(MSG_KEY, "value"),
344 "39:23: " + getCheckMessage(MSG_KEY, "list"),
345 };
346 verifyWithInlineConfigParser(
347 getPath("InputVisibilityModifiersPublicImmutable2.java"), expected);
348 }
349
350 @Test
351 public void testPublicFinalFieldsAllowed() throws Exception {
352 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
353 verifyWithInlineConfigParser(
354 getPath("InputVisibilityModifiersPublicImmutable3.java"), expected);
355 }
356
357 @Test
358 public void testPublicFinalFieldInEnum() throws Exception {
359 final String[] expected = {
360 "35:23: " + getCheckMessage(MSG_KEY, "hole"),
361 };
362 verifyWithInlineConfigParser(
363 getPath("InputVisibilityModifiersEnumIsSealed.java"), expected);
364 }
365
366 @Test
367 public void testWrongTokenType() {
368 final VisibilityModifierCheck obj = new VisibilityModifierCheck();
369 final DetailAstImpl ast = new DetailAstImpl();
370 ast.initialize(new CommonToken(TokenTypes.CLASS_DEF, "class"));
371 try {
372 obj.visitToken(ast);
373 assertWithMessage("exception expected").fail();
374 }
375 catch (IllegalArgumentException exc) {
376 assertWithMessage("Invalid exception message")
377 .that(exc.getMessage())
378 .isEqualTo("Unexpected token type: class");
379 }
380 }
381
382 @Test
383 public void testNullModifiers() throws Exception {
384 final String[] expected = {
385 "32:50: " + getCheckMessage(MSG_KEY, "i"),
386 };
387 verifyWithInlineConfigParser(
388 getPath("InputVisibilityModifiersNullModifiers.java"), expected);
389 }
390
391 @Test
392 public void testVisibilityModifiersOfGenericFields() throws Exception {
393 final String[] expected = {
394 "31:56: " + getCheckMessage(MSG_KEY, "perfSeries"),
395 "33:66: " + getCheckMessage(MSG_KEY, "peopleMap"),
396 "35:66: " + getCheckMessage(MSG_KEY, "someMap"),
397 "37:76: " + getCheckMessage(MSG_KEY, "newMap"),
398 "40:45: " + getCheckMessage(MSG_KEY, "optionalOfObject"),
399 "42:35: " + getCheckMessage(MSG_KEY, "obj"),
400 "45:19: " + getCheckMessage(MSG_KEY, "rqUID"),
401 "47:29: " + getCheckMessage(MSG_KEY, "rqTime"),
402 "49:45: " + getCheckMessage(MSG_KEY, "rates"),
403 "51:50: " + getCheckMessage(MSG_KEY, "loans"),
404 "53:60: " + getCheckMessage(MSG_KEY, "cards"),
405 "55:60: " + getCheckMessage(MSG_KEY, "values"),
406 "57:70: " + getCheckMessage(MSG_KEY, "permissions"),
407 "60:38: " + getCheckMessage(MSG_KEY, "mapOfStrings"),
408 "62:48: " + getCheckMessage(MSG_KEY, "names"),
409 "64:48: " + getCheckMessage(MSG_KEY, "links"),
410 "66:38: " + getCheckMessage(MSG_KEY, "presentations"),
411 "68:48: " + getCheckMessage(MSG_KEY, "collection"),
412 "72:73: " + getCheckMessage(MSG_KEY, "exceptions"),
413 };
414 verifyWithInlineConfigParser(
415 getPath("InputVisibilityModifierGenerics.java"), expected);
416 }
417
418
419
420
421
422
423
424
425
426
427 @Test
428 public void testIsStarImportNullAst() throws Exception {
429 final DetailAST importAst = JavaParser.parseFile(
430 new File(getPath("InputVisibilityModifierIsStarImport.java")),
431 JavaParser.Options.WITHOUT_COMMENTS).getFirstChild().getNextSibling();
432 final VisibilityModifierCheck check = new VisibilityModifierCheck();
433 final boolean actual = TestUtil.invokeMethod(check, "isStarImport",
434 Boolean.class, importAst);
435
436 assertWithMessage("Should return true when star import is passed")
437 .that(actual)
438 .isTrue();
439 }
440
441 @Test
442 public void testPackageClassName() throws Exception {
443 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
444 verifyWithInlineConfigParser(
445 getNonCompilablePath("InputVisibilityModifierPackageClassName.java"),
446 expected);
447 }
448
449 }