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.naming;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
30 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31
32 public class ParameterNameCheckTest
33 extends AbstractModuleTestSupport {
34
35 @Override
36 protected String getPackageLocation() {
37 return "com/puppycrawl/tools/checkstyle/checks/naming/parametername";
38 }
39
40 @Test
41 public void testGetRequiredTokens() {
42 final ParameterNameCheck checkObj = new ParameterNameCheck();
43 final int[] expected = {TokenTypes.PARAMETER_DEF};
44 assertWithMessage("Default required tokens are invalid")
45 .that(checkObj.getRequiredTokens())
46 .isEqualTo(expected);
47 }
48
49 @Test
50 public void testCatch()
51 throws Exception {
52 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
53 verifyWithInlineConfigParser(
54 getPath("InputParameterNameCatchOnly.java"), expected);
55 }
56
57 @Test
58 public void testSpecified()
59 throws Exception {
60
61 final String pattern = "^a[A-Z][a-zA-Z0-9]*$";
62
63 final String[] expected = {
64 "68:19: " + getCheckMessage(MSG_INVALID_PATTERN, "badFormat1", pattern),
65 "68:34: " + getCheckMessage(MSG_INVALID_PATTERN, "badFormat2", pattern),
66 "69:25: " + getCheckMessage(MSG_INVALID_PATTERN, "badFormat3", pattern),
67 };
68 verifyWithInlineConfigParser(
69 getPath("InputParameterNameOne.java"), expected);
70 }
71
72 @Test
73 public void testWhitespaceInAccessModifierProperty() throws Exception {
74 final String pattern = "^h$";
75 final String[] expected = {
76 "14:69: " + getCheckMessage(MSG_INVALID_PATTERN, "parameter1", pattern),
77 "18:31: " + getCheckMessage(MSG_INVALID_PATTERN, "parameter2", pattern),
78 };
79 verifyWithInlineConfigParser(
80 getPath("InputParameterNameWhitespaceInAccessModifierProperty.java"), expected);
81 }
82
83 @Test
84 public void testDefault()
85 throws Exception {
86 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
87 verifyWithInlineConfigParser(
88 getPath("InputParameterName.java"), expected);
89 }
90
91 @Test
92 public void testGetAcceptableTokens() {
93 final ParameterNameCheck parameterNameCheckObj = new ParameterNameCheck();
94 final int[] actual = parameterNameCheckObj.getAcceptableTokens();
95 final int[] expected = {
96 TokenTypes.PARAMETER_DEF,
97 };
98 assertWithMessage("Default acceptable tokens are invalid")
99 .that(actual)
100 .isEqualTo(expected);
101 }
102
103 @Test
104 public void testSkipMethodsWithOverrideAnnotationTrue()
105 throws Exception {
106
107 final String pattern = "^h$";
108
109 final String[] expected = {
110 "20:28: " + getCheckMessage(MSG_INVALID_PATTERN, "object", pattern),
111 "24:30: " + getCheckMessage(MSG_INVALID_PATTERN, "aaaa", pattern),
112 "28:19: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern),
113 "28:28: " + getCheckMessage(MSG_INVALID_PATTERN, "bd", pattern),
114 "30:18: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern),
115 "37:46: " + getCheckMessage(MSG_INVALID_PATTERN, "fie", pattern),
116 "37:73: " + getCheckMessage(MSG_INVALID_PATTERN, "pkgNames", pattern),
117 };
118 verifyWithInlineConfigParser(
119 getPath("InputParameterNameOverrideAnnotation.java"), expected);
120 }
121
122 @Test
123 public void testSkipMethodsWithOverrideAnnotationFalse()
124 throws Exception {
125
126 final String pattern = "^h$";
127
128 final String[] expected = {
129 "15:34: " + getCheckMessage(MSG_INVALID_PATTERN, "o", pattern),
130 "20:28: " + getCheckMessage(MSG_INVALID_PATTERN, "object", pattern),
131 "24:30: " + getCheckMessage(MSG_INVALID_PATTERN, "aaaa", pattern),
132 "28:19: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern),
133 "28:28: " + getCheckMessage(MSG_INVALID_PATTERN, "bd", pattern),
134 "30:18: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern),
135 "37:49: " + getCheckMessage(MSG_INVALID_PATTERN, "fie", pattern),
136 "37:76: " + getCheckMessage(MSG_INVALID_PATTERN, "pkgNames", pattern),
137 };
138 verifyWithInlineConfigParser(
139 getPath("InputParameterNameOverrideAnnotationOne.java"), expected);
140 }
141
142 @Test
143 public void testPublicAccessModifier()
144 throws Exception {
145
146 final String pattern = "^h$";
147
148 final String[] expected = {
149 "14:49: " + getCheckMessage(MSG_INVALID_PATTERN, "pubconstr", pattern),
150 "18:31: " + getCheckMessage(MSG_INVALID_PATTERN, "inner", pattern),
151 "28:24: " + getCheckMessage(MSG_INVALID_PATTERN, "pubpub", pattern),
152 "39:21: " + getCheckMessage(MSG_INVALID_PATTERN, "pubifc", pattern),
153 "53:24: " + getCheckMessage(MSG_INVALID_PATTERN, "packpub", pattern),
154 "69:21: " + getCheckMessage(MSG_INVALID_PATTERN, "packifc", pattern),
155 };
156 verifyWithInlineConfigParser(
157 getPath("InputParameterNameAccessModifier.java"), expected);
158 }
159
160 @Test
161 public void testIsOverriddenNoNullPointerException()
162 throws Exception {
163 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
164 verifyWithInlineConfigParser(
165 getPath("InputParameterNameOverrideAnnotationNoNPE.java"), expected);
166 }
167
168 @Test
169 public void testReceiverParameter() throws Exception {
170 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
171 verifyWithInlineConfigParser(
172 getPath("InputParameterNameReceiver.java"), expected);
173 }
174
175 @Test
176 public void testLambdaParameterNoViolationAtAll() throws Exception {
177 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
178 verifyWithInlineConfigParser(
179 getPath("InputParameterNameLambda.java"), expected);
180 }
181
182 @Test
183 public void testWhitespaceInConfig() throws Exception {
184 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
185 verifyWithInlineConfigParser(
186 getPath("InputParameterNameWhitespaceInConfig.java"), expected);
187 }
188
189 @Test
190 public void testSetAccessModifiers() throws Exception {
191 final AccessModifierOption[] input = {
192 AccessModifierOption.PACKAGE,
193 };
194 final ParameterNameCheck check = new ParameterNameCheck();
195 check.setAccessModifiers(input);
196
197 assertWithMessage("check creates its own instance of access modifier array")
198 .that(System.identityHashCode(
199 TestUtil.getClassDeclaredField(ParameterNameCheck.class, "accessModifiers")
200 .get(check)))
201 .isNotEqualTo(System.identityHashCode(input));
202 }
203
204 }