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.utils.CommonUtil;
32
33 public class ConstantNameCheckTest
34 extends AbstractModuleTestSupport {
35
36 @Override
37 public String getPackageLocation() {
38 return "com/puppycrawl/tools/checkstyle/checks/naming/constantname";
39 }
40
41 @Test
42 public void testGetRequiredTokens() {
43 final ConstantNameCheck checkObj = new ConstantNameCheck();
44 final int[] expected = {TokenTypes.VARIABLE_DEF};
45 assertWithMessage("Default required tokens are invalid")
46 .that(checkObj.getRequiredTokens())
47 .isEqualTo(expected);
48 }
49
50 @Test
51 public void testIllegalRegexp()
52 throws Exception {
53 final DefaultConfiguration checkConfig =
54 createModuleConfig(ConstantNameCheck.class);
55 checkConfig.addProperty("format", "\\");
56 try {
57 createChecker(checkConfig);
58 assertWithMessage("CheckstyleException is expected").fail();
59 }
60 catch (CheckstyleException exc) {
61 assertWithMessage("Invalid exception message")
62 .that(exc.getMessage())
63 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
64 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
65 + "naming.ConstantNameCheck");
66 }
67 }
68
69 @Test
70 public void testDefault()
71 throws Exception {
72
73 final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
74
75 final String[] expected = {
76 "31:29: " + getCheckMessage(MSG_INVALID_PATTERN, "badConstant", pattern),
77 "148:30: " + getCheckMessage(MSG_INVALID_PATTERN, "BAD__NAME", pattern),
78 };
79 verifyWithInlineConfigParser(
80 getPath("InputConstantNameSimple1.java"), expected);
81 }
82
83 @Test
84 public void testAccessControlTuning()
85 throws Exception {
86
87 final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
88
89 final String[] expected = {
90 "148:30: " + getCheckMessage(MSG_INVALID_PATTERN, "BAD__NAME", pattern),
91 };
92 verifyWithInlineConfigParser(
93 getPath("InputConstantNameSimple2.java"), expected);
94 }
95
96 @Test
97 public void testInterfaceAndAnnotation()
98 throws Exception {
99
100 final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
101
102 final String[] expected = {
103 "31:16: " + getCheckMessage(MSG_INVALID_PATTERN, "data", pattern),
104 "71:16: " + getCheckMessage(MSG_INVALID_PATTERN, "data", pattern),
105 };
106 verifyWithInlineConfigParser(
107 getPath("InputConstantNameInner.java"), expected);
108 }
109
110 @Test
111 public void testDefault1()
112 throws Exception {
113 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
114 verifyWithInlineConfigParser(
115 getPath("InputConstantName.java"), expected);
116 }
117
118 @Test
119 public void testIntoInterface() throws Exception {
120
121 final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
122
123 final String[] expected = {
124 "56:16: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
125 "57:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
126 "58:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
127 "59:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
128 "61:16: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
129 "62:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
130 "63:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
131 "64:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_private", pattern),
132 };
133 verifyWithInlineConfigParser(
134 getPath("InputConstantNameMemberExtended.java"), expected);
135 }
136
137 @Test
138 public void testIntoInterfaceExcludePublic() throws Exception {
139
140 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
141 verifyWithInlineConfigParser(
142 getPath("InputConstantNameInterfaceIgnorePublic.java"), expected);
143 }
144
145 @Test
146 public void testStaticMethodInInterface()
147 throws Exception {
148 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
149 verifyWithInlineConfigParser(
150 getPath("InputConstantNameStaticModifierInInterface.java"), expected);
151 }
152
153 @Test
154 public void testGetAcceptableTokens() {
155 final ConstantNameCheck constantNameCheckObj = new ConstantNameCheck();
156 final int[] actual = constantNameCheckObj.getAcceptableTokens();
157 final int[] expected = {
158 TokenTypes.VARIABLE_DEF,
159 };
160 assertWithMessage("Default acceptable should not be null")
161 .that(actual)
162 .isNotNull();
163 assertWithMessage("Default acceptable tokens are invalid")
164 .that(actual)
165 .isEqualTo(expected);
166 }
167
168 @Test
169 public void testApplyPropertiesSetters() throws Exception {
170
171 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
172
173 verifyWithInlineConfigParser(
174 getPath("InputConstantNameProperties.java"), expected);
175 }
176
177 }