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.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 testDefault()
51              throws Exception {
52  
53          final String pattern = "^[a-z][a-zA-Z0-9]*$";
54  
55          final String[] expected = {
56              "144:10: " + getCheckMessage(MSG_INVALID_PATTERN, "ALL_UPPERCASE_METHOD", pattern),
57          };
58          verifyWithInlineConfigParser(
59                  getPath("InputMethodNameSimple.java"), expected);
60      }
61  
62      @Test
63      public void testMethodEqClass() throws Exception {
64  
65          final String pattern = "^[a-z][a-zA-Z0-9]*$";
66  
67          final String[] expected = {
68              "24:16: " + getCheckMessage(MSG_KEY, "InputMethodNameEqualClassName"),
69              "24:16: " + getCheckMessage(MSG_INVALID_PATTERN,
70                      "InputMethodNameEqualClassName", pattern),
71              "29:17: " + getCheckMessage(MSG_INVALID_PATTERN, "PRIVATEInputMethodNameEqualClassName",
72                      pattern),
73              "35:20: " + getCheckMessage(MSG_KEY, "Inner"),
74              "35:20: " + getCheckMessage(MSG_INVALID_PATTERN, "Inner", pattern),
75              "40:20: " + getCheckMessage(MSG_INVALID_PATTERN,
76                      "InputMethodNameEqualClassName", pattern),
77              "49:24: " + getCheckMessage(MSG_KEY, "InputMethodNameEqualClassName"),
78              "49:24: " + getCheckMessage(MSG_INVALID_PATTERN,
79                      "InputMethodNameEqualClassName", pattern),
80              "59:9: " + getCheckMessage(MSG_KEY, "SweetInterface"),
81              "59:9: " + getCheckMessage(MSG_INVALID_PATTERN, "SweetInterface", pattern),
82              "65:17: " + getCheckMessage(MSG_KEY, "Outer"),
83              "65:17: " + getCheckMessage(MSG_INVALID_PATTERN, "Outer", pattern),
84          };
85  
86          verifyWithInlineConfigParser(
87                  getPath("InputMethodNameEqualClassName.java"), expected);
88      }
89  
90      @Test
91      public void testMethodEqClassAllow() throws Exception {
92          final String pattern = "^[a-z][a-zA-Z0-9]*$";
93  
94          final String[] expected = {
95              "24:16: " + getCheckMessage(MSG_INVALID_PATTERN,
96                      "InputMethodNameEqualClassName2", pattern),
97              "29:17: " + getCheckMessage(MSG_INVALID_PATTERN, "PRIVATEInputMethodNameEqualClassName",
98                      pattern),
99              "35:20: " + getCheckMessage(MSG_INVALID_PATTERN, "Inner", pattern),
100             "40:20: " + getCheckMessage(MSG_INVALID_PATTERN,
101                     "InputMethodNameEqualClassName2", pattern),
102             "49:24: " + getCheckMessage(MSG_INVALID_PATTERN,
103                     "InputMethodNameEqualClassName2", pattern),
104             "59:9: " + getCheckMessage(MSG_INVALID_PATTERN, "SweetInterface", pattern),
105             "65:17: " + getCheckMessage(MSG_INVALID_PATTERN, "Outer", pattern),
106         };
107 
108         verifyWithInlineConfigParser(
109                 getPath("InputMethodNameEqualClassName2.java"), expected);
110     }
111 
112     @Test
113     public void testAccessTuning() throws Exception {
114         final String pattern = "^[a-z][a-zA-Z0-9]*$";
115 
116         final String[] expected = {
117             "24:16: " + getCheckMessage(MSG_INVALID_PATTERN,
118                     "InputMethodNameEqualClassName3", pattern),
119             "35:20: " + getCheckMessage(MSG_INVALID_PATTERN, "Inner", pattern),
120             "40:20: " + getCheckMessage(MSG_INVALID_PATTERN,
121                     "InputMethodNameEqualClassName3", pattern),
122             "49:24: " + getCheckMessage(MSG_INVALID_PATTERN,
123                     "InputMethodNameEqualClassName3", pattern),
124             "59:9: " + getCheckMessage(MSG_INVALID_PATTERN, "SweetInterface", pattern),
125             "65:17: " + getCheckMessage(MSG_INVALID_PATTERN, "Outer", pattern),
126         };
127 
128         verifyWithInlineConfigParser(
129                 getPath("InputMethodNameEqualClassName3.java"), expected);
130     }
131 
132     @Test
133     public void testForNpe() throws Exception {
134 
135         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
136 
137         verifyWithInlineConfigParser(
138                 getPath("InputMethodNameExtra.java"), expected);
139     }
140 
141     @Test
142     public void testOverriddenMethods() throws Exception {
143 
144         final String pattern = "^[a-z][a-zA-Z0-9]*$";
145 
146         final String[] expected = {
147             "29:17: " + getCheckMessage(MSG_INVALID_PATTERN, "PUBLICfoo", pattern),
148             "32:20: " + getCheckMessage(MSG_INVALID_PATTERN, "PROTECTEDfoo", pattern),
149         };
150 
151         verifyWithInlineConfigParser(
152                 getPath("InputMethodNameOverriddenMethods.java"), expected);
153     }
154 
155     @Test
156     public void testInterfacesExcludePublic() throws Exception {
157         final String pattern = "^[a-z][a-zA-Z0-9]*$";
158 
159         final String[] expected = {
160             "18:18: " + getCheckMessage(MSG_INVALID_PATTERN, "PrivateMethod", pattern),
161             "20:25: " + getCheckMessage(MSG_INVALID_PATTERN, "PrivateMethod2", pattern),
162         };
163 
164         verifyWithInlineConfigParser(
165                 getPath("InputMethodNamePublicMethodsInInterfaces.java"),
166             expected);
167     }
168 
169     @Test
170     public void testInterfacesExcludePrivate() throws Exception {
171         final String pattern = "^[a-z][a-zA-Z0-9]*$";
172 
173         final String[] expected = {
174             "22:18: " + getCheckMessage(MSG_INVALID_PATTERN, "DefaultMethod", pattern),
175             "25:25: " + getCheckMessage(MSG_INVALID_PATTERN, "DefaultMethod2", pattern),
176             "28:10: " + getCheckMessage(MSG_INVALID_PATTERN, "PublicMethod", pattern),
177             "30:17: " + getCheckMessage(MSG_INVALID_PATTERN, "PublicMethod2", pattern),
178         };
179 
180         verifyWithInlineConfigParser(
181                 getPath("InputMethodNamePrivateMethodsInInterfaces.java"),
182             expected);
183     }
184 
185     @Test
186     public void testGetAcceptableTokens() {
187         final MethodNameCheck methodNameCheckObj = new MethodNameCheck();
188         final int[] actual = methodNameCheckObj.getAcceptableTokens();
189         final int[] expected = {
190             TokenTypes.METHOD_DEF,
191         };
192         assertWithMessage("Default acceptable tokens are invalid")
193             .that(actual)
194             .isEqualTo(expected);
195     }
196 
197     @Test
198     public void testRecordInInterfaceBody() throws Exception {
199 
200         final String pattern = "^[a-z][a-zA-Z0-9]*$";
201 
202         final String[] expected = {
203             "24:14: " + getCheckMessage(MSG_INVALID_PATTERN, "VIOLATION", pattern),
204         };
205 
206         verifyWithInlineConfigParser(
207                 getNonCompilablePath("InputMethodNameRecordInInterfaceBody.java"), expected);
208     }
209 
210 }