From f08beadc255ab9f5d38ac81a0d90e005777813c4 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Mon, 30 Jan 2023 13:54:36 +0100 Subject: [PATCH] [test] Remove deprecated/old (base) classes --- .../pmd/testframework/PMDTestRunner.java | 143 ------------- .../pmd/testframework/RuleTestRunner.java | 152 -------------- .../pmd/testframework/TestDescriptor.java | 197 ------------------ .../pmd/testframework/TestDescriptorTest.java | 48 ----- 4 files changed, 540 deletions(-) delete mode 100644 pmd-test/src/main/java/net/sourceforge/pmd/testframework/PMDTestRunner.java delete mode 100644 pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTestRunner.java delete mode 100644 pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java delete mode 100644 pmd-test/src/test/java/net/sourceforge/pmd/testframework/TestDescriptorTest.java diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/PMDTestRunner.java b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/PMDTestRunner.java deleted file mode 100644 index 88d69bb935..0000000000 --- a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/PMDTestRunner.java +++ /dev/null @@ -1,143 +0,0 @@ -/** - * 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.Description; -import org.junit.runner.Runner; -import org.junit.runner.manipulation.Filter; -import org.junit.runner.manipulation.Filterable; -import org.junit.runner.manipulation.NoTestsRemainException; -import org.junit.runner.manipulation.Sortable; -import org.junit.runner.manipulation.Sorter; -import org.junit.runner.notification.RunNotifier; -import org.junit.runners.JUnit4; -import org.junit.runners.ParentRunner; -import org.junit.runners.model.InitializationError; - -/** - * A JUnit Runner, that combines the default {@link JUnit4} - * and our custom {@link RuleTestRunner}. - * It allows to selectively execute single test cases (it is {@link Filterable}). - * - *

Note: Since we actually run two runners one after another, the static {@code BeforeClass} - * and {@Code AfterClass} methods will be executed twice and the test class will be instantiated twice, too.

- * - *

In order to use it, you'll need to subclass {@link SimpleAggregatorTst} and - * annotate your test class with RunWith:

- * - *
- * @RunWith(PMDTestRunner.class)
- * public class MyRuleSetTest extends SimpleAggregatorTst {
- * ...
- * }
- * 
- * - * @deprecated This is not needed anymore with JUnit5. Just extend from {@link SimpleAggregatorTst}. - */ -@Deprecated -public class PMDTestRunner extends Runner implements Filterable, Sortable { - private final Class klass; - private final RuleTestRunner ruleTests; - private final ParentRunner unitTests; - - private final Description description; - - public PMDTestRunner(final Class klass) throws InitializationError { - this.klass = klass; - ruleTests = new RuleTestRunner(klass); - - if (ruleTests.hasUnitTests()) { - unitTests = new JUnit4(klass); - } else { - unitTests = new EmptyRunner(klass); - } - - this.description = createDescription(); - } - - @Override - public void filter(Filter filter) throws NoTestsRemainException { - boolean noRuleTests = false; - try { - ruleTests.filter(filter); - } catch (NoTestsRemainException e) { - noRuleTests = true; - } - - boolean noUnitTests = false; - try { - unitTests.filter(filter); - } catch (NoTestsRemainException e) { - noUnitTests = true; - } - - if (noRuleTests && noUnitTests) { - throw new NoTestsRemainException(); - } - } - - @Override - public Description getDescription() { - return description; - } - - private Description createDescription() { - Description description = Description.createSuiteDescription(klass); - description.addChild(createChildrenDescriptions(ruleTests, "Rule Tests")); - if (ruleTests.hasUnitTests()) { - description.addChild(createChildrenDescriptions(unitTests, "Unit Tests")); - } - return description; - } - - private Description createChildrenDescriptions(Runner runner, String suiteName) { - Description suite = Description.createSuiteDescription(suiteName); - for (Description child : runner.getDescription().getChildren()) { - suite.addChild(child); - } - return suite; - } - - @Override - public void run(RunNotifier notifier) { - ruleTests.run(notifier); - unitTests.run(notifier); - } - - @Override - public void sort(Sorter sorter) { - ruleTests.sort(sorter); - unitTests.sort(sorter); - } - - private static class EmptyRunner extends ParentRunner { - protected EmptyRunner(Class testClass) throws InitializationError { - super(testClass); - } - - @Override - public Description getDescription() { - return Description.EMPTY; - } - - @Override - protected List getChildren() { - return Collections.emptyList(); - } - - @Override - protected Description describeChild(Object child) { - return Description.EMPTY; - } - - @Override - protected void runChild(Object child, RunNotifier notifier) { - // there are no tests - nothing to execute - } - } -} diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTestRunner.java b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTestRunner.java deleted file mode 100644 index 7f959ccb54..0000000000 --- a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTestRunner.java +++ /dev/null @@ -1,152 +0,0 @@ -/** - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.testframework; - -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.internal.runners.statements.RunAfters; -import org.junit.internal.runners.statements.RunBefores; -import org.junit.rules.RunRules; -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runner.notification.RunNotifier; -import org.junit.runners.ParentRunner; -import org.junit.runners.model.FrameworkMethod; -import org.junit.runners.model.InitializationError; -import org.junit.runners.model.Statement; - -import net.sourceforge.pmd.Rule; -import net.sourceforge.pmd.test.schema.RuleTestCollection; -import net.sourceforge.pmd.test.schema.RuleTestDescriptor; - -/** - * A JUnit Runner, that executes all declared rule tests in the class. It supports Before and After methods as well as - * TestRules. - * - * @author Andreas Dangel - * @deprecated This is not needed anymore with JUnit5 - */ -@Deprecated -public class RuleTestRunner extends ParentRunner { - private ConcurrentMap testDescriptions = new ConcurrentHashMap<>(); - private final RuleTst instance; - - /* default */ RuleTestRunner(final Class testClass) throws InitializationError { - super(testClass); - instance = createTestClass(); - instance.setUp(); - } - - @Override - protected Description describeChild(final TestDescriptor testCase) { - Description description = testDescriptions.get(testCase); - if (description == null) { - description = Description.createTestDescription(getTestClass().getJavaClass().getName(), testCase.getTestMethodName()); - testDescriptions.putIfAbsent(testCase, description); - } - return description; - } - - /** - * Checks whether this test class has additionally unit test methods. - * - * @return true if there is at least one unit test method. - */ - /* default */ boolean hasUnitTests() { - return !getTestClass().getAnnotatedMethods(Test.class).isEmpty(); - } - - @Override - protected List getChildren() { - List rules = new ArrayList<>(instance.getRules()); - rules.sort(Comparator.comparing(Rule::getName)); - - List tests = new ArrayList<>(); - for (Rule r : rules) { - RuleTestCollection ruleTests = instance.parseTestCollection(r); - RuleTestDescriptor focused = ruleTests.getFocusedTestOrNull(); - for (RuleTestDescriptor t : ruleTests.getTests()) { - TestDescriptor td = new TestDescriptor(t, ruleTests.getAbsoluteUriToTestXmlFile()); - if (focused != null && !focused.equals(t)) { - td.setRegressionTest(false); // disable it - } - tests.add(td); - } - } - - return tests; - } - - private RuleTst createTestClass() throws InitializationError { - try { - return (RuleTst) getTestClass().getOnlyConstructor().newInstance(); - } catch (final Exception e) { - throw new InitializationError(e); - } - } - - @Override - protected void runChild(final TestDescriptor testCase, final RunNotifier notifier) { - final Description description = describeChild(testCase); - if (isIgnored(testCase)) { - notifier.fireTestIgnored(description); - } else { - runLeaf(ruleTestBlock(testCase), description, notifier); - } - } - - /** - * 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. - */ - private Statement ruleTestBlock(final TestDescriptor testCase) { - RuleTestDescriptor newTestCase = new RuleTestDescriptor(testCase.getNumberInDocument(), testCase.getRule()); - newTestCase.setLanguageVersion(testCase.getLanguageVersion()); - newTestCase.setCode(testCase.getCode()); - newTestCase.setDescription(testCase.getDescription()); - newTestCase.setDisabled(!testCase.isRegressionTest()); - newTestCase.recordExpectedViolations(testCase.getNumberOfProblemsExpected(), testCase.getExpectedLineNumbers(), testCase.getExpectedMessages()); - Statement statement = new Statement() { - @Override - public void evaluate() { - instance.runTest(newTestCase); - } - }; - statement = withBefores(statement); - statement = withAfters(statement); - statement = withRules(testCase, statement); - return statement; - } - - private Statement withBefores(final Statement statement) { - final List befores = getTestClass().getAnnotatedMethods(Before.class); - return befores.isEmpty() ? statement : new RunBefores(statement, befores, instance); - } - - private Statement withAfters(final Statement statement) { - final List afters = getTestClass().getAnnotatedMethods(After.class); - return afters.isEmpty() ? statement : new RunAfters(statement, afters, instance); - } - - private Statement withRules(final TestDescriptor testCase, Statement statement) { - final List testRules = - getTestClass().getAnnotatedFieldValues(instance, org.junit.Rule.class, TestRule.class); - return testRules.isEmpty() ? statement : new RunRules(statement, testRules, describeChild(testCase)); - } - - @Override - protected boolean isIgnored(final TestDescriptor child) { - return TestDescriptor.inRegressionTestMode() && !child.isRegressionTest(); - } -} diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java deleted file mode 100644 index 817b8f6a43..0000000000 --- a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java +++ /dev/null @@ -1,197 +0,0 @@ -/** - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.testframework; - -import java.net.URI; -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; - -import org.junit.jupiter.api.Disabled; - -import net.sourceforge.pmd.Rule; -import net.sourceforge.pmd.lang.LanguageVersion; -import net.sourceforge.pmd.test.schema.RuleTestDescriptor; - -/** - * Stores the information required to run a complete test. - * - * @deprecated Use {@link RuleTestDescriptor} instead - */ -@Disabled("this is not a unit test") -@Deprecated -public class TestDescriptor { - private Rule rule; - private Properties properties; - private String description; - private int numberOfProblemsExpected; - private List expectedMessages = new ArrayList<>(); - private List expectedLineNumbers = new ArrayList<>(); - private String code; - private LanguageVersion languageVersion; - // default, avoids unintentional mixing of state between test cases - private boolean reinitializeRule = true; - private boolean isRegressionTest = true; - private boolean useAuxClasspath = true; - private int numberInDocument = -1; - - private URI testSourceUri; - - public TestDescriptor() { - // Empty default descriptor added to please mvn surefire plugin - } - - public TestDescriptor(String code, String description, int numberOfProblemsExpected, Rule rule) { - this(code, description, numberOfProblemsExpected, rule, rule.getLanguage().getDefaultVersion()); - } - - public TestDescriptor(String code, String description, int numberOfProblemsExpected, Rule rule, - LanguageVersion languageVersion) { - this.rule = rule; - this.code = code; - this.description = description; - this.numberOfProblemsExpected = numberOfProblemsExpected; - this.languageVersion = languageVersion; - } - - // for compatibility - TestDescriptor(RuleTestDescriptor td, String absoluteUriToTestXmlFile) { - this.rule = td.getRule(); - this.code = td.getCode(); - this.description = td.getDescription(); - this.numberOfProblemsExpected = td.getExpectedProblems(); - this.expectedLineNumbers = td.getExpectedLineNumbers(); - this.expectedMessages = td.getExpectedMessages(); - this.isRegressionTest = !td.isDisabled(); - this.numberInDocument = td.getIndex(); - this.properties = td.getProperties(); - this.languageVersion = td.getLanguageVersion(); - this.numberInDocument = td.getIndex(); - this.setTestSourceUri(absoluteUriToTestXmlFile, td.getLineNumber()); - } - - public int getNumberInDocument() { - return numberInDocument; - } - - public void setNumberInDocument(int numberInDocument) { - this.numberInDocument = numberInDocument; - } - - public void setExpectedMessages(List messages) { - expectedMessages.clear(); - expectedMessages.addAll(messages); - } - - public List getExpectedMessages() { - return expectedMessages; - } - - public void setExpectedLineNumbers(List expectedLineNumbers) { - this.expectedLineNumbers.clear(); - this.expectedLineNumbers.addAll(expectedLineNumbers); - } - - public List getExpectedLineNumbers() { - return expectedLineNumbers; - } - - public void setProperties(Properties properties) { - this.properties = properties; - } - - public Properties getProperties() { - return properties; - } - - public String getCode() { - return code; - } - - public LanguageVersion getLanguageVersion() { - return languageVersion; - } - - public String getDescription() { - return description; - } - - public int getNumberOfProblemsExpected() { - return numberOfProblemsExpected; - } - - public Rule getRule() { - return rule; - } - - public boolean getReinitializeRule() { - return reinitializeRule; - } - - public void setReinitializeRule(boolean reinitializeRule) { - this.reinitializeRule = reinitializeRule; - } - - /** - * Checks whether we are testing for regression problems only. Return value - * is based on the system property "pmd.regress". - * - * @return false if system property "pmd.regress" is set to - * false, true otherwise - */ - public static boolean inRegressionTestMode() { - boolean inRegressionMode = true; // default - try { - // get the "pmd.regress" System property - String property = System.getProperty("pmd.regress"); - if (property != null) { - inRegressionMode = Boolean.parseBoolean(property); - } - } catch (IllegalArgumentException e) { - throw new RuntimeException("Invalid system property 'pmd.regress'", e); - } - - return inRegressionMode; - } - - public boolean isRegressionTest() { - return isRegressionTest; - } - - public void setRegressionTest(boolean isRegressionTest) { - this.isRegressionTest = isRegressionTest; - } - - public void setUseAuxClasspath(boolean useAuxClasspath) { - this.useAuxClasspath = useAuxClasspath; - } - - public boolean isUseAuxClasspath() { - return useAuxClasspath; - } - - @Override - public String toString() { - return description + "\n\n" + code; - } - - public String getTestMethodName() { - return getRule().getName() + "_" - + getNumberInDocument() - + "_" - + getDescription() - .replaceAll("\n|\r", "_") - .replaceAll("[^\\w\\d_$]", "_") - .replaceAll("\\s+", "_"); - } - - public void setTestSourceUri(String absoluteUri, int line) { - this.testSourceUri = URI.create(absoluteUri + "?line=" + line); - } - - public URI getTestSourceUri() { - return testSourceUri; - } -} diff --git a/pmd-test/src/test/java/net/sourceforge/pmd/testframework/TestDescriptorTest.java b/pmd-test/src/test/java/net/sourceforge/pmd/testframework/TestDescriptorTest.java deleted file mode 100644 index 6a6eef1faa..0000000000 --- a/pmd-test/src/test/java/net/sourceforge/pmd/testframework/TestDescriptorTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.testframework; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -import net.sourceforge.pmd.RuleContext; -import net.sourceforge.pmd.lang.Language; -import net.sourceforge.pmd.lang.PlainTextLanguage; -import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.rule.AbstractRule; - -class TestDescriptorTest { - @Test - void testMethodName() { - assertEquals("MockRule_1_Name", create("Name")); - assertEquals("MockRule_1_Tests_xyz", create("Tests xyz")); - assertEquals("MockRule_1_Tests_xyz__false_positive_", create("Tests xyz (false positive)")); - assertEquals("MockRule_1_Tests_xyz__123", create("Tests xyz #123")); - } - - private String create(String description) { - TestDescriptor descriptor = new TestDescriptor("foo", description, 0, - new MockRule()); - descriptor.setNumberInDocument(1); - return descriptor.getTestMethodName(); - } - - private static final class MockRule extends AbstractRule { - @Override - public Language getLanguage() { - return PlainTextLanguage.getInstance(); - } - - @Override - public String getName() { - return "MockRule"; - } - - @Override - public void apply(Node target, RuleContext ctx) { - } - } -}