forked from phoedos/pmd
progress, remove ContextualizedTest
This commit is contained in:
@@ -8,6 +8,7 @@ import static net.sourceforge.pmd.util.CollectionUtil.listOf;
|
||||
|
||||
import net.sourceforge.pmd.lang.BaseLanguageModule;
|
||||
import net.sourceforge.pmd.lang.Language;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
|
||||
import apex.jorje.services.Version;
|
||||
|
||||
@@ -18,13 +19,12 @@ public class ApexLanguageModule extends BaseLanguageModule {
|
||||
public static final String NAME = "Apex";
|
||||
public static final String TERSE_NAME = "apex";
|
||||
|
||||
private ApexLanguageModule() {
|
||||
public ApexLanguageModule() {
|
||||
super(NAME, null, TERSE_NAME, listOf("cls", "trigger"));
|
||||
addVersion(String.valueOf((int) Version.CURRENT.getExternal()), new ApexHandler(), true);
|
||||
}
|
||||
|
||||
// fixme check syntax of ServiceLoader
|
||||
public static Language provide() {
|
||||
return INSTANCE;
|
||||
public static Language getInstance() {
|
||||
return LanguageRegistry.PMD.getLanguageByFullName(NAME);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,10 +12,6 @@ import net.sourceforge.pmd.lang.rule.AbstractRule;
|
||||
public abstract class AbstractApexRule extends AbstractRule
|
||||
implements ApexParserVisitor {
|
||||
|
||||
public AbstractApexRule() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(Node target, RuleContext ctx) {
|
||||
target.acceptVisitor(this, ctx);
|
||||
|
||||
@@ -17,6 +17,7 @@ public class FooRule extends AbstractApexRule {
|
||||
|
||||
public FooRule() {
|
||||
setMessage("No Foo allowed");
|
||||
setLanguage(ApexLanguageModule.getInstance());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -25,6 +25,10 @@ public class SuppressWarningsTest extends ApexParserTestBase {
|
||||
|
||||
private static class BarRule extends AbstractApexRule {
|
||||
|
||||
BarRule() {
|
||||
setLanguage(ApexLanguageModule.getInstance());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return "a message";
|
||||
|
||||
@@ -4,9 +4,7 @@
|
||||
|
||||
package net.sourceforge.pmd.lang.apex.ast;
|
||||
|
||||
import net.sourceforge.pmd.PmdContextualizedTest;
|
||||
|
||||
public class ApexParserTestBase extends PmdContextualizedTest {
|
||||
public class ApexParserTestBase {
|
||||
|
||||
protected final ApexParsingHelper apex = ApexParsingHelper.DEFAULT.withResourceContext(getClass());
|
||||
|
||||
|
||||
@@ -9,10 +9,8 @@ import static net.sourceforge.pmd.lang.ast.test.TestUtilsKt.assertSize;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.sourceforge.pmd.Report;
|
||||
import net.sourceforge.pmd.lang.apex.ApexLanguageModule;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ApexParserTestBase;
|
||||
import net.sourceforge.pmd.lang.rule.XPathRule;
|
||||
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
|
||||
|
||||
/**
|
||||
* @author daniels
|
||||
@@ -20,10 +18,7 @@ import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
|
||||
public class ApexXPathRuleTest extends ApexParserTestBase {
|
||||
|
||||
private XPathRule makeXPath(String expression) {
|
||||
XPathRule rule = new XPathRule(XPathVersion.XPATH_2_0, expression);
|
||||
rule.setLanguage(ApexLanguageModule.INSTANCE);
|
||||
rule.setMessage("XPath Rule Failed");
|
||||
return rule;
|
||||
return apex.newXpathRule(expression);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -650,6 +650,7 @@ public class RuleSet implements ChecksumAware {
|
||||
public static boolean applies(Rule rule, LanguageVersion languageVersion) {
|
||||
final LanguageVersion min = rule.getMinimumLanguageVersion();
|
||||
final LanguageVersion max = rule.getMaximumLanguageVersion();
|
||||
Objects.requireNonNull(rule.getLanguage(), "Rule has no language!");
|
||||
return rule.getLanguage().equals(languageVersion.getLanguage())
|
||||
&& (min == null || min.compareTo(languageVersion) <= 0)
|
||||
&& (max == null || max.compareTo(languageVersion) >= 0);
|
||||
|
||||
@@ -50,6 +50,7 @@ public class HtmlJavaRuleTest {
|
||||
return super.visit(node, data);
|
||||
}
|
||||
};
|
||||
rule.setLanguage(HtmlParsingHelper.DEFAULT.getLanguage());
|
||||
List<RuleViolation> violations = runRule(LIGHTNING_WEB_COMPONENT, rule);
|
||||
Assert.assertEquals(2, violations.size());
|
||||
Assert.assertEquals(4, violations.get(0).getBeginLine());
|
||||
|
||||
@@ -1,29 +1,47 @@
|
||||
/**
|
||||
/*
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
import net.sourceforge.pmd.lang.LanguageVersionDiscoverer;
|
||||
import net.sourceforge.pmd.lang.jsp.ast.AbstractJspNodesTst;
|
||||
|
||||
public class LanguageVersionDiscovererTest extends AbstractJspNodesTst {
|
||||
class LanguageVersionDiscovererTest extends AbstractJspNodesTst {
|
||||
|
||||
/**
|
||||
* Test on JSP file.
|
||||
*/
|
||||
@Test
|
||||
public void testJspFile() {
|
||||
LanguageVersionDiscoverer discoverer = new LanguageVersionDiscoverer(languageRegistry());
|
||||
File jspFile = new File("/path/to/MyPage.jsp");
|
||||
LanguageVersion languageVersion = discoverer.getDefaultLanguageVersionForFile(jspFile);
|
||||
assertEquals("LanguageVersion must be JSP!", jsp.getLanguage().getDefaultVersion(), languageVersion);
|
||||
void testParseJsp() {
|
||||
testLanguageIsJsp("sample.jsp");
|
||||
testLanguageIsJsp("sample.jspx");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTag() {
|
||||
testLanguageIsJsp("sample.tag");
|
||||
}
|
||||
|
||||
|
||||
void testLanguageIsJsp(String first) {
|
||||
Assertions.assertEquals(jsp.getLanguage().getDefaultVersion(),
|
||||
getLanguageVersion(Paths.get(first)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseWrong() {
|
||||
Assertions.assertNotEquals(jsp.getLanguage().getDefaultVersion(),
|
||||
getLanguageVersion(Paths.get("sample.xxx")));
|
||||
}
|
||||
|
||||
private LanguageVersion getLanguageVersion(Path jspFile) {
|
||||
LanguageVersionDiscoverer discoverer = new LanguageVersionDiscoverer(LanguageRegistry.PMD);
|
||||
return discoverer.getDefaultLanguageVersionForFile(jspFile.toFile());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,8 @@
|
||||
|
||||
package net.sourceforge.pmd.lang.jsp;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
import net.sourceforge.pmd.lang.LanguageVersionDiscoverer;
|
||||
import net.sourceforge.pmd.lang.jsp.ast.AbstractJspNodesTst;
|
||||
|
||||
/**
|
||||
@@ -45,27 +39,4 @@ public class JspParserTest extends AbstractJspNodesTst {
|
||||
jsp.parse("<label><input type='checkbox' checked name=cheese disabled=''> Cheese</label>");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseJsp() {
|
||||
testInternalJspFile(Paths.get("sample.jsp").toFile());
|
||||
testInternalJspFile(Paths.get("sample.jspx").toFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseTag() {
|
||||
testInternalJspFile(Paths.get("sample.tag").toFile());
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void testParseWrong() {
|
||||
testInternalJspFile(Paths.get("sample.xxx").toFile());
|
||||
}
|
||||
|
||||
private void testInternalJspFile(File jspFile) {
|
||||
LanguageVersionDiscoverer discoverer = LanguageVersionDiscovererTest.createForcedDiscoverer(languageRegistry());
|
||||
LanguageVersion languageVersion = discoverer.getDefaultLanguageVersionForFile(jspFile);
|
||||
Assert.assertEquals("LanguageVersion must be JSP!",
|
||||
jsp.getLanguage().getDefaultVersion(), languageVersion);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,11 +4,8 @@
|
||||
|
||||
package net.sourceforge.pmd.lang.jsp.ast;
|
||||
|
||||
import net.sourceforge.pmd.PmdContextualizedTest;
|
||||
public abstract class AbstractJspNodesTst {
|
||||
|
||||
public abstract class AbstractJspNodesTst extends PmdContextualizedTest {
|
||||
|
||||
protected JspParsingHelper jsp = JspParsingHelper.DEFAULT.withResourceContext(getClass())
|
||||
.withLanguageRegistry(languageRegistry());
|
||||
protected JspParsingHelper jsp = JspParsingHelper.DEFAULT.withResourceContext(getClass());
|
||||
|
||||
}
|
||||
|
||||
@@ -11,24 +11,16 @@ import org.junit.Test;
|
||||
import net.sourceforge.pmd.Report;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleViolation;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.jsp.JspLanguageModule;
|
||||
import net.sourceforge.pmd.lang.rule.XPathRule;
|
||||
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
|
||||
import net.sourceforge.pmd.testframework.RuleTst;
|
||||
|
||||
public class XPathJspRuleTest extends RuleTst {
|
||||
public class XPathJspRuleTest extends AbstractJspNodesTst {
|
||||
|
||||
/**
|
||||
* Test matching a XPath expression against a JSP source.
|
||||
*/
|
||||
@Test
|
||||
public void testExpressionMatching() {
|
||||
Rule rule = new XPathRule(XPathVersion.XPATH_3_1, XPATH_EXPRESSION);
|
||||
rule.setMessage("Test");
|
||||
rule.setLanguage(LanguageRegistry.getLanguage(JspLanguageModule.NAME));
|
||||
|
||||
Report report = JspParsingHelper.DEFAULT.executeRule(rule, MATCH);
|
||||
Rule rule = jsp.newXpathRule("//Element [@Name='hr']");
|
||||
Report report = jsp.executeRule(rule, "<html><hr/></html>");
|
||||
|
||||
assertEquals("One violation expected!", 1, report.getViolations().size());
|
||||
|
||||
@@ -36,7 +28,4 @@ public class XPathJspRuleTest extends RuleTst {
|
||||
assertEquals(1, rv.getBeginLine());
|
||||
}
|
||||
|
||||
private static final String MATCH = "<html><hr/></html>";
|
||||
|
||||
private static final String XPATH_EXPRESSION = "//Element [@Name='hr']";
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ abstract class BaseParsingHelper<Self : BaseParsingHelper<Self, T>, T : RootNode
|
||||
params.languageRegistry.getLanguageByFullName(langName)
|
||||
?: run {
|
||||
val langNames = params.languageRegistry.commaSeparatedList { it.name }
|
||||
throw AssertionError("'$langName' is not a supported language (available $langNames")
|
||||
throw AssertionError("'$langName' is not a supported language (available $langNames)")
|
||||
}
|
||||
|
||||
|
||||
@@ -222,6 +222,8 @@ abstract class BaseParsingHelper<Self : BaseParsingHelper<Self, T>, T : RootNode
|
||||
code: String,
|
||||
fileName: String = "testfile.${language.extensions[0]}"
|
||||
): Report {
|
||||
if (rule.language == null)
|
||||
rule.language = language
|
||||
val config = PMDConfiguration().apply {
|
||||
suppressMarker = params.suppressMarker
|
||||
setDefaultLanguageVersion(defaultVersion)
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
/*
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd;
|
||||
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
|
||||
/**
|
||||
* A base class for PMD tests that rely on a {@link LanguageRegistry}.
|
||||
*/
|
||||
public class PmdContextualizedTest {
|
||||
private final LanguageRegistry registry;
|
||||
|
||||
public PmdContextualizedTest() {
|
||||
this.registry = LanguageRegistry.PMD;
|
||||
}
|
||||
|
||||
public final LanguageRegistry languageRegistry() {
|
||||
return registry;
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,6 @@ import org.xml.sax.SAXException;
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
import net.sourceforge.pmd.PMDConfiguration;
|
||||
import net.sourceforge.pmd.PmdContextualizedTest;
|
||||
import net.sourceforge.pmd.Report;
|
||||
import net.sourceforge.pmd.Report.GlobalReportBuilderListener;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
@@ -51,6 +50,7 @@ import net.sourceforge.pmd.RuleSet;
|
||||
import net.sourceforge.pmd.RuleSetLoadException;
|
||||
import net.sourceforge.pmd.RuleSetLoader;
|
||||
import net.sourceforge.pmd.RuleViolation;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
import net.sourceforge.pmd.lang.document.Chars;
|
||||
import net.sourceforge.pmd.lang.document.TextFile;
|
||||
@@ -63,7 +63,7 @@ import net.sourceforge.pmd.util.IOUtil;
|
||||
/**
|
||||
* Advanced methods for test cases
|
||||
*/
|
||||
public abstract class RuleTst extends PmdContextualizedTest {
|
||||
public abstract class RuleTst {
|
||||
private final DocumentBuilder documentBuilder;
|
||||
|
||||
/** Use a single classloader for all tests. */
|
||||
@@ -562,7 +562,7 @@ public abstract class RuleTst extends PmdContextualizedTest {
|
||||
version = null;
|
||||
terseName = terseNameAndVersion;
|
||||
}
|
||||
return languageRegistry().getLanguageVersionById(terseName, version);
|
||||
return LanguageRegistry.PMD.getLanguageVersionById(terseName, version);
|
||||
}
|
||||
|
||||
private String getNodeValue(Element parentElm, String nodeName, boolean required) {
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
package net.sourceforge.pmd.testframework;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -15,17 +15,17 @@ import java.util.Arrays;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import net.sourceforge.pmd.PmdContextualizedTest;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
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;
|
||||
|
||||
public class RuleTstTest extends PmdContextualizedTest {
|
||||
private LanguageVersion dummyLanguage = languageRegistry().findLanguageByTerseName("dummy").getDefaultVersion();
|
||||
public class RuleTstTest {
|
||||
private LanguageVersion dummyLanguage = DummyLanguageModule.getInstance().getDefaultVersion();
|
||||
|
||||
private Rule rule = mock(Rule.class);
|
||||
|
||||
@@ -43,12 +43,12 @@ public class RuleTstTest extends PmdContextualizedTest {
|
||||
|
||||
verify(rule).start(any(RuleContext.class));
|
||||
verify(rule).end(any(RuleContext.class));
|
||||
verify(rule).getLanguage();
|
||||
verify(rule, times(2)).getTargetSelector();
|
||||
verify(rule, atLeastOnce()).getLanguage();
|
||||
verify(rule, atLeastOnce()).getTargetSelector();
|
||||
verify(rule).getMinimumLanguageVersion();
|
||||
verify(rule).getMaximumLanguageVersion();
|
||||
verify(rule).apply(any(Node.class), any(RuleContext.class));
|
||||
verify(rule, times(4)).getName();
|
||||
verify(rule, atLeastOnce()).getName();
|
||||
verify(rule).getPropertiesByPropertyDescriptor();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,13 +4,10 @@
|
||||
|
||||
package net.sourceforge.pmd.lang.vf.ast;
|
||||
|
||||
import net.sourceforge.pmd.PmdContextualizedTest;
|
||||
|
||||
public abstract class AbstractVfTest extends PmdContextualizedTest {
|
||||
public abstract class AbstractVfTest {
|
||||
|
||||
protected final VfParsingHelper vf =
|
||||
VfParsingHelper.DEFAULT
|
||||
.withResourceContext(getClass())
|
||||
.withLanguageRegistry(languageRegistry());
|
||||
.withResourceContext(getClass());
|
||||
|
||||
}
|
||||
|
||||
@@ -10,8 +10,6 @@ import org.junit.Test;
|
||||
|
||||
import net.sourceforge.pmd.Report;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.xml.XmlLanguageModule;
|
||||
import net.sourceforge.pmd.lang.xml.XmlParsingHelper;
|
||||
|
||||
public class XmlXPathRuleTest {
|
||||
@@ -36,7 +34,7 @@ public class XmlXPathRuleTest {
|
||||
|
||||
private Rule makeXPath(String expression, String nsUri) {
|
||||
DomXPathRule rule = new DomXPathRule(expression, nsUri);
|
||||
rule.setLanguage(LanguageRegistry.getLanguage(XmlLanguageModule.NAME));
|
||||
rule.setLanguage(xml.getLanguage());
|
||||
rule.setMessage("XPath Rule Failed");
|
||||
return rule;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user