forked from phoedos/pmd
[test] Refactor SimpleAggregatorTst to also use dynamic tests
This commit is contained in:
@ -4,16 +4,8 @@
|
||||
|
||||
package net.sourceforge.pmd.testframework;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.DynamicTest;
|
||||
import org.junit.jupiter.api.TestFactory;
|
||||
|
||||
import net.sourceforge.pmd.Rule;
|
||||
|
||||
@ -34,33 +26,4 @@ public class PmdRuleTst extends RuleTst {
|
||||
Rule rule = findRule(rulesetXml, getClass().getSimpleName().replaceFirst("Test$", ""));
|
||||
return Collections.singletonList(rule);
|
||||
}
|
||||
|
||||
@TestFactory
|
||||
Collection<DynamicTest> ruleTests() {
|
||||
final List<Rule> rules = new ArrayList<>(getRules());
|
||||
rules.sort(Comparator.comparing(Rule::getName));
|
||||
|
||||
final List<TestDescriptor> tests = new LinkedList<>();
|
||||
for (final Rule r : rules) {
|
||||
final TestDescriptor[] ruleTests = extractTestsFromXml(r);
|
||||
Collections.addAll(tests, ruleTests);
|
||||
}
|
||||
|
||||
return tests.stream().map(this::toDynamicTest).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private DynamicTest toDynamicTest(TestDescriptor testDescriptor) {
|
||||
if (isIgnored(testDescriptor)) {
|
||||
return DynamicTest.dynamicTest("[IGNORED] " + testDescriptor.getTestMethodName(),
|
||||
testDescriptor.getTestSourceUri(),
|
||||
() -> {});
|
||||
}
|
||||
return DynamicTest.dynamicTest(testDescriptor.getTestMethodName(),
|
||||
testDescriptor.getTestSourceUri(),
|
||||
() -> runTest(testDescriptor));
|
||||
}
|
||||
|
||||
private static boolean isIgnored(TestDescriptor testDescriptor) {
|
||||
return TestDescriptor.inRegressionTestMode() && !testDescriptor.isRegressionTest();
|
||||
}
|
||||
}
|
||||
|
@ -13,10 +13,14 @@ import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
@ -28,6 +32,8 @@ import javax.xml.validation.SchemaFactory;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.DynamicTest;
|
||||
import org.junit.jupiter.api.TestFactory;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
@ -587,4 +593,34 @@ public abstract class RuleTst {
|
||||
}
|
||||
return buffer.toString().trim();
|
||||
}
|
||||
|
||||
@TestFactory
|
||||
Collection<DynamicTest> ruleTests() {
|
||||
setUp();
|
||||
final List<Rule> rules = new ArrayList<>(getRules());
|
||||
rules.sort(Comparator.comparing(Rule::getName));
|
||||
|
||||
final List<TestDescriptor> tests = new LinkedList<>();
|
||||
for (final Rule r : rules) {
|
||||
final TestDescriptor[] ruleTests = extractTestsFromXml(r);
|
||||
Collections.addAll(tests, ruleTests);
|
||||
}
|
||||
|
||||
return tests.stream().map(this::toDynamicTest).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private DynamicTest toDynamicTest(TestDescriptor testDescriptor) {
|
||||
if (isIgnored(testDescriptor)) {
|
||||
return DynamicTest.dynamicTest("[IGNORED] " + testDescriptor.getTestMethodName(),
|
||||
testDescriptor.getTestSourceUri(),
|
||||
() -> {});
|
||||
}
|
||||
return DynamicTest.dynamicTest(testDescriptor.getTestMethodName(),
|
||||
testDescriptor.getTestSourceUri(),
|
||||
() -> runTest(testDescriptor));
|
||||
}
|
||||
|
||||
private static boolean isIgnored(TestDescriptor testDescriptor) {
|
||||
return TestDescriptor.inRegressionTestMode() && !testDescriptor.isRegressionTest();
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,6 @@ package net.sourceforge.pmd.testframework;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import net.sourceforge.pmd.Rule;
|
||||
|
||||
/**
|
||||
@ -19,7 +17,6 @@ import net.sourceforge.pmd.Rule;
|
||||
* rules, that should be tested via calls to
|
||||
* {@link #addRule(String, String)}.
|
||||
*/
|
||||
@RunWith(PMDTestRunner.class)
|
||||
public abstract class SimpleAggregatorTst extends RuleTst {
|
||||
|
||||
private List<Rule> rules = new ArrayList<>();
|
||||
|
@ -12,7 +12,7 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import net.sourceforge.pmd.Rule;
|
||||
|
@ -4,8 +4,8 @@
|
||||
|
||||
package net.sourceforge.pmd.testframework;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.lang.Language;
|
||||
@ -16,10 +16,10 @@ import net.sourceforge.pmd.lang.rule.AbstractRule;
|
||||
public class TestDescriptorTest {
|
||||
@Test
|
||||
public void testMethodName() {
|
||||
Assert.assertEquals("MockRule_1_Name", create("Name"));
|
||||
Assert.assertEquals("MockRule_1_Tests_xyz", create("Tests xyz"));
|
||||
Assert.assertEquals("MockRule_1_Tests_xyz__false_positive_", create("Tests xyz (false positive)"));
|
||||
Assert.assertEquals("MockRule_1_Tests_xyz__123", create("Tests xyz #123"));
|
||||
Assertions.assertEquals("MockRule_1_Name", create("Name"));
|
||||
Assertions.assertEquals("MockRule_1_Tests_xyz", create("Tests xyz"));
|
||||
Assertions.assertEquals("MockRule_1_Tests_xyz__false_positive_", create("Tests xyz (false positive)"));
|
||||
Assertions.assertEquals("MockRule_1_Tests_xyz__123", create("Tests xyz #123"));
|
||||
}
|
||||
|
||||
private String create(String description) {
|
||||
|
Reference in New Issue
Block a user