View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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  
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/sizes/parameternumber";
38      }
39  
40      @Test
41      public void testGetRequiredTokens() {
42          final ParameterNumberCheck checkObj = new ParameterNumberCheck();
43          assertArrayEquals(
44              "ParameterNumberCheck#getRequiredTokens should return empty array by default",
45              CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
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          assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
59      }
60  
61      @Test
62      public void testDefault()
63              throws Exception {
64          final DefaultConfiguration checkConfig =
65              createModuleConfig(ParameterNumberCheck.class);
66          final String[] expected = {
67              "194:10: " + getCheckMessage(MSG_KEY, 7, 9),
68          };
69          verify(checkConfig, getPath("InputParameterNumberSimple.java"), expected);
70      }
71  
72      @Test
73      public void testNum()
74              throws Exception {
75          final DefaultConfiguration checkConfig =
76              createModuleConfig(ParameterNumberCheck.class);
77          checkConfig.addAttribute("max", "2");
78          final String[] expected = {
79              "71:9: " + getCheckMessage(MSG_KEY, 2, 3),
80              "194:10: " + getCheckMessage(MSG_KEY, 2, 9),
81          };
82          verify(checkConfig, getPath("InputParameterNumberSimple.java"), expected);
83      }
84  
85      @Test
86      public void testMaxParam()
87              throws Exception {
88          final DefaultConfiguration checkConfig =
89                  createModuleConfig(ParameterNumberCheck.class);
90          checkConfig.addAttribute("max", "9");
91          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
92          verify(checkConfig, getPath("InputParameterNumberSimple.java"), expected);
93      }
94  
95      @Test
96      public void shouldLogActualParameterNumber()
97              throws Exception {
98          final DefaultConfiguration checkConfig =
99              createModuleConfig(ParameterNumberCheck.class);
100         checkConfig.addMessage("maxParam", "{0},{1}");
101         final String[] expected = {
102             "194:10: 7,9",
103         };
104         verify(checkConfig, getPath("InputParameterNumberSimple.java"), expected);
105     }
106 
107     @Test
108     public void shouldIgnoreMethodsWithOverrideAnnotation()
109             throws Exception {
110         final DefaultConfiguration checkConfig =
111                 createModuleConfig(ParameterNumberCheck.class);
112         checkConfig.addAttribute("ignoreOverriddenMethods", "true");
113         final String[] expected = {
114             "6:10: " + getCheckMessage(MSG_KEY, 7, 8),
115             "11:10: " + getCheckMessage(MSG_KEY, 7, 8),
116         };
117         verify(checkConfig, getPath("InputParameterNumber.java"), expected);
118     }
119 
120 }