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 TypeName} check.
23   *
24   * @since 0.73.2
25   */
26  final class CheckstyleTypeNameTest {
27  
28      @Test
29      void rejectsUnderscoredNameInProductionClass() throws Exception {
30          final String file = "Underscored_Name.java";
31          MatcherAssert.assertThat(
32              "Underscored production type name cannot pass",
33              this.runValidation(file, false),
34              Matchers.hasItem(
35                  new ViolationMatcher(
36                      "must match pattern",
37                      file,
38                      "11",
39                      "TypeNameCheck"
40                  )
41              )
42          );
43      }
44  
45      @Test
46      void acceptsUnderscoredNameInUnitTestClass() throws Exception {
47          MatcherAssert.assertThat(
48              "Underscored unit test type name cannot fail",
49              this.runValidation("Underscored_NameTest.java", true),
50              Matchers.empty()
51          );
52      }
53  
54      @Test
55      void acceptsUnderscoredNameInIntegrationTestClass() throws Exception {
56          MatcherAssert.assertThat(
57              "Underscored integration test type name cannot fail",
58              this.runValidation("Underscored_NameIT.java", true),
59              Matchers.empty()
60          );
61      }
62  
63      private Collection<Violation> runValidation(final String file,
64          final boolean passes) throws IOException {
65          final Environment.Mock mock = new Environment.Mock();
66          final Environment env = mock.withParam(
67              "license",
68              String.format(
69                  "file:%s",
70                  new License().savePackageInfo(
71                      new File(mock.basedir(), "src/main/java/foo")
72                  ).withLines("Hello.")
73                      .withEol(String.valueOf('\n')).file()
74              )
75          ).withFile(
76              String.format("src/main/java/foo/%s", file),
77              new IoCheckedText(
78                  new TextOf(
79                      new ResourceOf(
80                          new FormattedText("com/qulice/checkstyle/%s", file)
81                      )
82                  )
83              ).asString()
84          );
85          final Collection<Violation> results =
86              new CheckstyleValidator(env).validate(env.files(file));
87          MatcherAssert.assertThat(
88              "validation result should match expected state",
89              results.isEmpty(),
90              Matchers.is(passes)
91          );
92          return results;
93      }
94  }