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.naming;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.naming.GoogleMethodNameCheck.MSG_KEY_FORMAT_REGULAR;
24  import static com.puppycrawl.tools.checkstyle.checks.naming.GoogleMethodNameCheck.MSG_KEY_FORMAT_TEST;
25  import static com.puppycrawl.tools.checkstyle.checks.naming.GoogleMethodNameCheck.MSG_KEY_UNDERSCORE_REGULAR;
26  import static com.puppycrawl.tools.checkstyle.checks.naming.GoogleMethodNameCheck.MSG_KEY_UNDERSCORE_TEST;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33  
34  public class GoogleMethodNameCheckTest extends AbstractModuleTestSupport {
35  
36      @Override
37      public String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/naming/googlemethodname";
39      }
40  
41      @Test
42      public void testGetAcceptableTokens() {
43          final GoogleMethodNameCheck checkObj = new GoogleMethodNameCheck();
44          final int[] expected = {TokenTypes.METHOD_DEF};
45          assertWithMessage("Default acceptable tokens are invalid")
46              .that(checkObj.getAcceptableTokens())
47              .isEqualTo(expected);
48      }
49  
50      @Test
51      public void testValidRegularMethodNames() throws Exception {
52          final String[] expected = {};
53          verifyWithInlineConfigParser(
54                  getPath("InputGoogleMethodNameValidRegular.java"), expected);
55      }
56  
57      @Test
58      public void testInvalidRegularMethodNames() throws Exception {
59          final String[] expected = {
60              "9:10: " + getCheckMessage(MSG_KEY_FORMAT_REGULAR, "Foo"),
61              "13:10: " + getCheckMessage(MSG_KEY_FORMAT_REGULAR, "f"),
62              "17:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "foo_bar"),
63              "21:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "foo_Bar"),
64              "25:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "foo__bar"),
65              "29:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "gradle_9_5_1"),
66              "33:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "jdk_9_0_392"),
67              "37:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "guava_33_4_5"),
68              "41:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "a_1"),
69              "45:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "guava33_4_5_"),
70              "49:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "guava33__4_5"),
71              "53:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "guava33_4_a"),
72              "57:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "_foo"),
73              "61:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "foo_"),
74              "65:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "__foo"),
75              "69:10: " + getCheckMessage(MSG_KEY_FORMAT_REGULAR, "FOO"),
76              "73:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR,
77                      "transferMoney_deductsFromSource"),
78              "77:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "foo_bar_baz"),
79              "81:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "foo123_456_"),
80              "85:10: " + getCheckMessage(MSG_KEY_FORMAT_REGULAR, "fO"),
81              "89:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_REGULAR, "xY_z"),
82              "93:10: " + getCheckMessage(MSG_KEY_FORMAT_REGULAR, "mName"),
83              "97:10: " + getCheckMessage(MSG_KEY_FORMAT_REGULAR, "fooBAR"),
84          };
85          verifyWithInlineConfigParser(
86                  getPath("InputGoogleMethodNameInvalidRegular.java"), expected);
87      }
88  
89      @Test
90      public void testValidTestMethodNames() throws Exception {
91          final String[] expected = {};
92          verifyWithInlineConfigParser(
93                  getPath("InputGoogleMethodNameValidTest.java"), expected);
94      }
95  
96      @Test
97      public void testInvalidTestMethodNames() throws Exception {
98          final String[] expected = {
99              "12:10: " + getCheckMessage(MSG_KEY_FORMAT_TEST, "transferMoney_DeductsFromSource"),
100             "18:10: " + getCheckMessage(MSG_KEY_FORMAT_TEST, "Testing_Foo"),
101             "24:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_TEST, "testing__foo"),
102             "29:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_TEST, "testing_foo_"),
103             "34:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_TEST, "_testing"),
104             "39:10: " + getCheckMessage(MSG_KEY_FORMAT_TEST, "TestingFooBad"),
105             "45:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_TEST, "test_1value"),
106             "50:10: " + getCheckMessage(MSG_KEY_FORMAT_TEST, "test_FOO_bar"),
107             "57:10: " + getCheckMessage(MSG_KEY_FORMAT_TEST, "testing_a"),
108             "63:10: " + getCheckMessage(MSG_KEY_FORMAT_TEST, "test_fO_bar"),
109             "70:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_TEST, "solve6x6_returnsTrue"),
110             "75:10: " + getCheckMessage(MSG_KEY_UNDERSCORE_TEST,
111                     "solve6x6_noSolution_returnsFalse"),
112             "80:10: " + getCheckMessage(MSG_KEY_FORMAT_TEST, "fooBAR"),
113         };
114         verifyWithInlineConfigParser(
115                 getPath("InputGoogleMethodNameInvalidTest.java"), expected);
116     }
117 
118     @Test
119     public void testOverriddenMethods() throws Exception {
120         final String[] expected = {
121             "11:20: " + getCheckMessage(MSG_KEY_FORMAT_REGULAR, "Foo"),
122         };
123         verifyWithInlineConfigParser(
124                 getPath("InputGoogleMethodNameOverride.java"), expected);
125     }
126 
127     @Test
128     public void testMethodNumberingSuffix() throws Exception {
129         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
130         verifyWithInlineConfigParser(
131                 getPath("InputGoogleMethodNameNumberingSuffix.java"), expected);
132     }
133 
134     @Test
135     public void testInsideInterface() throws Exception {
136         final String[] expected = {
137             "15:10: " + getCheckMessage(MSG_KEY_FORMAT_REGULAR, "Bar"),
138             "21:18: " + getCheckMessage(MSG_KEY_FORMAT_REGULAR, "mValue"),
139         };
140         verifyWithInlineConfigParser(
141                 getPath("InputGoogleMethodNameInsideInterface.java"), expected);
142     }
143 
144     @Test
145     public void testInsideRecord() throws Exception {
146         final String[] expected = {
147             "16:10: " + getCheckMessage(MSG_KEY_FORMAT_REGULAR, "Foo"),
148             "20:10: " + getCheckMessage(MSG_KEY_FORMAT_REGULAR, "mName"),
149         };
150         verifyWithInlineConfigParser(
151                 getPath("InputGoogleMethodNameInsideRecord.java"), expected);
152     }
153 
154     @Test
155     public void testCompactSourceFile() throws Exception {
156         final String[] expected = {
157             "14:5: " + getCheckMessage(MSG_KEY_FORMAT_REGULAR, "InvalidMethodName"),
158         };
159         verifyWithInlineConfigParser(
160                 getNonCompilablePath("compact/InputGoogleMethodNameCompactSourceFile.java"),
161                 expected
162         );
163     }
164 
165 }