View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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      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          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 testDefaults() throws Exception {
73  
74          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
75  
76          verifyWithInlineConfigParser(
77                  getPath("InputMethodCount.java"), expected);
78      }
79  
80      @Test
81      public void testThreesOne() throws Exception {
82  
83          final String[] expected = {
84              "15:1: " + getCheckMessage(MSG_PACKAGE_METHODS, 4, 3),
85              "15:1: " + getCheckMessage(MSG_PRIVATE_METHODS, 4, 3),
86              "15:1: " + getCheckMessage(MSG_PROTECTED_METHODS, 4, 3),
87              "15:1: " + getCheckMessage(MSG_PUBLIC_METHODS, 4, 3),
88              "15:1: " + getCheckMessage(MSG_MANY_METHODS, 16, 3),
89          };
90  
91          verifyWithInlineConfigParser(
92                  getPath("InputMethodCount1One.java"), expected);
93      }
94  
95      @Test
96      public void testThreesTwo() throws Exception {
97  
98          final String[] expected = {
99              "14:1: " + getCheckMessage(MSG_PACKAGE_METHODS, 4, 3),
100             "14:1: " + getCheckMessage(MSG_PRIVATE_METHODS, 4, 3),
101             "14:1: " + getCheckMessage(MSG_MANY_METHODS, 8, 3),
102             "20:3: " + getCheckMessage(MSG_PROTECTED_METHODS, 4, 3),
103             "20:3: " + getCheckMessage(MSG_MANY_METHODS, 4, 3),
104             "50:3: " + getCheckMessage(MSG_PUBLIC_METHODS, 4, 3),
105             "50:3: " + getCheckMessage(MSG_MANY_METHODS, 4, 3),
106         };
107 
108         verifyWithInlineConfigParser(
109                 getPath("InputMethodCount1Two.java"), expected);
110     }
111 
112     @Test
113     public void testEnum() throws Exception {
114 
115         final String[] expected = {
116             "21:5: " + getCheckMessage(MSG_PRIVATE_METHODS, 1, 0),
117             "21:5: " + getCheckMessage(MSG_MANY_METHODS, 3, 2),
118         };
119 
120         verifyWithInlineConfigParser(
121                 getPath("InputMethodCount2.java"), expected);
122     }
123 
124     @Test
125     public void testWithPackageModifier() throws Exception {
126 
127         final String[] expected = {
128             "15:1: " + getCheckMessage(MSG_MANY_METHODS, 5, 2),
129         };
130 
131         verifyWithInlineConfigParser(
132                 getPath("InputMethodCount3.java"), expected);
133     }
134 
135     @Test
136     public void testOnInterfaceDefinitionWithField() throws Exception {
137 
138         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
139 
140         verifyWithInlineConfigParser(
141                 getPath("InputMethodCount4.java"), expected);
142     }
143 
144     @Test
145     public void testWithInterfaceDefinitionInClass() throws Exception {
146 
147         final String[] expected = {
148             "15:1: " + getCheckMessage(MSG_MANY_METHODS, 2, 1),
149         };
150 
151         verifyWithInlineConfigParser(
152                 getPath("InputMethodCount5.java"), expected);
153     }
154 
155     @Test
156     public void testPartialTokens() throws Exception {
157 
158         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
159 
160         verifyWithInlineConfigParser(
161                 getPath("InputMethodCount6.java"), expected);
162     }
163 
164     @Test
165     public void testCountMethodToCorrectDefinition() throws Exception {
166 
167         final String[] expected = {
168             "22:5: " + getCheckMessage(MSG_MANY_METHODS, 2, 1),
169         };
170 
171         verifyWithInlineConfigParser(
172                 getPath("InputMethodCount7.java"), expected);
173     }
174 
175     @Test
176     public void testInterfaceMemberScopeIsPublic() throws Exception {
177 
178         final String[] expected = {
179             "17:5: " + getCheckMessage(MSG_PUBLIC_METHODS, 2, 1),
180             "27:5: " + getCheckMessage(MSG_PUBLIC_METHODS, 2, 1),
181         };
182 
183         verifyWithInlineConfigParser(
184                 getPath("InputMethodCountInterfaceMemberScopeIsPublic.java"),
185                 expected);
186     }
187 
188     @Test
189     public void testMethodCountRecords() throws Exception {
190         final int max = 2;
191 
192         final String[] expected = {
193             "18:5: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
194             "50:13: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
195             "68:5: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
196             "78:9: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
197             "87:13: " + getCheckMessage(MSG_MANY_METHODS, 4, max),
198             "99:21: " + getCheckMessage(MSG_MANY_METHODS, 3, max),
199         };
200 
201         verifyWithInlineConfigParser(
202                 getNonCompilablePath("InputMethodCountRecords.java"), expected);
203     }
204 
205 }