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.blocks;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck.MSG_KEY_BLOCK_EMPTY;
24 import static com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck.MSG_KEY_BLOCK_NO_STATEMENT;
25
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
30 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31
32 public class EmptyBlockCheckTest
33 extends AbstractModuleTestSupport {
34
35 @Override
36 public String getPackageLocation() {
37 return "com/puppycrawl/tools/checkstyle/checks/blocks/emptyblock";
38 }
39
40
41
42
43
44 @Test
45 public void testBlockOptionValueOf() {
46 final BlockOption option = BlockOption.valueOf("TEXT");
47 assertWithMessage("Invalid valueOf result")
48 .that(option)
49 .isEqualTo(BlockOption.TEXT);
50 }
51
52 @Test
53 public void testDefault()
54 throws Exception {
55 final String[] expected = {
56 "38:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
57 "40:17: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
58 "42:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
59 "45:17: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
60 "68:5: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
61 "76:29: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
62 "78:41: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
63 "89:12: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
64 };
65 verifyWithInlineConfigParser(
66 getPath("InputEmptyBlockSemantic.java"), expected);
67 }
68
69 @Test
70 public void testText()
71 throws Exception {
72 final String[] expected = {
73 "38:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "try"),
74 "40:17: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "finally"),
75 "68:5: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "INSTANCE_INIT"),
76 "76:29: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "synchronized"),
77 "88:12: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "STATIC_INIT"),
78 };
79 verifyWithInlineConfigParser(
80 getPath("InputEmptyBlockSemanticText.java"), expected);
81 }
82
83 @Test
84 public void testStatement()
85 throws Exception {
86 final String[] expected = {
87 "38:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
88 "40:17: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
89 "42:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
90 "45:17: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
91 "68:5: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
92 "76:29: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
93 "78:41: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
94 "89:12: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
95 };
96 verifyWithInlineConfigParser(
97 getPath("InputEmptyBlockSemanticStatement.java"), expected);
98 }
99
100 @Test
101 public void allowEmptyLoops() throws Exception {
102 final String[] expected = {
103 "21:21: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
104 "24:34: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
105 "27:21: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
106 "28:20: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
107 };
108 verifyWithInlineConfigParser(
109 getPath("InputEmptyBlockSemantic2Statement.java"), expected);
110 }
111
112 @Test
113 public void allowEmptyLoopsText() throws Exception {
114 final String[] expected = {
115 "26:21: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "if"),
116 "29:34: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "if"),
117 "32:21: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "if"),
118 "33:20: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "switch"),
119 };
120 verifyWithInlineConfigParser(
121 getPath("InputEmptyBlockSemantic2Text.java"), expected);
122 }
123
124 @Test
125 public void testInvalidOption() throws Exception {
126
127 try {
128 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
129
130 verifyWithInlineConfigParser(
131 getPath("InputEmptyBlockSemanticInvalid.java"), expected);
132 assertWithMessage("exception expected").fail();
133 }
134 catch (CheckstyleException exc) {
135 assertWithMessage("Invalid exception message")
136 .that(exc.getMessage())
137 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
138 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
139 + "blocks.EmptyBlockCheck");
140 }
141 }
142
143 @Test
144 public void testAllowEmptyCaseWithText() throws Exception {
145 final String[] expected = {
146 "16:28: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
147 "22:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
148 "33:29: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
149 "35:37: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
150 "36:29: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
151 };
152 verifyWithInlineConfigParser(
153 getPath("InputEmptyBlockCase.java"), expected);
154 }
155
156 @Test
157 public void testForbidCaseWithoutStmt() throws Exception {
158 final String[] expected = {
159 "16:28: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
160 "22:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
161 "26:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
162 "33:29: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
163 "35:37: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
164 "36:29: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
165 "36:40: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
166 };
167 verifyWithInlineConfigParser(
168 getPath("InputEmptyBlockCase2.java"), expected);
169 }
170
171 @Test
172 public void testAllowEmptyDefaultWithText() throws Exception {
173 final String[] expected = {
174 "15:30: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
175 "21:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
176 "46:22: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
177 "54:47: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
178 "60:22: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
179 "88:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
180 };
181 verifyWithInlineConfigParser(
182 getPath("InputEmptyBlockDefault.java"), expected);
183 }
184
185 @Test
186 public void testForbidDefaultWithoutStatement() throws Exception {
187 final String[] expected = {
188 "15:30: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
189 "21:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
190 "25:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
191 "36:30: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
192 "46:22: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
193 "54:47: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
194 "60:22: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
195 "75:22: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
196 "88:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
197 };
198 verifyWithInlineConfigParser(
199 getPath("InputEmptyBlockDefault2.java"), expected);
200 }
201
202 @Test
203 public void testEmptyBlockWithEmoji() throws Exception {
204 final String[] expected = {
205 "15:12: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "STATIC_INIT"),
206 "25:27: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "if"),
207 "28:34: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "if"),
208 "30:62: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "for"),
209 "31:25: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "if"),
210 "33:25: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "switch"),
211 "40:22: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
212 "45:46: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
213 };
214 verifyWithInlineConfigParser(
215 getPath("InputEmptyBlockWithEmoji.java"), expected);
216
217 }
218
219 @Test
220 public void testAnnotationDefaultKeyword() throws Exception {
221 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
222 final String path = getPath("InputEmptyBlockAnnotationDefaultKeyword.java");
223 verifyWithInlineConfigParser(
224 path, expected);
225 }
226
227 @Test
228 public void testEmptyBlockSwitchExpressionsOne() throws Exception {
229 final String[] expected = {
230 "17:30: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
231 };
232 verifyWithInlineConfigParser(
233 getPath("InputEmptyBlockSwitchExpressionsOne.java"), expected);
234 }
235
236 @Test
237 public void testEmptyBlockSwitchExpressionsTwo() throws Exception {
238 final String[] expected = {
239 "25:32: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
240 "27:26: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
241 };
242 verifyWithInlineConfigParser(
243 getPath("InputEmptyBlockSwitchExpressionsTwo.java"), expected);
244 }
245
246 @Test
247 public void testUppercaseProperty() throws Exception {
248 final String[] expected = {
249 "16:30: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
250 "22:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
251 };
252 verifyWithInlineConfigParser(
253 getPath("InputEmptyBlockTestUppercaseOptionProperty.java"), expected);
254 }
255
256 @Test
257 public void testEmptyBlockCaseAndDefaultWithTextOption() throws Exception {
258 final String[] expected = {
259 "20:28: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
260 "24:22: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
261 "33:30: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
262 "37:24: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
263 };
264 verifyWithInlineConfigParser(
265 getPath("InputEmptyBlockCaseAndDefaultWithTextOption.java"),
266 expected);
267 }
268 }