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 org.checkstyle.suppressionxpathfilter;
21  
22  import java.io.File;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck;
30  import com.puppycrawl.tools.checkstyle.checks.naming.MethodNameCheck;
31  
32  public class XpathRegressionMethodNameTest extends AbstractXpathTestSupport {
33  
34      private final String checkName = MethodNameCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Test
42      public void testDefault() throws Exception {
43          final File fileToProcess =
44                  new File(getPath("InputXpathMethodNameDefault.java"));
45  
46          final String pattern = "^[a-z][a-zA-Z0-9]*$";
47          final DefaultConfiguration moduleConfig =
48                  createModuleConfig(MethodNameCheck.class);
49  
50          final String[] expectedViolation = {
51              "6:16: " + getCheckMessage(MethodNameCheck.class,
52                          AbstractNameCheck.MSG_INVALID_PATTERN, "SecondMethod", pattern),
53          };
54  
55          final List<String> expectedXpathQueries = Collections.singletonList(
56                  "/COMPILATION_UNIT"
57                          + "/CLASS_DEF[./IDENT[@text"
58                          + "='InputXpathMethodNameDefault']]"
59                          + "/OBJBLOCK/METHOD_DEF/IDENT[@text='SecondMethod']"
60          );
61          runVerifications(moduleConfig, fileToProcess, expectedViolation,
62                  expectedXpathQueries);
63      }
64  
65      @Test
66      public void testInnerClass() throws Exception {
67          final File fileToProcess =
68                  new File(getPath("InputXpathMethodNameInner.java"));
69  
70          final String pattern = "^[a-z](_?[a-zA-Z0-9]+)*$";
71          final DefaultConfiguration moduleConfig =
72                  createModuleConfig(MethodNameCheck.class);
73          moduleConfig.addProperty("format", "^[a-z](_?[a-zA-Z0-9]+)*$");
74  
75          final String[] expectedViolation = {
76              "7:21: " + getCheckMessage(MethodNameCheck.class,
77                          AbstractNameCheck.MSG_INVALID_PATTERN, "MyMethod2", pattern),
78          };
79  
80          final List<String> expectedXpathQueries = Collections.singletonList(
81                  "/COMPILATION_UNIT"
82                          + "/CLASS_DEF[./IDENT[@text"
83                          + "='InputXpathMethodNameInner']]"
84                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]"
85                          + "/OBJBLOCK/METHOD_DEF/IDENT[@text='MyMethod2']"
86          );
87          runVerifications(moduleConfig, fileToProcess, expectedViolation,
88                  expectedXpathQueries);
89      }
90  
91      @Test
92      public void testCustomProperties() throws Exception {
93          final File fileToProcess =
94                  new File(getPath("InputXpathMethodNameCustomProperties.java"));
95  
96          final String pattern = "^[a-z](_?[a-zA-Z0-9]+)*$";
97          final DefaultConfiguration moduleConfig =
98                  createModuleConfig(MethodNameCheck.class);
99          moduleConfig.addProperty("format", "^[a-z](_?[a-zA-Z0-9]+)*$");
100         moduleConfig.addProperty("applyToPublic", "false");
101         moduleConfig.addProperty("applyToProtected", "false");
102 
103         final String[] expectedViolation = {
104             "7:19: " + getCheckMessage(MethodNameCheck.class,
105                         AbstractNameCheck.MSG_INVALID_PATTERN,
106                         "ThirdMethod", pattern),
107         };
108 
109         final List<String> expectedXpathQueries = Collections.singletonList(
110                 "/COMPILATION_UNIT"
111                         + "/INTERFACE_DEF[./IDENT[@text='Check']]"
112                         + "/OBJBLOCK/METHOD_DEF/IDENT[@text='ThirdMethod']"
113         );
114         runVerifications(moduleConfig, fileToProcess, expectedViolation,
115                 expectedXpathQueries);
116     }
117 
118 }