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 final class CheckstyleHiddenFieldTest {
26
27 @Test
28 void acceptsConstructorParametersNamedJustLikeFields() throws Exception {
29 final String file = "ConstructorParams.java";
30 final String name = "HiddenFieldCheck";
31 MatcherAssert.assertThat(
32 "Two hidden fields in ctor should be found",
33 this.runValidation(file, false),
34 Matchers.allOf(
35 Matchers.hasItem(
36 new ViolationMatcher(
37 "'number' hides a field.", file, "29", name
38 )
39 ),
40 Matchers.not(
41 Matchers.hasItem(
42 new ViolationMatcher(
43 "'number' hides a field.", file, "20", name
44 )
45 )
46 )
47 )
48 );
49 }
50
51 @Test
52 void rejectsHiddenParameters() throws Exception {
53 final String file = "HiddenParameter.java";
54 MatcherAssert.assertThat(
55 "Hidden parameter in methods should be found",
56 this.runValidation(file, false),
57 Matchers.hasItems(
58 new ViolationMatcher(
59 "'test' hides a field.", file, "18", "HiddenFieldCheck"
60 )
61 )
62 );
63 }
64
65 private Collection<Violation> runValidation(final String file,
66 final boolean passes) throws IOException {
67 final Environment.Mock mock = new Environment.Mock();
68 final Environment env = mock.withParam(
69 "license",
70 String.format(
71 "file:%s",
72 new License().savePackageInfo(
73 new File(mock.basedir(), "src/main/java/foo")
74 ).withLines("Hello.")
75 .withEol(String.valueOf('\n')).file()
76 )
77 ).withFile(
78 String.format("src/main/java/foo/%s", file),
79 new IoCheckedText(
80 new TextOf(
81 new ResourceOf(
82 new FormattedText("com/qulice/checkstyle/%s", file)
83 )
84 )
85 ).asString()
86 );
87 final Collection<Violation> results =
88 new CheckstyleValidator(env).validate(env.files(file));
89 MatcherAssert.assertThat(
90 "validation result should match expected state",
91 results.isEmpty(),
92 Matchers.is(passes)
93 );
94 return results;
95 }
96 }