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.Assertions;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Test case for {@link CheckstyleValidator}'s handling of
23   * {@link EnumValueNameCheck}.
24   * @since 0.25.1
25   */
26  final class EnumValueNameCheckTest {
27  
28      @Test
29      void acceptsProperlyNamedEnumValues() throws Exception {
30          Assertions.assertDoesNotThrow(
31              () -> this.runValidation("ValidEnumValues.java", true)
32          );
33      }
34  
35      @Test
36      void rejectsImproperlyNamedEnumValues() throws Exception {
37          final String file = "InvalidEnumValues.java";
38          final String name = "EnumValueNameCheck";
39          MatcherAssert.assertThat(
40              "All three enum value naming violations should be reported",
41              this.runValidation(file, false),
42              Matchers.hasItems(
43                  new ViolationMatcher(
44                      "Enum value anyName must match pattern",
45                      file, "16", name
46                  ),
47                  new ViolationMatcher(
48                      "Enum value MixedCase must match pattern",
49                      file, "21", name
50                  ),
51                  new ViolationMatcher(
52                      "Enum value lowercase must match pattern",
53                      file, "26", name
54                  )
55              )
56          );
57      }
58  
59      private Collection<Violation> runValidation(final String file,
60          final boolean passes) throws IOException {
61          final Environment.Mock mock = new Environment.Mock();
62          final Environment env = mock.withParam(
63              "license",
64              String.format(
65                  "file:%s",
66                  new License().savePackageInfo(
67                      new File(mock.basedir(), "src/main/java/foo")
68                  ).withLines("Hello.")
69                      .withEol(String.valueOf('\n')).file()
70              )
71          ).withFile(
72              String.format("src/main/java/foo/%s", file),
73              new IoCheckedText(
74                  new TextOf(
75                      new ResourceOf(
76                          new FormattedText("com/qulice/checkstyle/%s", file)
77                      )
78                  )
79              ).asString()
80          );
81          final Collection<Violation> results =
82              new CheckstyleValidator(env).validate(env.files(file));
83          MatcherAssert.assertThat(
84              "validation result should match expected state",
85              results.isEmpty(),
86              Matchers.is(passes)
87          );
88          return results;
89      }
90  }