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 ReturnCount} check, including a regression guard
23   * against cross-test violation leakage (issue #547).
24   * @since 0.25.1
25   */
26  final class CheckstyleReturnCountTest {
27  
28      @Test
29      void reportsErrorOnMoreThanOneReturnStatement() throws Exception {
30          final String file = "ReturnCount.java";
31          MatcherAssert.assertThat(
32              "More than one return must be reported",
33              this.runValidation(file, false),
34              Matchers.hasItem(
35                  new ViolationMatcher(
36                      "Return count is 2 (max allowed for non-void methods/lambdas is 1)",
37                      file
38                  )
39              )
40          );
41      }
42  
43      @Test
44      void doesNotLeakViolationsBetweenTests() throws Exception {
45          MatcherAssert.assertThat(
46              "ReturnCount.java must not yield violations from ParametrizedClass.java",
47              this.runValidation("ReturnCount.java", false),
48              Matchers.not(
49                  Matchers.hasItem(
50                      new ViolationMatcher(
51                          "Type Javadoc comment is missing @param <T> tag.",
52                          "ReturnCount.java"
53                      )
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  }