From 7922fb756499b6470c9099153dca67658847bb6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Tue, 25 Aug 2020 15:36:20 +0200 Subject: [PATCH] Move MockRule into test sources --- .../net/sourceforge/pmd/RuleSetFactory.java | 8 --- .../java/net/sourceforge/pmd/RuleSetTest.java | 54 ++++++++++++------- .../sourceforge/pmd/lang/rule/MockRule.java | 9 +--- 3 files changed, 37 insertions(+), 34 deletions(-) rename pmd-core/src/{main => test}/java/net/sourceforge/pmd/lang/rule/MockRule.java (83%) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java index 9331c6f792..5135a30a32 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java @@ -36,7 +36,6 @@ import org.xml.sax.SAXException; import net.sourceforge.pmd.RuleSet.RuleSetBuilder; import net.sourceforge.pmd.lang.Language; import net.sourceforge.pmd.lang.LanguageRegistry; -import net.sourceforge.pmd.lang.rule.MockRule; import net.sourceforge.pmd.lang.rule.RuleReference; import net.sourceforge.pmd.lang.rule.XPathRule; import net.sourceforge.pmd.rules.RuleFactory; @@ -697,13 +696,6 @@ public class RuleSetFactory { + ". PMD " + PMDVersion.getNextMajorRelease() + " will remove support for this deprecated Rule name usage."); } - } else if (referencedRule instanceof MockRule) { - if (LOG.isLoggable(Level.WARNING)) { - LOG.warning("Discontinue using Rule name " + otherRuleSetReferenceId - + " as it has been removed from PMD and no longer functions." - + " PMD " + PMDVersion.getNextMajorRelease() - + " will remove support for this Rule."); - } } else { if (LOG.isLoggable(Level.WARNING)) { LOG.warning("Discontinue using Rule name " + otherRuleSetReferenceId diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetTest.java index c66405e0cf..900355273b 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetTest.java @@ -31,16 +31,18 @@ import net.sourceforge.pmd.Report.ProcessingError; import net.sourceforge.pmd.RuleSet.RuleSetBuilder; import net.sourceforge.pmd.lang.Dummy2LanguageModule; import net.sourceforge.pmd.lang.DummyLanguageModule; +import net.sourceforge.pmd.lang.Language; import net.sourceforge.pmd.lang.LanguageRegistry; import net.sourceforge.pmd.lang.ast.DummyNode; import net.sourceforge.pmd.lang.ast.DummyRoot; import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.rule.MockRule; import net.sourceforge.pmd.lang.rule.RuleReference; import net.sourceforge.pmd.lang.rule.RuleTargetSelector; public class RuleSetTest { + private final Language dummyLang = LanguageRegistry.getLanguage(DummyLanguageModule.NAME); + @Test(expected = NullPointerException.class) public void testRuleSetRequiresName() { new RuleSetBuilder(new Random().nextLong()) @@ -169,7 +171,7 @@ public class RuleSetTest { } @Test - public void testApply0Rules() throws Exception { + public void testApply0Rules() { RuleSet ruleset = createRuleSetBuilder("ruleset").build(); verifyRuleSet(ruleset, 0, new HashSet()); } @@ -241,25 +243,23 @@ public class RuleSetTest { Rule rule = new MockRule(); - rule.setLanguage(LanguageRegistry.getLanguage(DummyLanguageModule.NAME)); assertFalse("Different languages should not apply", RuleSet.applies(rule, LanguageRegistry.getLanguage(Dummy2LanguageModule.NAME).getDefaultVersion())); - rule.setLanguage(LanguageRegistry.getLanguage(DummyLanguageModule.NAME)); assertTrue("Same language with no min/max should apply", - RuleSet.applies(rule, LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.5"))); + RuleSet.applies(rule, dummyLang.getVersion("1.5"))); - rule.setMinimumLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.5")); + rule.setMinimumLanguageVersion(dummyLang.getVersion("1.5")); assertTrue("Same language with valid min only should apply", - RuleSet.applies(rule, LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.5"))); + RuleSet.applies(rule, dummyLang.getVersion("1.5"))); - rule.setMaximumLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.6")); + rule.setMaximumLanguageVersion(dummyLang.getVersion("1.6")); assertTrue("Same language with valid min and max should apply", - RuleSet.applies(rule, LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.5"))); + RuleSet.applies(rule, dummyLang.getVersion("1.5"))); assertFalse("Same language with outside range of min/max should not apply", - RuleSet.applies(rule, LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.4"))); + RuleSet.applies(rule, dummyLang.getVersion("1.4"))); assertFalse("Same language with outside range of min/max should not apply", - RuleSet.applies(rule, LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.7"))); + RuleSet.applies(rule, dummyLang.getVersion("1.7"))); } @Test @@ -404,7 +404,6 @@ public class RuleSetTest { Rule rule = new FooRule(); rule.setName("FooRule1"); - rule.setLanguage(LanguageRegistry.getLanguage(DummyLanguageModule.NAME)); RuleSet ruleSet1 = createRuleSetBuilder("RuleSet1") .addRule(rule) .build(); @@ -420,7 +419,7 @@ public class RuleSetTest { Report r = new Report(); ctx.setReport(r); ctx.setSourceCodeFile(file); - ctx.setLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getDefaultVersion()); + ctx.setLanguageVersion(dummyLang.getDefaultVersion()); ruleSets.apply(makeCompilationUnits(), ctx); assertEquals("Violations", 2, r.getViolations().size()); @@ -442,7 +441,6 @@ public class RuleSetTest { public void copyConstructorDeepCopies() { Rule rule = new FooRule(); rule.setName("FooRule1"); - rule.setLanguage(LanguageRegistry.getLanguage(DummyLanguageModule.NAME)); RuleSet ruleSet1 = createRuleSetBuilder("RuleSet1") .addRule(rule) .build(); @@ -495,7 +493,7 @@ public class RuleSetTest { .build(); RuleContext context = new RuleContext(); context.setReport(new Report()); - context.setLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getDefaultVersion()); + context.setLanguageVersion(dummyLang.getDefaultVersion()); context.setSourceCodeFile(new File(RuleSetTest.class.getName() + ".ruleExceptionShouldBeReported")); context.setIgnoreExceptions(true); // the default ruleset.apply(makeCompilationUnits(), context); @@ -519,7 +517,7 @@ public class RuleSetTest { .build(); RuleContext context = new RuleContext(); context.setReport(new Report()); - context.setLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getDefaultVersion()); + context.setLanguageVersion(dummyLang.getDefaultVersion()); context.setSourceCodeFile(new File(RuleSetTest.class.getName() + ".ruleExceptionShouldBeThrownIfNotIgnored")); context.setIgnoreExceptions(false); ruleset.apply(makeCompilationUnits(), context); @@ -540,7 +538,7 @@ public class RuleSetTest { }).build(); RuleContext context = new RuleContext(); context.setReport(new Report()); - context.setLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getDefaultVersion()); + context.setLanguageVersion(dummyLang.getDefaultVersion()); context.setSourceCodeFile(new File(RuleSetTest.class.getName() + ".ruleExceptionShouldBeReported")); context.setIgnoreExceptions(true); // the default ruleset.apply(makeCompilationUnits(), context); @@ -581,7 +579,7 @@ public class RuleSetTest { }).build(); RuleContext context = new RuleContext(); context.setReport(new Report()); - context.setLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getDefaultVersion()); + context.setLanguageVersion(dummyLang.getDefaultVersion()); context.setSourceCodeFile(new File(RuleSetTest.class.getName() + ".ruleExceptionShouldBeReported")); context.setIgnoreExceptions(true); // the default RuleSets rulesets = new RuleSets(ruleset); @@ -595,4 +593,24 @@ public class RuleSetTest { assertEquals("There should be a violation", 1, context.getReport().getViolations().size()); } + + class MockRule extends net.sourceforge.pmd.lang.rule.MockRule { + + MockRule() { + super(); + setLanguage(dummyLang); + } + + MockRule(String name, String description, String message, String ruleSetName, RulePriority priority) { + super(name, description, message, ruleSetName, priority); + setLanguage(dummyLang); + } + + MockRule(String name, String description, String message, String ruleSetName) { + super(name, description, message, ruleSetName); + setLanguage(dummyLang); + } + + } + } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/MockRule.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/MockRule.java similarity index 83% rename from pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/MockRule.java rename to pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/MockRule.java index b613a336fc..0168ff1931 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/MockRule.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/MockRule.java @@ -1,4 +1,4 @@ -/** +/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ @@ -8,7 +8,6 @@ import static net.sourceforge.pmd.properties.constraints.NumericConstraints.inRa import net.sourceforge.pmd.RuleContext; import net.sourceforge.pmd.RulePriority; -import net.sourceforge.pmd.lang.LanguageRegistry; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.properties.PropertyFactory; @@ -18,17 +17,11 @@ import net.sourceforge.pmd.properties.PropertyFactory; * functional Rule is not needed. For example, during unit testing, or as an * editable surrogate used by IDE plugins. The Language of this Rule defaults to * Java. - * - * @deprecated This is not a supported API. You need the pmd-test module - * on your classpath, or pmd-core's test sources. This will be removed - * in 7.0.0 */ -@Deprecated public class MockRule extends AbstractRule { public MockRule() { super(); - setLanguage(LanguageRegistry.getLanguage("Dummy")); definePropertyDescriptor(PropertyFactory.intProperty("testIntProperty").desc("testIntProperty").require(inRange(1, 100)).defaultValue(1).build()); }