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.GoogleNonConstantFieldNameCheck.MSG_KEY_INVALID_FORMAT;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29
30 public class GoogleNonConstantFieldNameCheckTest extends AbstractModuleTestSupport {
31
32 @Override
33 public String getPackageLocation() {
34 return "com/puppycrawl/tools/checkstyle/checks/naming/googlenonconstantfieldname";
35 }
36
37 @Test
38 public void testGetAcceptableTokens() {
39 final GoogleNonConstantFieldNameCheck checkObj = new GoogleNonConstantFieldNameCheck();
40 final int[] expected = {TokenTypes.VARIABLE_DEF};
41 assertWithMessage("Default acceptable tokens are invalid")
42 .that(checkObj.getAcceptableTokens())
43 .isEqualTo(expected);
44 }
45
46 @Test
47 public void testNonConstantFieldNameValid() throws Exception {
48 final String[] expected = {};
49 verifyWithInlineConfigParser(
50 getPath("InputGoogleNonConstantFieldNameValid.java"), expected);
51 }
52
53 @Test
54 public void testNonConstantFieldNameInvalid() throws Exception {
55 final String[] expected = {
56 "11:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "Foo"),
57 "13:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "f"),
58 "15:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "foo_bar"),
59 "17:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "foo_Bar"),
60 "19:16: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "foo__bar"),
61 "22:19: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "gradle_9_5_1"),
62 "25:15: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "jdk_9_0_392"),
63 "28:25: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "guava_33_4_5"),
64 "31:17: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "a_1"),
65 "34:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "guava33_4_5_"),
66 "37:23: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "guava33__4_5"),
67 "40:22: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "guava33_4_a"),
68 "43:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "_foo"),
69 "45:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "foo_"),
70 "47:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "__foo"),
71 "49:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "FOO"),
72 "51:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "f$bar"),
73 "54:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "FO"),
74 "56:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "mField"),
75 "58:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "pValue"),
76 "61:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "sInstance"),
77 "64:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "bFlag"),
78 "67:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "nCount"),
79 "70:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "iIndex"),
80 "73:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "a"),
81 "75:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "x"),
82 "77:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "z"),
83 "79:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "aB"),
84 "81:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "xY"),
85 "83:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "zA"),
86 "85:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "FooBar"),
87 "88:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "XMLParser"),
88 "91:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "HTTPClient"),
89 "94:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "foo_1bar"),
90 "97:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "foo1_bar"),
91 "100:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "foo_bar_baz"),
92 "103:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "foo$bar"),
93 "106:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "$foo"),
94 "109:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "bar$"),
95 "112:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "foo_1_a"),
96 "115:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "foo1__2"),
97 };
98 verifyWithInlineConfigParser(
99 getPath("InputGoogleNonConstantFieldNameInvalid.java"), expected);
100 }
101
102 @Test
103 public void testStaticFieldNameSkipped() throws Exception {
104 final String[] expected = {};
105 verifyWithInlineConfigParser(
106 getPath("InputGoogleNonConstantFieldNameStaticSkipped.java"), expected);
107 }
108
109 @Test
110 public void testLocalVariablesSkipped() throws Exception {
111 final String[] expected = {};
112 verifyWithInlineConfigParser(
113 getPath("InputGoogleNonConstantFieldNameLocalVariablesSkipped.java"),
114 expected);
115 }
116
117 @Test
118 public void testInterfaceAndAnnotationSkipped() throws Exception {
119 final String[] expected = {};
120 verifyWithInlineConfigParser(
121 getPath("InputGoogleNonConstantFieldNameInterfaceAnnotationSkipped.java"),
122 expected);
123 }
124
125 @Test
126 public void testInnerClasses() throws Exception {
127 final String[] expected = {
128 "12:13: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "Inner_Bad"),
129 "19:17: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "Nested_Bad"),
130 "28:13: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "Static_Bad"),
131 };
132 verifyWithInlineConfigParser(
133 getPath("InputGoogleNonConstantFieldNameInnerClasses.java"), expected);
134 }
135
136 @Test
137 public void testStaticFinalFieldNameSkipped() throws Exception {
138 final String[] expected = {};
139 verifyWithInlineConfigParser(
140 getPath("InputGoogleNonConstantFieldNameStaticFinalSkipped.java"), expected);
141 }
142
143 @Test
144 public void testNonConstantFieldNameMixedModifiers() throws Exception {
145 final String[] expected = {
146 "25:15: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "Final_Instance"),
147 "28:22: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "Public_Final"),
148 "31:23: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "mPrivateFinal"),
149 "34:25: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "f"),
150 "42:9: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "Instance_Bad"),
151 "45:16: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "Public_Bad"),
152 "48:17: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "mField"),
153 "51:19: " + getCheckMessage(MSG_KEY_INVALID_FORMAT, "a"),
154 };
155 verifyWithInlineConfigParser(
156 getPath("InputGoogleNonConstantFieldNameMixedModifiers.java"), expected);
157 }
158 }