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.puppycrawl.tools.checkstyle.checks.naming.MethodNameCheck.MSG_INVALID_PATTERN;
23  import static com.puppycrawl.tools.checkstyle.checks.naming.MethodNameCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
28  
29  public class MethodNameCheckExamplesTest extends AbstractExamplesModuleTestSupport {
30  
31      @Override
32      public String getPackageLocation() {
33          return "com/puppycrawl/tools/checkstyle/checks/naming/methodname";
34      }
35  
36      @Test
37      public void testExample1() throws Exception {
38          final String[] expected = {
39              "18:18: " + getCheckMessage(MSG_INVALID_PATTERN, "Method2", "^[a-z][a-zA-Z0-9]*$"),
40              "19:16: " + getCheckMessage(MSG_INVALID_PATTERN, "Method3", "^[a-z][a-zA-Z0-9]*$"),
41              "20:15: " + getCheckMessage(MSG_INVALID_PATTERN, "Example3", "^[a-z][a-zA-Z0-9]*$"),
42              "21:8: " + getCheckMessage(MSG_INVALID_PATTERN, "Method5", "^[a-z][a-zA-Z0-9]*$"),
43          };
44  
45          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
46      }
47  
48      @Test
49      public void testExample2() throws Exception {
50          final String[] expected = {
51              "17:15: " + getCheckMessage(MSG_INVALID_PATTERN, "method1", "^[a-z][a-zA-Z0-9]{7,}$"),
52              "18:18: " + getCheckMessage(MSG_INVALID_PATTERN, "Method2", "^[a-z][a-zA-Z0-9]{7,}$"),
53              "19:16: " + getCheckMessage(MSG_INVALID_PATTERN, "Method3", "^[a-z][a-zA-Z0-9]{7,}$"),
54              "20:15: " + getCheckMessage(MSG_INVALID_PATTERN, "Example3",
55                      "^[a-z][a-zA-Z0-9]{7,}$"),
56              "21:8: " + getCheckMessage(MSG_INVALID_PATTERN, "Method5", "^[a-z][a-zA-Z0-9]{7,}$"),
57          };
58  
59          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
60      }
61  
62      @Test
63      public void testExample3() throws Exception {
64          final String[] expected = {
65          };
66  
67          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
68      }
69  
70      @Test
71      public void testExample4() throws Exception {
72          final String[] expected = {
73              "18:18: " + getCheckMessage(MSG_INVALID_PATTERN, "Method2", "^[a-z][a-zA-Z0-9]*$"),
74              "19:16: " + getCheckMessage(MSG_INVALID_PATTERN, "Method3", "^[a-z][a-zA-Z0-9]*$"),
75              "21:8: " + getCheckMessage(MSG_INVALID_PATTERN, "Method5", "^[a-z][a-zA-Z0-9]*$"),
76          };
77  
78          verifyWithInlineConfigParser(getPath("Example4.java"), expected);
79      }
80  
81      @Test
82      public void testExample5() throws Exception {
83          final String[] expected = {
84              "19:16: " + getCheckMessage(MSG_INVALID_PATTERN, "Method3", "^[a-z][a-zA-Z0-9]*$"),
85              "20:15: " + getCheckMessage(MSG_INVALID_PATTERN, "Example3", "^[a-z][a-zA-Z0-9]*$"),
86              "21:8: " + getCheckMessage(MSG_INVALID_PATTERN, "Method5", "^[a-z][a-zA-Z0-9]*$"),
87          };
88  
89          verifyWithInlineConfigParser(getPath("Example5.java"), expected);
90      }
91  
92      @Test
93      public void testExample6() throws Exception {
94          final String[] expected = {
95              "18:18: " + getCheckMessage(MSG_INVALID_PATTERN, "Method2", "^[a-z][a-zA-Z0-9]*$"),
96              "20:15: " + getCheckMessage(MSG_INVALID_PATTERN, "Example3", "^[a-z][a-zA-Z0-9]*$"),
97              "21:8: " + getCheckMessage(MSG_INVALID_PATTERN, "Method5", "^[a-z][a-zA-Z0-9]*$"),
98          };
99  
100         verifyWithInlineConfigParser(getPath("Example6.java"), expected);
101     }
102 
103     @Test
104     public void testExample7() throws Exception {
105         final String[] expected = {
106             "18:18: " + getCheckMessage(MSG_INVALID_PATTERN, "Method2", "^[a-z][a-zA-Z0-9]*$"),
107             "19:16: " + getCheckMessage(MSG_INVALID_PATTERN, "Method3", "^[a-z][a-zA-Z0-9]*$"),
108             "20:15: " + getCheckMessage(MSG_INVALID_PATTERN, "Example3", "^[a-z][a-zA-Z0-9]*$"),
109         };
110 
111         verifyWithInlineConfigParser(getPath("Example7.java"), expected);
112     }
113 
114     @Test
115     public void testUseCase1() throws Exception {
116         final String[] expected = {
117             "18:15: " + getCheckMessage(MSG_KEY, "UseCase1"),
118         };
119 
120         verifyWithInlineConfigParser(getPath("UseCase1.java"), expected);
121     }
122 
123 }