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.LocalFinalVariableNameCheck.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.LocalFinalVariableNameCheck;
32
33 public class XpathRegressionLocalFinalVariableNameTest extends AbstractXpathTestSupport {
34
35 private final String checkName = LocalFinalVariableNameCheck.class.getSimpleName();
36
37 @Override
38 protected String getCheckName() {
39 return checkName;
40 }
41
42 @Test
43 public void testResource() throws Exception {
44 final File fileToProcess =
45 new File(getPath("InputXpathLocalFinalVariableNameResource.java"));
46
47 final DefaultConfiguration moduleConfig =
48 createModuleConfig(LocalFinalVariableNameCheck.class);
49 moduleConfig.addProperty("format", "^[A-Z][A-Z0-9]*$");
50 moduleConfig.addProperty("tokens", "PARAMETER_DEF,RESOURCE");
51
52 final String[] expectedViolation = {
53 "7:21: " + getCheckMessage(LocalFinalVariableNameCheck.class,
54 MSG_INVALID_PATTERN, "scanner", "^[A-Z][A-Z0-9]*$"),
55 };
56
57 final List<String> expectedXpathQueries = Collections.singletonList(
58 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
59 + "@text='InputXpathLocalFinalVariableNameResource']]"
60 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/LITERAL_TRY"
61 + "/RESOURCE_SPECIFICATION/RESOURCES/RESOURCE/IDENT[@text='scanner']"
62 );
63
64 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
65 }
66
67 @Test
68 public void testVariable() throws Exception {
69 final File fileToProcess =
70 new File(getPath("InputXpathLocalFinalVariableNameVar.java"));
71
72 final DefaultConfiguration moduleConfig =
73 createModuleConfig(LocalFinalVariableNameCheck.class);
74 moduleConfig.addProperty("format", "^[A-Z][a-z0-9]*$");
75
76 final String[] expectedViolation = {
77 "5:19: " + getCheckMessage(LocalFinalVariableNameCheck.class,
78 MSG_INVALID_PATTERN, "VAR1", "^[A-Z][a-z0-9]*$"),
79 };
80
81 final List<String> expectedXpathQueries = Collections.singletonList(
82 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
83 + "@text='InputXpathLocalFinalVariableNameVar']]"
84 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/VARIABLE_DEF"
85 + "/IDENT[@text='VAR1']"
86 );
87
88 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
89 }
90
91 @Test
92 public void testInnerClass() throws Exception {
93 final File fileToProcess =
94 new File(getPath("InputXpathLocalFinalVariableNameInner.java"));
95
96 final DefaultConfiguration moduleConfig =
97 createModuleConfig(LocalFinalVariableNameCheck.class);
98 moduleConfig.addProperty("format", "^[A-Z][a-z0-9]*$");
99
100 final String[] expectedViolation = {
101 "8:23: " + getCheckMessage(LocalFinalVariableNameCheck.class,
102 MSG_INVALID_PATTERN, "VAR1", "^[A-Z][a-z0-9]*$"),
103 };
104
105 final List<String> expectedXpathQueries = Collections.singletonList(
106 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
107 + "@text='InputXpathLocalFinalVariableNameInner']]"
108 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK"
109 + "/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/VARIABLE_DEF"
110 + "/IDENT[@text='VAR1']"
111 );
112
113 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
114 }
115 }