1
2
3
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
22
23
24
25
26
27 final class CheckstyleReturnCountTest {
28
29 @Test
30 void reportsErrorOnMoreThanOneReturnStatement() throws Exception {
31 final String file = "ReturnCount.java";
32 MatcherAssert.assertThat(
33 "More than one return must be reported",
34 this.runValidation(file, false),
35 Matchers.hasItem(
36 new ViolationMatcher(
37 "Return count is 2 (max allowed for non-void methods/lambdas is 1)",
38 file
39 )
40 )
41 );
42 }
43
44 @Test
45 void doesNotLeakViolationsBetweenTests() throws Exception {
46 MatcherAssert.assertThat(
47 "ReturnCount.java must not yield violations from ParametrizedClass.java",
48 this.runValidation("ReturnCount.java", false),
49 Matchers.not(
50 Matchers.hasItem(
51 new ViolationMatcher(
52 "Type Javadoc comment is missing @param <T> tag.",
53 "ReturnCount.java"
54 )
55 )
56 )
57 );
58 }
59
60 private Collection<Violation> runValidation(final String file,
61 final boolean passes) throws IOException {
62 final Environment.Mock mock = new Environment.Mock();
63 final Environment env = mock.withParam(
64 "license",
65 String.format(
66 "file:%s",
67 new License().savePackageInfo(
68 new File(mock.basedir(), "src/main/java/foo")
69 ).withLines("Hello.")
70 .withEol(String.valueOf('\n')).file()
71 )
72 ).withFile(
73 String.format("src/main/java/foo/%s", file),
74 new IoCheckedText(
75 new TextOf(
76 new ResourceOf(
77 new FormattedText("com/qulice/checkstyle/%s", file)
78 )
79 )
80 ).asString()
81 );
82 final Collection<Violation> results =
83 new CheckstyleValidator(env).validate(env.files(file));
84 MatcherAssert.assertThat(
85 "validation result should match expected state",
86 results.isEmpty(),
87 Matchers.is(passes)
88 );
89 return results;
90 }
91 }