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 the
22   * stock {@code HexLiteralCase} check.
23   *
24   * @since 0.25.1
25   */
26  final class CheckstyleHexLiteralCaseTest {
27  
28      @Test
29      void rejectsLowerCaseHexadecimalLetters() throws Exception {
30          final String file = "HexLiteralCase.java";
31          MatcherAssert.assertThat(
32              "Hexadecimal literal in lower case cannot pass",
33              this.runValidation(file, false),
34              Matchers.hasItem(
35                  new ViolationMatcher(
36                      "Should use uppercase hexadecimal letters",
37                      file,
38                      "20",
39                      "HexLiteralCaseCheck"
40                  )
41              )
42          );
43      }
44  
45      private Collection<Violation> runValidation(final String file,
46          final boolean passes) throws IOException {
47          final Environment.Mock mock = new Environment.Mock();
48          final Environment env = mock.withParam(
49              "license",
50              String.format(
51                  "file:%s",
52                  new License().savePackageInfo(
53                      new File(mock.basedir(), "src/main/java/foo")
54                  ).withLines("Hello.")
55                      .withEol(String.valueOf('\n')).file()
56              )
57          ).withFile(
58              String.format("src/main/java/foo/%s", file),
59              new IoCheckedText(
60                  new TextOf(
61                      new ResourceOf(
62                          new FormattedText("com/qulice/checkstyle/%s", file)
63                      )
64                  )
65              ).asString()
66          );
67          final Collection<Violation> results =
68              new CheckstyleValidator(env).validate(env.files(file));
69          MatcherAssert.assertThat(
70              "validation result should match expected state",
71              results.isEmpty(),
72              Matchers.is(passes)
73          );
74          return results;
75      }
76  }