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