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