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 protected 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 + "Cannot set property 'option' to 'invalid_option'");
141 }
142 }
143
144 @Test
145 public void testAllowEmptyCaseWithText() throws Exception {
146 final String[] expected = {
147 "16:28: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
148 "22:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
149 "33:29: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
150 "35:37: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
151 "36:29: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
152 };
153 verifyWithInlineConfigParser(
154 getPath("InputEmptyBlockCase.java"), expected);
155 }
156
157 @Test
158 public void testForbidCaseWithoutStmt() throws Exception {
159 final String[] expected = {
160 "16:28: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
161 "22:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
162 "26:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
163 "33:29: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
164 "35:37: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
165 "36:29: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
166 "36:40: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
167 };
168 verifyWithInlineConfigParser(
169 getPath("InputEmptyBlockCase2.java"), expected);
170 }
171
172 @Test
173 public void testAllowEmptyDefaultWithText() throws Exception {
174 final String[] expected = {
175 "15:30: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
176 "21:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
177 "46:22: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
178 "54:47: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
179 "60:22: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
180 "88:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
181 };
182 verifyWithInlineConfigParser(
183 getPath("InputEmptyBlockDefault.java"), expected);
184 }
185
186 @Test
187 public void testForbidDefaultWithoutStatement() throws Exception {
188 final String[] expected = {
189 "15:30: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
190 "21:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
191 "25:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
192 "36:30: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
193 "46:22: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
194 "54:47: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
195 "60:22: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
196 "75:22: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
197 "88:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
198 };
199 verifyWithInlineConfigParser(
200 getPath("InputEmptyBlockDefault2.java"), expected);
201 }
202
203 @Test
204 public void testEmptyBlockWithEmoji() throws Exception {
205 final String[] expected = {
206 "15:12: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "STATIC_INIT"),
207 "25:27: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "if"),
208 "28:34: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "if"),
209 "30:62: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "for"),
210 "31:25: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "if"),
211 "33:25: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "switch"),
212 "40:22: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
213 "45:46: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
214 };
215 verifyWithInlineConfigParser(
216 getPath("InputEmptyBlockWithEmoji.java"), expected);
217
218 }
219
220 @Test
221 public void testAnnotationDefaultKeyword() throws Exception {
222 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
223 final String path = getPath("InputEmptyBlockAnnotationDefaultKeyword.java");
224 verifyWithInlineConfigParser(
225 path, expected);
226 }
227
228 @Test
229 public void testEmptyBlockSwitchExpressionsOne() throws Exception {
230 final String[] expected = {
231 "17:30: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
232 };
233 verifyWithInlineConfigParser(
234 getNonCompilablePath("InputEmptyBlockSwitchExpressionsOne.java"), expected);
235 }
236
237 @Test
238 public void testEmptyBlockSwitchExpressionsTwo() throws Exception {
239 final String[] expected = {
240 "25:32: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
241 "27:26: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
242 };
243 verifyWithInlineConfigParser(
244 getNonCompilablePath("InputEmptyBlockSwitchExpressionsTwo.java"), expected);
245 }
246
247 @Test
248 public void testUppercaseProperty() throws Exception {
249 final String[] expected = {
250 "16:30: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
251 "22:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
252 };
253 verifyWithInlineConfigParser(
254 getPath("InputEmptyBlockTestUppercaseOptionProperty.java"), expected);
255 }
256
257 @Test
258 public void testEmptyBlockCaseAndDefaultWithTextOption() throws Exception {
259 final String[] expected = {
260 "20:28: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
261 "24:22: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
262 "33:30: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
263 "37:24: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
264 };
265 verifyWithInlineConfigParser(
266 getNonCompilablePath("InputEmptyBlockCaseAndDefaultWithTextOption.java"),
267 expected);
268 }
269 }