Cleanup a test

This commit is contained in:
Clément Fournier 2020-03-22 05:07:23 +01:00
parent 961e78954a
commit 5067c79eb2
5 changed files with 32 additions and 46 deletions

View File

@ -22,7 +22,8 @@ import net.sourceforge.pmd.properties.PropertyFactory;
* Java.
*
* @deprecated This is not a supported API. You need the pmd-test module
* on your classpath. This will be removed in 7.0.0
* on your classpath, or pmd-core's test sources. This will be removed
* in 7.0.0
*/
@Deprecated
public class MockRule extends AbstractRule {

View File

@ -63,13 +63,9 @@ public enum XPathVersion {
*
* @return An XPath version
*
* @throws IllegalArgumentException If the argument doesn't match any known version
* @return Null if the argument is not a valid version
*/
public static XPathVersion fromString(String version) {
XPathVersion v = BY_NAME.get(version);
if (v == null) {
throw new IllegalArgumentException("Version '" + version + "' is not a valid XPath version");
}
return v;
public static XPathVersion ofId(String version) {
return BY_NAME.get(version);
}
}

View File

@ -26,6 +26,7 @@ import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
import net.sourceforge.pmd.lang.java.xpath.MetricFunction;
import net.sourceforge.pmd.lang.rule.XPathRule;
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
/**
* @author Clément Fournier
@ -40,8 +41,7 @@ public class XPathMetricFunctionTest {
private Rule makeXpathRuleFromXPath(String xpath) {
XPathRule rule = new XPathRule();
rule.setXPath(xpath);
XPathRule rule = new XPathRule(XPathVersion.XPATH_2_0, xpath);
rule.setMessage(VIOLATION_MESSAGE);
rule.setLanguage(LanguageRegistry.getLanguage(JavaLanguageModule.NAME));
return rule;

View File

@ -4,7 +4,7 @@
package net.sourceforge.pmd.lang.plsql;
import java.util.Arrays;
import static java.util.Collections.singletonList;
import org.junit.Assert;
import org.junit.Test;
@ -13,21 +13,19 @@ import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.plsql.ast.ASTInput;
import net.sourceforge.pmd.lang.rule.XPathRule;
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
/**
* Tests to use XPath rules with PLSQL.
*/
public class PLSQLXPathRuleTest extends AbstractPLSQLParserTst {
private ASTInput node = plsql.parse(
private final ASTInput node = plsql.parse(
"create or replace\n" + "package pkg_xpath_problem\n" + "AS\n" + " PROCEDURE pkg_minimal\n" + " IS\n"
+ " a_variable VARCHAR2(1);\n" + " BEGIN \n" + " --PRAGMA INLINE(output,'YES');\n"
+ " a_variable := 'Y' ;\n" + " END ;\n" + "end pkg_xpath_problem;\n" + "/\n" + "");
private RuleContext ctx = new RuleContext();
public PLSQLXPathRuleTest() {
ctx.setLanguageVersion(plsql.getDefaultVersion());
}
/**
@ -35,10 +33,7 @@ public class PLSQLXPathRuleTest extends AbstractPLSQLParserTst {
*/
@Test
public void testXPathRule1() {
XPathRule rule = createRule("1.0");
rule.apply(Arrays.asList(node), ctx);
Assert.assertEquals(2, ctx.getReport().treeSize());
testOnVersion(XPathVersion.XPATH_1_0);
}
/**
@ -46,10 +41,7 @@ public class PLSQLXPathRuleTest extends AbstractPLSQLParserTst {
*/
@Test
public void testXPathRule1Compatibility() {
XPathRule rule = createRule("1.0 compatibility");
rule.apply(Arrays.asList(node), ctx);
Assert.assertEquals(2, ctx.getReport().treeSize());
testOnVersion(XPathVersion.XPATH_1_0_COMPATIBILITY);
}
/**
@ -57,18 +49,21 @@ public class PLSQLXPathRuleTest extends AbstractPLSQLParserTst {
*/
@Test
public void testXPathRule2() {
XPathRule rule = createRule("2.0");
rule.apply(Arrays.asList(node), ctx);
Assert.assertEquals(2, ctx.getReport().treeSize());
testOnVersion(XPathVersion.XPATH_2_0);
}
private XPathRule createRule(String version) {
XPathRule rule = new XPathRule("//PrimaryPrefix");
private void testOnVersion(XPathVersion xpath10) {
XPathRule rule = new XPathRule(xpath10, "//PrimaryPrefix");
rule.setLanguage(LanguageRegistry.getLanguage(PLSQLLanguageModule.NAME));
rule.setVersion(version);
rule.setMessage("Test Violation");
return rule;
RuleContext ctx = new RuleContext();
ctx.setLanguageVersion(plsql.getDefaultVersion());
rule.apply(singletonList(node), ctx);
Assert.assertEquals(2, ctx.getReport().size());
}
}

View File

@ -6,14 +6,13 @@ package net.sourceforge.pmd.lang.scala.rule;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.rule.XPathRule;
import net.sourceforge.pmd.lang.rule.xpath.XPathRuleQuery;
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
import net.sourceforge.pmd.lang.scala.ScalaLanguageModule;
import net.sourceforge.pmd.lang.scala.ast.BaseScalaTest;
@ -21,22 +20,17 @@ public class XPathRuleTest extends BaseScalaTest {
private static final String SCALA_TEST = "/parserFiles/helloworld.scala";
XPathRule rule;
@Before
public void setUp() {
rule = new XPathRule();
rule.setLanguage(LanguageRegistry.getLanguage(ScalaLanguageModule.NAME));
rule.setMessage("XPath Rule Failed");
}
@Test
public void testPrintHelloWorld() {
String xpath = "//TermApply/TermName[@Image=\"println\"]";
rule.setXPath(xpath);
rule.setVersion(XPathRuleQuery.XPATH_2_0);
Report report = scala.getReportForResource(rule, SCALA_TEST);
Report report = evaluate(SCALA_TEST, "//TermApply/TermName[@Image=\"println\"]");
RuleViolation rv = report.iterator().next();
assertEquals(2, rv.getBeginLine());
}
private Report evaluate(String testSource, String xpath) {
XPathRule rule = new XPathRule(XPathVersion.XPATH_2_0, xpath);
rule.setLanguage(LanguageRegistry.getLanguage(ScalaLanguageModule.NAME));
rule.setMessage("XPath Rule Failed");
return scala.getReportForResource(rule, testSource);
}
}