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.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 testDefault()
65              throws Exception {
66          final String[] expected = {
67              "198:10: " + getCheckMessage(MSG_KEY, 7, 9),
68          };
69          verifyWithInlineConfigParser(
70                  getPath("InputParameterNumberSimple.java"), expected);
71      }
72  
73      @Test
74      public void testNum()
75              throws Exception {
76          final String[] expected = {
77              "75:9: " + getCheckMessage(MSG_KEY, 2, 3),
78              "198:10: " + getCheckMessage(MSG_KEY, 2, 9),
79          };
80          verifyWithInlineConfigParser(
81                  getPath("InputParameterNumberSimple2.java"), expected);
82      }
83  
84      @Test
85      public void testMaxParam()
86              throws Exception {
87          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
88          verifyWithInlineConfigParser(
89                  getPath("InputParameterNumberSimple3.java"), expected);
90      }
91  
92      @Test
93      public void shouldLogActualParameterNumber()
94              throws Exception {
95          final String[] expected = {
96              "199:10: 7,9",
97          };
98          verifyWithInlineConfigParser(
99                  getPath("InputParameterNumberSimple4.java"), expected);
100     }
101 
102     @Test
103     public void testIgnoreOverriddenMethods()
104             throws Exception {
105         final String[] expected = {
106             "15:10: " + getCheckMessage(MSG_KEY, 7, 8),
107             "20:10: " + getCheckMessage(MSG_KEY, 7, 8),
108         };
109         verifyWithInlineConfigParser(
110                 getPath("InputParameterNumber.java"), expected);
111     }
112 
113     @Test
114     public void testIgnoreOverriddenMethodsFalse()
115             throws Exception {
116         final String[] expected = {
117             "15:10: " + getCheckMessage(MSG_KEY, 7, 8),
118             "20:10: " + getCheckMessage(MSG_KEY, 7, 8),
119             "28:10: " + getCheckMessage(MSG_KEY, 7, 8),
120             "33:10: " + getCheckMessage(MSG_KEY, 7, 8),
121         };
122         verifyWithInlineConfigParser(
123                 getPath("InputParameterNumber2.java"), expected);
124     }
125 
126 }