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              TokenTypes.COMPACT_COMPILATION_UNIT,
65          };
66  
67          assertWithMessage("Default acceptable tokens are invalid")
68              .that(actual)
69              .isEqualTo(expected);
70      }
71  
72      @Test
73      public void testDefaultsInnerClass() throws Exception {
74  
75          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
76  
77          verifyWithInlineConfigParser(
78                  getPath("InputMethodCountDefaultsInnerClass.java"), expected);
79      }
80  
81      @Test
82      public void testDefaultsInnerInterface() throws Exception {
83  
84          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
85  
86          verifyWithInlineConfigParser(
87                  getPath("InputMethodCountDefaultsInnerInterface.java"), expected);
88      }
89  
90      @Test
91      public void testDefaultsAllModifiers() throws Exception {
92  
93          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
94  
95          verifyWithInlineConfigParser(
96                  getPath("InputMethodCountDefaultsAllModifiers.java"), expected);
97      }
98  
99      @Test
100     public void testThreesOne() throws Exception {
101 
102         final String[] expected = {
103             "15:1: " + getCheckMessage(MSG_PACKAGE_METHODS, 4, 3),
104             "15:1: " + getCheckMessage(MSG_PRIVATE_METHODS, 4, 3),
105             "15:1: " + getCheckMessage(MSG_PROTECTED_METHODS, 4, 3),
106             "15:1: " + getCheckMessage(MSG_PUBLIC_METHODS, 4, 3),
107             "15:1: " + getCheckMessage(MSG_MANY_METHODS, 16, 3),
108         };
109 
110         verifyWithInlineConfigParser(
111                 getPath("InputMethodCount1One.java"), expected);
112     }
113 
114     @Test
115     public void testThreesTwo() throws Exception {
116 
117         final String[] expected = {
118             "14:1: " + getCheckMessage(MSG_PACKAGE_METHODS, 4, 3),
119             "14:1: " + getCheckMessage(MSG_PRIVATE_METHODS, 4, 3),
120             "14:1: " + getCheckMessage(MSG_MANY_METHODS, 8, 3),
121             "20:3: " + getCheckMessage(MSG_PROTECTED_METHODS, 4, 3),
122             "20:3: " + getCheckMessage(MSG_MANY_METHODS, 4, 3),
123             "50:3: " + getCheckMessage(MSG_PUBLIC_METHODS, 4, 3),
124             "50:3: " + getCheckMessage(MSG_MANY_METHODS, 4, 3),
125         };
126 
127         verifyWithInlineConfigParser(
128                 getPath("InputMethodCount1Two.java"), expected);
129     }
130 
131     @Test
132     public void testEnum() throws Exception {
133 
134         final String[] expected = {
135             "21:5: " + getCheckMessage(MSG_PRIVATE_METHODS, 1, 0),
136             "21:5: " + getCheckMessage(MSG_MANY_METHODS, 3, 2),
137         };
138 
139         verifyWithInlineConfigParser(
140                 getPath("InputMethodCount2.java"), expected);
141     }
142 
143     @Test
144     public void testWithPackageModifier() throws Exception {
145 
146         final String[] expected = {
147             "15:1: " + getCheckMessage(MSG_MANY_METHODS, 5, 2),
148         };
149 
150         verifyWithInlineConfigParser(
151                 getPath("InputMethodCount3.java"), expected);
152     }
153 
154     @Test
155     public void testOnInterfaceDefinitionWithField() throws Exception {
156 
157         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
158 
159         verifyWithInlineConfigParser(
160                 getPath("InputMethodCount4.java"), expected);
161     }
162 
163     @Test
164     public void testWithInterfaceDefinitionInClass() throws Exception {
165 
166         final String[] expected = {
167             "15:1: " + getCheckMessage(MSG_MANY_METHODS, 2, 1),
168         };
169 
170         verifyWithInlineConfigParser(
171                 getPath("InputMethodCount5.java"), expected);
172     }
173 
174     @Test
175     public void testPartialTokens() throws Exception {
176 
177         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
178 
179         verifyWithInlineConfigParser(
180                 getPath("InputMethodCount6.java"), expected);
181     }
182 
183     @Test
184     public void testCountMethodToCorrectDefinition() throws Exception {
185 
186         final String[] expected = {
187             "22:5: " + getCheckMessage(MSG_MANY_METHODS, 2, 1),
188         };
189 
190         verifyWithInlineConfigParser(
191                 getPath("InputMethodCount7.java"), expected);
192     }
193 
194     @Test
195     public void testInterfaceMemberScopeIsPublic() throws Exception {
196 
197         final String[] expected = {
198             "17:5: " + getCheckMessage(MSG_PUBLIC_METHODS, 2, 1),
199             "27:5: " + getCheckMessage(MSG_PUBLIC_METHODS, 2, 1),
200         };
201 
202         verifyWithInlineConfigParser(
203                 getPath("InputMethodCountInterfaceMemberScopeIsPublic.java"),
204                 expected);
205     }
206 
207     @Test
208     public void testMethodCountRecords() throws Exception {
209         final int max = 2;
210 
211         final String[] expected = {
212             "18:5: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
213             "50:13: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
214             "68:5: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
215             "78:9: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
216             "87:13: " + getCheckMessage(MSG_MANY_METHODS, 4, max),
217             "99:21: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
218         };
219 
220         verifyWithInlineConfigParser(
221                 getPath("InputMethodCountRecords.java"), expected);
222     }
223 
224     @Test
225     public void testCompactSourceFile() throws Exception {
226         final String[] expected = {
227             "15:1: " + getCheckMessage(MSG_MANY_METHODS, 5, 3),
228         };
229 
230         verifyWithInlineConfigParser(
231                 getNonCompilablePath("InputMethodCountCompactSourceFile.java"), expected);
232     }
233 
234     @Test
235     public void testCompactSourceFileModifiers() throws Exception {
236         final String[] expected = {
237             "19:1: " + getCheckMessage(MSG_PACKAGE_METHODS, 2, 1),
238             "19:1: " + getCheckMessage(MSG_PRIVATE_METHODS, 1, 0),
239             "19:1: " + getCheckMessage(MSG_PROTECTED_METHODS, 1, 0),
240             "19:1: " + getCheckMessage(MSG_PUBLIC_METHODS, 1, 0),
241         };
242 
243         verifyWithInlineConfigParser(
244                 getNonCompilablePath("InputMethodCountCompactSourceFileModifiers.java"), expected);
245     }
246 
247     @Test
248     public void testCompactSourceFileWithType() throws Exception {
249         final String[] expected = {
250             "15:1: " + getCheckMessage(MSG_MANY_METHODS, 3, 2),
251             "19:1: " + getCheckMessage(MSG_MANY_METHODS, 3, 2),
252         };
253 
254         verifyWithInlineConfigParser(
255                 getNonCompilablePath("InputMethodCountCompactSourceFileWithType.java"), expected);
256     }
257 
258     @Test
259     public void testCompactSourceFileInnerClasses() throws Exception {
260         final String[] expected = {
261             "15:1: " + getCheckMessage(MSG_MANY_METHODS, 2, 1),
262             "19:5: " + getCheckMessage(MSG_MANY_METHODS, 2, 1),
263         };
264 
265         verifyWithInlineConfigParser(
266                 getNonCompilablePath(
267                         "InputMethodCountCompactSourceFileInnerClasses.java"), expected);
268     }
269 
270     @Test
271     public void testCompactSourceFileValid() throws Exception {
272         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
273 
274         verifyWithInlineConfigParser(
275                 getNonCompilablePath("InputMethodCountCompactSourceFileValid.java"), expected);
276     }
277 
278     @Test
279     public void testCompactSourceFileFieldInitializer() throws Exception {
280         final String[] expected = {
281             "16:1: " + getCheckMessage(MSG_MANY_METHODS, 2, 1),
282         };
283 
284         verifyWithInlineConfigParser(
285                 getNonCompilablePath(
286                         "InputMethodCountCompactSourceFileFieldInitializer.java"), expected);
287     }
288 
289     @Test
290     public void testCompactSourceFileStatic() throws Exception {
291         final String[] expected = {
292             "15:1: " + getCheckMessage(MSG_PACKAGE_METHODS, 3, 1),
293         };
294 
295         verifyWithInlineConfigParser(
296                 getNonCompilablePath("InputMethodCountCompactSourceFileStatic.java"), expected);
297     }
298 
299     @Test
300     public void testEmptyFile() throws Exception {
301         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
302 
303         verifyWithInlineConfigParser(
304                 getPath("InputMethodCountEmpty.java"), expected);
305     }
306 
307 }