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