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.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      public 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     @Test
127     public void testCompactSourceFile() throws Exception {
128         final String[] expected = {
129             "16:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
130             "17:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
131             "23:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
132         };
133 
134         verifyWithInlineConfigParser(
135                 getNonCompilablePath("InputEmptyLineSeparatorCompactSourceFile.java"), expected);
136     }
137 
138     @Test
139     public void testCompactSourceFileMultipleEmptyLines() throws Exception {
140         final String[] expected = {
141             "18:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
142             "22:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
143         };
144 
145         verifyWithInlineConfigParser(
146                 getNonCompilablePath(
147                     "InputEmptyLineSeparatorCompactSourceFileMultipleEmptyLines.java"), expected);
148     }
149 
150     @Test
151     public void testMethodInAnonymousClass() throws Exception {
152         final String[] expected = {
153             "21:9: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
154         };
155 
156         verifyWithInlineConfigParser(
157                 getPath("InputEmptyLineSeparatorMethodInAnonymousClass.java"), expected);
158     }
159 
160     /**
161      * Config is defined in the method because strictly the file with one line
162      * is required to be tested.
163      */
164     @Test
165     public void testMultipleEmptyLinesInOneLine() throws Exception {
166         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
167         checkConfig.addProperty("allowNoEmptyLineBetweenFields", "true");
168         checkConfig.addProperty("allowMultipleEmptyLines", "false");
169         checkConfig.addProperty("allowMultipleEmptyLinesInsideClassMembers", "false");
170 
171         final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class);
172         treeWalkerConfig.addChild(checkConfig);
173 
174         final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig);
175 
176         final String[] expected = {
177             "1:79: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
178         };
179 
180         verify(checkerConfig, getPath("InputEmptyLineSeparatorOneLine.java"), expected);
181     }
182 
183     @Test
184     public void testDefault() throws Exception {
185 
186         final String[] expected = {
187             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
188             "31:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
189             "34:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
190             "35:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
191             "39:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INSTANCE_INIT"),
192             "53:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
193             "58:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
194             "75:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
195             "106:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
196         };
197         verifyWithInlineConfigParser(
198                 getPath("InputEmptyLineSeparator.java"), expected);
199     }
200 
201     @Test
202     public void testEmptyLineSeparatorException() throws Exception {
203 
204         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
205         verifyWithInlineConfigParser(
206                 getPath("InputEmptyLineSeparatorException.java"), expected);
207     }
208 
209     @Test
210     public void testAllowNoEmptyLineBetweenFields() throws Exception {
211 
212         final String[] expected = {
213             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
214             "31:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
215             "35:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
216             "39:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INSTANCE_INIT"),
217             "53:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
218             "58:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
219             "75:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
220             "106:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
221         };
222         verifyWithInlineConfigParser(
223                 getPath("InputEmptyLineSeparator2.java"), expected);
224     }
225 
226     @Test
227     public void testHeader() throws Exception {
228         final String[] expected = {
229             "12:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
230         };
231         verifyWithInlineConfigParser(
232                 getPath("InputEmptyLineSeparatorHeader.java"), expected);
233     }
234 
235     @Test
236     public void testMultipleEmptyLinesBetweenClassMembers() throws Exception {
237         final String[] expected = {
238             "14:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "package"),
239             "17:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "import"),
240             "22:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "CLASS_DEF"),
241             "26:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
242             "31:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
243             "36:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
244             "38:33: " + getCheckMessage(MSG_MULTIPLE_LINES_AFTER, "}"),
245         };
246         verifyWithInlineConfigParser(
247                 getPath("InputEmptyLineSeparatorMultipleEmptyLines.java"), expected);
248     }
249 
250     @Test
251     public void testFormerArrayIndexOutOfBounds() throws Exception {
252         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
253         verifyWithInlineConfigParser(
254                 getPath("InputEmptyLineSeparatorFormerException.java"), expected);
255     }
256 
257     @Test
258     public void testAllowMultipleFieldInClass() throws Exception {
259         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
260         verifyWithInlineConfigParser(
261                 getPath("InputEmptyLineSeparatorMultipleFieldsInClass.java"), expected);
262     }
263 
264     @Test
265     public void testMultiVariableDeclaration() throws Exception {
266         final String[] expected = {
267             "22:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
268         };
269         verifyWithInlineConfigParser(
270                 getPath("InputEmptyLineSeparatorMultiVariableDeclaration.java"), expected);
271     }
272 
273     @Test
274     public void testMultiVariableDeclarationAllowNoEmptyLine() throws Exception {
275         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
276         verifyWithInlineConfigParser(
277                 getPath("InputEmptyLineSeparatorMultiVariableDeclarationAllowNoEmptyLine.java"),
278                 expected);
279     }
280 
281     @Test
282     public void testAllowMultipleImportSeparatedFromPackage() throws Exception {
283         final String[] expected = {
284             "13:78: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
285         };
286         verifyWithInlineConfigParser(
287                 getPath("InputEmptyLineSeparatorMultipleImportEmptyClass.java"),
288             expected);
289     }
290 
291     @Test
292     public void testImportSeparatedFromPackage() throws Exception {
293         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
294         verifyWithInlineConfigParser(
295                 getPath("InputEmptyLineSeparatorImportSeparatedFromPackage.java"),
296             expected);
297     }
298 
299     @Test
300     public void testStaticImport() throws Exception {
301         final String[] expected = {
302             "17:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
303         };
304         verifyWithInlineConfigParser(
305                 getPath("InputEmptyLineSeparatorStaticImport.java"),
306             expected);
307     }
308 
309     @Test
310     public void testModuleImport() throws Exception {
311         final String[] expected = {
312             "19:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
313         };
314         verifyWithInlineConfigParser(
315                 getNonCompilablePath("InputEmptyLineSeparatorModuleImport.java"),
316             expected);
317     }
318 
319     @Test
320     public void testModuleImportSeparated() throws Exception {
321         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
322         verifyWithInlineConfigParser(
323                 getNonCompilablePath("InputEmptyLineSeparatorModuleImportSeparated.java"),
324             expected);
325     }
326 
327     @Test
328     public void testModuleImportMultipleEmptyLines() throws Exception {
329         final String[] expected = {
330             "20:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "import"),
331             "21:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
332         };
333         verifyWithInlineConfigParser(
334                 getNonCompilablePath("InputEmptyLineSeparatorModuleImportMultipleEmptyLines.java"),
335             expected);
336     }
337 
338     @Test
339     public void testBlockCommentNotSeparatedFromPackage() throws Exception {
340         final String[] expected = {
341             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "/*"),
342         };
343         verifyWithInlineConfigParser(
344                 getPath("InputEmptyLineSeparatorBlockCommentUnderPackage.java"),
345             expected);
346     }
347 
348     @Test
349     public void testSingleCommentNotSeparatedFromPackage() throws Exception {
350         final String[] expected = {
351             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "//"),
352         };
353         verifyWithInlineConfigParser(
354                 getPath("InputEmptyLineSeparatorSingleCommentUnderPackage.java"),
355             expected);
356     }
357 
358     @Test
359     public void testClassDefinitionNotSeparatedFromPackage() throws Exception {
360         final String[] expected = {
361             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
362         };
363         verifyWithInlineConfigParser(
364                 getPath("InputEmptyLineSeparatorModifierUnderPackage.java"),
365             expected);
366     }
367 
368     @Test
369     public void testCommentAfterPackageWithImports() throws Exception {
370         final String[] expected = {
371             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "//"),
372         };
373         verifyWithInlineConfigParser(
374                 getPath("InputEmptyLineSeparatorSingleLineCommentAfterPackage.java"),
375                 expected);
376     }
377 
378     @Test
379     public void testJavadocCommentAfterPackageWithImports() throws Exception {
380         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
381         final String[] expected = {
382             "2:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "/*"),
383         };
384         verify(checkConfig,
385                 getPath("InputEmptyLineSeparatorJavadocCommentAfterPackage.java"),
386                 expected);
387     }
388 
389     @Test
390     public void testPackageImportsClassInSingleLine() throws Exception {
391         final String[] expected = {
392             "13:79: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
393             "13:101: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
394         };
395         verifyWithInlineConfigParser(
396                 getPath("InputEmptyLineSeparatorPackageImportClassInOneLine.java"),
397                 expected);
398     }
399 
400     @Test
401     public void testEmptyLineAfterPackageForPackageAst() throws Exception {
402         final String[] expected = {
403             "12:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "/*"),
404         };
405         verifyWithInlineConfigParser(
406                 getPath("InputEmptyLineSeparatorEmptyLineAfterPackageForPackageAst.java"),
407                 expected);
408     }
409 
410     @Test
411     public void testEmptyLineAfterPackageForImportAst() throws Exception {
412         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
413         verifyWithInlineConfigParser(
414                 getPath("InputEmptyLineSeparatorEmptyLineAfterPackageForImportAst.java"),
415                 expected);
416     }
417 
418     @Test
419     public void testClassDefinitionAndCommentNotSeparatedFromPackage() throws Exception {
420         final String[] expected = {
421             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "//"),
422             "15:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
423         };
424         final String testFile =
425             "InputEmptyLineSeparatorClassDefinitionAndCommentNotSeparatedFromPackage.java";
426         verifyWithInlineConfigParser(
427                 getPath(testFile), expected);
428     }
429 
430     @Test
431     public void testBlockCommentSeparatedFromPackage() throws Exception {
432         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
433         verifyWithInlineConfigParser(
434                 getPath("InputEmptyLineSeparatorBlockCommentSeparatedFromPackage.java"),
435             expected);
436     }
437 
438     @Test
439     public void testSingleCommentSeparatedFromPackage() throws Exception {
440         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
441         verifyWithInlineConfigParser(
442                 getPath("InputEmptyLineSeparatorSingleCommentSeparatedFromPackage.java"),
443             expected);
444     }
445 
446     @Test
447     public void testEnumMembers() throws Exception {
448         final String[] expected = {
449             "22:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
450             "27:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
451             "28:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
452             "31:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "CTOR_DEF"),
453             "36:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
454             "40:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "STATIC_INIT"),
455         };
456         verifyWithInlineConfigParser(
457             getPath("InputEmptyLineSeparatorEnumMembers.java"), expected
458         );
459     }
460 
461     @Test
462     public void testInterfaceFields() throws Exception {
463         final String[] expected = {
464             "21:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
465             "25:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
466             "34:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
467             "38:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
468             "45:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
469         };
470         verifyWithInlineConfigParser(
471             getPath("InputEmptyLineSeparatorInterfaceFields.java"), expected
472         );
473     }
474 
475     @Test
476     public void testGetAcceptableTokens() {
477         final EmptyLineSeparatorCheck emptyLineSeparatorCheckObj = new EmptyLineSeparatorCheck();
478         final int[] actual = emptyLineSeparatorCheckObj.getAcceptableTokens();
479         final int[] expected = {
480             TokenTypes.PACKAGE_DEF,
481             TokenTypes.IMPORT,
482             TokenTypes.STATIC_IMPORT,
483             TokenTypes.MODULE_IMPORT,
484             TokenTypes.CLASS_DEF,
485             TokenTypes.INTERFACE_DEF,
486             TokenTypes.ENUM_DEF,
487             TokenTypes.STATIC_INIT,
488             TokenTypes.INSTANCE_INIT,
489             TokenTypes.METHOD_DEF,
490             TokenTypes.CTOR_DEF,
491             TokenTypes.VARIABLE_DEF,
492             TokenTypes.RECORD_DEF,
493             TokenTypes.COMPACT_CTOR_DEF,
494         };
495         assertWithMessage("Default acceptable tokens are invalid")
496             .that(actual)
497             .isEqualTo(expected);
498     }
499 
500     @Test
501     public void testPrePreviousLineEmptiness() throws Exception {
502         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
503         checkConfig.addProperty("allowMultipleEmptyLines", "false");
504         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
505         verify(checkConfig,
506             getPath("InputEmptyLineSeparatorPrePreviousLineEmptiness.java"), expected);
507     }
508 
509     @Test
510     public void testPrePreviousLineIsEmpty() throws Exception {
511         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
512         checkConfig.addProperty("allowMultipleEmptyLines", "false");
513         final String[] expected = {
514             "3:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "package"),
515         };
516         verify(checkConfig,
517                 getPath("InputEmptyLineSeparatorPrePreviousLineIsEmpty.java"), expected);
518     }
519 
520     @Test
521     public void testPreviousLineEmptiness() throws Exception {
522         final String[] expected = {
523             "21:30: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
524             "26:5: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
525             "32:67: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
526             "41:48: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
527             "51:21: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
528         };
529         verifyWithInlineConfigParser(
530                 getPath("InputEmptyLineSeparatorPreviousLineEmptiness.java"), expected);
531     }
532 
533     @Test
534     public void testDisAllowMultipleEmptyLinesInsideClassMembers() throws Exception {
535         final String[] expected = {
536             "18:11: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
537             "30:11: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
538             "36:11: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
539             "41:35: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
540             "46:11: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
541             "53:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
542         };
543         verifyWithInlineConfigParser(
544                 getPath("InputEmptyLineSeparatorMultipleEmptyLinesInside.java"),
545                 expected);
546     }
547 
548     @Test
549     public void testDisAllowMultipleEmptyLinesInsideClassMembers1() throws Exception {
550         final String[] expected = {
551             "23:27: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
552             "26:19: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
553             "31:64: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
554             "36:19: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
555         };
556         verifyWithInlineConfigParser(
557                 getPath("InputEmptyLineSeparatorInsideClassMembers.java"),
558                 expected);
559     }
560 
561     @Test
562     public void testAllowMultipleEmptyLinesInsideClassMembers() throws Exception {
563         final String[] expected = {
564             "53:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
565         };
566         verifyWithInlineConfigParser(
567                 getPath("InputEmptyLineSeparatorMultipleEmptyLinesInside2.java"),
568                 expected);
569     }
570 
571     @Test
572     public void testImportsAndStaticImports() throws Exception {
573         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
574         verifyWithInlineConfigParser(
575                 getPath("InputEmptyLineSeparatorImports.java"), expected);
576     }
577 
578     @Test
579     public void testAllowPackageAnnotation() throws Exception {
580         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
581         verifyWithInlineConfigParser(
582                 getPath("packageinfo/test1/package-info.java"),
583                 expected);
584     }
585 
586     @Test
587     public void testAllowJavadocBeforePackage() throws Exception {
588         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
589         verifyWithInlineConfigParser(
590                 getPath("packageinfo/test2/package-info.java"),
591                 expected);
592     }
593 
594     @Test
595     public void testDisAllowBlockCommentBeforePackage() throws Exception {
596         final String[] expected = {
597             "12:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
598         };
599         verifyWithInlineConfigParser(
600                 getPath("packageinfo/test3/package-info.java"),
601                 expected);
602     }
603 
604     @Test
605     public void testAllowSingleLineCommentPackage() throws Exception {
606         final String[] expected = {
607             "16:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
608         };
609         verifyWithInlineConfigParser(
610                 getPath("packageinfo/test4/package-info.java"),
611                 expected);
612     }
613 
614     @Test
615     public void testBlockCommentsBeforePackage() throws Exception {
616         final String[] expected = {
617             "16:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
618         };
619         verifyWithInlineConfigParser(
620                 getPath("packageinfo/test5/package-info.java"),
621                 expected);
622     }
623 
624     @Test
625     public void testNonPackageInfoWithJavadocBeforePackage() throws Exception {
626         final String[] expected = {
627             "15:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
628         };
629         verifyWithInlineConfigParser(
630                 getPath("InputEmptyLineSeparatorNonPackageInfoWithJavadocBeforePackage.java"),
631                 expected);
632     }
633 
634     @Test
635     public void testClassOnly() throws Exception {
636         final String[] expected = {
637             "51:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
638         };
639         verifyWithInlineConfigParser(
640                 getPath("InputEmptyLineSeparatorMultipleEmptyLinesInside3.java"),
641                 expected);
642     }
643 
644     @Test
645     public void testLineSeparationBeforeComments() throws Exception {
646         final String[] expected = {
647             "12:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
648             "16:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
649             "20:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "import"),
650             "25:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
651             "32:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
652             "43:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
653             "60:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
654             "71:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
655             "76:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "import"),
656             "82:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
657             "86:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
658             "94:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
659             "99:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
660             "106:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
661             "119:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
662             "132:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
663             "139:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
664             "149:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
665             "164:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
666             "181:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "CLASS_DEF"),
667             "187:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
668             "191:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
669             "197:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
670             "209:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
671             "222:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
672             "236:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
673             "239:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
674             "244:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "/*"),
675             "260:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
676             "269:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
677             "282:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
678             "287:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
679             "293:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
680             "301:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
681             "310:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
682             "316:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
683             "336:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
684             "344:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "//"),
685         };
686         verifyWithInlineConfigParser(
687                 getPath("InputEmptyLineSeparatorWithComments.java"), expected);
688     }
689 
690     @Test
691     public void testIgnoreEmptyLinesBeforeCommentsWhenItIsAllowed() throws Exception {
692         final String[] expected = {
693             "12:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
694             "239:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
695         };
696         verifyWithInlineConfigParser(
697                 getPath("InputEmptyLineSeparatorWithComments2.java"), expected);
698     }
699 
700     @Test
701     public void testNoViolationsOnEmptyLinesBeforeComments() throws Exception {
702         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
703         verifyWithInlineConfigParser(
704                 getPath("InputEmptyLineSeparatorNoViolationOnEmptyLineBeforeComments.java"),
705                 expected);
706     }
707 
708     @Test
709     public void testEmptyLineSeparatorRecordsAndCompactCtors() throws Exception {
710         final String[] expected = {
711             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
712             "18:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "RECORD_DEF"),
713             "20:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
714             "21:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "COMPACT_CTOR_DEF"),
715             "22:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
716             "23:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
717             "25:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "RECORD_DEF"),
718             "26:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
719             "28:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
720             "29:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
721             "30:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
722             "35:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "COMPACT_CTOR_DEF"),
723             "36:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
724             "41:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "COMPACT_CTOR_DEF"),
725             "42:9: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
726         };
727 
728         verifyWithInlineConfigParser(
729                 getPath("InputEmptyLineSeparatorRecordsAndCompactCtors.java"),
730                 expected);
731     }
732 
733     @Test
734     public void testEmptyLineSeparatorRecordsAndCompactCtorsNoEmptyLines() throws Exception {
735 
736         final String[] expected = {
737             "14:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
738             "17:27: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
739             "23:29: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
740         };
741 
742         verifyWithInlineConfigParser(
743                 getPath(
744                         "InputEmptyLineSeparatorRecordsAndCompactCtorsNoEmptyLines.java"),
745                 expected);
746     }
747 
748     @Test
749     public void testEmptyLineSeparatorMultipleSingleTypeVariables() throws Exception {
750 
751         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
752 
753         verifyWithInlineConfigParser(
754                 getPath("InputEmptyLineSeparatorSingleTypeVariables.java"),
755                 expected);
756     }
757 
758     @Test
759     public void testEmptyLineSeparatorEmptyLinesInsideClassMembersRecursive() throws Exception {
760         final String[] expected = {
761             "27:15: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
762         };
763         verifyWithInlineConfigParser(
764                 getPath("InputEmptyLineSeparatorRecursive.java"),
765                 expected);
766     }
767 
768     @Test
769     public void testEmptyLineSeparatorNewMethodDef() throws Exception {
770         final String[] expected = {
771             "29:34: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
772             "38:26: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
773         };
774         verifyWithInlineConfigParser(
775                 getPath("InputEmptyLineSeparatorNewMethodDef.java"),
776                 expected);
777     }
778 
779     @Test
780     public void testEmptyLineSeparatorPostFixCornerCases() throws Exception {
781         final String[] expected = {
782             "18:19: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
783             "32:29: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
784             "43:29: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
785         };
786         verifyWithInlineConfigParser(
787                 getPath("InputEmptyLineSeparatorPostFixCornerCases.java"),
788                 expected);
789     }
790 
791     @Test
792     public void testEmptyLineSeparatorAnnotation() throws Exception {
793         final String[] expected = {
794             "18:22: " + getCheckMessage(MSG_MULTIPLE_LINES_AFTER, "}"),
795         };
796         verifyWithInlineConfigParser(
797                 getPath("InputEmptyLineSeparatorAnnotations.java"),
798                 expected);
799     }
800 
801     @Test
802     public void testEmptyLineSeparatorWithEmoji() throws Exception {
803 
804         final String[] expected = {
805             "22:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
806             "27:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
807             "33:15: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
808             "41:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
809         };
810         verifyWithInlineConfigParser(
811                 getPath("InputEmptyLineSeparatorWithEmoji.java"),
812                 expected);
813     }
814 
815     @Test
816     public void testMultipleLines() throws Exception {
817         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
818         verifyWithInlineConfigParser(
819                 getPath("InputEmptyLineSeparatorMultipleLines.java"),
820                 expected);
821     }
822 
823     @Test
824     public void testMultipleLines2() throws Exception {
825         final String[] expected = {
826             "15:1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
827         };
828         verifyWithInlineConfigParser(
829                 getPath("InputEmptyLineSeparatorMultipleLines2.java"),
830                 expected);
831     }
832 
833     @Test
834     public void testMultipleLines3() throws Exception {
835         final String[] expected = {
836             "24:5: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
837         };
838         verifyWithInlineConfigParser(
839                 getPath("InputEmptyLineSeparatorMultipleLines3.java"),
840                 expected);
841     }
842 
843     @Test
844     public void testEmptyLineSeparator() throws Exception {
845         final String[] expected = {
846             "16:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "package"),
847             "21:1: " + getCheckMessage(MSG_MULTIPLE_LINES, "CLASS_DEF"),
848             "25:5: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
849         };
850         verifyWithInlineConfigParser(
851                 getPath("InputEmptyLineSeparator3.java"),
852                 expected
853         );
854     }
855 
856 }