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 VisibilityModifier} check.
23   *
24   * @since 0.28.0
25   */
26  final class CheckstyleVisibilityModifierTest {
27  
28      /**
29       * Name of the file with Maven parameters.
30       */
31      private static final String FILE = "MavenParameters.java";
32  
33      /**
34       * Name of the check.
35       */
36      private static final String NAME = "VisibilityModifierCheck";
37  
38      @Test
39      void acceptsProtectedMavenParameters() throws Exception {
40          MatcherAssert.assertThat(
41              "protected @Parameter fields should not be reported",
42              CheckstyleVisibilityModifierTest.violations(),
43              Matchers.not(
44                  Matchers.hasItems(
45                      new ViolationMatcher(
46                          "Variable 'dir' must be private and have accessor methods.",
47                          CheckstyleVisibilityModifierTest.FILE,
48                          "19",
49                          CheckstyleVisibilityModifierTest.NAME
50                      ),
51                      new ViolationMatcher(
52                          "Variable 'charset' must be private and have accessor methods.",
53                          CheckstyleVisibilityModifierTest.FILE,
54                          "25",
55                          CheckstyleVisibilityModifierTest.NAME
56                      )
57                  )
58              )
59          );
60      }
61  
62      @Test
63      void rejectsProtectedFieldsWithoutMavenParameter() throws Exception {
64          MatcherAssert.assertThat(
65              "protected field without @Parameter should be reported",
66              CheckstyleVisibilityModifierTest.violations(),
67              Matchers.hasItem(
68                  new ViolationMatcher(
69                      "Variable 'extra' must be private and have accessor methods.",
70                      CheckstyleVisibilityModifierTest.FILE,
71                      "30",
72                      CheckstyleVisibilityModifierTest.NAME
73                  )
74              )
75          );
76      }
77  
78      private static Collection<Violation> violations() throws IOException {
79          final Environment.Mock mock = new Environment.Mock();
80          final Environment env = mock.withParam(
81              "license",
82              String.format(
83                  "file:%s",
84                  new License().savePackageInfo(
85                      new File(mock.basedir(), "src/main/java/foo")
86                  ).withLines("Hello.")
87                      .withEol(String.valueOf('\n')).file()
88              )
89          ).withFile(
90              String.format(
91                  "src/main/java/foo/%s",
92                  CheckstyleVisibilityModifierTest.FILE
93              ),
94              new IoCheckedText(
95                  new TextOf(
96                      new ResourceOf(
97                          new FormattedText(
98                              "com/qulice/checkstyle/%s",
99                              CheckstyleVisibilityModifierTest.FILE
100                         )
101                     )
102                 )
103             ).asString()
104         );
105         return new CheckstyleValidator(env).validate(
106             env.files(CheckstyleVisibilityModifierTest.FILE)
107         );
108     }
109 }