View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
34  
35  public class LeftCurlyCheckTest extends AbstractModuleTestSupport {
36  
37      @Override
38      public String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/checks/blocks/leftcurly";
40      }
41  
42      /* Additional test for jacoco, since valueOf()
43       * is generated by javac and jacoco reports that
44       * valueOf() is uncovered.
45       */
46      @Test
47      public void testLeftCurlyOptionValueOf() {
48          final LeftCurlyOption option = LeftCurlyOption.valueOf("NL");
49          assertWithMessage("Invalid valueOf result")
50              .that(option)
51              .isEqualTo(LeftCurlyOption.NL);
52      }
53  
54      @Test
55      public void testGetRequiredTokens() {
56          final LeftCurlyCheck checkObj = new LeftCurlyCheck();
57          assertWithMessage("LeftCurlyCheck#getRequiredTokens should return empty array by default")
58              .that(checkObj.getRequiredTokens())
59              .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
60      }
61  
62      @Test
63      public void testDefault() throws Exception {
64          final String[] expected = {
65              "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
66              "19:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
67              "23:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
68              "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
69              "31:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
70          };
71          verifyWithInlineConfigParser(
72                  getPath("InputLeftCurlyTestDefault.java"), expected);
73      }
74  
75      @Test
76      public void testNl() throws Exception {
77          final String[] expected = {
78              "36:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
79              "40:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
80              "45:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
81              "49:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
82              "54:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
83              "59:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
84              "64:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
85              "67:27: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 27),
86              "68:23: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 23),
87              "69:25: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 25),
88          };
89          verifyWithInlineConfigParser(
90                  getPath("InputLeftCurlyDefaultTestNl.java"), expected);
91      }
92  
93      @Test
94      public void testNlow() throws Exception {
95          final String[] expected = {
96              "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
97              "19:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
98              "23:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
99              "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
100             "31:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
101             "36:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
102             "40:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
103             "45:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
104             "49:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
105             "54:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
106             "59:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
107             "64:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
108         };
109         verifyWithInlineConfigParser(
110                 getPath("InputLeftCurlyDefaultTestNlow.java"), expected);
111     }
112 
113     @Test
114     public void testDefault2() throws Exception {
115         final String[] expected = {
116             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
117             "22:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
118             "29:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
119             "32:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
120             "36:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
121             "44:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
122             "46:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
123             "51:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
124             "54:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
125             "58:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
126             "70:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
127             "74:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
128             "82:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
129             "85:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
130             "89:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
131         };
132         verifyWithInlineConfigParser(
133                 getPath("InputLeftCurlyMethod.java"), expected);
134     }
135 
136     @Test
137     public void testNewline2() throws Exception {
138         final String[] expected = {
139             "19:44: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 44),
140             "26:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
141             "39:31: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 31),
142             "48:24: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 24),
143             "61:35: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 35),
144             "65:17: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 17),
145             "79:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
146             "92:31: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 31),
147         };
148         verifyWithInlineConfigParser(
149                 getPath("InputLeftCurlyMethodTestNewLine2.java"), expected);
150     }
151 
152     @Test
153     public void testDefault3Basic() throws Exception {
154         final String[] expected = {
155             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
156             "20:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
157             "24:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
158             "26:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
159             "28:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
160             "35:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
161             "39:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
162             "47:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
163             "51:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
164             "57:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
165             "59:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
166             "68:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
167             "80:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
168             "88:19: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 19),
169         };
170         verifyWithInlineConfigParser(
171                 getPath("InputLeftCurlyTestDefault3Basic.java"), expected);
172     }
173 
174     @Test
175     public void testDefault3Empty() throws Exception {
176         final String[] expected = {
177             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
178             "30:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
179         };
180         verifyWithInlineConfigParser(
181                 getPath("InputLeftCurlyTestDefault3Empty.java"), expected);
182     }
183 
184     @Test
185     public void testDefault3Enum() throws Exception {
186         final String[] expected = {
187             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
188             "19:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
189         };
190         verifyWithInlineConfigParser(
191                 getPath("InputLeftCurlyTestDefault3Enum.java"), expected);
192     }
193 
194     @Test
195     public void testDefault3Initializer() throws Exception {
196         final String[] expected = {
197             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
198             "20:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
199             "26:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
200             "33:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
201         };
202         verifyWithInlineConfigParser(
203                 getPath("InputLeftCurlyTestDefault3Initializer.java"), expected);
204     }
205 
206     @Test
207     public void testDefault3Misc() throws Exception {
208         final String[] expected = {
209             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
210             "25:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
211             "28:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
212             "37:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
213             "39:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
214             "48:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
215             "50:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
216             "52:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
217         };
218         verifyWithInlineConfigParser(
219                 getPath("InputLeftCurlyTestDefault3Misc.java"), expected);
220     }
221 
222     @Test
223     public void testNewline3Basic() throws Exception {
224         final String[] expected = {
225             "31:33: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 33),
226             "82:19: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 19),
227             "88:19: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 19),
228         };
229         verifyWithInlineConfigParser(
230                 getPath("InputLeftCurlyTestNewLine3Basic.java"), expected);
231     }
232 
233     @Test
234     public void testNewline3Empty() throws Exception {
235         final String[] expected = {
236             "24:49: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 49),
237         };
238         verifyWithInlineConfigParser(
239                 getPath("InputLeftCurlyTestNewLine3Empty.java"), expected);
240     }
241 
242     @Test
243     public void testNewline3Enum() throws Exception {
244         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
245         verifyWithInlineConfigParser(
246                 getPath("InputLeftCurlyTestNewLine3Enum.java"), expected);
247     }
248 
249     @Test
250     public void testNewline3Initializer() throws Exception {
251         final String[] expected = {
252             "27:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
253             "34:16: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 16),
254         };
255         verifyWithInlineConfigParser(
256                 getPath("InputLeftCurlyTestNewLine3Initializer.java"), expected);
257     }
258 
259     @Test
260     public void testNewline3Misc() throws Exception {
261         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
262         verifyWithInlineConfigParser(
263                 getPath("InputLeftCurlyTestNewLine3Misc.java"), expected);
264     }
265 
266     @Test
267     public void testMissingBracesConditional() throws Exception {
268         final String[] expected = {
269             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
270             "20:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
271             "26:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
272         };
273         verifyWithInlineConfigParser(
274                 getPath("InputLeftCurlyTestMissingBracesConditional.java"), expected);
275     }
276 
277     @Test
278     public void testMissingBracesLoop() throws Exception {
279         final String[] expected = {
280             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
281             "20:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
282             "26:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
283             "39:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
284             "56:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
285         };
286         verifyWithInlineConfigParser(
287                 getPath("InputLeftCurlyTestMissingBracesLoop.java"), expected);
288     }
289 
290     @Test
291     public void testMissingBracesMisc() throws Exception {
292         final String[] expected = {
293             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
294             "20:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
295         };
296         verifyWithInlineConfigParser(
297                 getPath("InputLeftCurlyTestMissingBracesMisc.java"), expected);
298     }
299 
300     @Test
301     public void testDefaultWithAnnotations() throws Exception {
302         final String[] expected = {
303             "23:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
304             "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
305             "34:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
306             "40:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
307             "63:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
308             "71:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
309         };
310         verifyWithInlineConfigParser(
311                 getPath("InputLeftCurlyTestDefaultWithAnnotations.java"), expected);
312     }
313 
314     @Test
315     public void testNlWithAnnotations() throws Exception {
316         final String[] expected = {
317             "48:55: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 55),
318             "51:41: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 41),
319             "57:27: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 27),
320             "79:42: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 42),
321         };
322         verifyWithInlineConfigParser(
323                 getPath("InputLeftCurlyTestNlWithAnnotations.java"), expected);
324     }
325 
326     @Test
327     public void testNlowWithAnnotations() throws Exception {
328         final String[] expected = {
329             "23:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
330             "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
331             "34:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
332             "40:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
333             "63:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
334             "71:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
335         };
336         verifyWithInlineConfigParser(
337                 getPath("InputLeftCurlyTestNlowWithAnnotations.java"), expected);
338     }
339 
340     @Test
341     public void testLineBreakAfter() throws Exception {
342         final String[] expected = {
343             "22:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
344             "25:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
345             "29:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
346             "31:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
347             "33:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
348             "39:22: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 22),
349             "41:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
350             "48:33: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 33),
351             "49:21: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 21),
352             "52:29: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 29),
353             "52:34: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 34),
354             "58:37: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 37),
355             "64:12: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 12),
356             "67:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
357             "69:19: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 19),
358             "79:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
359         };
360         verifyWithInlineConfigParser(
361                 getPath("InputLeftCurlyTestLineBreakAfter.java"), expected);
362     }
363 
364     @Test
365     public void testIgnoreEnumsOptionTrue() throws Exception {
366         final String[] expectedWhileTrue = {
367             "21:44: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 44),
368         };
369         verifyWithInlineConfigParser(
370                 getPath("InputLeftCurlyIgnoreEnumsOptTrue.java"), expectedWhileTrue);
371     }
372 
373     @Test
374     public void testIgnoreEnumsOptionFalse() throws Exception {
375         final String[] expectedWhileFalse = {
376             "17:17: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 17),
377             "21:44: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 44),
378         };
379         verifyWithInlineConfigParser(
380                 getPath("InputLeftCurlyIgnoreEnumsOptFalse.java"), expectedWhileFalse);
381     }
382 
383     @Test
384     public void testDefaultLambda() throws Exception {
385         final String[] expected = {
386             "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
387             "24:32: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 32),
388             "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
389         };
390         verifyWithInlineConfigParser(
391                 getPath("InputLeftCurlyTestDefaultLambda.java"),
392                 expected);
393     }
394 
395     @Test
396     public void testNewLineOptionWithLambda() throws Exception {
397         final String[] expected = {
398             "18:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
399             "24:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
400         };
401         verifyWithInlineConfigParser(
402                 getPath("InputLeftCurlyTestNewLineOptionWithLambda.java"),
403                 expected);
404     }
405 
406     @Test
407     public void testEolSwitch() throws Exception {
408         final String[] expected = {
409             "22:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
410             "26:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
411             "33:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
412             "47:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
413             "52:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
414         };
415         verifyWithInlineConfigParser(
416                 getPath("InputLeftCurlyTestEolSwitch.java"), expected);
417     }
418 
419     @Test
420     public void testNlSwitch() throws Exception {
421         final String[] expected = {
422             "24:21: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 21),
423             "56:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
424         };
425         verifyWithInlineConfigParser(
426                 getPath("InputLeftCurlyTestNlSwitch.java"), expected);
427     }
428 
429     @Test
430     public void testNlowSwitch() throws Exception {
431         final String[] expected = {
432             "22:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
433         };
434         verifyWithInlineConfigParser(
435                 getPath("InputLeftCurlyTestNlowSwitch.java"), expected);
436     }
437 
438     @Test
439     public void testLeftCurlySwitchExpressions() throws Exception {
440         final String[] expected = {
441             "20:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
442             "22:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
443             "27:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
444             "32:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
445             "36:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
446             "45:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
447             "47:21: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 21),
448             "51:21: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 21),
449             "55:21: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 21),
450             "59:21: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 21),
451         };
452         verifyWithInlineConfigParser(
453                 getPath("InputLeftCurlyTestSwitchExpressions.java"), expected);
454     }
455 
456     @Test
457     public void testLeftCurlySwitchExpressionsNewLine() throws Exception {
458 
459         final String[] expected = {
460             "17:57: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 57),
461             "18:25: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 25),
462             "43:25: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 25),
463             "54:23: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 23),
464         };
465         verifyWithInlineConfigParser(
466                 getPath("InputLeftCurlyTestSwitchExpressionsNewLine.java"),
467             expected);
468     }
469 
470     @Test
471     public void testGetAcceptableTokens() {
472         final LeftCurlyCheck check = new LeftCurlyCheck();
473         final int[] actual = check.getAcceptableTokens();
474         final int[] expected = {
475             TokenTypes.ANNOTATION_DEF,
476             TokenTypes.CLASS_DEF,
477             TokenTypes.CTOR_DEF,
478             TokenTypes.ENUM_CONSTANT_DEF,
479             TokenTypes.ENUM_DEF,
480             TokenTypes.INTERFACE_DEF,
481             TokenTypes.LAMBDA,
482             TokenTypes.LITERAL_CASE,
483             TokenTypes.LITERAL_CATCH,
484             TokenTypes.LITERAL_DEFAULT,
485             TokenTypes.LITERAL_DO,
486             TokenTypes.LITERAL_ELSE,
487             TokenTypes.LITERAL_FINALLY,
488             TokenTypes.LITERAL_FOR,
489             TokenTypes.LITERAL_IF,
490             TokenTypes.LITERAL_SWITCH,
491             TokenTypes.LITERAL_SYNCHRONIZED,
492             TokenTypes.LITERAL_TRY,
493             TokenTypes.LITERAL_WHILE,
494             TokenTypes.METHOD_DEF,
495             TokenTypes.OBJBLOCK,
496             TokenTypes.STATIC_INIT,
497             TokenTypes.RECORD_DEF,
498             TokenTypes.COMPACT_CTOR_DEF,
499             TokenTypes.SWITCH_RULE,
500         };
501         assertWithMessage("Default acceptable tokens are invalid")
502             .that(actual)
503             .isEqualTo(expected);
504     }
505 
506     @Test
507     public void testFirstLine() throws Exception {
508         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
509         verifyWithInlineConfigParser(
510                 getPath("InputLeftCurlyTestFirstLine.java"), expected);
511     }
512 
513     @Test
514     public void testCoverageIncrease() throws Exception {
515         final String[] expected = {
516             "21:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
517             "30:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
518             "39:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
519             "48:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
520             "62:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
521             "67:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
522             "71:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
523             "76:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
524             "81:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
525         };
526         verifyWithInlineConfigParser(
527                 getPath("InputLeftCurlyTestCoverageIncrease.java"), expected);
528     }
529 
530     @Test
531     public void testLeftCurlyRecordsAndCompactCtors() throws Exception {
532         final String[] expected = {
533             "22:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
534             "24:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
535             "34:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
536             "36:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
537             "43:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
538             "56:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
539         };
540         verifyWithInlineConfigParser(
541                 getPath("InputLeftCurlyTestRecordsAndCompactCtors.java"), expected);
542     }
543 
544     @Test
545     public void testLeftCurlyWithEmoji() throws Exception {
546         final String[] expected = {
547             "17:32: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 32),
548             "37:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
549             "39:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
550             "46:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
551             "50:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
552             "54:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
553             "60:32: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 32),
554             "67:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
555             "72:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
556             "78:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
557             "81:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
558         };
559         verifyWithInlineConfigParser(getPath("InputLeftCurlyWithEmoji.java"), expected);
560     }
561 
562     @Test
563     public void testLeftCurlyWithEmojiNewLine() throws Exception {
564         final String[] expected = {
565             "18:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
566             "20:27: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 27),
567             "25:29: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 29),
568             "28:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
569             "31:28: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 28),
570             "34:28: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 28),
571             "43:39: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 39),
572             "60:27: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 27),
573             "61:28: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 28),
574             "75:26: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 26),
575             "76:26: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 26),
576             "77:24: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 24),
577             "89:52: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 52),
578         };
579         verifyWithInlineConfigParser(getPath("InputLeftCurlyWithEmojiNl.java"), expected);
580     }
581 
582     @Test
583     public void testInvalidOption() {
584 
585         final CheckstyleException exc = getExpectedThrowable(CheckstyleException.class,
586                 () -> {
587                     final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
588                     verifyWithInlineConfigParser(
589                             getPath("InputLeftCurlyTestInvalidOption.java"), expected);
590                 });
591         assertWithMessage("Invalid exception message")
592             .that(exc.getMessage())
593             .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
594                 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
595                 + "blocks.LeftCurlyCheck");
596     }
597 
598     @Test
599     public void testTrimOptionProperty() throws Exception {
600         final String[] expected = {
601             "13:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
602             "20:16: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 16),
603         };
604         verifyWithInlineConfigParser(
605                 getPath("InputLeftCurlyWithTrimOptionProperty.java"), expected);
606     }
607 
608     @Test
609     public void testForEnumConstantDef() throws Exception {
610         final String[] expected = {
611             "14:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
612             "19:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
613         };
614         verifyWithInlineConfigParser(
615                 getPath("InputLeftCurlyEnumConstantDef.java"), expected);
616     }
617 
618     @Test
619     public void commentBeforeLeftCurly() throws Exception {
620         final String[] expected = {
621             "32:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
622         };
623         verifyWithInlineConfigParser(
624                 getPath("InputLeftCurlyCommentBeforeLeftCurly.java"), expected);
625     }
626 
627     @Test
628     public void commentBeforeLeftCurly2() throws Exception {
629         final String[] expected = {
630             "54:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
631             "66:29: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 29),
632         };
633         verifyWithInlineConfigParser(
634                 getPath("InputLeftCurlyCommentBeforeLeftCurly2.java"), expected);
635     }
636 
637     @Test
638     public void testSwitchRuleLineBreakAfter() throws Exception {
639         final String[] expected = {
640             "27:31: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 31),
641             "29:31: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 31),
642             "43:24: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 24),
643             "93:7: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 7),
644         };
645         verifyWithInlineConfigParser(
646                 getPath("InputLeftCurlySwitchRuleLineBreakAfter.java"), expected);
647     }
648 
649     @Test
650     public void testSwitchRuleWithBlock() throws Exception {
651         final String[] expected = {
652             "17:23: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 23),
653         };
654         verifyWithInlineConfigParser(
655                 getPath("InputLeftCurlySwitchRuleWithBlock.java"), expected);
656     }
657 
658     @Test
659     public void testSwitchMutation() throws Exception {
660         final String[] expected = {
661             "17:23: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 23),
662             "19:24: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 24),
663         };
664         verifyWithInlineConfigParser(
665                 getPath("InputLeftCurlySwitchMutation.java"), expected);
666     }
667 
668 }