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 CatchParameterName} check.
23   *
24   * @since 0.25.1
25   */
26  final class CheckstyleCatchParameterNameTest {
27  
28      @Test
29      @SuppressWarnings("unchecked")
30      void distinguishesValidCatchParameterNames() throws Exception {
31          final String file = "CatchParameterNames.java";
32          final String name = "CatchParameterNameCheck";
33          MatcherAssert.assertThat(
34              "All naming violations should be found",
35              this.runValidation(file, false),
36              Matchers.<Iterable<Violation>>allOf(
37                  Matchers.iterableWithSize(4),
38                  Matchers.hasItems(
39                      new ViolationMatcher(
40                          "Name 'ex_invalid_1' must match pattern", file, "34", name
41                      ),
42                      new ViolationMatcher(
43                          "Name '$xxx' must match pattern", file, "36", name
44                      ),
45                      new ViolationMatcher(
46                          "Name '_exp' must match pattern", file, "38", name
47                      ),
48                      new ViolationMatcher(
49                          "Name '$xxx' must match pattern", file, "36",
50                          "IllegalIdentifierNameCheck"
51                      )
52                  )
53              )
54          );
55      }
56  
57      private Collection<Violation> runValidation(final String file,
58          final boolean passes) throws IOException {
59          final Environment.Mock mock = new Environment.Mock();
60          final Environment env = mock.withParam(
61              "license",
62              String.format(
63                  "file:%s",
64                  new License().savePackageInfo(
65                      new File(mock.basedir(), "src/main/java/foo")
66                  ).withLines("Hello.")
67                      .withEol(String.valueOf('\n')).file()
68              )
69          ).withFile(
70              String.format("src/main/java/foo/%s", file),
71              new IoCheckedText(
72                  new TextOf(
73                      new ResourceOf(
74                          new FormattedText("com/qulice/checkstyle/%s", file)
75                      )
76                  )
77              ).asString()
78          );
79          final Collection<Violation> results =
80              new CheckstyleValidator(env).validate(env.files(file));
81          MatcherAssert.assertThat(
82              "validation result should match expected state",
83              results.isEmpty(),
84              Matchers.is(passes)
85          );
86          return results;
87      }
88  }