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 import org.junit.jupiter.params.ParameterizedTest;
20 import org.junit.jupiter.params.provider.ValueSource;
21
22
23
24
25
26
27
28 final class CheckstyleParameterNumberTest {
29
30 @Test
31 void acceptsConstructorTakingAllAttributes() throws Exception {
32 MatcherAssert.assertThat(
33 "Constructor with as many parameters as attributes cannot fail",
34 this.runValidation("FourAttributes.java", true),
35 Matchers.empty()
36 );
37 }
38
39 @Test
40 void acceptsPrivateMethodWithManyParameters() throws Exception {
41 MatcherAssert.assertThat(
42 "Private method with four parameters cannot fail",
43 this.runValidation("PrivateFourParameters.java", true),
44 Matchers.empty()
45 );
46 }
47
48 @Test
49 void rejectsMethodWithTooManyParameters() throws Exception {
50 final String file = "FourParameters.java";
51 MatcherAssert.assertThat(
52 "Method with four parameters cannot pass",
53 this.runValidation(file, false),
54 Matchers.hasItem(
55 new ViolationMatcher(
56 "More than 3 parameters (found 4)",
57 file,
58 "22",
59 "ParameterNumberCheck"
60 )
61 )
62 );
63 }
64
65 @ParameterizedTest
66 @ValueSource(
67 strings = {
68 "JnaMappedLibrary.java",
69 "JnaStdCallBinding.java"
70 }
71 )
72 void acceptsManyParametersInJnaBinding(final String file) throws Exception {
73 MatcherAssert.assertThat(
74 "long parameter list in a JNA binding should not be reported",
75 this.runValidation(file, true),
76 Matchers.not(
77 Matchers.hasItem(
78 new ViolationMatcher("", file, "", "ParameterNumberCheck")
79 )
80 )
81 );
82 }
83
84
85
86
87
88
89
90
91 @Test
92 void acceptsManyParametersInQualifiedJnaBinding() throws Exception {
93 final String file = "JnaDirectBinding.java";
94 MatcherAssert.assertThat(
95 "long parameter list in a qualified JNA binding should not be reported",
96 this.runValidation(file, false),
97 Matchers.not(
98 Matchers.hasItem(
99 new ViolationMatcher("", file, "", "ParameterNumberCheck")
100 )
101 )
102 );
103 }
104
105 private Collection<Violation> runValidation(final String file,
106 final boolean passes) throws IOException {
107 final Environment.Mock mock = new Environment.Mock();
108 final Environment env = mock.withParam(
109 "license",
110 String.format(
111 "file:%s",
112 new License().savePackageInfo(
113 new File(mock.basedir(), "src/main/java/foo")
114 ).withLines("Hello.")
115 .withEol(String.valueOf('\n')).file()
116 )
117 ).withFile(
118 String.format("src/main/java/foo/%s", file),
119 new IoCheckedText(
120 new TextOf(
121 new ResourceOf(
122 new FormattedText("com/qulice/checkstyle/%s", file)
123 )
124 )
125 ).asString()
126 );
127 final Collection<Violation> results =
128 new CheckstyleValidator(env).validate(env.files(file));
129 MatcherAssert.assertThat(
130 "validation result should match expected state",
131 results.isEmpty(),
132 Matchers.is(passes)
133 );
134 return results;
135 }
136 }