1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }