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