From 8790c2a0be90880d6ecf25e68619e398962481b7 Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Tue, 30 Apr 2019 13:53:44 +0700 Subject: [PATCH] #939:Upgrade ChecksTest from qulice-checkstyle to Junit5. --- pom.xml | 6 + qulice-checkstyle/pom.xml | 4 + .../com/qulice/checkstyle/ChecksTest.java | 121 ++++++++---------- 3 files changed, 63 insertions(+), 68 deletions(-) diff --git a/pom.xml b/pom.xml index c9722dc88..8eea17ed6 100644 --- a/pom.xml +++ b/pom.xml @@ -146,6 +146,12 @@ 5.3.1 test + + org.junit.jupiter + junit-jupiter-params + 5.3.1 + test + diff --git a/qulice-checkstyle/pom.xml b/qulice-checkstyle/pom.xml index 547d3c165..0572adab2 100644 --- a/qulice-checkstyle/pom.xml +++ b/qulice-checkstyle/pom.xml @@ -102,6 +102,10 @@ org.junit.jupiter junit-jupiter-engine + + org.junit.jupiter + junit-jupiter-params + diff --git a/qulice-checkstyle/src/test/java/com/qulice/checkstyle/ChecksTest.java b/qulice-checkstyle/src/test/java/com/qulice/checkstyle/ChecksTest.java index 67291fa66..31750b446 100644 --- a/qulice-checkstyle/src/test/java/com/qulice/checkstyle/ChecksTest.java +++ b/qulice-checkstyle/src/test/java/com/qulice/checkstyle/ChecksTest.java @@ -37,17 +37,16 @@ import java.io.File; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.Collection; 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.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +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; @@ -57,75 +56,25 @@ * Integration test case for all checkstyle checks. * @since 0.3 */ -@RunWith(Parameterized.class) public final class ChecksTest { - /** - * Directories where test scripts are located. - */ - private static final String[] CHECKS = { - "MethodsOrderCheck", - "MultilineJavadocTagsCheck", - "StringLiteralsConcatenationCheck", - "EmptyLinesCheck", - "ImportCohesionCheck", - "BracketsStructureCheck", - "CurlyBracketsStructureCheck", - "JavadocLocationCheck", - "MethodBodyCommentsCheck", - "RequireThisCheck", - "ProtectedMethodInFinalClassCheck", - "NoJavadocForOverriddenMethodsCheck", - "NonStaticMethodCheck", - "ConstantUsageCheck", - "JavadocEmptyLineCheck", - "JavadocParameterOrderCheck", - "JavadocTagsCheck", - "ProhibitNonFinalClassesCheck", - }; - - /** - * Current directory we're working with. - */ - private final String dir; - - /** - * Public ctor. - * @param name The name of the check to work with - */ - public ChecksTest(final String name) { - this.dir = String.format("ChecksTest/%s", name); - } - - /** - * Returns full list of checks. - * @return The list - */ - @Parameterized.Parameters - @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") - public static Collection dirs() { - final Collection dirs = new LinkedList<>(); - for (final String url : ChecksTest.CHECKS) { - dirs.add(new Object[] {url}); - } - return dirs; - } - /** * Test checkstyle for true positive. + * @param dir Directory where test scripts are located. * @throws Exception If something goes wrong */ - @Test - public void testCheckstyleTruePositive() throws Exception { + @ParameterizedTest + @MethodSource("checks") + public 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("/Invalid.java", listener); + this.check(dir, "/Invalid.java", listener); final String[] violations = StringUtils.split( IOUtils.toString( this.getClass().getResourceAsStream( - String.format("%s/violations.txt", this.dir) + String.format("%s/violations.txt", dir) ), StandardCharsets.UTF_8 ), @@ -142,7 +91,7 @@ public void testCheckstyleTruePositive() throws Exception { "Line no.%d ('%s') not reported by %s: '%s'", pos, needle, - this.dir, + dir, collector.summary() ), Matchers.is(true) @@ -153,15 +102,17 @@ public void testCheckstyleTruePositive() throws Exception { /** * Test checkstyle for true negative. + * @param dir Directory where test scripts are located. * @throws Exception If something goes wrong */ - @Test - public void testCheckstyleTrueNegative() throws Exception { + @ParameterizedTest + @MethodSource("checks") + public 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("/Valid.java", listener); + this.check(dir, "/Valid.java", listener); MatcherAssert.assertThat(collector.summary(), Matchers.equalTo("")); Mockito.verify(listener, Mockito.times(0)) .addError(Mockito.any(AuditEvent.class)); @@ -169,16 +120,18 @@ public void testCheckstyleTrueNegative() throws Exception { /** * 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 name, final AuditListener listener) - throws Exception { + 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", this.dir) + String.format("%s/config.xml", dir) ) ); checker.setClassLoader(Thread.currentThread().getContextClassLoader()); @@ -196,7 +149,7 @@ private void check(final String name, final AuditListener listener) files.add( new File( this.getClass().getResource( - String.format("%s%s", this.dir, name) + String.format("%s%s", dir, name) ).getFile() ) ); @@ -205,19 +158,50 @@ private void check(final String name, final AuditListener listener) checker.destroy(); } + /** + * Returns full list of checks. + * @return The list + */ + @SuppressWarnings("PMD.UnusedPrivateMethod") + private static Stream checks() { + return Stream.of( + "MethodsOrderCheck", + "MultilineJavadocTagsCheck", + "StringLiteralsConcatenationCheck", + "EmptyLinesCheck", + "ImportCohesionCheck", + "BracketsStructureCheck", + "CurlyBracketsStructureCheck", + "JavadocLocationCheck", + "MethodBodyCommentsCheck", + "RequireThisCheck", + "ProtectedMethodInFinalClassCheck", + "NoJavadocForOverriddenMethodsCheck", + "NonStaticMethodCheck", + "ConstantUsageCheck", + "JavadocEmptyLineCheck", + "JavadocParameterOrderCheck", + "JavadocTagsCheck", + "ProhibitNonFinalClassesCheck" + ).map(s -> String.format("ChecksTest/%s", s)); + } + /** * Mocked collector of checkstyle events. */ private static class Collector implements Answer { + /** * List of events received. */ private final List events = new LinkedList<>(); + @Override public Object answer(final InvocationOnMock invocation) { this.events.add((AuditEvent) invocation.getArguments()[0]); return null; } + /** * Do we have this message for this line? * @param line The number of the line @@ -234,6 +218,7 @@ public boolean has(final Integer line, final String msg) { } return has; } + /** * Returns full summary. * @return The test summary of all events