View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.checkstyle;
6   
7   import com.qulice.spi.Environment;
8   import com.qulice.spi.Violation;
9   import java.io.File;
10  import java.io.IOException;
11  import java.util.Collection;
12  import org.cactoos.io.ResourceOf;
13  import org.cactoos.text.FormattedText;
14  import org.cactoos.text.IoCheckedText;
15  import org.cactoos.text.TextOf;
16  import org.hamcrest.MatcherAssert;
17  import org.hamcrest.Matchers;
18  import org.junit.jupiter.api.Assertions;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Test case for {@link CheckstyleValidator}'s handling of
23   * {@link EmptyLinesCheck}.
24   * @since 0.25.1
25   */
26  final class EmptyLinesCheckTest {
27  
28      @Test
29      void allowsSpacesBetweenMethodsOfAnonymousClasses() throws Exception {
30          Assertions.assertDoesNotThrow(
31              () -> this.runValidation("BlankLinesOutsideMethodsPass.java", true)
32          );
33      }
34  
35      @Test
36      @SuppressWarnings("unchecked")
37      void rejectsSpacesInsideMethods() throws Exception {
38          final String file = "BlankLinesInsideMethodsFail.java";
39          final String name = "EmptyLinesCheck";
40          final String message = "Empty line inside method";
41          MatcherAssert.assertThat(
42              "All empty lines should be found",
43              this.runValidation(file, false),
44              Matchers.hasItems(
45                  new ViolationMatcher(message, file, "15", name),
46                  new ViolationMatcher(message, file, "19", name),
47                  new ViolationMatcher(message, file, "21", name),
48                  new ViolationMatcher(message, file, "25", name),
49                  new ViolationMatcher(message, file, "28", name),
50                  new ViolationMatcher(message, file, "32", name),
51                  new ViolationMatcher(message, file, "34", name),
52                  new ViolationMatcher(message, file, "38", name),
53                  new ViolationMatcher(message, file, "41", name),
54                  new ViolationMatcher(message, file, "48", name),
55                  new ViolationMatcher(message, file, "50", name),
56                  new ViolationMatcher(message, file, "52", name)
57              )
58          );
59      }
60  
61      private Collection<Violation> runValidation(final String file,
62          final boolean passes) throws IOException {
63          final Environment.Mock mock = new Environment.Mock();
64          final Environment env = mock.withParam(
65              "license",
66              String.format(
67                  "file:%s",
68                  new License().savePackageInfo(
69                      new File(mock.basedir(), "src/main/java/foo")
70                  ).withLines("Hello.")
71                      .withEol(String.valueOf('\n')).file()
72              )
73          ).withFile(
74              String.format("src/main/java/foo/%s", file),
75              new IoCheckedText(
76                  new TextOf(
77                      new ResourceOf(
78                          new FormattedText("com/qulice/checkstyle/%s", file)
79                      )
80                  )
81              ).asString()
82          );
83          final Collection<Violation> results =
84              new CheckstyleValidator(env).validate(env.files(file));
85          MatcherAssert.assertThat(
86              "validation result should match expected state",
87              results.isEmpty(),
88              Matchers.is(passes)
89          );
90          return results;
91      }
92  }