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.google.checkstyle.test.chapter5naming.rule523methodnames;
21  
22  import java.util.Map;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import com.google.checkstyle.test.base.AbstractGoogleModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.api.Configuration;
28  
29  public class MethodNameTest extends AbstractGoogleModuleTestSupport {
30  
31      @Override
32      protected String getPackageLocation() {
33          return "com/google/checkstyle/test/chapter5naming/rule523methodnames";
34      }
35  
36      @Test
37      public void testMethodName() throws Exception {
38          final Configuration checkConfig = getModuleConfig("MethodName");
39          final String msgKey = "name.invalidPattern";
40          final String format = "^[a-z][a-z0-9]\\w*$";
41          final Map<String, String> messages = checkConfig.getMessages();
42  
43          final String[] expected = {
44              "11:10: " + getCheckMessage(messages, msgKey, "Foo", format),
45              "12:10: " + getCheckMessage(messages, msgKey, "fOo", format),
46              "14:10: " + getCheckMessage(messages, msgKey, "f$o", format),
47              "15:10: " + getCheckMessage(messages, msgKey, "f_oo", format),
48              "16:10: " + getCheckMessage(messages, msgKey, "f", format),
49              "17:10: " + getCheckMessage(messages, msgKey, "fO", format),
50              "21:14: " + getCheckMessage(messages, msgKey, "Foo", format),
51              "22:14: " + getCheckMessage(messages, msgKey, "fOo", format),
52              "24:14: " + getCheckMessage(messages, msgKey, "f$o", format),
53              "25:14: " + getCheckMessage(messages, msgKey, "f_oo", format),
54              "26:14: " + getCheckMessage(messages, msgKey, "f", format),
55              "27:14: " + getCheckMessage(messages, msgKey, "fO", format),
56              "32:14: " + getCheckMessage(messages, msgKey, "Foo", format),
57              "33:14: " + getCheckMessage(messages, msgKey, "fOo", format),
58              "35:14: " + getCheckMessage(messages, msgKey, "f$o", format),
59              "36:14: " + getCheckMessage(messages, msgKey, "f_oo", format),
60              "37:14: " + getCheckMessage(messages, msgKey, "f", format),
61              "38:14: " + getCheckMessage(messages, msgKey, "fO", format),
62              "44:10: " + getCheckMessage(messages, msgKey, "Foo", format),
63              "45:10: " + getCheckMessage(messages, msgKey, "fOo", format),
64              "47:10: " + getCheckMessage(messages, msgKey, "f$o", format),
65              "48:10: " + getCheckMessage(messages, msgKey, "f_oo", format),
66              "49:10: " + getCheckMessage(messages, msgKey, "f", format),
67              "50:10: " + getCheckMessage(messages, msgKey, "fO", format),
68          };
69  
70          final String filePath = getPath("InputMethodName.java");
71  
72          final Integer[] warnList = getLinesWithWarn(filePath);
73          verify(checkConfig, filePath, expected, warnList);
74      }
75  
76  }