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 LocalVariableName} and {@code CatchParameterName} checks.
23   *
24   * @since 0.25.1
25   */
26  final class CheckstyleLocalVariableNameTest {
27  
28      @Test
29      @SuppressWarnings("unchecked")
30      void allowsOnlyProperlyNamedLocalVariables() throws Exception {
31          final String file = "LocalVariableNames.java";
32          MatcherAssert.assertThat(
33              "Only invalid variables name should be found",
34              this.runValidation(file, false),
35              Matchers.<Iterable<Violation>>allOf(
36                  Matchers.iterableWithSize(10),
37                  Matchers.not(
38                      Matchers.hasItems(
39                          new ViolationMatcher("aaa", file),
40                          new ViolationMatcher("twelveletter", file),
41                          new ViolationMatcher("ise", file),
42                          new ViolationMatcher("id", file),
43                          new ViolationMatcher("parametername", file)
44                      )
45                  ),
46                  Matchers.hasItems(
47                      new ViolationMatcher(
48                          "Name 'prolongations' must match pattern", file
49                      ),
50                      new ViolationMatcher(
51                          "Name 'very_long_variable_id' must match pattern", file
52                      ),
53                      new ViolationMatcher(
54                          "Name 'camelCase' must match pattern", file
55                      ),
56                      new ViolationMatcher(
57                          "Name 'it' must match pattern", file
58                      ),
59                      new ViolationMatcher(
60                          "Name 'number1' must match pattern", file
61                      ),
62                      new ViolationMatcher(
63                          "Name 'ex' must match pattern", file
64                      ),
65                      new ViolationMatcher(
66                          "Name 'a' must match pattern", file
67                      ),
68                      new ViolationMatcher(
69                          "Name 'ae' must match pattern", file
70                      ),
71                      new ViolationMatcher(
72                          "Name 'e' must match pattern", file
73                      ),
74                      new ViolationMatcher(
75                          "Name 'it' must match pattern", file
76                      )
77                  )
78              )
79          );
80      }
81  
82      private Collection<Violation> runValidation(final String file,
83          final boolean passes) throws IOException {
84          final Environment.Mock mock = new Environment.Mock();
85          final Environment env = mock.withParam(
86              "license",
87              String.format(
88                  "file:%s",
89                  new License().savePackageInfo(
90                      new File(mock.basedir(), "src/main/java/foo")
91                  ).withLines("Hello.")
92                      .withEol(String.valueOf('\n')).file()
93              )
94          ).withFile(
95              String.format("src/main/java/foo/%s", file),
96              new IoCheckedText(
97                  new TextOf(
98                      new ResourceOf(
99                          new FormattedText("com/qulice/checkstyle/%s", file)
100                     )
101                 )
102             ).asString()
103         );
104         final Collection<Violation> results =
105             new CheckstyleValidator(env).validate(env.files(file));
106         MatcherAssert.assertThat(
107             "validation result should match expected state",
108             results.isEmpty(),
109             Matchers.is(passes)
110         );
111         return results;
112     }
113 }