View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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              "27:26: " + getCheckMessage(MSG_KEY, "s"),
40              "42:26: " + getCheckMessage(MSG_KEY, "i"),
41              "47:26: " + getCheckMessage(MSG_KEY, "s"),
42              "57:17: " + getCheckMessage(MSG_KEY, "s"),
43              "73:17: " + getCheckMessage(MSG_KEY, "s"),
44              "79:17: " + getCheckMessage(MSG_KEY, "s"),
45              "94:45: " + getCheckMessage(MSG_KEY, "e"),
46              "97:36: " + getCheckMessage(MSG_KEY, "e"),
47              "114:18: " + getCheckMessage(MSG_KEY, "aParam"),
48              "117:18: " + getCheckMessage(MSG_KEY, "args"),
49              "120: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              "28:27: " + getCheckMessage(MSG_KEY, "s"),
59              "43:27: " + getCheckMessage(MSG_KEY, "i"),
60              "48: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              "58:17: " + getCheckMessage(MSG_KEY, "s"),
70              "74:17: " + getCheckMessage(MSG_KEY, "s"),
71              "80:17: " + getCheckMessage(MSG_KEY, "s"),
72              "95:45: " + getCheckMessage(MSG_KEY, "e"),
73              "98:36: " + getCheckMessage(MSG_KEY, "e"),
74              "115:18: " + getCheckMessage(MSG_KEY, "aParam"),
75              "118:18: " + getCheckMessage(MSG_KEY, "args"),
76              "121: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              "130:16: " + getCheckMessage(MSG_KEY, "npe"),
86              "136:16: " + getCheckMessage(MSG_KEY, "e"),
87              "139: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              "157:13: " + getCheckMessage(MSG_KEY, "s"),
97              "165: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             "14:22: " + getCheckMessage(MSG_KEY, "k"),
107             "15:15: " + getCheckMessage(MSG_KEY, "s"),
108             "15:25: " + getCheckMessage(MSG_KEY, "o"),
109             "16:15: " + getCheckMessage(MSG_KEY, "array"),
110             "17:31: " + getCheckMessage(MSG_KEY, "s"),
111             "18:22: " + getCheckMessage(MSG_KEY, "l"),
112             "18: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             "13:14: " + getCheckMessage(MSG_KEY, "i"),
122             "14:15: " + getCheckMessage(MSG_KEY, "i"),
123             "14:22: " + getCheckMessage(MSG_KEY, "k"),
124             "14:32: " + getCheckMessage(MSG_KEY, "s"),
125             "15:15: " + getCheckMessage(MSG_KEY, "s"),
126             "15:25: " + getCheckMessage(MSG_KEY, "o"),
127             "15:35: " + getCheckMessage(MSG_KEY, "l"),
128             "16:15: " + getCheckMessage(MSG_KEY, "array"),
129             "17:15: " + getCheckMessage(MSG_KEY, "i"),
130             "17:22: " + getCheckMessage(MSG_KEY, "x"),
131             "17:31: " + getCheckMessage(MSG_KEY, "s"),
132             "18:15: " + getCheckMessage(MSG_KEY, "x"),
133             "18:22: " + getCheckMessage(MSG_KEY, "l"),
134             "18: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 }