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