1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 static final Class<ModifiedControlVariableCheck> CLAZZ =
33 ModifiedControlVariableCheck.class;
34
35 @Override
36 protected String getCheckName() {
37 return CLAZZ.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(CLAZZ);
45 final String[] expectedViolation = {
46 "6:14: " + getCheckMessage(CLAZZ,
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(CLAZZ);
67 final String[] expectedViolation = {
68 "7:15: " + getCheckMessage(CLAZZ,
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(CLAZZ);
88 moduleConfig.addProperty("skipEnhancedForLoopVariable", "true");
89 final String[] expectedViolation = {
90 "10:14: " + getCheckMessage(CLAZZ,
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(CLAZZ);
111 final String[] expectedViolation = {
112 "7:19: " + getCheckMessage(CLAZZ,
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(CLAZZ);
135 final String[] expectedViolation = {
136 "8:19: " + getCheckMessage(CLAZZ,
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(CLAZZ);
159 moduleConfig.addProperty("skipEnhancedForLoopVariable", "true");
160 final String[] expectedViolation = {
161 "10:15: " + getCheckMessage(CLAZZ,
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 }