Search in sources :

Example 1 with PMD

use of net.sourceforge.pmd.PMD in project pmd by pmd.

the class XPathRuleTest method getReportForTestString.

private static Report getReportForTestString(Rule r, String test) throws PMDException {
    PMD p = new PMD();
    RuleContext ctx = new RuleContext();
    Report report = new Report();
    ctx.setReport(report);
    ctx.setSourceCodeFilename("n/a");
    RuleSet rules = new RuleSetFactory().createSingleRuleRuleSet(r);
    p.getSourceCodeProcessor().processSourceCode(new StringReader(test), new RuleSets(rules), ctx);
    return report;
}
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)

Example 2 with PMD

use of net.sourceforge.pmd.PMD in project pmd by pmd.

the class RuleTst method runTestFromString.

public void runTestFromString(String code, Rule rule, Report report, LanguageVersion languageVersion, boolean isUseAuxClasspath) {
    try {
        PMD p = new PMD();
        p.getConfiguration().setDefaultLanguageVersion(languageVersion);
        p.getConfiguration().setIgnoreIncrementalAnalysis(true);
        if (isUseAuxClasspath) {
            // configure the "auxclasspath" option for unit testing
            p.getConfiguration().prependClasspath(".");
        }
        RuleContext ctx = new RuleContext();
        ctx.setReport(report);
        ctx.setSourceCodeFilename("n/a");
        ctx.setLanguageVersion(languageVersion);
        ctx.setIgnoreExceptions(false);
        RuleSet rules = new RuleSetFactory().createSingleRuleRuleSet(rule);
        p.getSourceCodeProcessor().processSourceCode(new StringReader(code), new RuleSets(rules), ctx);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSet(net.sourceforge.pmd.RuleSet) RuleContext(net.sourceforge.pmd.RuleContext) PMD(net.sourceforge.pmd.PMD) RuleSets(net.sourceforge.pmd.RuleSets) StringReader(java.io.StringReader) PMDException(net.sourceforge.pmd.PMDException) IOException(java.io.IOException) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 3 with PMD

use of net.sourceforge.pmd.PMD 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 4 with PMD

use of net.sourceforge.pmd.PMD in project pmd by pmd.

the class XPathMetricFunctionTest method getViolations.

private Iterator<RuleViolation> getViolations(Rule rule, String code) throws PMDException {
    PMD p = new PMD();
    RuleContext ctx = new RuleContext();
    Report report = new Report();
    ctx.setReport(report);
    ctx.setSourceCodeFilename("n/a");
    RuleSet rules = new RuleSetFactory().createSingleRuleRuleSet(rule);
    p.getSourceCodeProcessor().processSourceCode(new StringReader(code), new RuleSets(rules), ctx);
    return report.iterator();
}
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)

Aggregations

StringReader (java.io.StringReader)4 PMD (net.sourceforge.pmd.PMD)4 RuleContext (net.sourceforge.pmd.RuleContext)4 RuleSet (net.sourceforge.pmd.RuleSet)4 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)4 RuleSets (net.sourceforge.pmd.RuleSets)4 Report (net.sourceforge.pmd.Report)3 IOException (java.io.IOException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 PMDException (net.sourceforge.pmd.PMDException)1 Rule (net.sourceforge.pmd.Rule)1 RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)1 RuleViolation (net.sourceforge.pmd.RuleViolation)1 XPathRule (net.sourceforge.pmd.lang.rule.XPathRule)1 Test (org.junit.Test)1 SAXException (org.xml.sax.SAXException)1 SAXParseException (org.xml.sax.SAXParseException)1