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 UnknownSuppressionCheck}.
24   *
25   * @since 1.0
26   */
27  final class UnknownSuppressionCheckTest {
28  
29      @Test
30      void rejectsSuppressionOfCheckThatIsNotConfigured() throws Exception {
31          final String file = "UnknownSuppression.java";
32          final String name = "UnknownSuppressionCheck";
33          MatcherAssert.assertThat(
34              "Invented and absent check names must be reported",
35              this.runValidation(file, false),
36              Matchers.hasItems(
37                  new ViolationMatcher("FooBar", file, "4", name),
38                  new ViolationMatcher(
39                      "ClassDataAbstractionCoupling", file, "12", name
40                  )
41              )
42          );
43      }
44  
45      @Test
46      void rejectsPlainTextSuppressionOfUnknownCheck() throws Exception {
47          final String file = "DisabledUnknownSuppression.java";
48          MatcherAssert.assertThat(
49              "Disabling a check that does not exist must be reported",
50              this.runValidation(file, false),
51              Matchers.hasItem(
52                  new ViolationMatcher(
53                      "Bogus", file, "4", "UnknownSuppressionCheck"
54                  )
55              )
56          );
57      }
58  
59      @Test
60      void acceptsSuppressionOfConfiguredCheck() throws Exception {
61          Assertions.assertDoesNotThrow(
62              () -> this.runValidation("KnownSuppression.java", true)
63          );
64      }
65  
66      private Collection<Violation> runValidation(final String file,
67          final boolean passes) throws IOException {
68          final Environment.Mock mock = new Environment.Mock();
69          final Environment env = mock.withParam(
70              "license",
71              String.format(
72                  "file:%s",
73                  new License().savePackageInfo(
74                      new File(mock.basedir(), "src/main/java/foo")
75                  ).withLines("Hello.")
76                      .withEol(String.valueOf('\n')).file()
77              )
78          ).withFile(
79              String.format("src/main/java/foo/%s", file),
80              new IoCheckedText(
81                  new TextOf(
82                      new ResourceOf(
83                          new FormattedText("com/qulice/checkstyle/%s", file)
84                      )
85                  )
86              ).asString()
87          );
88          final Collection<Violation> results =
89              new CheckstyleValidator(env).validate(env.files(file));
90          MatcherAssert.assertThat(
91              "validation result should match expected state",
92              results.isEmpty(),
93              Matchers.is(passes)
94          );
95          return results;
96      }
97  }