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.sizes;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck.MSG_KEY;
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.utils.CommonUtil;
30  
31  public class ParameterNumberCheckTest
32      extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/sizes/parameternumber";
37      }
38  
39      @Test
40      public void testGetRequiredTokens() {
41          final ParameterNumberCheck checkObj = new ParameterNumberCheck();
42          assertWithMessage("ParameterNumberCheck#getRequiredTokens should return empty array "
43                  + "by default")
44              .that(checkObj.getRequiredTokens())
45              .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
46      }
47  
48      @Test
49      public void testGetAcceptableTokens() {
50          final ParameterNumberCheck paramNumberCheckObj =
51              new ParameterNumberCheck();
52          final int[] actual = paramNumberCheckObj.getAcceptableTokens();
53          final int[] expected = {
54              TokenTypes.METHOD_DEF,
55              TokenTypes.CTOR_DEF,
56          };
57  
58          assertWithMessage("Default acceptable tokens are invalid")
59              .that(actual)
60              .isEqualTo(expected);
61      }
62  
63      @Test
64      public void testDefaultOne()
65              throws Exception {
66          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
67          verifyWithInlineConfigParser(
68                  getPath("InputParameterNumberSimpleOne.java"), expected);
69      }
70  
71      @Test
72      public void testDefaultTwo()
73              throws Exception {
74          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
75          verifyWithInlineConfigParser(
76                  getPath("InputParameterNumberSimpleTwo.java"), expected);
77      }
78  
79      @Test
80      public void testDefaultThree()
81              throws Exception {
82          final String[] expected = {
83              "47:10: " + getCheckMessage(MSG_KEY, 7, 9),
84          };
85          verifyWithInlineConfigParser(
86                  getPath("InputParameterNumberSimpleThree.java"), expected);
87      }
88  
89      @Test
90      public void testNum()
91              throws Exception {
92          final String[] expected = {
93              "75:9: " + getCheckMessage(MSG_KEY, 2, 3),
94              "198:10: " + getCheckMessage(MSG_KEY, 2, 9),
95          };
96          verifyWithInlineConfigParser(
97                  getPath("InputParameterNumberSimple2.java"), expected);
98      }
99  
100     @Test
101     public void testMaxParam()
102             throws Exception {
103         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
104         verifyWithInlineConfigParser(
105                 getPath("InputParameterNumberSimple3.java"), expected);
106     }
107 
108     @Test
109     public void shouldLogActualParameterNumber()
110             throws Exception {
111         final String[] expected = {
112             "199:10: 7,9",
113         };
114         verifyWithInlineConfigParser(
115                 getPath("InputParameterNumberSimple4.java"), expected);
116     }
117 
118     @Test
119     public void testIgnoreOverriddenMethods()
120             throws Exception {
121         final String[] expected = {
122             "15:10: " + getCheckMessage(MSG_KEY, 7, 8),
123             "20:10: " + getCheckMessage(MSG_KEY, 7, 8),
124         };
125         verifyWithInlineConfigParser(
126                 getPath("InputParameterNumber.java"), expected);
127     }
128 
129     @Test
130     public void testIgnoreOverriddenMethodsFalse()
131             throws Exception {
132         final String[] expected = {
133             "15:10: " + getCheckMessage(MSG_KEY, 7, 8),
134             "20:10: " + getCheckMessage(MSG_KEY, 7, 8),
135             "28:10: " + getCheckMessage(MSG_KEY, 7, 8),
136             "33:10: " + getCheckMessage(MSG_KEY, 7, 8),
137         };
138         verifyWithInlineConfigParser(
139                 getPath("InputParameterNumber2.java"), expected);
140     }
141 
142     @Test
143     public void testIgnoreAnnotatedBy() throws Exception {
144         final String[] expected = {
145             "23:10: " + getCheckMessage(MSG_KEY, 2, 3),
146             "30:10: " + getCheckMessage(MSG_KEY, 2, 4),
147             "35:14: " + getCheckMessage(MSG_KEY, 2, 3),
148             "43:9: " + getCheckMessage(MSG_KEY, 2, 4),
149             "58:30: " + getCheckMessage(MSG_KEY, 2, 3),
150             "62:29: " + getCheckMessage(MSG_KEY, 2, 3),
151             "77:34: " + getCheckMessage(MSG_KEY, 2, 4),
152             "97:10: " + getCheckMessage(MSG_KEY, 2, 3),
153             "106:14: " + getCheckMessage(MSG_KEY, 2, 3),
154         };
155         verifyWithInlineConfigParser(
156                 getPath("InputParameterNumberIgnoreAnnotatedBy.java"), expected);
157     }
158 
159     @Test
160     public void testIgnoreAnnotatedByFullyQualifiedClassName() throws Exception {
161         final String[] expected = {
162             "15:10: " + getCheckMessage(MSG_KEY, 2, 3),
163             "17:10: " + getCheckMessage(MSG_KEY, 2, 3),
164             "23:10: " + getCheckMessage(MSG_KEY, 2, 3),
165             "27:10: " + getCheckMessage(MSG_KEY, 2, 3),
166             "41:14: " + getCheckMessage(MSG_KEY, 2, 3),
167             "45:14: " + getCheckMessage(MSG_KEY, 2, 3),
168         };
169         verifyWithInlineConfigParser(
170                 getPath("InputParameterNumberIgnoreAnnotatedByFullyQualifiedClassName.java"),
171                 expected);
172     }
173 }