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.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  
34      private final String checkName = VariableDeclarationUsageDistanceCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Override
42      public String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/coding/variabledeclarationusagedistance";
44      }
45  
46      @Test
47      public void testOne() throws Exception {
48          final File fileToProcess = new File(getPath(
49                  "InputXpathVariableDeclarationUsageDistanceOne.java"));
50  
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
53          moduleConfig.addProperty("allowedDistance", "1");
54          moduleConfig.addProperty("ignoreVariablePattern", "");
55          moduleConfig.addProperty("validateBetweenScopes", "true");
56          moduleConfig.addProperty("ignoreFinal", "false");
57  
58          final String[] expectedViolation = {
59              "7:9: " + getCheckMessage(VariableDeclarationUsageDistanceCheck.class,
60                      VariableDeclarationUsageDistanceCheck.MSG_KEY, "temp", 2, 1),
61          };
62  
63          final List<String> expectedXpathQueries = Arrays.asList(
64                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
65                          + "'InputXpathVariableDeclarationUsageDistanceOne']]/"
66                          + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
67                          + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]",
68                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
69                          + "'InputXpathVariableDeclarationUsageDistanceOne']]/"
70                          + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
71                          + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]/MODIFIERS",
72                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
73                          + "'InputXpathVariableDeclarationUsageDistanceOne']]/"
74                          + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
75                          + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]/TYPE",
76                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
77                          + "'InputXpathVariableDeclarationUsageDistanceOne']]/"
78                          + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
79                          + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]/TYPE/LITERAL_INT"
80          );
81  
82          runVerifications(moduleConfig, fileToProcess, expectedViolation,
83                  expectedXpathQueries);
84      }
85  
86      @Test
87      public void testTwo() throws Exception {
88          final File fileToProcess = new File(getPath(
89                  "InputXpathVariableDeclarationUsageDistanceTwo.java"));
90  
91          final DefaultConfiguration moduleConfig =
92                  createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
93  
94          moduleConfig.addProperty("allowedDistance", "1");
95          moduleConfig.addProperty("ignoreVariablePattern", "");
96          moduleConfig.addProperty("validateBetweenScopes", "true");
97          moduleConfig.addProperty("ignoreFinal", "false");
98  
99          final String[] expectedViolation = {
100             "6:9: " + getCheckMessage(VariableDeclarationUsageDistanceCheck.class,
101                     VariableDeclarationUsageDistanceCheck.MSG_KEY, "count", 2, 1),
102         };
103 
104         final List<String> expectedXpathQueries = Arrays.asList(
105                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
106                         + "'InputXpathVariableDeclarationUsageDistanceTwo']]"
107                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod2']]"
108                         + "/SLIST/VARIABLE_DEF[./IDENT[@text='count']]",
109                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
110                         + "'InputXpathVariableDeclarationUsageDistanceTwo']]"
111                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod2']]"
112                         + "/SLIST/VARIABLE_DEF[./IDENT[@text='count']]/MODIFIERS",
113                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
114                         + "'InputXpathVariableDeclarationUsageDistanceTwo']]"
115                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod2']]"
116                         + "/SLIST/VARIABLE_DEF[./IDENT[@text='count']]/TYPE",
117                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
118                         + "'InputXpathVariableDeclarationUsageDistanceTwo']]"
119                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod2']]"
120                         + "/SLIST/VARIABLE_DEF[./IDENT[@text='count']]/TYPE/LITERAL_INT"
121         );
122 
123         runVerifications(moduleConfig, fileToProcess, expectedViolation,
124                 expectedXpathQueries);
125     }
126 
127     @Test
128     public void testConstructor() throws Exception {
129         final File fileToProcess = new File(getPath(
130                 "InputXpathVariableDeclarationUsageDistanceConstructor.java"));
131 
132         final DefaultConfiguration moduleConfig =
133                 createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
134         moduleConfig.addProperty("allowedDistance", "1");
135         moduleConfig.addProperty("ignoreVariablePattern", "");
136         moduleConfig.addProperty("validateBetweenScopes", "true");
137         moduleConfig.addProperty("ignoreFinal", "false");
138 
139         final String[] expectedViolation = {
140             "7:9: " + getCheckMessage(VariableDeclarationUsageDistanceCheck.class,
141                     VariableDeclarationUsageDistanceCheck.MSG_KEY, "temp", 2, 1),
142         };
143 
144         final List<String> expectedXpathQueries = Arrays.asList(
145                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
146                         + "'InputXpathVariableDeclarationUsageDistanceConstructor']]"
147                         + "/OBJBLOCK/CTOR_DEF[./IDENT[@text="
148                         + "'InputXpathVariableDeclarationUsageDistanceConstructor']]"
149                         + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]",
150                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
151                         + "'InputXpathVariableDeclarationUsageDistanceConstructor']]"
152                         + "/OBJBLOCK/CTOR_DEF[./IDENT[@text="
153                         + "'InputXpathVariableDeclarationUsageDistanceConstructor']]"
154                         + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]/MODIFIERS",
155                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
156                         + "'InputXpathVariableDeclarationUsageDistanceConstructor']]"
157                         + "/OBJBLOCK/CTOR_DEF[./IDENT[@text="
158                         + "'InputXpathVariableDeclarationUsageDistanceConstructor']]"
159                         + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]/TYPE",
160                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
161                         + "'InputXpathVariableDeclarationUsageDistanceConstructor']]"
162                         + "/OBJBLOCK/CTOR_DEF[./IDENT[@text="
163                         + "'InputXpathVariableDeclarationUsageDistanceConstructor']]"
164                         + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]/TYPE/LITERAL_INT"
165         );
166 
167         runVerifications(moduleConfig, fileToProcess, expectedViolation,
168                 expectedXpathQueries);
169     }
170 
171 }