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.GoogleNonConstantFieldNameCheck;
30  
31  public class XpathRegressionGoogleNonConstantFieldNameTest extends AbstractXpathTestSupport {
32  
33      private static final Class<GoogleNonConstantFieldNameCheck> CLASS =
34              GoogleNonConstantFieldNameCheck.class;
35  
36      @Override
37      protected String getCheckName() {
38          return CLASS.getSimpleName();
39      }
40  
41      @Override
42      public String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/naming/googlenonconstantfieldname";
44      }
45  
46      @Test
47      public void testNonConstantFieldName() throws Exception {
48          final File fileToProcess = new File(
49              getPath("InputXpathGoogleNonConstantFieldName.java"));
50  
51          final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
52  
53          final String[] expectedViolation = {
54              "5:9: " + getCheckMessage(CLASS, GoogleNonConstantFieldNameCheck.MSG_KEY_INVALID_FORMAT,
55                      "Foo"),
56          };
57  
58          final List<String> expectedXpathQueries = List.of(
59              "/COMPILATION_UNIT/CLASS_DEF"
60                  + "[./IDENT[@text='InputXpathGoogleNonConstantFieldName']]"
61                  + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='Foo']"
62          );
63  
64          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
65      }
66  
67      @Test
68      public void testInnerClassField() throws Exception {
69          final File fileToProcess = new File(
70              getPath("InputXpathGoogleNonConstantFieldNameInnerClass.java"));
71          final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
72          final String[] expectedViolation = {
73              "6:14: " + getCheckMessage(CLASS,
74                      GoogleNonConstantFieldNameCheck.MSG_KEY_INVALID_FORMAT,
75                      "gradle_9_5_1"),
76          };
77          final List<String> expectedXpathQueries = List.of(
78              "/COMPILATION_UNIT/INTERFACE_DEF"
79                  + "[./IDENT[@text='InputXpathGoogleNonConstantFieldNameInnerClass']]"
80                  + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]"
81                  + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='gradle_9_5_1']"
82          );
83          runVerifications(moduleConfig, fileToProcess, expectedViolation,
84                  expectedXpathQueries);
85      }
86  
87      @Test
88      public void testAnonymousClassField() throws Exception {
89          final File fileToProcess = new File(
90              getPath("InputXpathGoogleNonConstantFieldNameAnonymousClass.java"));
91          final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
92          final String[] expectedViolation = {
93              "6:13: " + getCheckMessage(CLASS,
94                      GoogleNonConstantFieldNameCheck.MSG_KEY_INVALID_FORMAT,
95                      "f"),
96          };
97          final List<String> expectedXpathQueries = List.of(
98              "/COMPILATION_UNIT/CLASS_DEF"
99                  + "[./IDENT[@text='InputXpathGoogleNonConstantFieldNameAnonymousClass']]"
100                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='run']]/ASSIGN/EXPR"
101                 + "/LITERAL_NEW[./IDENT[@text='Runnable']]/OBJBLOCK/VARIABLE_DEF/IDENT[@text='f']"
102         );
103         runVerifications(moduleConfig, fileToProcess, expectedViolation,
104                 expectedXpathQueries);
105     }
106 }