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;
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      protected 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              "115:18: " + getCheckMessage(MSG_KEY, "aParam"),
48              "118:18: " + getCheckMessage(MSG_KEY, "args"),
49              "121:18: " + getCheckMessage(MSG_KEY, "args"),
50          };
51          verifyWithInlineConfigParser(
52                  getPath("InputFinalParameters.java"), expected);
53      }
54  
55      @Test
56      public void testCtorToken() throws Exception {
57          final String[] expected = {
58              "29:27: " + getCheckMessage(MSG_KEY, "s"),
59              "44:27: " + getCheckMessage(MSG_KEY, "i"),
60              "49:27: " + getCheckMessage(MSG_KEY, "s"),
61          };
62          verifyWithInlineConfigParser(
63                  getPath("InputFinalParameters2.java"), expected);
64      }
65  
66      @Test
67      public void testMethodToken() throws Exception {
68          final String[] expected = {
69              "59:17: " + getCheckMessage(MSG_KEY, "s"),
70              "75:17: " + getCheckMessage(MSG_KEY, "s"),
71              "81:17: " + getCheckMessage(MSG_KEY, "s"),
72              "96:45: " + getCheckMessage(MSG_KEY, "e"),
73              "99:36: " + getCheckMessage(MSG_KEY, "e"),
74              "116:18: " + getCheckMessage(MSG_KEY, "aParam"),
75              "119:18: " + getCheckMessage(MSG_KEY, "args"),
76              "122:18: " + getCheckMessage(MSG_KEY, "args"),
77          };
78          verifyWithInlineConfigParser(
79                  getPath("InputFinalParameters3.java"), expected);
80      }
81  
82      @Test
83      public void testCatchToken() throws Exception {
84          final String[] expected = {
85              "131:16: " + getCheckMessage(MSG_KEY, "npe"),
86              "137:16: " + getCheckMessage(MSG_KEY, "e"),
87              "140:16: " + getCheckMessage(MSG_KEY, "e"),
88          };
89          verifyWithInlineConfigParser(
90                  getPath("InputFinalParameters4.java"), expected);
91      }
92  
93      @Test
94      public void testForEachClauseToken() throws Exception {
95          final String[] expected = {
96              "158:13: " + getCheckMessage(MSG_KEY, "s"),
97              "166:13: " + getCheckMessage(MSG_KEY, "s"),
98          };
99          verifyWithInlineConfigParser(
100                 getPath("InputFinalParameters5.java"), expected);
101     }
102 
103     @Test
104     public void testIgnorePrimitiveTypesParameters() throws Exception {
105         final String[] expected = {
106             "15:22: " + getCheckMessage(MSG_KEY, "k"),
107             "16:15: " + getCheckMessage(MSG_KEY, "s"),
108             "16:25: " + getCheckMessage(MSG_KEY, "o"),
109             "20:15: " + getCheckMessage(MSG_KEY, "array"),
110             "21:31: " + getCheckMessage(MSG_KEY, "s"),
111             "22:22: " + getCheckMessage(MSG_KEY, "l"),
112             "22:32: " + getCheckMessage(MSG_KEY, "s"),
113         };
114         verifyWithInlineConfigParser(
115                 getPath("InputFinalParametersPrimitiveTypes.java"), expected);
116     }
117 
118     @Test
119     public void testPrimitiveTypesParameters() throws Exception {
120         final String[] expected = {
121             "14:14: " + getCheckMessage(MSG_KEY, "i"),
122             "15:15: " + getCheckMessage(MSG_KEY, "i"),
123             "15:22: " + getCheckMessage(MSG_KEY, "k"),
124             "15:32: " + getCheckMessage(MSG_KEY, "s"),
125             "20:15: " + getCheckMessage(MSG_KEY, "s"),
126             "20:25: " + getCheckMessage(MSG_KEY, "o"),
127             "20:35: " + getCheckMessage(MSG_KEY, "l"),
128             "25:15: " + getCheckMessage(MSG_KEY, "array"),
129             "26:15: " + getCheckMessage(MSG_KEY, "i"),
130             "26:22: " + getCheckMessage(MSG_KEY, "x"),
131             "26:31: " + getCheckMessage(MSG_KEY, "s"),
132             "31:15: " + getCheckMessage(MSG_KEY, "x"),
133             "31:22: " + getCheckMessage(MSG_KEY, "l"),
134             "31:32: " + getCheckMessage(MSG_KEY, "s"),
135         };
136         verifyWithInlineConfigParser(
137                 getPath("InputFinalParametersPrimitiveTypes2.java"), expected);
138     }
139 
140     @Test
141     public void testReceiverParameters() throws Exception {
142         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
143         verifyWithInlineConfigParser(
144                 getPath("InputFinalParametersReceiver.java"), expected);
145     }
146 
147     @Test
148     public void testUnnamedParametersPropertyTrue() throws Exception {
149         final String[] expected = {
150             "25:18: " + getCheckMessage(MSG_KEY, "__"),
151             "30:18: " + getCheckMessage(MSG_KEY, "_e"),
152             "35:18: " + getCheckMessage(MSG_KEY, "e_"),
153             "46:14: " + getCheckMessage(MSG_KEY, "__"),
154             "49:14: " + getCheckMessage(MSG_KEY, "_i"),
155             "52:14: " + getCheckMessage(MSG_KEY, "i_"),
156         };
157         verifyWithInlineConfigParser(
158                 getNonCompilablePath("InputFinalParametersUnnamedPropertyTrue.java"), expected);
159     }
160 
161     @Test
162     public void testUnnamedParametersPropertyFalse() throws Exception {
163         final String[] expected = {
164             "20:18: " + getCheckMessage(MSG_KEY, "_"),
165             "25:18: " + getCheckMessage(MSG_KEY, "__"),
166             "30:18: " + getCheckMessage(MSG_KEY, "_e"),
167             "35:18: " + getCheckMessage(MSG_KEY, "e_"),
168             "46:14: " + getCheckMessage(MSG_KEY, "__"),
169             "43:14: " + getCheckMessage(MSG_KEY, "_"),
170             "49:14: " + getCheckMessage(MSG_KEY, "_i"),
171             "52:14: " + getCheckMessage(MSG_KEY, "i_"),
172         };
173         verifyWithInlineConfigParser(
174                 getNonCompilablePath("InputFinalParametersUnnamedPropertyFalse.java"), expected);
175     }
176 }