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 ConstantUsageCheck}.
24   *
25   * @since 0.25.1
26   */
27  final class ConstantUsageCheckTest {
28  
29      @Test
30      void acceptsConstantUsedInMethodAnnotation() throws Exception {
31          Assertions.assertDoesNotThrow(
32              () -> this.runValidation("AnnotationConstant.java", true)
33          );
34      }
35  
36      @Test
37      void acceptsConstantUsedInFieldAndClassAnnotations() throws Exception {
38          Assertions.assertDoesNotThrow(
39              () -> this.runValidation("AnnotationConstantField.java", true)
40          );
41      }
42  
43      @Test
44      void acceptsConstantUsedBeforeItsDeclaration() throws Exception {
45          final String file = "ConstantUsedBeforeDeclaration.java";
46          MatcherAssert.assertThat(
47              "private constant used by earlier-declared constants must not be flagged as unused",
48              this.runValidation(file, false),
49              Matchers.not(
50                  Matchers.hasItem(
51                      new ViolationMatcher(
52                          "Private constant \"OP_ZERO\" is not used", file
53                      )
54                  )
55              )
56          );
57      }
58  
59      private Collection<Violation> runValidation(final String file,
60          final boolean passes) throws IOException {
61          final Environment.Mock mock = new Environment.Mock();
62          final Environment env = mock.withParam(
63              "license",
64              String.format(
65                  "file:%s",
66                  new License().savePackageInfo(
67                      new File(mock.basedir(), "src/main/java/foo")
68                  ).withLines("Hello.")
69                      .withEol(String.valueOf('\n')).file()
70              )
71          ).withFile(
72              String.format("src/main/java/foo/%s", file),
73              new IoCheckedText(
74                  new TextOf(
75                      new ResourceOf(
76                          new FormattedText("com/qulice/checkstyle/%s", file)
77                      )
78                  )
79              ).asString()
80          );
81          final Collection<Violation> results =
82              new CheckstyleValidator(env).validate(env.files(file));
83          MatcherAssert.assertThat(
84              "validation result should match expected state",
85              results.isEmpty(),
86              Matchers.is(passes)
87          );
88          return results;
89      }
90  }