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.Test;
19  
20  /**
21   * Test case for {@link CheckstyleValidator}'s handling of
22   * {@link ExtraSemicolonCheck}.
23   * @since 0.25.1
24   */
25  final class ExtraSemicolonCheckTest {
26  
27      @Test
28      void rejectsUnnecessarySemicolonsAfterDeclarations() throws Exception {
29          final String file = "ExtraSemicolonInDeclaration.java";
30          final String name = "ExtraSemicolonCheck";
31          final String message = "Unnecessary semicolon";
32          MatcherAssert.assertThat(
33              "Stray semicolons after constructor, method and class must be reported",
34              this.runValidation(file, false),
35              Matchers.hasItems(
36                  new ViolationMatcher(message, file, "20", name),
37                  new ViolationMatcher(message, file, "28", name),
38                  new ViolationMatcher(message, file, "29", name)
39              )
40          );
41      }
42  
43      private Collection<Violation> runValidation(final String file,
44          final boolean passes) throws IOException {
45          final Environment.Mock mock = new Environment.Mock();
46          final Environment env = mock.withParam(
47              "license",
48              String.format(
49                  "file:%s",
50                  new License().savePackageInfo(
51                      new File(mock.basedir(), "src/main/java/foo")
52                  ).withLines("Hello.")
53                      .withEol(String.valueOf('\n')).file()
54              )
55          ).withFile(
56              String.format("src/main/java/foo/%s", file),
57              new IoCheckedText(
58                  new TextOf(
59                      new ResourceOf(
60                          new FormattedText("com/qulice/checkstyle/%s", file)
61                      )
62                  )
63              ).asString()
64          );
65          final Collection<Violation> results =
66              new CheckstyleValidator(env).validate(env.files(file));
67          MatcherAssert.assertThat(
68              "validation result should match expected state",
69              results.isEmpty(),
70              Matchers.is(passes)
71          );
72          return results;
73      }
74  }