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.ConstantNameCheck;
33
34 public class XpathRegressionConstantNameTest extends AbstractXpathTestSupport {
35
36 private static final Class<ConstantNameCheck> CLASS = ConstantNameCheck.class;
37 private static final String PATTERN = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
38 private final String checkName = CLASS.getSimpleName();
39
40 @Override
41 protected String getCheckName() {
42 return checkName;
43 }
44
45 @Override
46 protected String getPackageLocation() {
47 return "org/checkstyle/suppressionxpathfilter/naming/constantname";
48 }
49
50 @Test
51 public void testLowercase() throws Exception {
52 final File fileToProcess = new File(
53 getPath("InputXpathConstantNameLowercase.java"));
54
55 final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
56
57 final String[] expectedViolation = {
58 "4:29: " + getCheckMessage(CLASS, MSG_INVALID_PATTERN, "number", PATTERN),
59 };
60
61 final List<String> expectedXpathQueries = Collections.singletonList(
62 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
63 + "[@text='InputXpathConstantNameLowercase']]"
64 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='number']"
65 );
66
67 runVerifications(moduleConfig, fileToProcess, expectedViolation,
68 expectedXpathQueries);
69 }
70
71 @Test
72 public void testCamelCase() throws Exception {
73 final File fileToProcess =
74 new File(getPath("InputXpathConstantNameCamelCase.java"));
75
76 final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
77
78 final String[] expectedViolation = {
79 "4:29: " + getCheckMessage(CLASS,
80 MSG_INVALID_PATTERN, "badConstant", PATTERN),
81 };
82
83 final List<String> expectedXpathQueries = Collections.singletonList(
84 "/COMPILATION_UNIT/CLASS_DEF"
85 + "[./IDENT[@text='InputXpathConstantNameCamelCase']]"
86 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='badConstant']"
87 );
88
89 runVerifications(moduleConfig, fileToProcess, expectedViolation,
90 expectedXpathQueries);
91 }
92
93 @Test
94 public void testWithBeginningUnderscore() throws Exception {
95 final File fileToProcess = new File(
96 getPath("InputXpathConstantNameWithBeginningUnderscore.java"));
97
98 final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
99
100 final String[] expectedViolation = {
101 "4:33: " + getCheckMessage(CLASS, MSG_INVALID_PATTERN, "_CONSTANT", PATTERN),
102 };
103
104 final List<String> expectedXpathQueries = Collections.singletonList(
105 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
106 + "[@text='InputXpathConstantNameWithBeginningUnderscore']]"
107 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='_CONSTANT']"
108 );
109
110 runVerifications(moduleConfig, fileToProcess, expectedViolation,
111 expectedXpathQueries);
112 }
113
114 @Test
115 public void testWithTwoUnderscores() throws Exception {
116 final File fileToProcess = new File(
117 getPath("InputXpathConstantNameWithTwoUnderscores.java"));
118
119 final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
120
121 final String[] expectedViolation = {
122 "4:30: " + getCheckMessage(CLASS, MSG_INVALID_PATTERN, "BAD__NAME", PATTERN),
123 };
124
125 final List<String> expectedXpathQueries = Collections.singletonList(
126 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
127 + "[@text='InputXpathConstantNameWithTwoUnderscores']]"
128 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='BAD__NAME']"
129 );
130
131 runVerifications(moduleConfig, fileToProcess, expectedViolation,
132 expectedXpathQueries);
133 }
134 }