forked from yegor256/qulice
/
ChecksTest.java
262 lines (249 loc) · 9.17 KB
/
ChecksTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright (c) 2011-2024 Qulice.com
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the Qulice.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.qulice.checkstyle;
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.AuditListener;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.xml.sax.InputSource;
/**
* Integration test case for all checkstyle checks.
* @since 0.3
*/
final class ChecksTest {
/**
* Test checkstyle for true negative.
* @param dir Directory where test scripts are located.
* @throws Exception If something goes wrong
*/
@ParameterizedTest
@MethodSource("checks")
void testCheckstyleTruePositive(final String dir) throws Exception {
final AuditListener listener = Mockito.mock(AuditListener.class);
final Collector collector = new ChecksTest.Collector();
Mockito.doAnswer(collector).when(listener)
.addError(Mockito.any(AuditEvent.class));
this.check(dir, "/Invalid.java", listener);
final String[] violations = StringUtils.split(
IOUtils.toString(
this.getClass().getResourceAsStream(
String.format("%s/violations.txt", dir)
),
StandardCharsets.UTF_8
),
"\n"
);
for (final String line : violations) {
final String[] sectors = StringUtils.split(line, ":");
final Integer pos = Integer.valueOf(sectors[0]);
final String needle = sectors[1].trim();
MatcherAssert.assertThat(
collector.has(pos, needle),
Matchers.describedAs(
String.format(
"Line no.%d ('%s') not reported by %s: '%s'",
pos,
needle,
dir,
collector.summary()
),
Matchers.is(true)
)
);
}
MatcherAssert.assertThat(
collector.eventCount(),
Matchers.describedAs(
String.format(
"Got more violations that expected for directory %s (%s)",
dir, collector.summary()
),
Matchers.equalTo(violations.length)
)
);
}
/**
* Test checkstyle for true negative.
* @param dir Directory where test scripts are located.
* @throws Exception If something goes wrong
*/
@ParameterizedTest
@MethodSource("checks")
void testCheckstyleTrueNegative(final String dir) throws Exception {
final AuditListener listener = Mockito.mock(AuditListener.class);
final Collector collector = new ChecksTest.Collector();
Mockito.doAnswer(collector).when(listener)
.addError(Mockito.any(AuditEvent.class));
this.check(dir, "/Valid.java", listener);
MatcherAssert.assertThat(collector.summary(), Matchers.equalTo(""));
Mockito.verify(listener, Mockito.times(0))
.addError(Mockito.any(AuditEvent.class));
}
/**
* Check one file.
* @param dir Directory where test scripts are located.
* @param name The name of the check
* @param listener The listener
* @throws Exception If something goes wrong inside
*/
private void check(
final String dir, final String name, final AuditListener listener
) throws Exception {
final Checker checker = new Checker();
final InputSource src = new InputSource(
this.getClass().getResourceAsStream(
String.format("%s/config.xml", dir)
)
);
checker.setModuleClassLoader(
Thread.currentThread().getContextClassLoader()
);
checker.configure(
ConfigurationLoader.loadConfiguration(
src,
new PropertiesExpander(new Properties()),
ConfigurationLoader.IgnoredModulesOptions.OMIT
)
);
final List<File> files = new ArrayList<>(0);
files.add(
new File(
this.getClass().getResource(
String.format("%s%s", dir, name)
).getFile()
)
);
checker.addListener(listener);
checker.process(files);
checker.destroy();
}
/**
* Returns full list of checks.
* @return The list
*/
@SuppressWarnings("PMD.UnusedPrivateMethod")
private static Stream<String> checks() {
return Stream.of(
"MethodsOrderCheck",
"MultilineJavadocTagsCheck",
"StringLiteralsConcatenationCheck",
"EmptyLinesCheck",
"ImportCohesionCheck",
"BracketsStructureCheck",
"CurlyBracketsStructureCheck",
"JavadocLocationCheck",
"JavadocParameterOrderCheck",
"MethodBodyCommentsCheck",
"RequireThisCheck",
"ProtectedMethodInFinalClassCheck",
"NoJavadocForOverriddenMethodsCheck",
"NonStaticMethodCheck",
"ConstantUsageCheck",
"JavadocEmptyLineCheck",
"JavadocTagsCheck",
"ProhibitNonFinalClassesCheck",
"QualifyInnerClassCheck"
).map(s -> String.format("ChecksTest/%s", s));
}
/**
* Mocked collector of checkstyle events.
*
* @since 0.1
*/
private static class Collector implements Answer<Object> {
/**
* List of events received.
*/
private final List<AuditEvent> events = new LinkedList<>();
@Override
public Object answer(final InvocationOnMock invocation) {
this.events.add((AuditEvent) invocation.getArguments()[0]);
return null;
}
/**
* How many messages do we have?
* @return Amount of messages reported
*/
public int eventCount() {
return this.events.size();
}
/**
* Do we have this message for this line?
* @param line The number of the line
* @param msg The message we're looking for
* @return This message was reported for the give line?
*/
public boolean has(final Integer line, final String msg) {
boolean has = false;
for (final AuditEvent event : this.events) {
if (event.getLine() == line && event.getMessage().equals(msg)) {
has = true;
break;
}
}
return has;
}
/**
* Returns full summary.
* @return The test summary of all events
*/
public String summary() {
final List<String> msgs = new LinkedList<>();
for (final AuditEvent event : this.events) {
msgs.add(
String.format(
"%s:%s",
event.getLine(),
event.getMessage()
)
);
}
return StringUtils.join(msgs, "; ");
}
}
}