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 UnusedSuppressions}.
24   *
25   * @since 1.0
26   */
27  final class UnusedSuppressionTest {
28  
29      @Test
30      void rejectsNearbySuppressionThatCoversNoViolation() throws Exception {
31          final String file = "UnusedSuppression.java";
32          MatcherAssert.assertThat(
33              "A nearby suppression of a check that never fires must be reported",
34              this.runValidation(file, false),
35              Matchers.hasItem(
36                  new ViolationMatcher(
37                      "This suppression covers no violation of \"LineLengthCheck\"",
38                      file, "4", "UnusedSuppressionCheck"
39                  )
40              )
41          );
42      }
43  
44      @Test
45      void rejectsDisableSuppressionThatCoversNoViolation() throws Exception {
46          final String file = "UnusedDisableSuppression.java";
47          MatcherAssert.assertThat(
48              "A disable suppression of a check that never fires must be reported",
49              this.runValidation(file, false),
50              Matchers.hasItem(
51                  new ViolationMatcher(
52                      "This suppression covers no violation of \"LineLengthCheck\"",
53                      file, "4", "UnusedSuppressionCheck"
54                  )
55              )
56          );
57      }
58  
59      @Test
60      void acceptsSuppressionThatCoversAViolation() 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  }