View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.naming;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
24  import static com.puppycrawl.tools.checkstyle.checks.naming.MethodNameCheck.MSG_KEY;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31  
32  public class MethodNameCheckTest
33      extends AbstractModuleTestSupport {
34  
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/naming/methodname";
38      }
39  
40      @Test
41      public void testGetRequiredTokens() {
42          final MethodNameCheck checkObj = new MethodNameCheck();
43          final int[] expected = {TokenTypes.METHOD_DEF};
44          assertWithMessage("Default required tokens are invalid")
45              .that(checkObj.getRequiredTokens())
46              .isEqualTo(expected);
47      }
48  
49      @Test
50      public void testDefaultOne()
51              throws Exception {
52  
53          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
54          verifyWithInlineConfigParser(
55                  getPath("InputMethodNameSimpleOne.java"), expected);
56      }
57  
58      @Test
59      public void testDefaultTwo()
60              throws Exception {
61  
62          final String pattern = "^[a-z][a-zA-Z0-9]*$";
63  
64          final String[] expected = {
65              "61:10: " + getCheckMessage(MSG_INVALID_PATTERN, "ALL_UPPERCASE_METHOD", pattern),
66          };
67          verifyWithInlineConfigParser(
68                  getPath("InputMethodNameSimpleTwo.java"), expected);
69      }
70  
71      @Test
72      public void testDefaultThree()
73              throws Exception {
74  
75          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
76          verifyWithInlineConfigParser(
77                  getPath("InputMethodNameSimpleThree.java"), expected);
78      }
79  
80      @Test
81      public void testMethodEqClass() throws Exception {
82  
83          final String pattern = "^[a-z][a-zA-Z0-9]*$";
84  
85          final String[] expected = {
86              "27:16: " + getCheckMessage(MSG_KEY, "InputMethodNameEqualClassName"),
87              "27:16: " + getCheckMessage(MSG_INVALID_PATTERN,
88                      "InputMethodNameEqualClassName", pattern),
89              "33:17: " + getCheckMessage(MSG_INVALID_PATTERN, "PRIVATEInputMethodNameEqualClassName",
90                      pattern),
91              "42:20: " + getCheckMessage(MSG_KEY, "Inner"),
92              "42:20: " + getCheckMessage(MSG_INVALID_PATTERN, "Inner", pattern),
93              "48:20: " + getCheckMessage(MSG_INVALID_PATTERN,
94                      "InputMethodNameEqualClassName", pattern),
95              "60:24: " + getCheckMessage(MSG_KEY, "InputMethodNameEqualClassName"),
96              "60:24: " + getCheckMessage(MSG_INVALID_PATTERN,
97                      "InputMethodNameEqualClassName", pattern),
98              "73:9: " + getCheckMessage(MSG_KEY, "SweetInterface"),
99              "73:9: " + getCheckMessage(MSG_INVALID_PATTERN, "SweetInterface", pattern),
100             "82:17: " + getCheckMessage(MSG_KEY, "Outer"),
101             "82:17: " + getCheckMessage(MSG_INVALID_PATTERN, "Outer", pattern),
102         };
103 
104         verifyWithInlineConfigParser(
105                 getPath("InputMethodNameEqualClassName.java"), expected);
106     }
107 
108     @Test
109     public void testMethodEqClassAllow() throws Exception {
110         final String pattern = "^[a-z][a-zA-Z0-9]*$";
111 
112         final String[] expected = {
113             "25:16: " + getCheckMessage(MSG_INVALID_PATTERN,
114                     "InputMethodNameEqualClassName2", pattern),
115             "31:17: " + getCheckMessage(MSG_INVALID_PATTERN, "PRIVATEInputMethodNameEqualClassName",
116                     pattern),
117             "38:20: " + getCheckMessage(MSG_INVALID_PATTERN, "Inner", pattern),
118             "44:20: " + getCheckMessage(MSG_INVALID_PATTERN,
119                     "InputMethodNameEqualClassName2", pattern),
120             "54:24: " + getCheckMessage(MSG_INVALID_PATTERN,
121                     "InputMethodNameEqualClassName2", pattern),
122             "64:9: " + getCheckMessage(MSG_INVALID_PATTERN, "SweetInterface", pattern),
123             "70:17: " + getCheckMessage(MSG_INVALID_PATTERN, "Outer", pattern),
124         };
125 
126         verifyWithInlineConfigParser(
127                 getPath("InputMethodNameEqualClassName2.java"), expected);
128     }
129 
130     @Test
131     public void testAccessTuning() throws Exception {
132         final String pattern = "^[a-z][a-zA-Z0-9]*$";
133 
134         final String[] expected = {
135             "25:16: " + getCheckMessage(MSG_INVALID_PATTERN,
136                     "InputMethodNameEqualClassName3", pattern),
137             "36:20: " + getCheckMessage(MSG_INVALID_PATTERN, "Inner", pattern),
138             "42:20: " + getCheckMessage(MSG_INVALID_PATTERN,
139                     "InputMethodNameEqualClassName3", pattern),
140             "52:24: " + getCheckMessage(MSG_INVALID_PATTERN,
141                     "InputMethodNameEqualClassName3", pattern),
142             "62:9: " + getCheckMessage(MSG_INVALID_PATTERN, "SweetInterface", pattern),
143             "68:17: " + getCheckMessage(MSG_INVALID_PATTERN, "Outer", pattern),
144         };
145 
146         verifyWithInlineConfigParser(
147                 getPath("InputMethodNameEqualClassName3.java"), expected);
148     }
149 
150     @Test
151     public void testForNpe() throws Exception {
152 
153         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
154 
155         verifyWithInlineConfigParser(
156                 getPath("InputMethodNameExtra.java"), expected);
157     }
158 
159     @Test
160     public void testOverriddenMethods() throws Exception {
161 
162         final String pattern = "^[a-z][a-zA-Z0-9]*$";
163 
164         final String[] expected = {
165             "29:17: " + getCheckMessage(MSG_INVALID_PATTERN, "PUBLICfoo", pattern),
166             "32:20: " + getCheckMessage(MSG_INVALID_PATTERN, "PROTECTEDfoo", pattern),
167         };
168 
169         verifyWithInlineConfigParser(
170                 getPath("InputMethodNameOverriddenMethods.java"), expected);
171     }
172 
173     @Test
174     public void testInterfacesExcludePublic() throws Exception {
175         final String pattern = "^[a-z][a-zA-Z0-9]*$";
176 
177         final String[] expected = {
178             "18:18: " + getCheckMessage(MSG_INVALID_PATTERN, "PrivateMethod", pattern),
179             "20:25: " + getCheckMessage(MSG_INVALID_PATTERN, "PrivateMethod2", pattern),
180         };
181 
182         verifyWithInlineConfigParser(
183                 getPath("InputMethodNamePublicMethodsInInterfaces.java"),
184             expected);
185     }
186 
187     @Test
188     public void testInterfacesExcludePrivate() throws Exception {
189         final String pattern = "^[a-z][a-zA-Z0-9]*$";
190 
191         final String[] expected = {
192             "22:18: " + getCheckMessage(MSG_INVALID_PATTERN, "DefaultMethod", pattern),
193             "25:25: " + getCheckMessage(MSG_INVALID_PATTERN, "DefaultMethod2", pattern),
194             "28:10: " + getCheckMessage(MSG_INVALID_PATTERN, "PublicMethod", pattern),
195             "30:17: " + getCheckMessage(MSG_INVALID_PATTERN, "PublicMethod2", pattern),
196         };
197 
198         verifyWithInlineConfigParser(
199                 getPath("InputMethodNamePrivateMethodsInInterfaces.java"),
200             expected);
201     }
202 
203     @Test
204     public void testGetAcceptableTokens() {
205         final MethodNameCheck methodNameCheckObj = new MethodNameCheck();
206         final int[] actual = methodNameCheckObj.getAcceptableTokens();
207         final int[] expected = {
208             TokenTypes.METHOD_DEF,
209         };
210         assertWithMessage("Default acceptable tokens are invalid")
211             .that(actual)
212             .isEqualTo(expected);
213     }
214 
215     @Test
216     public void testRecordInInterfaceBody() throws Exception {
217 
218         final String pattern = "^[a-z][a-zA-Z0-9]*$";
219 
220         final String[] expected = {
221             "24:14: " + getCheckMessage(MSG_INVALID_PATTERN, "VIOLATION", pattern),
222         };
223 
224         verifyWithInlineConfigParser(
225                 getNonCompilablePath("InputMethodNameRecordInInterfaceBody.java"), expected);
226     }
227 
228 }