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.MethodCountCheck.MSG_MANY_METHODS;
23  import static com.puppycrawl.tools.checkstyle.checks.sizes.MethodCountCheck.MSG_PACKAGE_METHODS;
24  import static com.puppycrawl.tools.checkstyle.checks.sizes.MethodCountCheck.MSG_PRIVATE_METHODS;
25  import static com.puppycrawl.tools.checkstyle.checks.sizes.MethodCountCheck.MSG_PROTECTED_METHODS;
26  import static com.puppycrawl.tools.checkstyle.checks.sizes.MethodCountCheck.MSG_PUBLIC_METHODS;
27  import static org.junit.Assert.assertArrayEquals;
28  
29  import org.junit.Test;
30  
31  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
33  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
34  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
35  
36  public class MethodCountCheckTest extends AbstractModuleTestSupport {
37  
38      @Override
39      protected String getPackageLocation() {
40          return "com/puppycrawl/tools/checkstyle/checks/sizes/methodcount";
41      }
42  
43      @Test
44      public void testGetRequiredTokens() {
45          final MethodCountCheck checkObj = new MethodCountCheck();
46          final int[] expected = {TokenTypes.METHOD_DEF};
47          assertArrayEquals("Default required tokens are invalid",
48              expected, checkObj.getRequiredTokens());
49      }
50  
51      @Test
52      public void testGetAcceptableTokens() {
53          final MethodCountCheck methodCountCheckObj =
54              new MethodCountCheck();
55          final int[] actual = methodCountCheckObj.getAcceptableTokens();
56          final int[] expected = {
57              TokenTypes.CLASS_DEF,
58              TokenTypes.ENUM_CONSTANT_DEF,
59              TokenTypes.ENUM_DEF,
60              TokenTypes.INTERFACE_DEF,
61              TokenTypes.ANNOTATION_DEF,
62              TokenTypes.METHOD_DEF,
63          };
64  
65          assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
66      }
67  
68      @Test
69      public void testDefaults() throws Exception {
70          final DefaultConfiguration checkConfig =
71              createModuleConfig(MethodCountCheck.class);
72  
73          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
74  
75          verify(checkConfig, getPath("InputMethodCount.java"), expected);
76      }
77  
78      @Test
79      public void testThrees() throws Exception {
80          final DefaultConfiguration checkConfig =
81              createModuleConfig(MethodCountCheck.class);
82          checkConfig.addAttribute("maxPrivate", "3");
83          checkConfig.addAttribute("maxPackage", "3");
84          checkConfig.addAttribute("maxProtected", "3");
85          checkConfig.addAttribute("maxPublic", "3");
86          checkConfig.addAttribute("maxTotal", "3");
87  
88          final String[] expected = {
89              "3: " + getCheckMessage(MSG_PACKAGE_METHODS, 5, 3),
90              "3: " + getCheckMessage(MSG_PRIVATE_METHODS, 5, 3),
91              "3: " + getCheckMessage(MSG_PROTECTED_METHODS, 5, 3),
92              "3: " + getCheckMessage(MSG_PUBLIC_METHODS, 5, 3),
93              "3: " + getCheckMessage(MSG_MANY_METHODS, 20, 3),
94              "9: " + getCheckMessage(MSG_PUBLIC_METHODS, 5, 3),
95              "9: " + getCheckMessage(MSG_MANY_METHODS, 5, 3),
96              "45: " + getCheckMessage(MSG_PUBLIC_METHODS, 5, 3),
97              "45: " + getCheckMessage(MSG_MANY_METHODS, 5, 3),
98          };
99  
100         verify(checkConfig, getPath("InputMethodCount.java"), expected);
101     }
102 
103     @Test
104     public void testEnum() throws Exception {
105         final DefaultConfiguration checkConfig =
106             createModuleConfig(MethodCountCheck.class);
107         checkConfig.addAttribute("maxPrivate", "0");
108         checkConfig.addAttribute("maxTotal", "2");
109 
110         final String[] expected = {
111             "9: " + getCheckMessage(MSG_PRIVATE_METHODS, 1, 0),
112             "9: " + getCheckMessage(MSG_MANY_METHODS, 3, 2),
113         };
114 
115         verify(checkConfig, getPath("InputMethodCount2.java"), expected);
116     }
117 
118     @Test
119     public void testWithPackageModifier() throws Exception {
120         final DefaultConfiguration checkConfig = createModuleConfig(MethodCountCheck.class);
121         checkConfig.addAttribute("maxPrivate", "0");
122         checkConfig.addAttribute("maxTotal", "2");
123 
124         final String[] expected = {
125             "3: " + getCheckMessage(MSG_MANY_METHODS, 5, 2),
126         };
127 
128         verify(checkConfig, getPath("InputMethodCount3.java"), expected);
129     }
130 
131     @Test
132     public void testOnInterfaceDefinitionWithField() throws Exception {
133         final DefaultConfiguration checkConfig =
134                 createModuleConfig(MethodCountCheck.class);
135 
136         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
137 
138         verify(checkConfig, getPath("InputMethodCount4.java"), expected);
139     }
140 
141     @Test
142     public void testWithInterfaceDefinitionInClass() throws Exception {
143         final DefaultConfiguration checkConfig = createModuleConfig(MethodCountCheck.class);
144         checkConfig.addAttribute("maxTotal", "1");
145 
146         final String[] expected = {
147             "1: " + getCheckMessage(MSG_MANY_METHODS, 2, 1),
148         };
149 
150         verify(checkConfig, getPath("InputMethodCount5.java"), expected);
151     }
152 
153     @Test
154     public void testPartialTokens() throws Exception {
155         final DefaultConfiguration checkConfig = createModuleConfig(MethodCountCheck.class);
156         checkConfig.addAttribute("maxTotal", "1");
157         checkConfig.addAttribute("tokens", "ENUM_DEF");
158 
159         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
160 
161         verify(checkConfig, getPath("InputMethodCount6.java"), expected);
162     }
163 
164     @Test
165     public void testCountMethodToCorrectDefinition() throws Exception {
166         final DefaultConfiguration checkConfig = createModuleConfig(MethodCountCheck.class);
167         checkConfig.addAttribute("maxTotal", "1");
168         checkConfig.addAttribute("tokens", "ENUM_DEF");
169 
170         final String[] expected = {
171             "8: " + getCheckMessage(MSG_MANY_METHODS, 2, 1),
172         };
173 
174         verify(checkConfig, getPath("InputMethodCount7.java"), expected);
175     }
176 
177 }