Skip to content

Commit 3729f7a

Browse files
committedMay 2, 2019
Merge branch '__rultor'
2 parents 259b642 + 8790c2a commit 3729f7a

File tree

2 files changed

+57
-68
lines changed

2 files changed

+57
-68
lines changed
 

‎qulice-checkstyle/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@
101101
<groupId>org.junit.jupiter</groupId>
102102
<artifactId>junit-jupiter-engine</artifactId>
103103
</dependency>
104+
<dependency>
105+
<groupId>org.junit.jupiter</groupId>
106+
<artifactId>junit-jupiter-params</artifactId>
107+
</dependency>
104108
</dependencies>
105109
<build>
106110
<plugins>

‎qulice-checkstyle/src/test/java/com/qulice/checkstyle/ChecksTest.java

+53-68
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,16 @@
3737
import java.io.File;
3838
import java.nio.charset.StandardCharsets;
3939
import java.util.ArrayList;
40-
import java.util.Collection;
4140
import java.util.LinkedList;
4241
import java.util.List;
4342
import java.util.Properties;
43+
import java.util.stream.Stream;
4444
import org.apache.commons.io.IOUtils;
4545
import org.apache.commons.lang3.StringUtils;
4646
import org.hamcrest.MatcherAssert;
4747
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;
5150
import org.mockito.Mockito;
5251
import org.mockito.invocation.InvocationOnMock;
5352
import org.mockito.stubbing.Answer;
@@ -57,75 +56,25 @@
5756
* Integration test case for all checkstyle checks.
5857
* @since 0.3
5958
*/
60-
@RunWith(Parameterized.class)
6159
public final class ChecksTest {
6260

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-
11461
/**
11562
* Test checkstyle for true positive.
63+
* @param dir Directory where test scripts are located.
11664
* @throws Exception If something goes wrong
11765
*/
118-
@Test
119-
public void testCheckstyleTruePositive() throws Exception {
66+
@ParameterizedTest
67+
@MethodSource("checks")
68+
public void testCheckstyleTruePositive(final String dir) throws Exception {
12069
final AuditListener listener = Mockito.mock(AuditListener.class);
12170
final Collector collector = new ChecksTest.Collector();
12271
Mockito.doAnswer(collector).when(listener)
12372
.addError(Mockito.any(AuditEvent.class));
124-
this.check("/Invalid.java", listener);
73+
this.check(dir, "/Invalid.java", listener);
12574
final String[] violations = StringUtils.split(
12675
IOUtils.toString(
12776
this.getClass().getResourceAsStream(
128-
String.format("%s/violations.txt", this.dir)
77+
String.format("%s/violations.txt", dir)
12978
),
13079
StandardCharsets.UTF_8
13180
),
@@ -142,7 +91,7 @@ public void testCheckstyleTruePositive() throws Exception {
14291
"Line no.%d ('%s') not reported by %s: '%s'",
14392
pos,
14493
needle,
145-
this.dir,
94+
dir,
14695
collector.summary()
14796
),
14897
Matchers.is(true)
@@ -153,32 +102,36 @@ public void testCheckstyleTruePositive() throws Exception {
153102

154103
/**
155104
* Test checkstyle for true negative.
105+
* @param dir Directory where test scripts are located.
156106
* @throws Exception If something goes wrong
157107
*/
158-
@Test
159-
public void testCheckstyleTrueNegative() throws Exception {
108+
@ParameterizedTest
109+
@MethodSource("checks")
110+
public void testCheckstyleTrueNegative(final String dir) throws Exception {
160111
final AuditListener listener = Mockito.mock(AuditListener.class);
161112
final Collector collector = new ChecksTest.Collector();
162113
Mockito.doAnswer(collector).when(listener)
163114
.addError(Mockito.any(AuditEvent.class));
164-
this.check("/Valid.java", listener);
115+
this.check(dir, "/Valid.java", listener);
165116
MatcherAssert.assertThat(collector.summary(), Matchers.equalTo(""));
166117
Mockito.verify(listener, Mockito.times(0))
167118
.addError(Mockito.any(AuditEvent.class));
168119
}
169120

170121
/**
171122
* Check one file.
123+
* @param dir Directory where test scripts are located.
172124
* @param name The name of the check
173125
* @param listener The listener
174126
* @throws Exception If something goes wrong inside
175127
*/
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 {
178131
final Checker checker = new Checker();
179132
final InputSource src = new InputSource(
180133
this.getClass().getResourceAsStream(
181-
String.format("%s/config.xml", this.dir)
134+
String.format("%s/config.xml", dir)
182135
)
183136
);
184137
checker.setClassLoader(Thread.currentThread().getContextClassLoader());
@@ -196,7 +149,7 @@ private void check(final String name, final AuditListener listener)
196149
files.add(
197150
new File(
198151
this.getClass().getResource(
199-
String.format("%s%s", this.dir, name)
152+
String.format("%s%s", dir, name)
200153
).getFile()
201154
)
202155
);
@@ -205,19 +158,50 @@ private void check(final String name, final AuditListener listener)
205158
checker.destroy();
206159
}
207160

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+
208189
/**
209190
* Mocked collector of checkstyle events.
210191
*/
211192
private static class Collector implements Answer<Object> {
193+
212194
/**
213195
* List of events received.
214196
*/
215197
private final List<AuditEvent> events = new LinkedList<>();
198+
216199
@Override
217200
public Object answer(final InvocationOnMock invocation) {
218201
this.events.add((AuditEvent) invocation.getArguments()[0]);
219202
return null;
220203
}
204+
221205
/**
222206
* Do we have this message for this line?
223207
* @param line The number of the line
@@ -234,6 +218,7 @@ public boolean has(final Integer line, final String msg) {
234218
}
235219
return has;
236220
}
221+
237222
/**
238223
* Returns full summary.
239224
* @return The test summary of all events

0 commit comments

Comments
 (0)
Please sign in to comment.