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.ModifiedControlVariableCheck;
31  
32  public class XpathRegressionModifiedControlVariableTest extends AbstractXpathTestSupport {
33      private static final Class<ModifiedControlVariableCheck> CLAZZ =
34              ModifiedControlVariableCheck.class;
35  
36      @Override
37      protected String getCheckName() {
38          return CLAZZ.getSimpleName();
39      }
40  
41      @Override
42      protected String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/coding/modifiedcontrolvariable";
44      }
45  
46      @Test
47      public void testDefaultForLoop() throws Exception {
48          final File fileToProcess = new File(
49                  getPath("InputXpathModifiedControlVariableWithFor.java"));
50          final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
51          final String[] expectedViolation = {
52              "6:14: " + getCheckMessage(CLAZZ,
53                      ModifiedControlVariableCheck.MSG_KEY, "i"),
54          };
55  
56          final List<String> expectedXpathQueries = Arrays.asList(
57                  "/COMPILATION_UNIT/CLASS_DEF"
58                  + "[./IDENT[@text='InputXpathModifiedControlVariableWithFor']]"
59                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_FOR/SLIST/EXPR",
60                  "/COMPILATION_UNIT/CLASS_DEF"
61                  + "[./IDENT[@text='InputXpathModifiedControlVariableWithFor']]"
62                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
63                  + "/SLIST/LITERAL_FOR/SLIST/EXPR/POST_INC[./IDENT[@text='i']]");
64  
65          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
66      }
67  
68      @Test
69      public void testDefaultForeach() throws Exception {
70          final File fileToProcess = new File(
71                  getPath("InputXpathModifiedControlVariableWithForeach.java"));
72          final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
73          final String[] expectedViolation = {
74              "7:15: " + getCheckMessage(CLAZZ,
75                          ModifiedControlVariableCheck.MSG_KEY, "s"),
76          };
77          final List<String> expectedXpathQueries = Arrays.asList(
78                  "/COMPILATION_UNIT/CLASS_DEF"
79                  + "[./IDENT[@text='InputXpathModifiedControlVariableWithForeach']]"
80                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_FOR/SLIST/EXPR",
81                  "/COMPILATION_UNIT/CLASS_DEF"
82                  + "[./IDENT[@text='InputXpathModifiedControlVariableWithForeach']]"
83                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
84                  + "/SLIST/LITERAL_FOR/SLIST/EXPR/PLUS_ASSIGN[./IDENT[@text='s']]"
85          );
86          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
87      }
88  
89      @Test
90      public void testSkipEnhancedForLoop() throws Exception {
91          final File fileToProcess = new File(
92                  getPath("InputXpathModifiedControlVariableSkipEnhancedForLoop.java"));
93          final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
94          moduleConfig.addProperty("skipEnhancedForLoopVariable", "true");
95          final String[] expectedViolation = {
96              "10:14: " + getCheckMessage(CLAZZ,
97                          ModifiedControlVariableCheck.MSG_KEY, "i"),
98          };
99  
100         final List<String> expectedXpathQueries = Arrays.asList(
101                 "/COMPILATION_UNIT/CLASS_DEF"
102                 + "[./IDENT[@text='InputXpathModifiedControlVariableSkipEnhancedForLoop']]"
103                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_FOR/SLIST/EXPR",
104                 "/COMPILATION_UNIT/CLASS_DEF"
105                 + "[./IDENT[@text='InputXpathModifiedControlVariableSkipEnhancedForLoop']]"
106                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
107                 + "/SLIST/LITERAL_FOR/SLIST/EXPR/POST_INC[./IDENT[@text='i']]"
108         );
109         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
110     }
111 
112     @Test
113     public void testDefaultNestedForLoop() throws Exception {
114         final File fileToProcess = new File(
115                 getPath("InputXpathModifiedControlVariableNestedWithFor.java"));
116         final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
117         final String[] expectedViolation = {
118             "7:19: " + getCheckMessage(CLAZZ,
119                     ModifiedControlVariableCheck.MSG_KEY, "j"),
120         };
121 
122         final List<String> expectedXpathQueries = Arrays.asList(
123                 "/COMPILATION_UNIT/CLASS_DEF"
124                 + "[./IDENT[@text='InputXpathModifiedControlVariableNestedWithFor']]"
125                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
126                 + "/SLIST/LITERAL_FOR/SLIST/LITERAL_FOR/SLIST/EXPR",
127                 "/COMPILATION_UNIT/CLASS_DEF"
128                 + "[./IDENT[@text='InputXpathModifiedControlVariableNestedWithFor']]"
129                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
130                 + "/SLIST/LITERAL_FOR/SLIST"
131                 + "/LITERAL_FOR/SLIST/EXPR/STAR_ASSIGN[./IDENT[@text='j']]");
132 
133         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
134     }
135 
136     @Test
137     public void testForeachNested() throws Exception {
138         final File fileToProcess = new File(
139                 getPath("InputXpathModifiedControlVariableNestedWithForeach.java"));
140         final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
141         final String[] expectedViolation = {
142             "8:19: " + getCheckMessage(CLAZZ,
143                     ModifiedControlVariableCheck.MSG_KEY, "s"),
144         };
145 
146         final List<String> expectedXpathQueries = Arrays.asList(
147                 "/COMPILATION_UNIT/CLASS_DEF"
148                 + "[./IDENT[@text='InputXpathModifiedControlVariableNestedWithForeach']]"
149                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
150                 + "/SLIST/LITERAL_FOR/SLIST/LITERAL_FOR/SLIST/EXPR",
151                 "/COMPILATION_UNIT/CLASS_DEF"
152                 + "[./IDENT[@text='InputXpathModifiedControlVariableNestedWithForeach']]"
153                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
154                 + "/SLIST/LITERAL_FOR/SLIST/LITERAL_FOR/SLIST/EXPR"
155                 + "/PLUS_ASSIGN[./IDENT[@text='s']]");
156 
157         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
158     }
159 
160     @Test
161     public void testSkipEnhancedForLoopNested() throws Exception {
162         final File fileToProcess = new File(
163                 getPath("InputXpathModifiedControlVariableNestedSkipEnhancedForLoop.java"));
164         final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
165         moduleConfig.addProperty("skipEnhancedForLoopVariable", "true");
166         final String[] expectedViolation = {
167             "10:15: " + getCheckMessage(CLAZZ,
168                     ModifiedControlVariableCheck.MSG_KEY, "i"),
169         };
170 
171         final List<String> expectedXpathQueries = Arrays.asList(
172                 "/COMPILATION_UNIT/CLASS_DEF"
173                 + "[./IDENT[@text='InputXpathModifiedControlVariableNestedSkipEnhancedForLoop']]"
174                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_FOR/SLIST/EXPR",
175                 "/COMPILATION_UNIT/CLASS_DEF"
176                 + "[./IDENT[@text='InputXpathModifiedControlVariableNestedSkipEnhancedForLoop']]"
177                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_FOR/SLIST/EXPR"
178                 + "/PLUS_ASSIGN[./IDENT[@text='i']]");
179 
180         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
181     }
182 
183 }