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.design;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck.MSG_KEY;
24
25 import java.util.List;
26
27 import org.junit.jupiter.api.Test;
28
29 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
32
33 public class OneTopLevelClassCheckTest extends AbstractModuleTestSupport {
34
35 @Override
36 public String getPackageLocation() {
37 return "com/puppycrawl/tools/checkstyle/checks/design/onetoplevelclass";
38 }
39
40 @Test
41 public void testGetRequiredTokens() {
42 final OneTopLevelClassCheck checkObj = new OneTopLevelClassCheck();
43 final int[] expected = {TokenTypes.COMPILATION_UNIT};
44 assertWithMessage("Required tokens are invalid.")
45 .that(checkObj.getRequiredTokens())
46 .isEqualTo(expected);
47 }
48
49 @Test
50 public void testClearState() throws Exception {
51 final String firstInputFilePath = getPath("InputOneTopLevelClassDeclarationOrder.java");
52 final String secondInputFilePath = getPath("InputOneTopLevelClassInterface2.java");
53
54 final List<String> expectedFirstInput = List.of(
55 "17:1: " + getCheckMessage(MSG_KEY, "InputDeclarationOrderEnum"),
56 "28:1: " + getCheckMessage(MSG_KEY, "InputDeclarationOrderAnnotation"));
57 final List<String> expectedSecondInput = List.of(
58 "10:1: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassInterface2inner1"),
59 "19:1: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassInterface2inner2"));
60
61 verifyWithInlineConfigParser(firstInputFilePath, secondInputFilePath,
62 expectedFirstInput, expectedSecondInput);
63 }
64
65 @Test
66 public void testAcceptableTokens() {
67 final OneTopLevelClassCheck check = new OneTopLevelClassCheck();
68 final int[] expected = {TokenTypes.COMPILATION_UNIT};
69 assertWithMessage("Default required tokens are invalid")
70 .that(check.getAcceptableTokens())
71 .isEqualTo(expected);
72 }
73
74 @Test
75 public void testFileWithOneTopLevelClass() throws Exception {
76 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
77 verifyWithInlineConfigParser(
78 getPath("InputOneTopLevelClass.java"), expected);
79 }
80
81 @Test
82 public void testFileWithOneTopLevelInterface() throws Exception {
83 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
84 verifyWithInlineConfigParser(
85 getPath("InputOneTopLevelClassInterface.java"), expected);
86 }
87
88 @Test
89 public void testFileWithOneTopLevelEnum() throws Exception {
90 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
91 verifyWithInlineConfigParser(
92 getPath("InputOneTopLevelClassEnum.java"), expected);
93 }
94
95 @Test
96 public void testFileWithOneTopLevelAnnotation() throws Exception {
97 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
98 verifyWithInlineConfigParser(
99 getPath("InputOneTopLevelClassAnnotation.java"), expected);
100 }
101
102 @Test
103 public void testFileWithNoPublicTopLevelClass() throws Exception {
104 final String[] expected = {
105 "15:1: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassNoPublic2"),
106 };
107 verifyWithInlineConfigParser(
108 getPath("InputOneTopLevelClassNoPublic.java"), expected);
109 }
110
111 @Test
112 public void testFileWithThreeTopLevelInterface() throws Exception {
113 final String[] expected = {
114 "10:1: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassInterface3inner1"),
115 "19:1: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassInterface3inner2"),
116 };
117 verifyWithInlineConfigParser(
118 getPath("InputOneTopLevelClassInterface3.java"), expected);
119 }
120
121 @Test
122 public void testFileWithThreeTopLevelEnum() throws Exception {
123 final String[] expected = {
124 "10:1: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassEnum2inner1"),
125 "19:1: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassEnum2inner2"),
126 };
127 verifyWithInlineConfigParser(
128 getPath("InputOneTopLevelClassEnum2.java"), expected);
129 }
130
131 @Test
132 public void testFileWithThreeTopLevelAnnotation() throws Exception {
133 final String[] expected = {
134 "16:1: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassAnnotation2A"),
135 "22:1: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassAnnotation2B"),
136 };
137 verifyWithInlineConfigParser(
138 getPath("InputOneTopLevelClassAnnotation2.java"), expected);
139 }
140
141 @Test
142 public void testFileWithFewTopLevelClasses() throws Exception {
143 final String[] expected = {
144 "32:1: " + getCheckMessage(MSG_KEY, "NoSuperClone"),
145 "37:1: " + getCheckMessage(MSG_KEY, "InnerClone"),
146 "42:1: " + getCheckMessage(MSG_KEY, "CloneWithTypeArguments"),
147 "47:1: " + getCheckMessage(MSG_KEY, "CloneWithTypeArgumentsAndNoSuper"),
148 "52:1: " + getCheckMessage(MSG_KEY, "MyClassWithGenericSuperMethod"),
149 "57:1: " + getCheckMessage(MSG_KEY, "AnotherClass"),
150 "61:1: " + getCheckMessage(MSG_KEY, "NativeTest"),
151 };
152 verifyWithInlineConfigParser(
153 getPath("InputOneTopLevelClassClone.java"), expected);
154 }
155
156 @Test
157 public void testFileWithSecondEnumTopLevelClass() throws Exception {
158 final String[] expected = {
159 "17:1: " + getCheckMessage(MSG_KEY, "InputDeclarationOrderEnum2"),
160 "28:1: " + getCheckMessage(MSG_KEY, "InputDeclarationOrderAnnotation2"),
161 };
162 verifyWithInlineConfigParser(
163 getPath("InputOneTopLevelClassDeclarationOrder2.java"), expected);
164 }
165
166 @Test
167 public void testPackageInfoWithNoTypesDeclared() throws Exception {
168 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
169 verifyWithInlineConfigParser(
170 getPath("package-info.java"), expected);
171 }
172
173 @Test
174 public void testFileWithMultipleSameLine() throws Exception {
175 final String[] expected = {
176 "10:47: " + getCheckMessage(MSG_KEY, "ViolatingSecondType"),
177 };
178 verifyWithInlineConfigParser(
179 getPath("InputOneTopLevelClassSameLine.java"), expected);
180 }
181
182 @Test
183 public void testFileWithIndentation() throws Exception {
184 final String[] expected = {
185 "14:2: " + getCheckMessage(MSG_KEY, "ViolatingIndentedClass1"),
186 "18:5: " + getCheckMessage(MSG_KEY, "ViolatingIndentedClass2"),
187 "22:1: " + getCheckMessage(MSG_KEY, "ViolatingNonIndentedInterface"),
188 };
189 verifyWithInlineConfigParser(
190 getPath("InputOneTopLevelClassIndentation.java"), expected);
191 }
192
193 @Test
194 public void testOneTopLevelClassRecords() throws Exception {
195 final String[] expected = {
196 "14:1: " + getCheckMessage(MSG_KEY, "TestRecord1"),
197 "19:1: " + getCheckMessage(MSG_KEY, "TestRecord2"),
198 };
199 verifyWithInlineConfigParser(
200 getPath("InputOneTopLevelClassRecords.java"), expected);
201 }
202
203 @Test
204 public void testOneTopLevelClassEmpty() throws Exception {
205 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
206 verifyWithInlineConfigParser(
207 getPath("InputOneTopLevelClassEmpty.java"), expected);
208 }
209
210 }