View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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.LeftCurlyCheck.MSG_KEY_LINE_BREAK_AFTER;
24  import static com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck.MSG_KEY_LINE_NEW;
25  import static com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck.MSG_KEY_LINE_PREVIOUS;
26  
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33  
34  public class LeftCurlyCheckTest extends AbstractModuleTestSupport {
35  
36      @Override
37      protected String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/blocks/leftcurly";
39      }
40  
41      /* Additional test for jacoco, since valueOf()
42       * is generated by javac and jacoco reports that
43       * valueOf() is uncovered.
44       */
45      @Test
46      public void testLeftCurlyOptionValueOf() {
47          final LeftCurlyOption option = LeftCurlyOption.valueOf("NL");
48          assertWithMessage("Invalid valueOf result")
49              .that(option)
50              .isEqualTo(LeftCurlyOption.NL);
51      }
52  
53      @Test
54      public void testGetRequiredTokens() {
55          final LeftCurlyCheck checkObj = new LeftCurlyCheck();
56          assertWithMessage("LeftCurlyCheck#getRequiredTokens should return empty array by default")
57              .that(checkObj.getRequiredTokens())
58              .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
59      }
60  
61      @Test
62      public void testDefault() throws Exception {
63          final String[] expected = {
64              "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
65              "19:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
66              "23:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
67              "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
68              "31:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
69          };
70          verifyWithInlineConfigParser(
71                  getPath("InputLeftCurlyTestDefault.java"), expected);
72      }
73  
74      @Test
75      public void testNl() throws Exception {
76          final String[] expected = {
77              "36:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
78              "40:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
79              "45:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
80              "49:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
81              "54:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
82              "59:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
83              "64:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
84              "67:27: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 27),
85              "68:23: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 23),
86              "69:25: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 25),
87          };
88          verifyWithInlineConfigParser(
89                  getPath("InputLeftCurlyDefaultTestNl.java"), expected);
90      }
91  
92      @Test
93      public void testNlow() throws Exception {
94          final String[] expected = {
95              "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
96              "19:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
97              "23:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
98              "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
99              "31:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
100             "36:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
101             "40:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
102             "45:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
103             "49:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
104             "54:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
105             "59:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
106             "64:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
107         };
108         verifyWithInlineConfigParser(
109                 getPath("InputLeftCurlyDefaultTestNlow.java"), expected);
110     }
111 
112     @Test
113     public void testDefault2() throws Exception {
114         final String[] expected = {
115             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
116             "22:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
117             "29:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
118             "32:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
119             "36:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
120             "44:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
121             "46:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
122             "51:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
123             "54:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
124             "58:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
125             "70:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
126             "74:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
127             "82:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
128             "85:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
129             "89:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
130         };
131         verifyWithInlineConfigParser(
132                 getPath("InputLeftCurlyMethod.java"), expected);
133     }
134 
135     @Test
136     public void testNewline2() throws Exception {
137         final String[] expected = {
138             "19:44: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 44),
139             "26:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
140             "39:31: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 31),
141             "48:24: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 24),
142             "61:35: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 35),
143             "65:17: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 17),
144             "79:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
145             "92:31: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 31),
146         };
147         verifyWithInlineConfigParser(
148                 getPath("InputLeftCurlyMethodTestNewLine2.java"), expected);
149     }
150 
151     @Test
152     public void testDefault3() throws Exception {
153         final String[] expected = {
154             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
155             "20:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
156             "24:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
157             "26:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
158             "28:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
159             "35:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
160             "39:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
161             "47:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
162             "51:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
163             "57:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
164             "59:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
165             "68:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
166             "81:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
167             "88:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
168             "94:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
169             "102:19: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 19),
170             "111:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
171             "114:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
172             "123:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
173             "125:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
174             "134:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
175             "136:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
176             "138:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
177             "153:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
178             "162:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
179             "169:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
180         };
181         verifyWithInlineConfigParser(
182                 getPath("InputLeftCurlyTestDefault3.java"), expected);
183     }
184 
185     @Test
186     public void testNewline3() throws Exception {
187         final String[] expected = {
188             "31:33: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 33),
189             "96:19: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 19),
190             "102:19: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 19),
191             "147:49: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 49),
192             "163:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
193             "170:16: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 16),
194         };
195         verifyWithInlineConfigParser(
196                 getPath("InputLeftCurlyTestNewLine3.java"), expected);
197     }
198 
199     @Test
200     public void testMissingBraces() throws Exception {
201         final String[] expected = {
202             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
203             "20:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
204             "26:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
205             "39:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
206             "56:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
207             "74:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
208             "110:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
209         };
210         verifyWithInlineConfigParser(
211                 getPath("InputLeftCurlyTestMissingBraces.java"), expected);
212     }
213 
214     @Test
215     public void testDefaultWithAnnotations() throws Exception {
216         final String[] expected = {
217             "23:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
218             "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
219             "34:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
220             "40:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
221             "63:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
222             "71:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
223         };
224         verifyWithInlineConfigParser(
225                 getPath("InputLeftCurlyTestDefaultWithAnnotations.java"), expected);
226     }
227 
228     @Test
229     public void testNlWithAnnotations() throws Exception {
230         final String[] expected = {
231             "48:55: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 55),
232             "51:41: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 41),
233             "57:27: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 27),
234             "79:42: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 42),
235         };
236         verifyWithInlineConfigParser(
237                 getPath("InputLeftCurlyTestNlWithAnnotations.java"), expected);
238     }
239 
240     @Test
241     public void testNlowWithAnnotations() throws Exception {
242         final String[] expected = {
243             "23:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
244             "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
245             "34:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
246             "40:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
247             "63:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
248             "71:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
249         };
250         verifyWithInlineConfigParser(
251                 getPath("InputLeftCurlyTestNlowWithAnnotations.java"), expected);
252     }
253 
254     @Test
255     public void testLineBreakAfter() throws Exception {
256         final String[] expected = {
257             "22:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
258             "25:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
259             "29:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
260             "31:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
261             "33:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
262             "39:22: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 22),
263             "41:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
264             "48:33: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 33),
265             "49:21: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 21),
266             "52:29: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 29),
267             "52:34: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 34),
268             "58:37: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 37),
269             "64:12: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 12),
270             "67:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
271             "69:19: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 19),
272             "79:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
273         };
274         verifyWithInlineConfigParser(
275                 getPath("InputLeftCurlyTestLineBreakAfter.java"), expected);
276     }
277 
278     @Test
279     public void testIgnoreEnumsOptionTrue() throws Exception {
280         final String[] expectedWhileTrue = {
281             "21:44: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 44),
282         };
283         verifyWithInlineConfigParser(
284                 getPath("InputLeftCurlyIgnoreEnumsOptTrue.java"), expectedWhileTrue);
285     }
286 
287     @Test
288     public void testIgnoreEnumsOptionFalse() throws Exception {
289         final String[] expectedWhileFalse = {
290             "17:17: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 17),
291             "21:44: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 44),
292         };
293         verifyWithInlineConfigParser(
294                 getPath("InputLeftCurlyIgnoreEnumsOptFalse.java"), expectedWhileFalse);
295     }
296 
297     @Test
298     public void testDefaultLambda() throws Exception {
299         final String[] expected = {
300             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
301             "24:32: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 32),
302             "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
303         };
304         verifyWithInlineConfigParser(
305                 getPath("InputLeftCurlyTestDefaultLambda.java"),
306                 expected);
307     }
308 
309     @Test
310     public void testNewLineOptionWithLambda() throws Exception {
311         final String[] expected = {
312             "18:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
313             "24:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
314         };
315         verifyWithInlineConfigParser(
316                 getPath("InputLeftCurlyTestNewLineOptionWithLambda.java"),
317                 expected);
318     }
319 
320     @Test
321     public void testEolSwitch() throws Exception {
322         final String[] expected = {
323             "22:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
324             "26:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
325             "33:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
326             "47:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
327             "52:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
328         };
329         verifyWithInlineConfigParser(
330                 getPath("InputLeftCurlyTestEolSwitch.java"), expected);
331     }
332 
333     @Test
334     public void testNlSwitch() throws Exception {
335         final String[] expected = {
336             "24:21: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 21),
337             "56:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
338         };
339         verifyWithInlineConfigParser(
340                 getPath("InputLeftCurlyTestNlSwitch.java"), expected);
341     }
342 
343     @Test
344     public void testNlowSwitch() throws Exception {
345         final String[] expected = {
346             "22:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
347         };
348         verifyWithInlineConfigParser(
349                 getPath("InputLeftCurlyTestNlowSwitch.java"), expected);
350     }
351 
352     @Test
353     public void testLeftCurlySwitchExpressions() throws Exception {
354         final String[] expected = {
355             "20:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
356             "22:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
357             "27:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
358             "32:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
359             "36:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
360             "45:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
361             "47:21: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 21),
362             "51:21: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 21),
363             "55:21: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 21),
364             "59:21: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 21),
365         };
366         verifyWithInlineConfigParser(
367                 getNonCompilablePath("InputLeftCurlyTestSwitchExpressions.java"), expected);
368     }
369 
370     @Test
371     public void testLeftCurlySwitchExpressionsNewLine() throws Exception {
372 
373         final String[] expected = {
374             "17:57: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 57),
375             "18:25: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 25),
376             "43:25: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 25),
377             "54:23: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 23),
378         };
379         verifyWithInlineConfigParser(
380                 getNonCompilablePath("InputLeftCurlyTestSwitchExpressionsNewLine.java"),
381             expected);
382     }
383 
384     @Test
385     public void testGetAcceptableTokens() {
386         final LeftCurlyCheck check = new LeftCurlyCheck();
387         final int[] actual = check.getAcceptableTokens();
388         final int[] expected = {
389             TokenTypes.ANNOTATION_DEF,
390             TokenTypes.CLASS_DEF,
391             TokenTypes.CTOR_DEF,
392             TokenTypes.ENUM_CONSTANT_DEF,
393             TokenTypes.ENUM_DEF,
394             TokenTypes.INTERFACE_DEF,
395             TokenTypes.LAMBDA,
396             TokenTypes.LITERAL_CASE,
397             TokenTypes.LITERAL_CATCH,
398             TokenTypes.LITERAL_DEFAULT,
399             TokenTypes.LITERAL_DO,
400             TokenTypes.LITERAL_ELSE,
401             TokenTypes.LITERAL_FINALLY,
402             TokenTypes.LITERAL_FOR,
403             TokenTypes.LITERAL_IF,
404             TokenTypes.LITERAL_SWITCH,
405             TokenTypes.LITERAL_SYNCHRONIZED,
406             TokenTypes.LITERAL_TRY,
407             TokenTypes.LITERAL_WHILE,
408             TokenTypes.METHOD_DEF,
409             TokenTypes.OBJBLOCK,
410             TokenTypes.STATIC_INIT,
411             TokenTypes.RECORD_DEF,
412             TokenTypes.COMPACT_CTOR_DEF,
413         };
414         assertWithMessage("Default acceptable tokens are invalid")
415             .that(actual)
416             .isEqualTo(expected);
417     }
418 
419     @Test
420     public void testFirstLine() throws Exception {
421         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
422         verifyWithInlineConfigParser(
423                 getPath("InputLeftCurlyTestFirstLine.java"), expected);
424     }
425 
426     @Test
427     public void testCoverageIncrease() throws Exception {
428         final String[] expected = {
429             "21:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
430             "30:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
431             "39:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
432             "48:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
433             "62:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
434             "67:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
435             "71:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
436             "76:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
437             "81:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
438         };
439         verifyWithInlineConfigParser(
440                 getPath("InputLeftCurlyTestCoverageIncrease.java"), expected);
441     }
442 
443     @Test
444     public void testLeftCurlyRecordsAndCompactCtors() throws Exception {
445         final String[] expected = {
446             "22:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
447             "24:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
448             "34:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
449             "36:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
450             "43:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
451             "56:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
452         };
453         verifyWithInlineConfigParser(
454                 getNonCompilablePath("InputLeftCurlyTestRecordsAndCompactCtors.java"), expected);
455     }
456 
457     @Test
458     public void testLeftCurlyWithEmoji() throws Exception {
459         final String[] expected = {
460             "17:32: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 32),
461             "37:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
462             "39:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
463             "46:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
464             "50:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
465             "54:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
466             "60:32: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 32),
467             "67:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
468             "72:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
469             "78:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
470             "81:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
471         };
472         verifyWithInlineConfigParser(getPath("InputLeftCurlyWithEmoji.java"), expected);
473     }
474 
475     @Test
476     public void testLeftCurlyWithEmojiNewLine() throws Exception {
477         final String[] expected = {
478             "18:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
479             "20:27: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 27),
480             "25:29: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 29),
481             "28:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
482             "31:28: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 28),
483             "34:28: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 28),
484             "43:39: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 39),
485             "60:27: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 27),
486             "61:28: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 28),
487             "75:26: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 26),
488             "76:26: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 26),
489             "77:24: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 24),
490             "89:52: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 52),
491         };
492         verifyWithInlineConfigParser(getPath("InputLeftCurlyWithEmojiNl.java"), expected);
493     }
494 
495     @Test
496     public void testInvalidOption() throws Exception {
497 
498         try {
499             final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
500 
501             verifyWithInlineConfigParser(
502                     getPath("InputLeftCurlyTestInvalidOption.java"), expected);
503             assertWithMessage("exception expected").fail();
504         }
505         catch (CheckstyleException ex) {
506             assertWithMessage("Invalid exception message")
507                 .that(ex.getMessage())
508                 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
509                     + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
510                     + "blocks.LeftCurlyCheck - "
511                     + "Cannot set property 'option' to 'invalid_option'");
512         }
513     }
514 
515     @Test
516     public void testTrimOptionProperty() throws Exception {
517         final String[] expected = {
518             "13:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
519             "20:16: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 16),
520         };
521         verifyWithInlineConfigParser(
522                 getPath("InputLeftCurlyWithTrimOptionProperty.java"), expected);
523     }
524 
525     @Test
526     public void testForEnumConstantDef() throws Exception {
527         final String[] expected = {
528             "14:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
529             "19:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
530         };
531         verifyWithInlineConfigParser(
532                 getPath("InputLeftCurlyEnumConstantDef.java"), expected);
533     }
534 
535     @Test
536     public void commentBeforeLeftCurly() throws Exception {
537         final String[] expected = {
538             "32:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
539         };
540         verifyWithInlineConfigParser(
541                 getPath("InputLeftCurlyCommentBeforeLeftCurly.java"), expected);
542     }
543 
544     @Test
545     public void commentBeforeLeftCurly2() throws Exception {
546         final String[] expected = {
547             "54:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
548             "66:29: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 29),
549         };
550         verifyWithInlineConfigParser(
551                 getPath("InputLeftCurlyCommentBeforeLeftCurly2.java"), expected);
552     }
553 }