View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.MethodCountCheck.MSG_MANY_METHODS;
24  import static com.puppycrawl.tools.checkstyle.checks.sizes.MethodCountCheck.MSG_PACKAGE_METHODS;
25  import static com.puppycrawl.tools.checkstyle.checks.sizes.MethodCountCheck.MSG_PRIVATE_METHODS;
26  import static com.puppycrawl.tools.checkstyle.checks.sizes.MethodCountCheck.MSG_PROTECTED_METHODS;
27  import static com.puppycrawl.tools.checkstyle.checks.sizes.MethodCountCheck.MSG_PUBLIC_METHODS;
28  
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
34  
35  public class MethodCountCheckTest extends AbstractModuleTestSupport {
36  
37      @Override
38      public 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          assertWithMessage("Default required tokens are invalid")
47              .that(checkObj.getRequiredTokens())
48              .isEqualTo(expected);
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              TokenTypes.RECORD_DEF,
64          };
65  
66          assertWithMessage("Default acceptable tokens are invalid")
67              .that(actual)
68              .isEqualTo(expected);
69      }
70  
71      @Test
72      public void testDefaultsInnerClass() throws Exception {
73  
74          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
75  
76          verifyWithInlineConfigParser(
77                  getPath("InputMethodCountDefaultsInnerClass.java"), expected);
78      }
79  
80      @Test
81      public void testDefaultsInnerInterface() throws Exception {
82  
83          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
84  
85          verifyWithInlineConfigParser(
86                  getPath("InputMethodCountDefaultsInnerInterface.java"), expected);
87      }
88  
89      @Test
90      public void testDefaultsAllModifiers() throws Exception {
91  
92          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
93  
94          verifyWithInlineConfigParser(
95                  getPath("InputMethodCountDefaultsAllModifiers.java"), expected);
96      }
97  
98      @Test
99      public void testThreesOne() throws Exception {
100 
101         final String[] expected = {
102             "15:1: " + getCheckMessage(MSG_PACKAGE_METHODS, 4, 3),
103             "15:1: " + getCheckMessage(MSG_PRIVATE_METHODS, 4, 3),
104             "15:1: " + getCheckMessage(MSG_PROTECTED_METHODS, 4, 3),
105             "15:1: " + getCheckMessage(MSG_PUBLIC_METHODS, 4, 3),
106             "15:1: " + getCheckMessage(MSG_MANY_METHODS, 16, 3),
107         };
108 
109         verifyWithInlineConfigParser(
110                 getPath("InputMethodCount1One.java"), expected);
111     }
112 
113     @Test
114     public void testThreesTwo() throws Exception {
115 
116         final String[] expected = {
117             "14:1: " + getCheckMessage(MSG_PACKAGE_METHODS, 4, 3),
118             "14:1: " + getCheckMessage(MSG_PRIVATE_METHODS, 4, 3),
119             "14:1: " + getCheckMessage(MSG_MANY_METHODS, 8, 3),
120             "20:3: " + getCheckMessage(MSG_PROTECTED_METHODS, 4, 3),
121             "20:3: " + getCheckMessage(MSG_MANY_METHODS, 4, 3),
122             "50:3: " + getCheckMessage(MSG_PUBLIC_METHODS, 4, 3),
123             "50:3: " + getCheckMessage(MSG_MANY_METHODS, 4, 3),
124         };
125 
126         verifyWithInlineConfigParser(
127                 getPath("InputMethodCount1Two.java"), expected);
128     }
129 
130     @Test
131     public void testEnum() throws Exception {
132 
133         final String[] expected = {
134             "21:5: " + getCheckMessage(MSG_PRIVATE_METHODS, 1, 0),
135             "21:5: " + getCheckMessage(MSG_MANY_METHODS, 3, 2),
136         };
137 
138         verifyWithInlineConfigParser(
139                 getPath("InputMethodCount2.java"), expected);
140     }
141 
142     @Test
143     public void testWithPackageModifier() throws Exception {
144 
145         final String[] expected = {
146             "15:1: " + getCheckMessage(MSG_MANY_METHODS, 5, 2),
147         };
148 
149         verifyWithInlineConfigParser(
150                 getPath("InputMethodCount3.java"), expected);
151     }
152 
153     @Test
154     public void testOnInterfaceDefinitionWithField() throws Exception {
155 
156         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
157 
158         verifyWithInlineConfigParser(
159                 getPath("InputMethodCount4.java"), expected);
160     }
161 
162     @Test
163     public void testWithInterfaceDefinitionInClass() throws Exception {
164 
165         final String[] expected = {
166             "15:1: " + getCheckMessage(MSG_MANY_METHODS, 2, 1),
167         };
168 
169         verifyWithInlineConfigParser(
170                 getPath("InputMethodCount5.java"), expected);
171     }
172 
173     @Test
174     public void testPartialTokens() throws Exception {
175 
176         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
177 
178         verifyWithInlineConfigParser(
179                 getPath("InputMethodCount6.java"), expected);
180     }
181 
182     @Test
183     public void testCountMethodToCorrectDefinition() throws Exception {
184 
185         final String[] expected = {
186             "22:5: " + getCheckMessage(MSG_MANY_METHODS, 2, 1),
187         };
188 
189         verifyWithInlineConfigParser(
190                 getPath("InputMethodCount7.java"), expected);
191     }
192 
193     @Test
194     public void testInterfaceMemberScopeIsPublic() throws Exception {
195 
196         final String[] expected = {
197             "17:5: " + getCheckMessage(MSG_PUBLIC_METHODS, 2, 1),
198             "27:5: " + getCheckMessage(MSG_PUBLIC_METHODS, 2, 1),
199         };
200 
201         verifyWithInlineConfigParser(
202                 getPath("InputMethodCountInterfaceMemberScopeIsPublic.java"),
203                 expected);
204     }
205 
206     @Test
207     public void testMethodCountRecords() throws Exception {
208         final int max = 2;
209 
210         final String[] expected = {
211             "18:5: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
212             "50:13: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
213             "68:5: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
214             "78:9: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
215             "87:13: " + getCheckMessage(MSG_MANY_METHODS, 4, max),
216             "99:21: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
217         };
218 
219         verifyWithInlineConfigParser(
220                 getPath("InputMethodCountRecords.java"), expected);
221     }
222 
223 }