diff --git a/pmd-test/pom.xml b/pmd-test/pom.xml index 67644b89f6..38d864e93f 100644 --- a/pmd-test/pom.xml +++ b/pmd-test/pom.xml @@ -72,5 +72,16 @@ mockito-core test + + + + net.sourceforge.pmd + pmd-core + ${project.version} + test + tests + test-jar + + diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/DummyLanguageModule.java b/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/DummyLanguageModule.java deleted file mode 100644 index 2a7026e9c5..0000000000 --- a/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/DummyLanguageModule.java +++ /dev/null @@ -1,80 +0,0 @@ -/** - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.test.lang; - -import net.sourceforge.pmd.annotation.InternalApi; -import net.sourceforge.pmd.lang.AbstractPmdLanguageVersionHandler; -import net.sourceforge.pmd.lang.LanguageRegistry; -import net.sourceforge.pmd.lang.ast.AstInfo; -import net.sourceforge.pmd.lang.ast.Parser; -import net.sourceforge.pmd.lang.ast.Parser.ParserTask; -import net.sourceforge.pmd.lang.ast.RootNode; -import net.sourceforge.pmd.lang.document.TextRegion; -import net.sourceforge.pmd.lang.impl.SimpleLanguageModuleBase; -import net.sourceforge.pmd.test.lang.ast.DummyNode; - -/** - * Dummy language used for testing PMD. - * - * @deprecated Don't use this directly. We can probably remove this in favour of plaintextlanguage - * when https://github.com/pmd/pmd/issues/3918 is merged - */ -@Deprecated -@InternalApi -public class DummyLanguageModule extends SimpleLanguageModuleBase { - - public static final String NAME = "Dummy"; - public static final String TERSE_NAME = "dummy"; - - public DummyLanguageModule() { - super(LanguageMetadata.withId(TERSE_NAME).name(NAME).extensions("dummy") - .addVersion("1.0") - .addVersion("1.1") - .addVersion("1.2") - .addVersion("1.3") - .addVersion("1.4") - .addVersion("1.5", "5") - .addVersion("1.6", "6") - .addDefaultVersion("1.7", "7") - .addVersion("1.8", "8"), new Handler()); - } - - public static DummyLanguageModule getInstance() { - return (DummyLanguageModule) LanguageRegistry.PMD.getLanguageByFullName(NAME); - } - - public static class Handler extends AbstractPmdLanguageVersionHandler { - - @Override - public Parser getParser() { - return DummyRootNode::new; - } - } - - public static class DummyRootNode extends DummyNode implements RootNode { - - - private final AstInfo astInfo; - - public DummyRootNode(ParserTask task) { - this.astInfo = new AstInfo<>(task, this); - withCoords(task.getTextDocument().getEntireRegion()); - setImage("Foo"); - } - - @Override - public DummyRootNode withCoords(TextRegion region) { - super.withCoords(region); - return this; - } - - - @Override - public AstInfo getAstInfo() { - return astInfo; - } - } - -} diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/ast/DummyNode.java b/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/ast/DummyNode.java deleted file mode 100644 index a7e124e100..0000000000 --- a/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/ast/DummyNode.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.test.lang.ast; - -import java.util.Objects; - -import net.sourceforge.pmd.lang.ast.impl.AbstractNode; -import net.sourceforge.pmd.lang.document.TextRegion; - -public class DummyNode extends AbstractNode { - - private String image; - private TextRegion region = TextRegion.caretAt(0); - - public DummyNode withCoords(TextRegion region) { - this.region = Objects.requireNonNull(region); - return this; - } - - public DummyNode newChild() { - DummyNode child = new DummyNode(); - addChild(child, getNumChildren()); - return child; - } - - @Override - public TextRegion getTextRegion() { - return region; - } - - @Deprecated - @Override - public String toString() { - return "dummyNode"; - } - - @Override - public String getXPathNodeName() { - return "dummyNode"; - } - - @Override - public String getImage() { - return image; - } - - public void setImage(String image) { - this.image = image; - } -} diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java index 57e4791f68..5c3041c7dc 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java @@ -55,11 +55,9 @@ public abstract class RuleTst { } /** - * Find a rule in a certain ruleset by name - * - * todo make this static + * Find a rule in a certain ruleset by name. */ - public Rule findRule(String ruleSet, String ruleName) { + public static Rule findRule(String ruleSet, String ruleName) { try { RuleSet parsedRset = new RuleSetLoader().warnDeprecated(false).loadFromResource(ruleSet); Rule rule = parsedRset.getRuleByName(ruleName); @@ -139,8 +137,8 @@ public abstract class RuleTst { * * @return The rule once it has been reinitialised */ - protected Rule reinitializeRule(Rule rule) { - return findRule(rule.getRuleSetName(), rule.getName()); + private Rule reinitializeRule(Rule rule) { + return rule.deepCopy(); } diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/SimpleAggregatorTst.java b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/SimpleAggregatorTst.java index 0172b975a2..fc547bf983 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/SimpleAggregatorTst.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/SimpleAggregatorTst.java @@ -19,7 +19,7 @@ import net.sourceforge.pmd.Rule; */ public abstract class SimpleAggregatorTst extends RuleTst { - private List rules = new ArrayList<>(); + private final List rules = new ArrayList<>(); /** * Configure the rule tests to be executed. Override this method in diff --git a/pmd-test/src/main/resources/META-INF/services/net.sourceforge.pmd.lang.Language b/pmd-test/src/main/resources/META-INF/services/net.sourceforge.pmd.lang.Language deleted file mode 100644 index 825247a1cc..0000000000 --- a/pmd-test/src/main/resources/META-INF/services/net.sourceforge.pmd.lang.Language +++ /dev/null @@ -1 +0,0 @@ -net.sourceforge.pmd.test.lang.DummyLanguageModule diff --git a/pmd-test/src/test/java/net/sourceforge/pmd/testframework/RuleTstTest.java b/pmd-test/src/test/java/net/sourceforge/pmd/testframework/RuleTstTest.java index ec4f67197b..63018beaa1 100644 --- a/pmd-test/src/test/java/net/sourceforge/pmd/testframework/RuleTstTest.java +++ b/pmd-test/src/test/java/net/sourceforge/pmd/testframework/RuleTstTest.java @@ -4,9 +4,11 @@ package net.sourceforge.pmd.testframework; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -18,26 +20,20 @@ import org.mockito.Mockito; import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.RuleContext; +import net.sourceforge.pmd.lang.DummyLanguageModule; import net.sourceforge.pmd.lang.LanguageProcessor; import net.sourceforge.pmd.lang.LanguageVersion; +import net.sourceforge.pmd.lang.ast.DummyNode.DummyRootNode; import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.document.TextRegion; import net.sourceforge.pmd.lang.rule.RuleTargetSelector; -import net.sourceforge.pmd.test.lang.DummyLanguageModule; -import net.sourceforge.pmd.test.lang.DummyLanguageModule.DummyRootNode; import net.sourceforge.pmd.test.schema.RuleTestDescriptor; class RuleTstTest { - private LanguageVersion dummyLanguage = DummyLanguageModule.getInstance().getDefaultVersion(); + private final LanguageVersion dummyLanguage = DummyLanguageModule.getInstance().getDefaultVersion(); - private Rule rule = mock(Rule.class); + private final Rule rule = mock(Rule.class); - private RuleTst ruleTester = new RuleTst() { - @Override - public Rule findRule(String ruleSet, String ruleName) { - return rule; - } - }; + private final RuleTst ruleTester = spy(RuleTst.class); @Test void shouldCallStartAndEnd() { @@ -68,16 +64,17 @@ class RuleTstTest { when(rule.getTargetSelector()).thenReturn(RuleTargetSelector.forRootOnly()); when(rule.deepCopy()).thenReturn(rule); - final String code = "the\ncode"; + final String code = "(a)(b)\n(c)"; Mockito.doAnswer(invocation -> { RuleContext context = invocation.getArgument(1, RuleContext.class); DummyRootNode node = invocation.getArgument(0, DummyRootNode.class); + assertEquals(3, node.getNumChildren()); // the violations are reported out of order // line 2 - context.addViolation(node.newChild().withCoords(TextRegion.fromOffsetLength("the\n".length(), "code".length()))); + context.addViolation(node.getChild(2)); // line 1 - context.addViolation(node.newChild().withCoords(TextRegion.fromOffsetLength(0, "the".length()))); + context.addViolation(node.getChild(1)); return null; }).when(rule).apply(any(Node.class), Mockito.any(RuleContext.class));