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.Joined;
16  import org.cactoos.text.TextOf;
17  import org.hamcrest.MatcherAssert;
18  import org.hamcrest.Matchers;
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Test case for {@link CheckstyleValidator}'s handling of the
24   * stock {@code AbbreviationAsWordInName} check.
25   * @since 0.25.1
26   */
27  final class CheckstyleAbbreviationAsWordInNameTest {
28  
29      @Test
30      @SuppressWarnings("unchecked")
31      void rejectsUppercaseAbbreviations() throws Exception {
32          final String file = "InvalidAbbreviationAsWordInNameXML.java";
33          final String name = "AbbreviationAsWordInNameCheck";
34          final String message = new Joined(
35              " ",
36              "Abbreviation in name '%s'",
37              "must contain no more than '2' consecutive capital letters."
38          ).asString();
39          MatcherAssert.assertThat(
40              "All long abbreviations should be found",
41              this.runValidation(file, false),
42              Matchers.hasItems(
43                  new ViolationMatcher(
44                      String.format(
45                          message, "InvalidAbbreviationAsWordInNameXML"
46                      ),
47                      file, "10", name
48                  ),
49                  new ViolationMatcher(
50                      String.format(message, "InvalidHTML"), file,
51                      "14", name
52                  )
53              )
54          );
55      }
56  
57      @Test
58      void allowsITUppercaseAbbreviation() throws Exception {
59          Assertions.assertDoesNotThrow(
60              () -> this.runValidation("ValidAbbreviationAsWordInNameIT.java", true)
61          );
62      }
63  
64      @Test
65      void allowsUppercaseAbbreviationExceptions() throws Exception {
66          Assertions.assertDoesNotThrow(
67              () -> this.runValidation("ValidAbbreviationAsWordInName.java", true)
68          );
69      }
70  
71      private Collection<Violation> runValidation(final String file,
72          final boolean passes) throws IOException {
73          final Environment.Mock mock = new Environment.Mock();
74          final Environment env = mock.withParam(
75              "license",
76              String.format(
77                  "file:%s",
78                  new License().savePackageInfo(
79                      new File(mock.basedir(), "src/main/java/foo")
80                  ).withLines("Hello.")
81                      .withEol(String.valueOf('\n')).file()
82              )
83          ).withFile(
84              String.format("src/main/java/foo/%s", file),
85              new IoCheckedText(
86                  new TextOf(
87                      new ResourceOf(
88                          new FormattedText("com/qulice/checkstyle/%s", file)
89                      )
90                  )
91              ).asString()
92          );
93          final Collection<Violation> results =
94              new CheckstyleValidator(env).validate(env.files(file));
95          MatcherAssert.assertThat(
96              "validation result should match expected state",
97              results.isEmpty(),
98              Matchers.is(passes)
99          );
100         return results;
101     }
102 }