Search in sources :

Example 21 with RuleContext

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());
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSet(net.sourceforge.pmd.RuleSet) RuleContext(net.sourceforge.pmd.RuleContext) Report(net.sourceforge.pmd.Report) PMD(net.sourceforge.pmd.PMD) RuleSets(net.sourceforge.pmd.RuleSets) StringReader(java.io.StringReader) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule) Rule(net.sourceforge.pmd.Rule) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule) RuleViolation(net.sourceforge.pmd.RuleViolation) Test(org.junit.Test)

Example 22 with RuleContext

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();
    }
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) RuleContext(net.sourceforge.pmd.RuleContext) Report(net.sourceforge.pmd.Report) SourceCodeProcessor(net.sourceforge.pmd.SourceCodeProcessor) RuleViolation(net.sourceforge.pmd.RuleViolation) RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSets(net.sourceforge.pmd.RuleSets) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException) PMDException(net.sourceforge.pmd.PMDException) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration) Test(org.junit.Test)

Example 23 with RuleContext

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();
    }
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) RuleContext(net.sourceforge.pmd.RuleContext) Report(net.sourceforge.pmd.Report) SourceCodeProcessor(net.sourceforge.pmd.SourceCodeProcessor) RuleViolation(net.sourceforge.pmd.RuleViolation) RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSets(net.sourceforge.pmd.RuleSets) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException) PMDException(net.sourceforge.pmd.PMDException) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration) Test(org.junit.Test)

Example 24 with RuleContext

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;
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) RuleContext(net.sourceforge.pmd.RuleContext) RuleSets(net.sourceforge.pmd.RuleSets) Node(net.sourceforge.pmd.lang.ast.Node) ArrayList(java.util.ArrayList) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule)

Example 25 with RuleContext

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);
    }
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleContext(net.sourceforge.pmd.RuleContext) ResourceNotFoundException(org.codehaus.plexus.resource.loader.ResourceNotFoundException) MavenReportException(org.apache.maven.reporting.MavenReportException) IOException(java.io.IOException) FileResourceCreationException(org.codehaus.plexus.resource.loader.FileResourceCreationException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) MavenReportException(org.apache.maven.reporting.MavenReportException)

Aggregations

RuleContext (net.sourceforge.pmd.RuleContext)51 Test (org.junit.Test)19 RuleSets (net.sourceforge.pmd.RuleSets)17 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)15 Report (net.sourceforge.pmd.Report)14 Node (net.sourceforge.pmd.lang.ast.Node)13 RuleSet (net.sourceforge.pmd.RuleSet)11 StringReader (java.io.StringReader)9 RuleViolation (net.sourceforge.pmd.RuleViolation)9 IOException (java.io.IOException)8 PMDConfiguration (net.sourceforge.pmd.PMDConfiguration)8 PMDException (net.sourceforge.pmd.PMDException)8 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)8 ArrayList (java.util.ArrayList)7 SourceCodeProcessor (net.sourceforge.pmd.SourceCodeProcessor)7 RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)6 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)6 DummyNode (net.sourceforge.pmd.lang.ast.DummyNode)5 FooRule (net.sourceforge.pmd.FooRule)4 Parser (net.sourceforge.pmd.lang.Parser)4