1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.naming;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
30 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
32 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33
34 public class ConstantNameCheckTest
35 extends AbstractModuleTestSupport {
36
37 @Override
38 public String getPackageLocation() {
39 return "com/puppycrawl/tools/checkstyle/checks/naming/constantname";
40 }
41
42 @Test
43 public void testGetRequiredTokens() {
44 final ConstantNameCheck checkObj = new ConstantNameCheck();
45 final int[] expected = {TokenTypes.VARIABLE_DEF};
46 assertWithMessage("Default required tokens are invalid")
47 .that(checkObj.getRequiredTokens())
48 .isEqualTo(expected);
49 }
50
51 @Test
52 public void testIllegalRegexp() {
53 final DefaultConfiguration checkConfig =
54 createModuleConfig(ConstantNameCheck.class);
55 checkConfig.addProperty("format", "\\");
56 final CheckstyleException exc =
57 TestUtil.getExpectedThrowable(
58 CheckstyleException.class, () -> {
59 createChecker(checkConfig);
60 });
61 assertWithMessage("Invalid exception message")
62 .that(exc.getMessage())
63 .isEqualTo("cannot initialize module "
64 + "com.puppycrawl.tools.checkstyle.TreeWalker - "
65 + "cannot initialize module "
66 + "com.puppycrawl.tools.checkstyle.checks."
67 + "naming.ConstantNameCheck");
68 }
69
70 @Test
71 public void testDefaultPartA()
72 throws Exception {
73
74 final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
75
76 final String[] expected = {
77 "34:29: " + getCheckMessage(MSG_INVALID_PATTERN, "badConstant", pattern),
78 };
79 verifyWithInlineConfigParser(
80 getPath("InputConstantNameSimple1a.java"), expected);
81 }
82
83 @Test
84 public void testDefaultPartB()
85 throws Exception {
86
87 final String[] expected = {};
88
89 verifyWithInlineConfigParser(
90 getPath("InputConstantNameSimple1b.java"), expected);
91 }
92
93 @Test
94 public void testDefaultPartC()
95 throws Exception {
96
97 final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
98
99 final String[] expected = {
100 "26:30: " + getCheckMessage(MSG_INVALID_PATTERN, "BAD__NAME", pattern),
101 };
102 verifyWithInlineConfigParser(
103 getPath("InputConstantNameSimple1c.java"), expected);
104 }
105
106 @Test
107 public void testAccessControlTuningPartA()
108 throws Exception {
109
110 final String[] expected = {};
111
112 verifyWithInlineConfigParser(
113 getPath("InputConstantNameSimple2a.java"), expected);
114 }
115
116 @Test
117 public void testAccessControlTuningPartB()
118 throws Exception {
119
120 final String[] expected = {};
121
122 verifyWithInlineConfigParser(
123 getPath("InputConstantNameSimple2b.java"), expected);
124 }
125
126 @Test
127 public void testAccessControlTuningPartC()
128 throws Exception {
129
130 final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
131
132 final String[] expected = {
133 "26:30: " + getCheckMessage(MSG_INVALID_PATTERN, "BAD__NAME", pattern),
134 };
135 verifyWithInlineConfigParser(
136 getPath("InputConstantNameSimple2c.java"), expected);
137 }
138
139 @Test
140 public void testInterfaceAndAnnotation()
141 throws Exception {
142
143 final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
144
145 final String[] expected = {
146 "31:16: " + getCheckMessage(MSG_INVALID_PATTERN, "data", pattern),
147 "71:16: " + getCheckMessage(MSG_INVALID_PATTERN, "data", pattern),
148 };
149 verifyWithInlineConfigParser(
150 getPath("InputConstantNameInner.java"), expected);
151 }
152
153 @Test
154 public void testDefault1()
155 throws Exception {
156 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
157 verifyWithInlineConfigParser(
158 getPath("InputConstantName.java"), expected);
159 }
160
161 @Test
162 public void testIntoInterface() throws Exception {
163
164 final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
165
166 final String[] expected = {
167 "56:16: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
168 "57:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
169 "58:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
170 "59:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
171 "61:16: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
172 "62:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
173 "63:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
174 "64:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_private", pattern),
175 };
176 verifyWithInlineConfigParser(
177 getPath("InputConstantNameMemberExtended.java"), expected);
178 }
179
180 @Test
181 public void testIntoInterfaceExcludePublic() throws Exception {
182
183 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
184 verifyWithInlineConfigParser(
185 getPath("InputConstantNameInterfaceIgnorePublic.java"), expected);
186 }
187
188 @Test
189 public void testStaticMethodInInterface()
190 throws Exception {
191 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
192 verifyWithInlineConfigParser(
193 getPath("InputConstantNameStaticModifierInInterface.java"), expected);
194 }
195
196 @Test
197 public void testGetAcceptableTokens() {
198 final ConstantNameCheck constantNameCheckObj = new ConstantNameCheck();
199 final int[] actual = constantNameCheckObj.getAcceptableTokens();
200 final int[] expected = {
201 TokenTypes.VARIABLE_DEF,
202 };
203 assertWithMessage("Default acceptable should not be null")
204 .that(actual)
205 .isNotNull();
206 assertWithMessage("Default acceptable tokens are invalid")
207 .that(actual)
208 .isEqualTo(expected);
209 }
210
211 @Test
212 public void testApplyPropertiesSetters() throws Exception {
213
214 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
215
216 verifyWithInlineConfigParser(
217 getPath("InputConstantNameProperties.java"), expected);
218 }
219
220 }