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