View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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 static com.puppycrawl.tools.checkstyle.checks.naming.LocalFinalVariableNameCheck.MSG_INVALID_PATTERN;
23  
24  import java.io.File;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32  import com.puppycrawl.tools.checkstyle.checks.naming.LocalFinalVariableNameCheck;
33  
34  public class XpathRegressionLocalFinalVariableNameTest extends AbstractXpathTestSupport {
35  
36      private final String checkName = LocalFinalVariableNameCheck.class.getSimpleName();
37  
38      @Override
39      protected String getCheckName() {
40          return checkName;
41      }
42  
43      @Override
44      protected String getPackageLocation() {
45          return "org/checkstyle/suppressionxpathfilter/naming/localfinalvariablename";
46      }
47  
48      @Test
49      public void testResource() throws Exception {
50          final File fileToProcess =
51                  new File(getPath("InputXpathLocalFinalVariableNameResource.java"));
52  
53          final DefaultConfiguration moduleConfig =
54                  createModuleConfig(LocalFinalVariableNameCheck.class);
55          moduleConfig.addProperty("format", "^[A-Z][A-Z0-9]*$");
56          moduleConfig.addProperty("tokens", "PARAMETER_DEF,RESOURCE");
57  
58          final String[] expectedViolation = {
59              "7:21: " + getCheckMessage(LocalFinalVariableNameCheck.class,
60                      MSG_INVALID_PATTERN, "scanner", "^[A-Z][A-Z0-9]*$"),
61          };
62  
63          final List<String> expectedXpathQueries = Collections.singletonList(
64                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
65                          + "@text='InputXpathLocalFinalVariableNameResource']]"
66                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/LITERAL_TRY"
67                          + "/RESOURCE_SPECIFICATION/RESOURCES/RESOURCE/IDENT[@text='scanner']"
68          );
69  
70          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
71      }
72  
73      @Test
74      public void testVariable() throws Exception {
75          final File fileToProcess =
76                  new File(getPath("InputXpathLocalFinalVariableNameVar.java"));
77  
78          final DefaultConfiguration moduleConfig =
79                  createModuleConfig(LocalFinalVariableNameCheck.class);
80          moduleConfig.addProperty("format", "^[A-Z][a-z0-9]*$");
81  
82          final String[] expectedViolation = {
83              "5:19: " + getCheckMessage(LocalFinalVariableNameCheck.class,
84                      MSG_INVALID_PATTERN, "VAR1", "^[A-Z][a-z0-9]*$"),
85          };
86  
87          final List<String> expectedXpathQueries = Collections.singletonList(
88                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
89                          + "@text='InputXpathLocalFinalVariableNameVar']]"
90                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/VARIABLE_DEF"
91                          + "/IDENT[@text='VAR1']"
92          );
93  
94          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
95      }
96  
97      @Test
98      public void testInnerClass() throws Exception {
99          final File fileToProcess =
100                 new File(getPath("InputXpathLocalFinalVariableNameInner.java"));
101 
102         final DefaultConfiguration moduleConfig =
103                 createModuleConfig(LocalFinalVariableNameCheck.class);
104         moduleConfig.addProperty("format", "^[A-Z][a-z0-9]*$");
105 
106         final String[] expectedViolation = {
107             "8:23: " + getCheckMessage(LocalFinalVariableNameCheck.class,
108                         MSG_INVALID_PATTERN, "VAR1", "^[A-Z][a-z0-9]*$"),
109         };
110 
111         final List<String> expectedXpathQueries = Collections.singletonList(
112                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
113                         + "@text='InputXpathLocalFinalVariableNameInner']]"
114                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK"
115                         + "/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/VARIABLE_DEF"
116                         + "/IDENT[@text='VAR1']"
117         );
118 
119         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
120     }
121 }