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 ex) {
135 assertWithMessage("Invalid exception message")
136 .that(ex.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 testEmptyBlockSwitchExpressions() throws Exception {
230 final String[] expected = {
231 "17:30: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
232 "116:32: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
233 "118:26: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
234 };
235 verifyWithInlineConfigParser(
236 getNonCompilablePath("InputEmptyBlockSwitchExpressions.java"), expected);
237 }
238
239 @Test
240 public void testUppercaseProperty() throws Exception {
241 final String[] expected = {
242 "16:30: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
243 "22:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
244 };
245 verifyWithInlineConfigParser(
246 getPath("InputEmptyBlockTestUppercaseOptionProperty.java"), expected);
247 }
248
249 @Test
250 public void testEmptyBlockCaseAndDefaultWithTextOption() throws Exception {
251 final String[] expected = {
252 "20:28: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
253 "24:22: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
254 "33:30: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
255 "37:24: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
256 };
257 verifyWithInlineConfigParser(
258 getNonCompilablePath("InputEmptyBlockCaseAndDefaultWithTextOption.java"),
259 expected);
260 }
261 }