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 java.io.File;
23 import java.util.Collections;
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.naming.AbstractNameCheck;
31 import com.puppycrawl.tools.checkstyle.checks.naming.StaticVariableNameCheck;
32
33 public class XpathRegressionStaticVariableNameTest extends AbstractXpathTestSupport {
34
35 private final String checkName = StaticVariableNameCheck.class.getSimpleName();
36
37 @Override
38 protected String getCheckName() {
39 return checkName;
40 }
41
42 @Override
43 protected String getPackageLocation() {
44 return "org/checkstyle/suppressionxpathfilter/naming/staticvariablename";
45 }
46
47 @Test
48 public void testStaticVariableName() throws Exception {
49 final File fileToProcess =
50 new File(getPath("InputXpathStaticVariableName.java"));
51
52 final String pattern = "^[a-z][a-zA-Z0-9]*$";
53 final DefaultConfiguration moduleConfig =
54 createModuleConfig(StaticVariableNameCheck.class);
55
56 final String[] expectedViolation = {
57 "6:24: " + getCheckMessage(StaticVariableNameCheck.class,
58 AbstractNameCheck.MSG_INVALID_PATTERN, "NUM2", pattern),
59 };
60
61 final List<String> expectedXpathQueries = Collections.singletonList(
62 "/COMPILATION_UNIT"
63 + "/CLASS_DEF[./IDENT[@text"
64 + "='InputXpathStaticVariableName']]"
65 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='NUM2']"
66
67 );
68 runVerifications(moduleConfig, fileToProcess, expectedViolation,
69 expectedXpathQueries);
70 }
71
72 @Test
73 public void testInnerClassField() throws Exception {
74 final File fileToProcess =
75 new File(getPath(
76 "InputXpathStaticVariableNameInnerClassField.java"));
77
78 final String pattern = "^[a-z][a-zA-Z0-9]*$";
79 final DefaultConfiguration moduleConfig =
80 createModuleConfig(StaticVariableNameCheck.class);
81
82 final String[] expectedViolation = {
83 "14:24: " + getCheckMessage(StaticVariableNameCheck.class,
84 AbstractNameCheck.MSG_INVALID_PATTERN, "NUM3", pattern),
85 };
86
87 final List<String> expectedXpathQueries = Collections.singletonList(
88 "/COMPILATION_UNIT"
89 + "/CLASS_DEF[./IDENT[@text"
90 + "='InputXpathStaticVariableNameInnerClassField']]"
91 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='outerMethod']]"
92 + "/SLIST/CLASS_DEF[./IDENT[@text='MyLocalClass']]"
93 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='NUM3']"
94 );
95 runVerifications(moduleConfig, fileToProcess, expectedViolation,
96 expectedXpathQueries);
97 }
98
99 @Test
100 public void testNoAccessModifier() throws Exception {
101 final File fileToProcess =
102 new File(getPath(
103 "InputXpathStaticVariableNameNoAccessModifier.java"));
104
105 final String pattern = "^[a-z][a-zA-Z0-9]*$";
106 final DefaultConfiguration moduleConfig =
107 createModuleConfig(StaticVariableNameCheck.class);
108
109 final String[] expectedViolation = {
110 "5:16: " + getCheckMessage(StaticVariableNameCheck.class,
111 AbstractNameCheck.MSG_INVALID_PATTERN, "NUM3", pattern),
112 };
113
114 final List<String> expectedXpathQueries = Collections.singletonList(
115 "/COMPILATION_UNIT"
116 + "/CLASS_DEF[./IDENT[@text"
117 + "='InputXpathStaticVariableNameNoAccessModifier']]"
118 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='NUM3']"
119 );
120 runVerifications(moduleConfig, fileToProcess, expectedViolation,
121 expectedXpathQueries);
122 }
123 }