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 org.checkstyle.suppressionxpathfilter.naming;
21  
22  import java.io.File;
23  import java.util.List;
24  
25  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.naming.GoogleMethodNameCheck;
30  
31  public class XpathRegressionGoogleMethodNameTest extends AbstractXpathTestSupport {
32  
33      private static final Class<GoogleMethodNameCheck> CLASS = GoogleMethodNameCheck.class;
34  
35      @Override
36      protected String getCheckName() {
37          return CLASS.getSimpleName();
38      }
39  
40      @Override
41      public String getPackageLocation() {
42          return "org/checkstyle/suppressionxpathfilter/naming/googlemethodname";
43      }
44  
45      @Test
46      public void testRegularMethodName() throws Exception {
47          final File fileToProcess = new File(
48              getPath("InputXpathGoogleMethodName.java"));
49  
50          final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
51  
52          final String[] expectedViolation = {
53              "5:10: " + getCheckMessage(CLASS, GoogleMethodNameCheck.MSG_KEY_FORMAT_REGULAR,
54                      "Foo"),
55          };
56  
57          final List<String> expectedXpathQueries = List.of(
58              "/COMPILATION_UNIT/CLASS_DEF"
59                  + "[./IDENT[@text='InputXpathGoogleMethodName']]"
60                  + "/OBJBLOCK/METHOD_DEF/IDENT[@text='Foo']"
61          );
62  
63          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
64      }
65  
66      @Test
67      public void testInsideInterface() throws Exception {
68          final File fileToProcess = new File(
69              getPath("InputXpathGoogleMethodNameInterface.java"));
70  
71          final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
72  
73          final String[] expectedViolation = {
74              "8:10: " + getCheckMessage(CLASS, GoogleMethodNameCheck.MSG_KEY_UNDERSCORE_TEST,
75                      "test_1"),
76          };
77  
78          final List<String> expectedXpathQueries = List.of(
79              "/COMPILATION_UNIT/INTERFACE_DEF"
80                  + "[./IDENT[@text='InputXpathGoogleMethodNameInterface']]"
81                  + "/OBJBLOCK/METHOD_DEF/IDENT[@text='test_1']"
82          );
83  
84          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
85      }
86  
87      @Test
88      public void testInsideRecord() throws Exception {
89          final File fileToProcess = new File(
90              getPath("InputXpathGoogleMethodNameRecord.java"));
91  
92          final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
93  
94          final String[] expectedViolation = {
95              "5:10: " + getCheckMessage(CLASS, GoogleMethodNameCheck.MSG_KEY_FORMAT_REGULAR,
96                      "f"),
97          };
98  
99          final List<String> expectedXpathQueries = List.of(
100             "/COMPILATION_UNIT/RECORD_DEF"
101                 + "[./IDENT[@text='InputXpathGoogleMethodNameRecord']]"
102                 + "/OBJBLOCK/METHOD_DEF/IDENT[@text='f']"
103         );
104 
105         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
106     }
107 
108 }