View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.whitespace;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck.MSG_MULTIPLE_LINES;
24  import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck.MSG_MULTIPLE_LINES_AFTER;
25  import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck.MSG_MULTIPLE_LINES_INSIDE;
26  import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32  import com.puppycrawl.tools.checkstyle.TreeWalker;
33  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
34  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
35  
36  public class EmptyLineSeparatorCheckTest
37      extends AbstractModuleTestSupport {
38  
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/whitespace/emptylineseparator";
42      }
43  
44      @Test
45      public void testGetRequiredTokens() {
46          final EmptyLineSeparatorCheck checkObj = new EmptyLineSeparatorCheck();
47          assertWithMessage("EmptyLineSeparatorCheck#getRequiredTokens should return empty array "
48                  + "by default")
49              .that(checkObj.getRequiredTokens())
50              .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
51      }
52  
53      @Test
54      public void testMultipleLinesEmptyWithJavadoc() throws Exception {
55  
56          final String[] expected = {
57              "27:3: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
58              "43:3: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
59              "51:3: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
60              "56:3: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
61              "65:3: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
62              "75:13: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
63              "86:3: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
64              "93:3: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
65              "99:3: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
66          };
67          verifyWithInlineXmlConfig(
68                  getPath("InputEmptyLineSeparatorWithJavadoc.java"), expected);
69      }
70  
71      @Test
72      public void testMultipleLinesEmptyWithJavadoc2() throws Exception {
73  
74          final String[] expected = {
75              "65:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
76              "70:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
77              "75:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
78              "85:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
79              "96:7: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
80              "107:7: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
81          };
82          verifyWithInlineXmlConfig(
83                  getPath("InputEmptyLineSeparatorWithJavadoc2.java"), expected);
84      }
85  
86      @Test
87      public void testSeparationOfClassAndPackageWithComment() throws Exception {
88  
89          final String[] expected = {
90              "12:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "//"),
91              "13:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
92          };
93  
94          verifyWithInlineXmlConfig(
95                  getPath("InputEmptyLineSeparatorClassPackageSeparation.java"), expected);
96      }
97  
98      /**
99       * Config is defined in the method because indexOutOfBond test is also required.
100      */
101     @Test
102     public void testCompactNoPackage() throws Exception {
103 
104         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
105         checkConfig.addProperty("allowMultipleEmptyLines", "false");
106 
107         final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class);
108         treeWalkerConfig.addChild(checkConfig);
109 
110         final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig);
111 
112         final String[] expected = {
113             "7:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
114             "11:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
115             "16:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
116             "20:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
117             "25:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
118             "29:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
119             "34:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
120         };
121 
122         verify(checkerConfig, getNonCompilablePath("InputEmptyLineSeparatorCompactNoPackage.java"),
123             expected);
124     }
125 
126     /**
127      * Config is defined in the method because strictly the file with one line
128      * is required to be tested.
129      */
130     @Test
131     public void testMultipleEmptyLinesInOneLine() throws Exception {
132         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
133         checkConfig.addProperty("allowNoEmptyLineBetweenFields", "true");
134         checkConfig.addProperty("allowMultipleEmptyLines", "false");
135         checkConfig.addProperty("allowMultipleEmptyLinesInsideClassMembers", "false");
136 
137         final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class);
138         treeWalkerConfig.addChild(checkConfig);
139 
140         final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig);
141 
142         final String[] expected = {
143             "1:79: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
144         };
145 
146         verify(checkerConfig, getPath("InputEmptyLineSeparatorOneLine.java"), expected);
147     }
148 
149     @Test
150     public void testDefault() throws Exception {
151 
152         final String[] expected = {
153             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
154             "31:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
155             "34:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
156             "35:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
157             "39:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INSTANCE_INIT"),
158             "53:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
159             "58:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
160             "75:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
161             "106:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
162         };
163         verifyWithInlineConfigParser(
164                 getPath("InputEmptyLineSeparator.java"), expected);
165     }
166 
167     @Test
168     public void testAllowNoEmptyLineBetweenFields() throws Exception {
169 
170         final String[] expected = {
171             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
172             "31:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
173             "35:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
174             "39:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INSTANCE_INIT"),
175             "53:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
176             "58:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
177             "75:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
178             "106:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
179         };
180         verifyWithInlineConfigParser(
181                 getPath("InputEmptyLineSeparator2.java"), expected);
182     }
183 
184     @Test
185     public void testHeader() throws Exception {
186         final String[] expected = {
187             "12:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
188         };
189         verifyWithInlineConfigParser(
190                 getPath("InputEmptyLineSeparatorHeader.java"), expected);
191     }
192 
193     @Test
194     public void testMultipleEmptyLinesBetweenClassMembers() throws Exception {
195         final String[] expected = {
196             "14:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "package"),
197             "17:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "import"),
198             "22:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "CLASS_DEF"),
199             "26:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
200             "31:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
201             "36:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
202             "38:33: " + getCheckMessage(MSG_MULTIPLE_LINES_AFTER, "}"),
203         };
204         verifyWithInlineConfigParser(
205                 getPath("InputEmptyLineSeparatorMultipleEmptyLines.java"), expected);
206     }
207 
208     @Test
209     public void testFormerArrayIndexOutOfBounds() throws Exception {
210         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
211         verifyWithInlineConfigParser(
212                 getPath("InputEmptyLineSeparatorFormerException.java"), expected);
213     }
214 
215     @Test
216     public void testAllowMultipleFieldInClass() throws Exception {
217         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
218         verifyWithInlineConfigParser(
219                 getPath("InputEmptyLineSeparatorMultipleFieldsInClass.java"), expected);
220     }
221 
222     @Test
223     public void testAllowMultipleImportSeparatedFromPackage() throws Exception {
224         final String[] expected = {
225             "13:78: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
226         };
227         verifyWithInlineConfigParser(
228                 getPath("InputEmptyLineSeparatorMultipleImportEmptyClass.java"),
229             expected);
230     }
231 
232     @Test
233     public void testImportSeparatedFromPackage() throws Exception {
234         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
235         verifyWithInlineConfigParser(
236                 getPath("InputEmptyLineSeparatorImportSeparatedFromPackage.java"),
237             expected);
238     }
239 
240     @Test
241     public void testStaticImport() throws Exception {
242         final String[] expected = {
243             "17:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
244         };
245         verifyWithInlineConfigParser(
246                 getPath("InputEmptyLineSeparatorStaticImport.java"),
247             expected);
248     }
249 
250     @Test
251     public void testBlockCommentNotSeparatedFromPackage() throws Exception {
252         final String[] expected = {
253             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "/*"),
254         };
255         verifyWithInlineConfigParser(
256                 getPath("InputEmptyLineSeparatorBlockCommentUnderPackage.java"),
257             expected);
258     }
259 
260     @Test
261     public void testSingleCommentNotSeparatedFromPackage() throws Exception {
262         final String[] expected = {
263             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "//"),
264         };
265         verifyWithInlineConfigParser(
266                 getPath("InputEmptyLineSeparatorSingleCommentUnderPackage.java"),
267             expected);
268     }
269 
270     @Test
271     public void testClassDefinitionNotSeparatedFromPackage() throws Exception {
272         final String[] expected = {
273             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
274         };
275         verifyWithInlineConfigParser(
276                 getPath("InputEmptyLineSeparatorModifierUnderPackage.java"),
277             expected);
278     }
279 
280     @Test
281     public void testCommentAfterPackageWithImports() throws Exception {
282         final String[] expected = {
283             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "//"),
284         };
285         verifyWithInlineConfigParser(
286                 getPath("InputEmptyLineSeparatorSingleLineCommentAfterPackage.java"),
287                 expected);
288     }
289 
290     @Test
291     public void testJavadocCommentAfterPackageWithImports() throws Exception {
292         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
293         final String[] expected = {
294             "2:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "/*"),
295         };
296         verify(checkConfig,
297                 getPath("InputEmptyLineSeparatorJavadocCommentAfterPackage.java"),
298                 expected);
299     }
300 
301     @Test
302     public void testPackageImportsClassInSingleLine() throws Exception {
303         final String[] expected = {
304             "13:79: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
305             "13:101: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
306         };
307         verifyWithInlineConfigParser(
308                 getPath("InputEmptyLineSeparatorPackageImportClassInOneLine.java"),
309                 expected);
310     }
311 
312     @Test
313     public void testEmptyLineAfterPackageForPackageAst() throws Exception {
314         final String[] expected = {
315             "12:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "/*"),
316         };
317         verifyWithInlineConfigParser(
318                 getPath("InputEmptyLineSeparatorEmptyLineAfterPackageForPackageAst.java"),
319                 expected);
320     }
321 
322     @Test
323     public void testEmptyLineAfterPackageForImportAst() throws Exception {
324         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
325         verifyWithInlineConfigParser(
326                 getPath("InputEmptyLineSeparatorEmptyLineAfterPackageForImportAst.java"),
327                 expected);
328     }
329 
330     @Test
331     public void testClassDefinitionAndCommentNotSeparatedFromPackage() throws Exception {
332         final String[] expected = {
333             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "//"),
334             "15:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
335         };
336         final String testFile =
337             "InputEmptyLineSeparatorClassDefinitionAndCommentNotSeparatedFromPackage.java";
338         verifyWithInlineConfigParser(
339                 getPath(testFile), expected);
340     }
341 
342     @Test
343     public void testBlockCommentSeparatedFromPackage() throws Exception {
344         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
345         verifyWithInlineConfigParser(
346                 getPath("InputEmptyLineSeparatorBlockCommentSeparatedFromPackage.java"),
347             expected);
348     }
349 
350     @Test
351     public void testSingleCommentSeparatedFromPackage() throws Exception {
352         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
353         verifyWithInlineConfigParser(
354                 getPath("InputEmptyLineSeparatorSingleCommentSeparatedFromPackage.java"),
355             expected);
356     }
357 
358     @Test
359     public void testEnumMembers() throws Exception {
360         final String[] expected = {
361             "22:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
362             "27:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
363             "28:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
364             "31:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "CTOR_DEF"),
365             "36:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
366             "40:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "STATIC_INIT"),
367         };
368         verifyWithInlineConfigParser(
369             getPath("InputEmptyLineSeparatorEnumMembers.java"), expected
370         );
371     }
372 
373     @Test
374     public void testInterfaceFields() throws Exception {
375         final String[] expected = {
376             "21:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
377             "25:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
378             "34:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
379             "38:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
380             "45:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
381         };
382         verifyWithInlineConfigParser(
383             getPath("InputEmptyLineSeparatorInterfaceFields.java"), expected
384         );
385     }
386 
387     @Test
388     public void testGetAcceptableTokens() {
389         final EmptyLineSeparatorCheck emptyLineSeparatorCheckObj = new EmptyLineSeparatorCheck();
390         final int[] actual = emptyLineSeparatorCheckObj.getAcceptableTokens();
391         final int[] expected = {
392             TokenTypes.PACKAGE_DEF,
393             TokenTypes.IMPORT,
394             TokenTypes.STATIC_IMPORT,
395             TokenTypes.CLASS_DEF,
396             TokenTypes.INTERFACE_DEF,
397             TokenTypes.ENUM_DEF,
398             TokenTypes.STATIC_INIT,
399             TokenTypes.INSTANCE_INIT,
400             TokenTypes.METHOD_DEF,
401             TokenTypes.CTOR_DEF,
402             TokenTypes.VARIABLE_DEF,
403             TokenTypes.RECORD_DEF,
404             TokenTypes.COMPACT_CTOR_DEF,
405         };
406         assertWithMessage("Default acceptable tokens are invalid")
407             .that(actual)
408             .isEqualTo(expected);
409     }
410 
411     @Test
412     public void testPrePreviousLineEmptiness() throws Exception {
413         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
414         checkConfig.addProperty("allowMultipleEmptyLines", "false");
415         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
416         verify(checkConfig,
417             getPath("InputEmptyLineSeparatorPrePreviousLineEmptiness.java"), expected);
418     }
419 
420     @Test
421     public void testPrePreviousLineIsEmpty() throws Exception {
422         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
423         checkConfig.addProperty("allowMultipleEmptyLines", "false");
424         final String[] expected = {
425             "3:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "package"),
426         };
427         verify(checkConfig,
428                 getPath("InputEmptyLineSeparatorPrePreviousLineIsEmpty.java"), expected);
429     }
430 
431     @Test
432     public void testPreviousLineEmptiness() throws Exception {
433         final String[] expected = {
434             "21:30: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
435             "26:5: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
436             "32:67: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
437             "41:48: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
438             "51:21: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
439         };
440         verifyWithInlineConfigParser(
441                 getPath("InputEmptyLineSeparatorPreviousLineEmptiness.java"), expected);
442     }
443 
444     @Test
445     public void testDisAllowMultipleEmptyLinesInsideClassMembers() throws Exception {
446         final String[] expected = {
447             "18:11: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
448             "30:11: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
449             "36:11: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
450             "41:35: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
451             "46:11: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
452             "53:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
453         };
454         verifyWithInlineConfigParser(
455                 getPath("InputEmptyLineSeparatorMultipleEmptyLinesInside.java"),
456                 expected);
457     }
458 
459     @Test
460     public void testAllowMultipleEmptyLinesInsideClassMembers() throws Exception {
461         final String[] expected = {
462             "53:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
463         };
464         verifyWithInlineConfigParser(
465                 getPath("InputEmptyLineSeparatorMultipleEmptyLinesInside2.java"),
466                 expected);
467     }
468 
469     @Test
470     public void testImportsAndStaticImports() throws Exception {
471         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
472         verifyWithInlineConfigParser(
473                 getPath("InputEmptyLineSeparatorImports.java"), expected);
474     }
475 
476     @Test
477     public void testAllowPackageAnnotation() throws Exception {
478         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
479         verifyWithInlineConfigParser(
480                 getPath("packageinfo/test1/package-info.java"),
481                 expected);
482     }
483 
484     @Test
485     public void testAllowJavadocBeforePackage() throws Exception {
486         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
487         verifyWithInlineConfigParser(
488                 getPath("packageinfo/test2/package-info.java"),
489                 expected);
490     }
491 
492     @Test
493     public void testDisAllowBlockCommentBeforePackage() throws Exception {
494         final String[] expected = {
495             "15:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
496         };
497         verifyWithInlineConfigParser(
498                 getPath("packageinfo/test3/package-info.java"),
499                 expected);
500     }
501 
502     @Test
503     public void testAllowSingleLineCommentPackage() throws Exception {
504         final String[] expected = {
505             "16:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
506         };
507         verifyWithInlineConfigParser(
508                 getPath("packageinfo/test4/package-info.java"),
509                 expected);
510     }
511 
512     @Test
513     public void testNonPackageInfoWithJavadocBeforePackage() throws Exception {
514         final String[] expected = {
515             "15:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
516         };
517         verifyWithInlineConfigParser(
518                 getPath("InputEmptyLineSeparatorNonPackageInfoWithJavadocBeforePackage.java"),
519                 expected);
520     }
521 
522     @Test
523     public void testClassOnly() throws Exception {
524         final String[] expected = {
525             "51:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
526         };
527         verifyWithInlineConfigParser(
528                 getPath("InputEmptyLineSeparatorMultipleEmptyLinesInside3.java"),
529                 expected);
530     }
531 
532     @Test
533     public void testLineSeparationBeforeComments() throws Exception {
534         final String[] expected = {
535             "12:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
536             "16:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
537             "20:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "import"),
538             "25:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
539             "32:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
540             "43:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
541             "60:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
542             "71:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
543             "76:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "import"),
544             "82:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
545             "86:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
546             "94:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
547             "99:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
548             "106:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
549             "119:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
550             "132:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
551             "139:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
552             "149:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
553             "164:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
554             "181:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "CLASS_DEF"),
555             "187:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
556             "191:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
557             "197:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
558             "209:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
559             "222:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
560             "236:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
561             "239:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
562             "244:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
563             "260:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
564             "269:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
565             "282:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
566             "287:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
567             "293:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
568             "301:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
569             "310:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
570             "316:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
571             "336:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
572             "344:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
573         };
574         verifyWithInlineConfigParser(
575                 getPath("InputEmptyLineSeparatorWithComments.java"), expected);
576     }
577 
578     @Test
579     public void testIgnoreEmptyLinesBeforeCommentsWhenItIsAllowed() throws Exception {
580         final String[] expected = {
581             "12:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
582             "239:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
583         };
584         verifyWithInlineConfigParser(
585                 getPath("InputEmptyLineSeparatorWithComments2.java"), expected);
586     }
587 
588     @Test
589     public void testNoViolationsOnEmptyLinesBeforeComments() throws Exception {
590         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
591         verifyWithInlineConfigParser(
592                 getPath("InputEmptyLineSeparatorNoViolationOnEmptyLineBeforeComments.java"),
593                 expected);
594     }
595 
596     @Test
597     public void testEmptyLineSeparatorRecordsAndCompactCtors() throws Exception {
598         final String[] expected = {
599             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
600             "18:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "RECORD_DEF"),
601             "20:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
602             "21:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "COMPACT_CTOR_DEF"),
603             "22:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
604             "23:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
605             "25:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "RECORD_DEF"),
606             "26:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
607             "28:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
608             "29:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
609             "30:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
610             "35:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "COMPACT_CTOR_DEF"),
611             "36:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
612             "41:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "COMPACT_CTOR_DEF"),
613             "42:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
614         };
615 
616         verifyWithInlineConfigParser(
617                 getNonCompilablePath("InputEmptyLineSeparatorRecordsAndCompactCtors.java"),
618                 expected);
619     }
620 
621     @Test
622     public void testEmptyLineSeparatorRecordsAndCompactCtorsNoEmptyLines() throws Exception {
623 
624         final String[] expected = {
625             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
626             "17:27: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
627             "23:29: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
628         };
629 
630         verifyWithInlineConfigParser(
631                 getNonCompilablePath(
632                         "InputEmptyLineSeparatorRecordsAndCompactCtorsNoEmptyLines.java"),
633                 expected);
634     }
635 
636     @Test
637     public void testEmptyLineSeparatorMultipleSingleTypeVariables() throws Exception {
638 
639         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
640 
641         verifyWithInlineConfigParser(
642                 getPath("InputEmptyLineSeparatorSingleTypeVariables.java"),
643                 expected);
644     }
645 
646     @Test
647     public void testEmptyLineSeparatorEmptyLinesInsideClassMembersRecursive() throws Exception {
648         final String[] expected = {
649             "27:15: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
650         };
651         verifyWithInlineConfigParser(
652                 getPath("InputEmptyLineSeparatorRecursive.java"),
653                 expected);
654     }
655 
656     @Test
657     public void testEmptyLineSeparatorNewMethodDef() throws Exception {
658         final String[] expected = {
659             "29:34: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
660             "38:26: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
661         };
662         verifyWithInlineConfigParser(
663                 getPath("InputEmptyLineSeparatorNewMethodDef.java"),
664                 expected);
665     }
666 
667     @Test
668     public void testEmptyLineSeparatorPostFixCornerCases() throws Exception {
669         final String[] expected = {
670             "18:19: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
671             "32:29: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
672             "43:29: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
673         };
674         verifyWithInlineConfigParser(
675                 getPath("InputEmptyLineSeparatorPostFixCornerCases.java"),
676                 expected);
677     }
678 
679     @Test
680     public void testEmptyLineSeparatorAnnotation() throws Exception {
681         final String[] expected = {
682             "18:22: " + getCheckMessage(MSG_MULTIPLE_LINES_AFTER, "}"),
683         };
684         verifyWithInlineConfigParser(
685                 getPath("InputEmptyLineSeparatorAnnotations.java"),
686                 expected);
687     }
688 
689     @Test
690     public void testEmptyLineSeparatorWithEmoji() throws Exception {
691 
692         final String[] expected = {
693             "22:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
694             "27:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
695             "33:15: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
696             "41:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
697         };
698         verifyWithInlineConfigParser(
699                 getPath("InputEmptyLineSeparatorWithEmoji.java"),
700                 expected);
701     }
702 
703     @Test
704     public void testMultipleLines() throws Exception {
705         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
706         verifyWithInlineConfigParser(
707                 getPath("InputEmptyLineSeparatorMultipleLines.java"),
708                 expected);
709     }
710 
711     @Test
712     public void testMultipleLines2() throws Exception {
713         final String[] expected = {
714             "15:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
715         };
716         verifyWithInlineConfigParser(
717                 getPath("InputEmptyLineSeparatorMultipleLines2.java"),
718                 expected);
719     }
720 
721     @Test
722     public void testMultipleLines3() throws Exception {
723         final String[] expected = {
724             "24:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
725         };
726         verifyWithInlineConfigParser(
727                 getNonCompilablePath("InputEmptyLineSeparatorMultipleLines3.java"),
728                 expected);
729     }
730 
731 }