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.coding;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck;
31  
32  public class XpathRegressionVariableDeclarationUsageDistanceTest extends AbstractXpathTestSupport {
33      private final String checkName = VariableDeclarationUsageDistanceCheck.class.getSimpleName();
34  
35      @Override
36      protected String getCheckName() {
37          return checkName;
38      }
39  
40      @Override
41      protected String getPackageLocation() {
42          return "org/checkstyle/suppressionxpathfilter/coding/variabledeclarationusagedistance";
43      }
44  
45      @Test
46      public void testOne() throws Exception {
47          final File fileToProcess = new File(getPath(
48                  "InputXpathVariableDeclarationUsageDistanceOne.java"));
49  
50          final DefaultConfiguration moduleConfig =
51                  createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
52          moduleConfig.addProperty("allowedDistance", "1");
53          moduleConfig.addProperty("ignoreVariablePattern", "");
54          moduleConfig.addProperty("validateBetweenScopes", "true");
55          moduleConfig.addProperty("ignoreFinal", "false");
56  
57          final String[] expectedViolation = {
58              "7:9: " + getCheckMessage(VariableDeclarationUsageDistanceCheck.class,
59                      VariableDeclarationUsageDistanceCheck.MSG_KEY, "temp", 2, 1),
60          };
61  
62          final List<String> expectedXpathQueries = Arrays.asList(
63                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
64                          + "'InputXpathVariableDeclarationUsageDistanceOne']]/"
65                          + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
66                          + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]",
67                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
68                          + "'InputXpathVariableDeclarationUsageDistanceOne']]/"
69                          + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
70                          + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]/MODIFIERS",
71                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
72                          + "'InputXpathVariableDeclarationUsageDistanceOne']]/"
73                          + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
74                          + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]/TYPE",
75                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
76                          + "'InputXpathVariableDeclarationUsageDistanceOne']]/"
77                          + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
78                          + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]/TYPE/LITERAL_INT"
79          );
80  
81          runVerifications(moduleConfig, fileToProcess, expectedViolation,
82                  expectedXpathQueries);
83      }
84  
85      @Test
86      public void testTwo() throws Exception {
87          final File fileToProcess = new File(getPath(
88                  "InputXpathVariableDeclarationUsageDistanceTwo.java"));
89  
90          final DefaultConfiguration moduleConfig =
91                  createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
92  
93          moduleConfig.addProperty("allowedDistance", "1");
94          moduleConfig.addProperty("ignoreVariablePattern", "");
95          moduleConfig.addProperty("validateBetweenScopes", "true");
96          moduleConfig.addProperty("ignoreFinal", "false");
97  
98          final String[] expectedViolation = {
99              "6:9: " + getCheckMessage(VariableDeclarationUsageDistanceCheck.class,
100                     VariableDeclarationUsageDistanceCheck.MSG_KEY, "count", 2, 1),
101         };
102 
103         final List<String> expectedXpathQueries = Arrays.asList(
104                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
105                         + "'InputXpathVariableDeclarationUsageDistanceTwo']]"
106                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod2']]"
107                         + "/SLIST/VARIABLE_DEF[./IDENT[@text='count']]",
108                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
109                         + "'InputXpathVariableDeclarationUsageDistanceTwo']]"
110                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod2']]"
111                         + "/SLIST/VARIABLE_DEF[./IDENT[@text='count']]/MODIFIERS",
112                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
113                         + "'InputXpathVariableDeclarationUsageDistanceTwo']]"
114                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod2']]"
115                         + "/SLIST/VARIABLE_DEF[./IDENT[@text='count']]/TYPE",
116                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
117                         + "'InputXpathVariableDeclarationUsageDistanceTwo']]"
118                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod2']]"
119                         + "/SLIST/VARIABLE_DEF[./IDENT[@text='count']]/TYPE/LITERAL_INT"
120         );
121 
122         runVerifications(moduleConfig, fileToProcess, expectedViolation,
123                 expectedXpathQueries);
124     }
125 
126 }