View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
24  
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
31  
32  public class ParameterNumberCheckTest
33      extends AbstractModuleTestSupport {
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          assertArrayEquals(
43              "ParameterNumberCheck#getRequiredTockens should return empty array by default",
44              CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
45      }
46  
47      @Test
48      public void testGetAcceptableTokens() {
49          final ParameterNumberCheck paramNumberCheckObj =
50              new ParameterNumberCheck();
51          final int[] actual = paramNumberCheckObj.getAcceptableTokens();
52          final int[] expected = {
53              TokenTypes.METHOD_DEF,
54              TokenTypes.CTOR_DEF,
55          };
56  
57          assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
58      }
59  
60      @Test
61      public void testDefault()
62              throws Exception {
63          final DefaultConfiguration checkConfig =
64              createModuleConfig(ParameterNumberCheck.class);
65          final String[] expected = {
66              "194:10: " + getCheckMessage(MSG_KEY, 7, 9),
67          };
68          verify(checkConfig, getPath("InputParameterNumberSimple.java"), expected);
69      }
70  
71      @Test
72      public void testNum()
73              throws Exception {
74          final DefaultConfiguration checkConfig =
75              createModuleConfig(ParameterNumberCheck.class);
76          checkConfig.addAttribute("max", "2");
77          final String[] expected = {
78              "71:9: " + getCheckMessage(MSG_KEY, 2, 3),
79              "194:10: " + getCheckMessage(MSG_KEY, 2, 9),
80          };
81          verify(checkConfig, getPath("InputParameterNumberSimple.java"), expected);
82      }
83  
84      @Test
85      public void testMaxParam()
86              throws Exception {
87          final DefaultConfiguration checkConfig =
88                  createModuleConfig(ParameterNumberCheck.class);
89          checkConfig.addAttribute("max", "9");
90          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
91          verify(checkConfig, getPath("InputParameterNumberSimple.java"), expected);
92      }
93  
94      @Test
95      public void shouldLogActualParameterNumber()
96              throws Exception {
97          final DefaultConfiguration checkConfig =
98              createModuleConfig(ParameterNumberCheck.class);
99          checkConfig.addMessage("maxParam", "{0},{1}");
100         final String[] expected = {
101             "194:10: 7,9",
102         };
103         verify(checkConfig, getPath("InputParameterNumberSimple.java"), expected);
104     }
105 
106     @Test
107     public void shouldIgnoreMethodsWithOverrideAnnotation()
108             throws Exception {
109         final DefaultConfiguration checkConfig =
110                 createModuleConfig(ParameterNumberCheck.class);
111         checkConfig.addAttribute("ignoreOverriddenMethods", "true");
112         final String[] expected = {
113             "6:10: " + getCheckMessage(MSG_KEY, 7, 8),
114             "11:10: " + getCheckMessage(MSG_KEY, 7, 8),
115         };
116         verify(checkConfig, getPath("InputParameterNumber.java"), expected);
117     }
118 }