use of net.sourceforge.pmd.RuleContext in project pmd by pmd.
the class XPathJspRuleTest method testExpressionMatching.
/**
* Test matching a XPath expression against a JSP source.
* @throws PMDException
*/
@Test
public void testExpressionMatching() throws PMDException {
Rule rule = new XPathRule(XPATH_EXPRESSION);
rule.setMessage("Test");
rule.setLanguage(LanguageRegistry.getLanguage(JspLanguageModule.NAME));
RuleSet rules = new RuleSetFactory().createSingleRuleRuleSet(rule);
RuleContext ctx = new RuleContext();
Report report = new Report();
ctx.setReport(report);
ctx.setSourceCodeFilename("n/a");
ctx.setLanguageVersion(LanguageRegistry.getLanguage(JspLanguageModule.NAME).getDefaultVersion());
PMD p = new PMD();
p.getSourceCodeProcessor().processSourceCode(new StringReader(MATCH), new RuleSets(rules), ctx);
assertEquals("One violation expected!", 1, report.size());
RuleViolation rv = report.iterator().next();
assertEquals(1, rv.getBeginLine());
}
use of net.sourceforge.pmd.RuleContext in project pmd-eclipse-plugin by pmd.
the class BasicPMDTest method testRunPmdJdk15.
/**
* Let see with Java 1.5
*/
@Test
public void testRunPmdJdk15() {
try {
PMDConfiguration configuration = new PMDConfiguration();
configuration.setDefaultLanguageVersion(LanguageRegistry.findLanguageVersionByTerseName("java 1.5"));
final String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}";
final RuleContext context = new RuleContext();
context.setSourceCodeFilename("foo.java");
context.setReport(new Report());
final RuleSet codeStyleRuleSet = new RuleSetFactory().createRuleSet("category/java/codestyle.xml/UnnecessaryReturn");
RuleSets rSets = new RuleSets(codeStyleRuleSet);
new SourceCodeProcessor(configuration).processSourceCode(new StringDataSource(sourceCode).getInputStream(), rSets, context);
final Iterator<RuleViolation> iter = context.getReport().iterator();
Assert.assertTrue("There should be at least one violation", iter.hasNext());
final RuleViolation violation = iter.next();
Assert.assertEquals(violation.getRule().getName(), "UnnecessaryReturn");
Assert.assertEquals(3, violation.getBeginLine());
} catch (final RuleSetNotFoundException e) {
e.printStackTrace();
Assert.fail();
} catch (final PMDException e) {
e.printStackTrace();
Assert.fail();
}
}
use of net.sourceforge.pmd.RuleContext in project pmd-eclipse-plugin by pmd.
the class BasicPMDTest method testRunPmdJdk13.
/**
* One first thing the plugin must be able to do is to run PMD
*/
@Test
public void testRunPmdJdk13() {
try {
PMDConfiguration configuration = new PMDConfiguration();
configuration.setDefaultLanguageVersion(LanguageRegistry.findLanguageVersionByTerseName("java 1.3"));
final String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}";
final RuleContext context = new RuleContext();
context.setSourceCodeFilename("foo.java");
context.setReport(new Report());
final RuleSet codeStyleRuleSet = new RuleSetFactory().createRuleSet("category/java/codestyle.xml/UnnecessaryReturn");
RuleSets rSets = new RuleSets(codeStyleRuleSet);
new SourceCodeProcessor(configuration).processSourceCode(new StringDataSource(sourceCode).getInputStream(), rSets, context);
final Iterator<RuleViolation> iter = context.getReport().iterator();
Assert.assertTrue("There should be at least one violation", iter.hasNext());
final RuleViolation violation = iter.next();
Assert.assertEquals(violation.getRule().getName(), "UnnecessaryReturn");
Assert.assertEquals(3, violation.getBeginLine());
} catch (final RuleSetNotFoundException e) {
e.printStackTrace();
Assert.fail();
} catch (final PMDException e) {
e.printStackTrace();
Assert.fail();
}
}
use of net.sourceforge.pmd.RuleContext in project pmd-eclipse-plugin by pmd.
the class XPathEvaluator method evaluate.
/**
* Builds a temporary XPathRule using the query provided and executes it
* against the source. Returns a list of nodes detailing any issues found
* with it.
*
* @param source
* @param xpathQuery
* @param xpathVersion
* @return
* @throws ParseException
*/
public List<Node> evaluate(String source, String xpathQuery, String xpathVersion) throws ParseException {
Node c = getCompilationUnit(source);
final List<Node> results = new ArrayList<Node>();
XPathRule xpathRule = new XPathRule() {
public void addViolation(Object data, Node node, String arg) {
results.add(node);
}
};
xpathRule.setMessage("");
xpathRule.setLanguage(getLanguageVersion().getLanguage());
xpathRule.setProperty(XPathRule.XPATH_DESCRIPTOR, xpathQuery);
xpathRule.setProperty(XPathRule.VERSION_DESCRIPTOR, xpathVersion);
RuleSet ruleSet = RuleSetUtil.newSingle(xpathRule);
RuleSets ruleSets = new RuleSets(ruleSet);
RuleContext ruleContext = new RuleContext();
ruleContext.setLanguageVersion(getLanguageVersion());
List<Node> nodes = new ArrayList<Node>(1);
nodes.add(c);
ruleSets.apply(nodes, ruleContext, xpathRule.getLanguage());
return results;
}
use of net.sourceforge.pmd.RuleContext in project maven-plugins by apache.
the class PmdReport method processFilesWithPMD.
private void processFilesWithPMD(PMDConfiguration pmdConfiguration, List<DataSource> dataSources) throws MavenReportException {
RuleSetFactory ruleSetFactory = new RuleSetFactory(RuleSetFactory.class.getClassLoader(), RulePriority.valueOf(this.minimumPriority), false, true);
RuleContext ruleContext = new RuleContext();
try {
getLog().debug("Executing PMD...");
PMD.processFiles(pmdConfiguration, ruleSetFactory, dataSources, ruleContext, Arrays.<Renderer>asList(renderer));
if (getLog().isDebugEnabled()) {
getLog().debug("PMD finished. Found " + renderer.getViolations().size() + " violations.");
}
} catch (Exception e) {
String message = "Failure executing PMD: " + e.getLocalizedMessage();
if (!skipPmdError) {
throw new MavenReportException(message, e);
}
getLog().warn(message, e);
}
}
Aggregations