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.modifier;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.modifier.InterfaceMemberImpliedModifierCheck.MSG_KEY;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28 import com.puppycrawl.tools.checkstyle.DetailAstImpl;
29 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
31 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
32
33 public class ClassMemberImpliedModifierCheckTest
34 extends AbstractModuleTestSupport {
35
36 @Override
37 public String getPackageLocation() {
38 return "com/puppycrawl/tools/checkstyle/checks/modifier/classmemberimpliedmodifier";
39 }
40
41 @Test
42 public void testMethodsOnClass() throws Exception {
43 final String[] expected = {
44 "52:9: " + getCheckMessage(MSG_KEY, "static"),
45 "60:9: " + getCheckMessage(MSG_KEY, "static"),
46 "65:5: " + getCheckMessage(MSG_KEY, "static"),
47 "73:9: " + getCheckMessage(MSG_KEY, "static"),
48 "81:9: " + getCheckMessage(MSG_KEY, "static"),
49 "89:5: " + getCheckMessage(MSG_KEY, "static"),
50 };
51 verifyWithInlineConfigParser(
52 getPath("InputClassMemberImpliedModifierOnClass.java"),
53 expected);
54 }
55
56 @Test
57 public void testGetRequiredTokens() {
58 final ClassMemberImpliedModifierCheck check = new ClassMemberImpliedModifierCheck();
59 final int[] actual = check.getRequiredTokens();
60 final int[] expected = {
61 TokenTypes.INTERFACE_DEF,
62 TokenTypes.ENUM_DEF,
63 TokenTypes.RECORD_DEF,
64 };
65 assertWithMessage("Required tokens are invalid")
66 .that(actual)
67 .isEqualTo(expected);
68 }
69
70 @Test
71 public void testMethodsOnClassNoImpliedStaticEnum() throws Exception {
72 final String[] expected = {
73 "60:9: " + getCheckMessage(MSG_KEY, "static"),
74 "79:9: " + getCheckMessage(MSG_KEY, "static"),
75 "87:5: " + getCheckMessage(MSG_KEY, "static"),
76 };
77 verifyWithInlineConfigParser(
78 getPath("InputClassMemberImpliedModifierOnClassSetEnumFalse.java"),
79 expected);
80 }
81
82 @Test
83 public void testMethodsOnClassNoImpliedStaticInterface() throws Exception {
84 final String[] expected = {
85 "53:9: " + getCheckMessage(MSG_KEY, "static"),
86 "65:5: " + getCheckMessage(MSG_KEY, "static"),
87 "73:9: " + getCheckMessage(MSG_KEY, "static"),
88 };
89 verifyWithInlineConfigParser(
90 getPath("InputClassMemberImpliedModifierOnClassSetInterfaceFalse.java"),
91 expected);
92 }
93
94 @Test
95 public void testMethodsOnClassNoViolationsChecked() throws Exception {
96 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
97 verifyWithInlineConfigParser(
98 getPath("InputClassMemberImpliedModifierOnClassNoViolations.java"),
99 expected);
100 }
101
102 @Test
103 public void testMethodsOnInterface() throws Exception {
104 final String[] expected = {
105 "61:13: " + getCheckMessage(MSG_KEY, "static"),
106 "69:13: " + getCheckMessage(MSG_KEY, "static"),
107 "74:9: " + getCheckMessage(MSG_KEY, "static"),
108 "82:13: " + getCheckMessage(MSG_KEY, "static"),
109 "90:13: " + getCheckMessage(MSG_KEY, "static"),
110 "98:9: " + getCheckMessage(MSG_KEY, "static"),
111 };
112 verifyWithInlineConfigParser(
113 getPath("InputClassMemberImpliedModifierOnInterface.java"),
114 expected);
115 }
116
117 @Test
118 public void testClassMemberImpliedModifierRecords() throws Exception {
119 final String[] expected = {
120 "16:5: " + getCheckMessage(MSG_KEY, "static"),
121 "20:5: " + getCheckMessage(MSG_KEY, "static"),
122 "24:5: " + getCheckMessage(MSG_KEY, "static"),
123 "29:9: " + getCheckMessage(MSG_KEY, "static"),
124 "33:9: " + getCheckMessage(MSG_KEY, "static"),
125 "37:9: " + getCheckMessage(MSG_KEY, "static"),
126 "42:9: " + getCheckMessage(MSG_KEY, "static"),
127 };
128 verifyWithInlineConfigParser(
129 getNonCompilablePath("InputClassMemberImpliedModifierRecords.java"),
130 expected);
131 }
132
133 @Test
134 public void testClassMemberImpliedModifierNoViolationRecords() throws Exception {
135 final String[] expected = {
136 "16:5: " + getCheckMessage(MSG_KEY, "static"),
137 "20:5: " + getCheckMessage(MSG_KEY, "static"),
138 "33:9: " + getCheckMessage(MSG_KEY, "static"),
139 "37:9: " + getCheckMessage(MSG_KEY, "static"),
140 };
141 verifyWithInlineConfigParser(
142 getNonCompilablePath(
143 "InputClassMemberImpliedModifierNoViolationRecords.java"),
144 expected);
145 }
146
147 @Test
148 public void testCompactSourceFile() throws Exception {
149 final String[] expected = {
150 "12:1: " + getCheckMessage(MSG_KEY, "static"),
151 "14:1: " + getCheckMessage(MSG_KEY, "static"),
152 "17:1: " + getCheckMessage(MSG_KEY, "static"),
153 "19:1: " + getCheckMessage(MSG_KEY, "static"),
154 };
155 verifyWithInlineConfigParser(
156 getNonCompilablePath(
157 "compact/InputClassMemberImpliedModifierCompactSource.java"),
158 expected);
159 }
160
161 @Test
162 public void testCompactSourceFilePropertiesDisabled() throws Exception {
163 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
164 verifyWithInlineConfigParser(
165 getNonCompilablePath(
166 "compact/InputClassMemberImpliedModifierCompactSourceCustom.java"),
167 expected);
168 }
169
170 @Test
171 public void testIllegalState() {
172 final DetailAstImpl init = new DetailAstImpl();
173 init.setType(TokenTypes.STATIC_INIT);
174 final DetailAstImpl objBlock = new DetailAstImpl();
175 objBlock.setType(TokenTypes.OBJBLOCK);
176 objBlock.addChild(init);
177 final DetailAstImpl interfaceAst = new DetailAstImpl();
178 interfaceAst.setType(TokenTypes.CLASS_DEF);
179 interfaceAst.addChild(objBlock);
180 final ClassMemberImpliedModifierCheck check =
181 new ClassMemberImpliedModifierCheck();
182 final IllegalStateException exc =
183 TestUtil.getExpectedThrowable(
184 IllegalStateException.class, () -> {
185 check.visitToken(init);
186 });
187 assertWithMessage("Error message is unexpected")
188 .that(exc.getMessage())
189 .isEqualTo(init.toString());
190 }
191
192 }