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 static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
23
24 import java.io.File;
25 import java.util.Collections;
26 import java.util.List;
27
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31 import com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck;
32
33 public class XpathRegressionLocalVariableNameTest extends AbstractXpathTestSupport {
34
35 @Override
36 protected String getCheckName() {
37 return LocalVariableNameCheck.class.getSimpleName();
38 }
39
40 @Test
41 public void testMethod() throws Exception {
42 final File fileToProcess = new File(getPath("InputXpathLocalVariableNameMethod.java"));
43
44 final DefaultConfiguration moduleConfig =
45 createModuleConfig(LocalVariableNameCheck.class);
46
47 final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
48 final String[] expectedViolations = {
49 "5:9: " + getCheckMessage(LocalVariableNameCheck.class, MSG_INVALID_PATTERN,
50 "VAR", pattern),
51 };
52
53 final List<String> expectedXpathQueries = Collections.singletonList(
54 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
55 + "@text='InputXpathLocalVariableNameMethod']]"
56 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]"
57 + "/SLIST/VARIABLE_DEF/IDENT[@text='VAR']"
58 );
59
60 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
61
62 }
63
64 @Test
65 public void testIteration() throws Exception {
66 final File fileToProcess = new File(getPath("InputXpathLocalVariableNameIteration.java"));
67
68 final DefaultConfiguration moduleConfig = createModuleConfig(LocalVariableNameCheck.class);
69
70 final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
71 final String[] expectedViolations = {
72 "7:14: " + getCheckMessage(LocalVariableNameCheck.class, MSG_INVALID_PATTERN,
73 "var_1", pattern),
74 };
75
76 final List<String> expectedXpathQueries = Collections.singletonList(
77 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
78 + "@text='InputXpathLocalVariableNameIteration']]"
79 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]/"
80 + "SLIST/LITERAL_FOR/FOR_INIT/VARIABLE_DEF/IDENT[@text='var_1']"
81 );
82
83 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
84
85 }
86
87 @Test
88 public void testInnerClass() throws Exception {
89 final File fileToProcess = new File(getPath("InputXpathLocalVariableNameInnerClass.java"));
90
91 final DefaultConfiguration moduleConfig = createModuleConfig(LocalVariableNameCheck.class);
92
93 final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
94 final String[] expectedViolations = {
95 "6:11: " + getCheckMessage(LocalVariableNameCheck.class, MSG_INVALID_PATTERN,
96 "VAR", pattern),
97 };
98
99 final List<String> expectedXpathQueries = Collections.singletonList(
100 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
101 + "@text='InputXpathLocalVariableNameInnerClass']]"
102 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
103 + "METHOD_DEF[./IDENT[@text='myMethod']]/SLIST/VARIABLE_DEF/IDENT[@text='VAR']"
104 );
105
106 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
107
108 }
109
110 }