View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.FinalParametersCheck.MSG_KEY;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
28  
29  public class FinalParametersCheckTest extends AbstractModuleTestSupport {
30  
31      @Override
32      public String getPackageLocation() {
33          return "com/puppycrawl/tools/checkstyle/checks/finalparameters";
34      }
35  
36      @Test
37      public void testDefaultTokens() throws Exception {
38          final String[] expected = {
39              "28:26: " + getCheckMessage(MSG_KEY, "s"),
40              "43:26: " + getCheckMessage(MSG_KEY, "i"),
41              "48:26: " + getCheckMessage(MSG_KEY, "s"),
42              "58:17: " + getCheckMessage(MSG_KEY, "s"),
43              "74:17: " + getCheckMessage(MSG_KEY, "s"),
44              "80:17: " + getCheckMessage(MSG_KEY, "s"),
45              "95:45: " + getCheckMessage(MSG_KEY, "e"),
46              "98:36: " + getCheckMessage(MSG_KEY, "e"),
47          };
48          verifyWithInlineConfigParser(
49                  getPath("InputFinalParameters.java"), expected);
50      }
51  
52      @Test
53      public void testCtorToken() throws Exception {
54          final String[] expected = {
55              "29:27: " + getCheckMessage(MSG_KEY, "s"),
56              "44:27: " + getCheckMessage(MSG_KEY, "i"),
57              "49:27: " + getCheckMessage(MSG_KEY, "s"),
58          };
59          verifyWithInlineConfigParser(
60                  getPath("InputFinalParameters2.java"), expected);
61      }
62  
63      @Test
64      public void testMethodToken() throws Exception {
65          final String[] expected = {
66              "59:17: " + getCheckMessage(MSG_KEY, "s"),
67              "75:17: " + getCheckMessage(MSG_KEY, "s"),
68              "81:17: " + getCheckMessage(MSG_KEY, "s"),
69              "96:45: " + getCheckMessage(MSG_KEY, "e"),
70              "99:36: " + getCheckMessage(MSG_KEY, "e"),
71          };
72          verifyWithInlineConfigParser(
73                  getPath("InputFinalParameters3.java"), expected);
74      }
75  
76      @Test
77      public void testCatchToken() throws Exception {
78          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
79          verifyWithInlineConfigParser(
80                  getPath("InputFinalParameters4.java"), expected);
81      }
82  
83      @Test
84      public void testForEachClauseToken() throws Exception {
85          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
86          verifyWithInlineConfigParser(
87                  getPath("InputFinalParameters5.java"), expected);
88      }
89  
90      @Test
91      public void testDefaultTokensSplit() throws Exception {
92          final String[] expected = {
93              "18:18: " + getCheckMessage(MSG_KEY, "aParam"),
94              "21:18: " + getCheckMessage(MSG_KEY, "args"),
95              "24:18: " + getCheckMessage(MSG_KEY, "args"),
96          };
97          verifyWithInlineConfigParser(
98                  getPath("InputFinalParameters6.java"), expected);
99      }
100 
101     @Test
102     public void testCtorTokenSplit() throws Exception {
103         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
104         verifyWithInlineConfigParser(
105                 getPath("InputFinalParameters7.java"), expected);
106     }
107 
108     @Test
109     public void testMethodTokenSplit() throws Exception {
110         final String[] expected = {
111             "18:18: " + getCheckMessage(MSG_KEY, "aParam"),
112             "21:18: " + getCheckMessage(MSG_KEY, "args"),
113             "24:18: " + getCheckMessage(MSG_KEY, "args"),
114         };
115         verifyWithInlineConfigParser(
116                 getPath("InputFinalParameters8.java"), expected);
117     }
118 
119     @Test
120     public void testCatchTokenSplit() throws Exception {
121         final String[] expected = {
122             "33:16: " + getCheckMessage(MSG_KEY, "npe"),
123             "39:16: " + getCheckMessage(MSG_KEY, "e"),
124             "42:16: " + getCheckMessage(MSG_KEY, "e"),
125         };
126         verifyWithInlineConfigParser(
127                 getPath("InputFinalParameters9.java"), expected);
128     }
129 
130     @Test
131     public void testForEachClauseTokenSplit() throws Exception {
132         final String[] expected = {
133             "60:13: " + getCheckMessage(MSG_KEY, "s"),
134             "68:13: " + getCheckMessage(MSG_KEY, "s"),
135         };
136         verifyWithInlineConfigParser(
137                 getPath("InputFinalParameters10.java"), expected);
138     }
139 
140     @Test
141     public void testIgnorePrimitiveTypesParameters() throws Exception {
142         final String[] expected = {
143             "15:22: " + getCheckMessage(MSG_KEY, "k"),
144             "16:15: " + getCheckMessage(MSG_KEY, "s"),
145             "16:25: " + getCheckMessage(MSG_KEY, "o"),
146             "20:15: " + getCheckMessage(MSG_KEY, "array"),
147             "21:31: " + getCheckMessage(MSG_KEY, "s"),
148             "22:22: " + getCheckMessage(MSG_KEY, "l"),
149             "22:32: " + getCheckMessage(MSG_KEY, "s"),
150         };
151         verifyWithInlineConfigParser(
152                 getPath("InputFinalParametersPrimitiveTypes.java"), expected);
153     }
154 
155     @Test
156     public void testPrimitiveTypesParameters() throws Exception {
157         final String[] expected = {
158             "14:14: " + getCheckMessage(MSG_KEY, "i"),
159             "15:15: " + getCheckMessage(MSG_KEY, "i"),
160             "15:22: " + getCheckMessage(MSG_KEY, "k"),
161             "15:32: " + getCheckMessage(MSG_KEY, "s"),
162             "20:15: " + getCheckMessage(MSG_KEY, "s"),
163             "20:25: " + getCheckMessage(MSG_KEY, "o"),
164             "20:35: " + getCheckMessage(MSG_KEY, "l"),
165             "25:15: " + getCheckMessage(MSG_KEY, "array"),
166             "26:15: " + getCheckMessage(MSG_KEY, "i"),
167             "26:22: " + getCheckMessage(MSG_KEY, "x"),
168             "26:31: " + getCheckMessage(MSG_KEY, "s"),
169             "31:15: " + getCheckMessage(MSG_KEY, "x"),
170             "31:22: " + getCheckMessage(MSG_KEY, "l"),
171             "31:32: " + getCheckMessage(MSG_KEY, "s"),
172         };
173         verifyWithInlineConfigParser(
174                 getPath("InputFinalParametersPrimitiveTypes2.java"), expected);
175     }
176 
177     @Test
178     public void testReceiverParameters() throws Exception {
179         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
180         verifyWithInlineConfigParser(
181                 getPath("InputFinalParametersReceiver.java"), expected);
182     }
183 
184     @Test
185     public void testUnnamedParametersPropertyTrue() throws Exception {
186         final String[] expected = {
187             "25:18: " + getCheckMessage(MSG_KEY, "__"),
188             "30:18: " + getCheckMessage(MSG_KEY, "_e"),
189             "35:18: " + getCheckMessage(MSG_KEY, "e_"),
190             "46:14: " + getCheckMessage(MSG_KEY, "__"),
191             "49:14: " + getCheckMessage(MSG_KEY, "_i"),
192             "52:14: " + getCheckMessage(MSG_KEY, "i_"),
193         };
194         verifyWithInlineConfigParser(
195                 getNonCompilablePath("InputFinalParametersUnnamedPropertyTrue.java"), expected);
196     }
197 
198     @Test
199     public void testUnnamedParametersPropertyFalse() throws Exception {
200         final String[] expected = {
201             "20:18: " + getCheckMessage(MSG_KEY, "_"),
202             "25:18: " + getCheckMessage(MSG_KEY, "__"),
203             "30:18: " + getCheckMessage(MSG_KEY, "_e"),
204             "35:18: " + getCheckMessage(MSG_KEY, "e_"),
205             "46:14: " + getCheckMessage(MSG_KEY, "__"),
206             "43:14: " + getCheckMessage(MSG_KEY, "_"),
207             "49:14: " + getCheckMessage(MSG_KEY, "_i"),
208             "52:14: " + getCheckMessage(MSG_KEY, "i_"),
209         };
210         verifyWithInlineConfigParser(
211                 getNonCompilablePath("InputFinalParametersUnnamedPropertyFalse.java"), expected);
212     }
213 
214     @Test
215     public void testMethodTokenInInterface() throws Exception {
216         final String[] expected = {
217             "16:26: " + getCheckMessage(MSG_KEY, "param1"),
218             "22:27: " + getCheckMessage(MSG_KEY, "param1"),
219             "28:27: " + getCheckMessage(MSG_KEY, "param1"),
220         };
221         verifyWithInlineConfigParser(getPath("InputFinalParametersInterfaceMethod.java"), expected);
222 
223     }
224 
225     @Test
226     public void testPatternVariableDefinitions() throws Exception {
227         final String[] expected = {
228             "17:47: " + getCheckMessage(MSG_KEY, "s"),
229             "19:26: " + getCheckMessage(MSG_KEY, "i"),
230             "24:34: " + getCheckMessage(MSG_KEY, "name"),
231             "30:18: " + getCheckMessage(MSG_KEY, "s"),
232             "32:26: " + getCheckMessage(MSG_KEY, "name"),
233         };
234         verifyWithInlineConfigParser(
235             getPath("InputFinalParametersPatternVariables.java"), expected);
236     }
237 
238     @Test
239     public void testRecordForLoopPatternVariableDefinitions() throws Exception {
240         final String[] expected = {
241             "17:22: " + getCheckMessage(MSG_KEY, "name"),
242         };
243         verifyWithInlineConfigParser(
244             getNonCompilablePath("InputFinalParametersRecordForLoopPatternVariables.java"),
245             expected
246         );
247     }
248 }