[test] Add a PMD specific PmdRuleTst base class for PMD's own tests.

Restore SimpleAggregatorTst to be general again.
This commit is contained in:
Andreas Dangel
2018-10-05 19:42:09 +02:00
parent 28fbbaa096
commit a28657d8dd
5 changed files with 86 additions and 43 deletions

View File

@@ -0,0 +1,32 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.testframework;
import java.util.Collections;
import java.util.List;
import org.junit.runner.RunWith;
import net.sourceforge.pmd.Rule;
@RunWith(PMDTestRunner.class)
public class PmdRuleTst extends RuleTst {
@Override
protected void setUp() {
// empty, nothing to do
}
@Override
protected List<Rule> getRules() {
String[] packages = getClass().getPackage().getName().split("\\.");
String categoryName = packages[packages.length - 1];
String language = packages[packages.length - 3];
String rulesetXml = "category/" + language + "/" + categoryName + ".xml";
Rule rule = findRule(rulesetXml, getClass().getSimpleName().replaceFirst("Test$", ""));
return Collections.singletonList(rule);
}
}

View File

@@ -35,9 +35,9 @@ import net.sourceforge.pmd.Rule;
*/
public class RuleTestRunner extends ParentRunner<TestDescriptor> {
private ConcurrentHashMap<TestDescriptor, Description> testDescriptions = new ConcurrentHashMap<>();
private final SimpleAggregatorTst instance;
private final RuleTst instance;
public RuleTestRunner(Class<? extends SimpleAggregatorTst> testClass) throws InitializationError {
public RuleTestRunner(Class<? extends RuleTst> testClass) throws InitializationError {
super(testClass);
instance = createTestClass();
instance.setUp();
@@ -85,9 +85,9 @@ public class RuleTestRunner extends ParentRunner<TestDescriptor> {
return tests;
}
private SimpleAggregatorTst createTestClass() throws InitializationError {
private RuleTst createTestClass() throws InitializationError {
try {
return (SimpleAggregatorTst) getTestClass().getOnlyConstructor().newInstance();
return (RuleTst) getTestClass().getOnlyConstructor().newInstance();
} catch (Exception e) {
throw new InitializationError(e);
}
@@ -106,7 +106,7 @@ public class RuleTestRunner extends ParentRunner<TestDescriptor> {
/**
* Executes the actual test case. If there are Before, After, or TestRules present,
* they are executed accordingly.
*
*
* @param testCase the PMD rule test case to be executed
* @return a single statement which includes any rules, if present.
*/

View File

@@ -12,6 +12,7 @@ import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -85,6 +86,14 @@ public abstract class RuleTst {
}
}
protected void setUp() {
// This method is intended to be overridden by subclasses.
}
protected List<Rule> getRules() {
return Collections.emptyList();
}
/**
* Find a rule in a certain ruleset by name
*/
@@ -340,6 +349,33 @@ public abstract class RuleTst {
return parseTests(rule, doc);
}
/**
* Run a set of tests defined in an XML test-data file for a rule. The file
* should be ./xml/RuleName.xml relative to the test-class. The format is
* defined in test-data.xsd.
*/
public void runTests(Rule rule) {
runTests(extractTestsFromXml(rule));
}
/**
* Run a set of tests defined in a XML test-data file. The file should be
* ./xml/[testsFileName].xml relative to the test-class. The format is
* defined in test-data.xsd.
*/
public void runTests(Rule rule, String testsFileName) {
runTests(extractTestsFromXml(rule, testsFileName));
}
/**
* Run a set of tests of a certain sourceType.
*/
public void runTests(TestDescriptor[] tests) {
for (int i = 0; i < tests.length; i++) {
runTest(tests[i]);
}
}
private TestDescriptor[] parseTests(Rule rule, Document doc) {
Element root = doc.getDocumentElement();
NodeList testCodes = root.getElementsByTagName("test-code");

View File

@@ -12,7 +12,12 @@ import org.junit.runner.RunWith;
import net.sourceforge.pmd.Rule;
/**
* Standard methods for (simple) testcases.
* Simple setup for a rule unit test,
* capable of testing multiple rules.
*
* <p>Override {@link #setUp()} to register the
* rules, that should be tested via calls to
* {@link #addRule(String, String)}.
*/
@RunWith(PMDTestRunner.class)
public abstract class SimpleAggregatorTst extends RuleTst {
@@ -21,46 +26,15 @@ public abstract class SimpleAggregatorTst extends RuleTst {
/**
* Configure the rule tests to be executed. Override this method in
* subclasses by calling addRule.
* <p>The default implementation will use the package name and test class name,
* to determine the ruleset and rule under test.
* subclasses by calling addRule, e.g.
*
* <pre>addRule("path/myruleset.xml", "CustomRule");</pre>
*
* @see #addRule(String, String)
*/
@Override
protected void setUp() {
String[] packages = getClass().getPackage().getName().split("\\.");
String categoryName = packages[packages.length - 1];
String language = packages[packages.length - 3];
String rulesetXml = "category/" + language + "/" + categoryName + ".xml";
addRule(rulesetXml, getClass().getSimpleName().replaceFirst("Test$", ""));
}
/**
* Run a set of tests defined in an XML test-data file for a rule. The file
* should be ./xml/RuleName.xml relative to the test-class. The format is
* defined in test-data.xsd.
*/
public void runTests(Rule rule) {
runTests(extractTestsFromXml(rule));
}
/**
* Run a set of tests defined in a XML test-data file. The file should be
* ./xml/[testsFileName].xml relative to the test-class. The format is
* defined in test-data.xsd.
*/
public void runTests(Rule rule, String testsFileName) {
runTests(extractTestsFromXml(rule, testsFileName));
}
/**
* Run a set of tests of a certain sourceType.
*/
public void runTests(TestDescriptor[] tests) {
for (int i = 0; i < tests.length; i++) {
runTest(tests[i]);
}
// empty, to be overridden.
}
/**
@@ -76,6 +50,7 @@ public abstract class SimpleAggregatorTst extends RuleTst {
*
* @return all configured rules.
*/
@Override
protected List<Rule> getRules() {
return rules;
}