View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 testParameterNameFields() throws Exception {
59          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
60          verifyWithInlineConfigParser(
61                  getPath("InputParameterNameFields.java"), expected);
62      }
63  
64      @Test
65      public void testParameterNameMethods() throws Exception {
66          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
67          verifyWithInlineConfigParser(
68                  getPath("InputParameterNameMethods.java"), expected);
69      }
70  
71      @Test
72      public void testParameterNameMisc() throws Exception {
73          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
74          verifyWithInlineConfigParser(
75                  getPath("InputParameterNameMisc.java"), expected);
76      }
77  
78      @Test
79      public void testParameterNameForEach() throws Exception {
80          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
81          verifyWithInlineConfigParser(
82                  getPath("InputParameterNameForEach.java"), expected);
83      }
84  
85      @Test
86      public void testParameterNameEnum() throws Exception {
87          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
88          verifyWithInlineConfigParser(
89                  getPath("InputParameterNameEnum.java"), expected);
90      }
91  
92      @Test
93      public void testWhitespaceInAccessModifierProperty() throws Exception {
94          final String pattern = "^h$";
95          final String[] expected = {
96              "14:69: " + getCheckMessage(MSG_INVALID_PATTERN, "parameter1", pattern),
97              "18:31: " + getCheckMessage(MSG_INVALID_PATTERN, "parameter2", pattern),
98          };
99          verifyWithInlineConfigParser(
100                 getPath("InputParameterNameWhitespaceInAccessModifierProperty.java"), expected);
101     }
102 
103     @Test
104     public void testParameterNameOneFields() throws Exception {
105         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
106         verifyWithInlineConfigParser(
107                 getPath("InputParameterNameOneFields.java"), expected);
108     }
109 
110     @Test
111     public void testParameterNameOneMethods() throws Exception {
112         final String pattern = "^a[A-Z][a-zA-Z0-9]*$";
113         final String[] expected = {
114             "26:19: " + getCheckMessage(MSG_INVALID_PATTERN, "badFormat1", pattern),
115             "26:34: " + getCheckMessage(MSG_INVALID_PATTERN, "badFormat2", pattern),
116             "27:25: " + getCheckMessage(MSG_INVALID_PATTERN, "badFormat3", pattern),
117         };
118         verifyWithInlineConfigParser(
119                 getPath("InputParameterNameOneMethods.java"), expected);
120     }
121 
122     @Test
123     public void testParameterNameOneMisc() throws Exception {
124         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
125         verifyWithInlineConfigParser(
126                 getPath("InputParameterNameOneMisc.java"), expected);
127     }
128 
129     @Test
130     public void testParameterNameOneForEach() throws Exception {
131         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
132 
133         verifyWithInlineConfigParser(
134                 getPath("InputParameterNameOneForEach.java"), expected);
135     }
136 
137     @Test
138     public void testParameterNameOneEnum() throws Exception {
139         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
140         verifyWithInlineConfigParser(
141                 getPath("InputParameterNameOneEnum.java"), expected);
142     }
143 
144     @Test
145     public void testGetAcceptableTokens() {
146         final ParameterNameCheck parameterNameCheckObj = new ParameterNameCheck();
147         final int[] actual = parameterNameCheckObj.getAcceptableTokens();
148         final int[] expected = {
149             TokenTypes.PARAMETER_DEF,
150         };
151         assertWithMessage("Default acceptable tokens are invalid")
152                 .that(actual)
153                 .isEqualTo(expected);
154     }
155 
156     @Test
157     public void testSkipMethodsWithOverrideAnnotationTrue()
158             throws Exception {
159 
160         final String pattern = "^h$";
161 
162         final String[] expected = {
163             "20:28: " + getCheckMessage(MSG_INVALID_PATTERN, "object", pattern),
164             "24:30: " + getCheckMessage(MSG_INVALID_PATTERN, "aaaa", pattern),
165             "28:19: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern),
166             "28:28: " + getCheckMessage(MSG_INVALID_PATTERN, "bd", pattern),
167             "30:18: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern),
168             "37:46: " + getCheckMessage(MSG_INVALID_PATTERN, "fie", pattern),
169             "37:73: " + getCheckMessage(MSG_INVALID_PATTERN, "pkgNames", pattern),
170             };
171         verifyWithInlineConfigParser(
172                 getPath("InputParameterNameOverrideAnnotation.java"), expected);
173     }
174 
175     @Test
176     public void testSkipMethodsWithOverrideAnnotationFalse()
177             throws Exception {
178 
179         final String pattern = "^h$";
180 
181         final String[] expected = {
182             "15:34: " + getCheckMessage(MSG_INVALID_PATTERN, "o", pattern),
183             "20:28: " + getCheckMessage(MSG_INVALID_PATTERN, "object", pattern),
184             "24:30: " + getCheckMessage(MSG_INVALID_PATTERN, "aaaa", pattern),
185             "28:19: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern),
186             "28:28: " + getCheckMessage(MSG_INVALID_PATTERN, "bd", pattern),
187             "30:18: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern),
188             "37:49: " + getCheckMessage(MSG_INVALID_PATTERN, "fie", pattern),
189             "37:76: " + getCheckMessage(MSG_INVALID_PATTERN, "pkgNames", pattern),
190             };
191         verifyWithInlineConfigParser(
192                 getPath("InputParameterNameOverrideAnnotationOne.java"), expected);
193     }
194 
195     @Test
196     public void testPublicAccessModifier()
197             throws Exception {
198 
199         final String pattern = "^h$";
200 
201         final String[] expected = {
202             "14:49: " + getCheckMessage(MSG_INVALID_PATTERN, "pubconstr", pattern),
203             "18:31: " + getCheckMessage(MSG_INVALID_PATTERN, "inner", pattern),
204             "28:24: " + getCheckMessage(MSG_INVALID_PATTERN, "pubpub", pattern),
205             "39:21: " + getCheckMessage(MSG_INVALID_PATTERN, "pubifc", pattern),
206             "53:24: " + getCheckMessage(MSG_INVALID_PATTERN, "packpub", pattern),
207             "69:21: " + getCheckMessage(MSG_INVALID_PATTERN, "packifc", pattern),
208             };
209         verifyWithInlineConfigParser(
210                 getPath("InputParameterNameAccessModifier.java"), expected);
211     }
212 
213     @Test
214     public void testIsOverriddenNoNullPointerException()
215             throws Exception {
216         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
217         verifyWithInlineConfigParser(
218                 getPath("InputParameterNameOverrideAnnotationNoNPE.java"), expected);
219     }
220 
221     @Test
222     public void testReceiverParameter() throws Exception {
223         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
224         verifyWithInlineConfigParser(
225                 getPath("InputParameterNameReceiver.java"), expected);
226     }
227 
228     @Test
229     public void testLambdaParameterNoViolationAtAll() throws Exception {
230         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
231         verifyWithInlineConfigParser(
232                 getPath("InputParameterNameLambda.java"), expected);
233     }
234 
235     @Test
236     public void testWhitespaceInConfig() throws Exception {
237         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
238         verifyWithInlineConfigParser(
239                 getPath("InputParameterNameWhitespaceInConfig.java"), expected);
240     }
241 
242     @Test
243     public void testSetAccessModifiers() {
244         final AccessModifierOption[] input = {
245             AccessModifierOption.PACKAGE,
246         };
247         final ParameterNameCheck check = new ParameterNameCheck();
248         check.setAccessModifiers(input);
249 
250         assertWithMessage("check creates its own instance of access modifier array")
251             .that(System.identityHashCode(
252                     TestUtil.getInternalState(
253                             check, "accessModifiers", AccessModifierOption[].class)))
254             .isNotEqualTo(System.identityHashCode(input));
255     }
256 
257 }