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