37
37
import java .io .File ;
38
38
import java .nio .charset .StandardCharsets ;
39
39
import java .util .ArrayList ;
40
- import java .util .Collection ;
41
40
import java .util .LinkedList ;
42
41
import java .util .List ;
43
42
import java .util .Properties ;
43
+ import java .util .stream .Stream ;
44
44
import org .apache .commons .io .IOUtils ;
45
45
import org .apache .commons .lang3 .StringUtils ;
46
46
import org .hamcrest .MatcherAssert ;
47
47
import org .hamcrest .Matchers ;
48
- import org .junit .Test ;
49
- import org .junit .runner .RunWith ;
50
- import org .junit .runners .Parameterized ;
48
+ import org .junit .jupiter .params .ParameterizedTest ;
49
+ import org .junit .jupiter .params .provider .MethodSource ;
51
50
import org .mockito .Mockito ;
52
51
import org .mockito .invocation .InvocationOnMock ;
53
52
import org .mockito .stubbing .Answer ;
57
56
* Integration test case for all checkstyle checks.
58
57
* @since 0.3
59
58
*/
60
- @ RunWith (Parameterized .class )
61
59
public final class ChecksTest {
62
60
63
- /**
64
- * Directories where test scripts are located.
65
- */
66
- private static final String [] CHECKS = {
67
- "MethodsOrderCheck" ,
68
- "MultilineJavadocTagsCheck" ,
69
- "StringLiteralsConcatenationCheck" ,
70
- "EmptyLinesCheck" ,
71
- "ImportCohesionCheck" ,
72
- "BracketsStructureCheck" ,
73
- "CurlyBracketsStructureCheck" ,
74
- "JavadocLocationCheck" ,
75
- "MethodBodyCommentsCheck" ,
76
- "RequireThisCheck" ,
77
- "ProtectedMethodInFinalClassCheck" ,
78
- "NoJavadocForOverriddenMethodsCheck" ,
79
- "NonStaticMethodCheck" ,
80
- "ConstantUsageCheck" ,
81
- "JavadocEmptyLineCheck" ,
82
- "JavadocParameterOrderCheck" ,
83
- "JavadocTagsCheck" ,
84
- "ProhibitNonFinalClassesCheck" ,
85
- };
86
-
87
- /**
88
- * Current directory we're working with.
89
- */
90
- private final String dir ;
91
-
92
- /**
93
- * Public ctor.
94
- * @param name The name of the check to work with
95
- */
96
- public ChecksTest (final String name ) {
97
- this .dir = String .format ("ChecksTest/%s" , name );
98
- }
99
-
100
- /**
101
- * Returns full list of checks.
102
- * @return The list
103
- */
104
- @ Parameterized .Parameters
105
- @ SuppressWarnings ("PMD.AvoidInstantiatingObjectsInLoops" )
106
- public static Collection <Object []> dirs () {
107
- final Collection <Object []> dirs = new LinkedList <>();
108
- for (final String url : ChecksTest .CHECKS ) {
109
- dirs .add (new Object [] {url });
110
- }
111
- return dirs ;
112
- }
113
-
114
61
/**
115
62
* Test checkstyle for true positive.
63
+ * @param dir Directory where test scripts are located.
116
64
* @throws Exception If something goes wrong
117
65
*/
118
- @ Test
119
- public void testCheckstyleTruePositive () throws Exception {
66
+ @ ParameterizedTest
67
+ @ MethodSource ("checks" )
68
+ public void testCheckstyleTruePositive (final String dir ) throws Exception {
120
69
final AuditListener listener = Mockito .mock (AuditListener .class );
121
70
final Collector collector = new ChecksTest .Collector ();
122
71
Mockito .doAnswer (collector ).when (listener )
123
72
.addError (Mockito .any (AuditEvent .class ));
124
- this .check ("/Invalid.java" , listener );
73
+ this .check (dir , "/Invalid.java" , listener );
125
74
final String [] violations = StringUtils .split (
126
75
IOUtils .toString (
127
76
this .getClass ().getResourceAsStream (
128
- String .format ("%s/violations.txt" , this . dir )
77
+ String .format ("%s/violations.txt" , dir )
129
78
),
130
79
StandardCharsets .UTF_8
131
80
),
@@ -142,7 +91,7 @@ public void testCheckstyleTruePositive() throws Exception {
142
91
"Line no.%d ('%s') not reported by %s: '%s'" ,
143
92
pos ,
144
93
needle ,
145
- this . dir ,
94
+ dir ,
146
95
collector .summary ()
147
96
),
148
97
Matchers .is (true )
@@ -153,32 +102,36 @@ public void testCheckstyleTruePositive() throws Exception {
153
102
154
103
/**
155
104
* Test checkstyle for true negative.
105
+ * @param dir Directory where test scripts are located.
156
106
* @throws Exception If something goes wrong
157
107
*/
158
- @ Test
159
- public void testCheckstyleTrueNegative () throws Exception {
108
+ @ ParameterizedTest
109
+ @ MethodSource ("checks" )
110
+ public void testCheckstyleTrueNegative (final String dir ) throws Exception {
160
111
final AuditListener listener = Mockito .mock (AuditListener .class );
161
112
final Collector collector = new ChecksTest .Collector ();
162
113
Mockito .doAnswer (collector ).when (listener )
163
114
.addError (Mockito .any (AuditEvent .class ));
164
- this .check ("/Valid.java" , listener );
115
+ this .check (dir , "/Valid.java" , listener );
165
116
MatcherAssert .assertThat (collector .summary (), Matchers .equalTo ("" ));
166
117
Mockito .verify (listener , Mockito .times (0 ))
167
118
.addError (Mockito .any (AuditEvent .class ));
168
119
}
169
120
170
121
/**
171
122
* Check one file.
123
+ * @param dir Directory where test scripts are located.
172
124
* @param name The name of the check
173
125
* @param listener The listener
174
126
* @throws Exception If something goes wrong inside
175
127
*/
176
- private void check (final String name , final AuditListener listener )
177
- throws Exception {
128
+ private void check (
129
+ final String dir , final String name , final AuditListener listener
130
+ ) throws Exception {
178
131
final Checker checker = new Checker ();
179
132
final InputSource src = new InputSource (
180
133
this .getClass ().getResourceAsStream (
181
- String .format ("%s/config.xml" , this . dir )
134
+ String .format ("%s/config.xml" , dir )
182
135
)
183
136
);
184
137
checker .setClassLoader (Thread .currentThread ().getContextClassLoader ());
@@ -196,7 +149,7 @@ private void check(final String name, final AuditListener listener)
196
149
files .add (
197
150
new File (
198
151
this .getClass ().getResource (
199
- String .format ("%s%s" , this . dir , name )
152
+ String .format ("%s%s" , dir , name )
200
153
).getFile ()
201
154
)
202
155
);
@@ -205,19 +158,50 @@ private void check(final String name, final AuditListener listener)
205
158
checker .destroy ();
206
159
}
207
160
161
+ /**
162
+ * Returns full list of checks.
163
+ * @return The list
164
+ */
165
+ @ SuppressWarnings ("PMD.UnusedPrivateMethod" )
166
+ private static Stream <String > checks () {
167
+ return Stream .of (
168
+ "MethodsOrderCheck" ,
169
+ "MultilineJavadocTagsCheck" ,
170
+ "StringLiteralsConcatenationCheck" ,
171
+ "EmptyLinesCheck" ,
172
+ "ImportCohesionCheck" ,
173
+ "BracketsStructureCheck" ,
174
+ "CurlyBracketsStructureCheck" ,
175
+ "JavadocLocationCheck" ,
176
+ "MethodBodyCommentsCheck" ,
177
+ "RequireThisCheck" ,
178
+ "ProtectedMethodInFinalClassCheck" ,
179
+ "NoJavadocForOverriddenMethodsCheck" ,
180
+ "NonStaticMethodCheck" ,
181
+ "ConstantUsageCheck" ,
182
+ "JavadocEmptyLineCheck" ,
183
+ "JavadocParameterOrderCheck" ,
184
+ "JavadocTagsCheck" ,
185
+ "ProhibitNonFinalClassesCheck"
186
+ ).map (s -> String .format ("ChecksTest/%s" , s ));
187
+ }
188
+
208
189
/**
209
190
* Mocked collector of checkstyle events.
210
191
*/
211
192
private static class Collector implements Answer <Object > {
193
+
212
194
/**
213
195
* List of events received.
214
196
*/
215
197
private final List <AuditEvent > events = new LinkedList <>();
198
+
216
199
@ Override
217
200
public Object answer (final InvocationOnMock invocation ) {
218
201
this .events .add ((AuditEvent ) invocation .getArguments ()[0 ]);
219
202
return null ;
220
203
}
204
+
221
205
/**
222
206
* Do we have this message for this line?
223
207
* @param line The number of the line
@@ -234,6 +218,7 @@ public boolean has(final Integer line, final String msg) {
234
218
}
235
219
return has ;
236
220
}
221
+
237
222
/**
238
223
* Returns full summary.
239
224
* @return The test summary of all events
0 commit comments