Search in sources :

Example 1 with ResourceLoader

use of net.sourceforge.pmd.util.ResourceLoader in project pmd by pmd.

the class RuleSetFactoryTest method testExcludeWithMinimumPriority.

@Test
public void testExcludeWithMinimumPriority() throws RuleSetNotFoundException {
    RuleSetFactory rsf = new RuleSetFactory(new ResourceLoader(), RulePriority.HIGH, true, true);
    RuleSet ruleset = rsf.createRuleSet("net/sourceforge/pmd/rulesets/ruleset-minimum-priority-exclusion.xml");
    // no rules should be loaded
    assertEquals("Number of Rules", 0, ruleset.getRules().size());
    // now, load with default minimum priority
    rsf = new RuleSetFactory();
    ruleset = rsf.createRuleSet("net/sourceforge/pmd/rulesets/ruleset-minimum-priority-exclusion.xml");
    // only one rule, we have excluded one...
    assertEquals("Number of Rules", 1, ruleset.getRules().size());
    // rule is excluded
    assertNull(ruleset.getRuleByName("DummyBasicMockRule"));
    // that's the remaining rule
    assertNotNull(ruleset.getRuleByName("SampleXPathRule"));
}
Also used : ResourceLoader(net.sourceforge.pmd.util.ResourceLoader) Test(org.junit.Test)

Example 2 with ResourceLoader

use of net.sourceforge.pmd.util.ResourceLoader in project pmd by pmd.

the class RuleSetFactoryTest method testEmptyRuleSetReferencedShouldNotBeDeprecated.

/**
 * See https://github.com/pmd/pmd/issues/782
 * Empty ruleset should be interpreted as deprecated.
 *
 * @throws Exception
 *             any error
 */
@Test
public void testEmptyRuleSetReferencedShouldNotBeDeprecated() throws Exception {
    InMemoryLogHandler logHandler = new InMemoryLogHandler();
    Logger logger = Logger.getLogger(RuleSetFactory.class.getName());
    try {
        logger.addHandler(logHandler);
        RuleSetReferenceId ref = createRuleSetReferenceId("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "\n" + "<ruleset name=\"Custom ruleset\" xmlns=\"http://pmd.sourceforge.net/ruleset/2.0.0\"\n" + "    xmlns:xsi=\"http:www.w3.org/2001/XMLSchema-instance\"\n" + "    xsi:schemaLocation=\"http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd\">\n" + "    <description>Ruleset which references a empty ruleset</description>\n" + "\n" + "    <rule ref=\"rulesets/dummy/empty-ruleset.xml\" />\n" + "</ruleset>\n");
        RuleSetFactory ruleSetFactory = new RuleSetFactory(new ResourceLoader(), RulePriority.LOW, true, true);
        RuleSet ruleset = ruleSetFactory.createRuleSet(ref);
        assertEquals(0, ruleset.getRules().size());
        assertEquals(0, logHandler.getLogRecords().size());
    } finally {
        logger.removeHandler(logHandler);
    }
}
Also used : ResourceLoader(net.sourceforge.pmd.util.ResourceLoader) Logger(java.util.logging.Logger) Test(org.junit.Test)

Example 3 with ResourceLoader

use of net.sourceforge.pmd.util.ResourceLoader in project pmd by pmd.

the class RuleSetFactoryTest method testSetPriority.

@Test
public void testSetPriority() throws RuleSetNotFoundException {
    ResourceLoader rl = new ResourceLoader();
    RuleSetFactory rsf = new RuleSetFactory(rl, RulePriority.MEDIUM_HIGH, false, true);
    assertEquals(0, rsf.createRuleSet(createRuleSetReferenceId(SINGLE_RULE)).size());
    rsf = new RuleSetFactory(rl, RulePriority.MEDIUM_LOW, false, true);
    assertEquals(1, rsf.createRuleSet(createRuleSetReferenceId(SINGLE_RULE)).size());
}
Also used : ResourceLoader(net.sourceforge.pmd.util.ResourceLoader) Test(org.junit.Test)

Example 4 with ResourceLoader

use of net.sourceforge.pmd.util.ResourceLoader in project pmd by pmd.

the class RuleSetReferenceIdTest method testConstructorGivenHttpUrlSingleRuleInputStream.

@Test
public void testConstructorGivenHttpUrlSingleRuleInputStream() throws Exception {
    String path = "/profiles/export?format=pmd&language=java&name=Sonar%2520way";
    String completePath = path + "/DummyBasicMockRule";
    String hostpart = "http://localhost:" + wireMockRule.port();
    String basicRuleSet = IOUtils.toString(RuleSetReferenceId.class.getResourceAsStream("/rulesets/dummy/basic.xml"));
    stubFor(head(urlEqualTo(completePath)).willReturn(aResponse().withStatus(404)));
    stubFor(head(urlEqualTo(path)).willReturn(aResponse().withStatus(200).withHeader("Content-type", "text/xml")));
    stubFor(get(urlEqualTo(path)).willReturn(aResponse().withStatus(200).withHeader("Content-type", "text/xml").withBody(basicRuleSet)));
    RuleSetReferenceId ruleSetReferenceId = new RuleSetReferenceId("  " + hostpart + completePath + "  ");
    assertRuleSetReferenceId(true, hostpart + path, false, "DummyBasicMockRule", hostpart + completePath, ruleSetReferenceId);
    try (InputStream inputStream = ruleSetReferenceId.getInputStream(new ResourceLoader())) {
        String loaded = IOUtils.toString(inputStream, "UTF-8");
        assertEquals(basicRuleSet, loaded);
    }
    verify(1, headRequestedFor(urlEqualTo(completePath)));
    verify(1, headRequestedFor(urlEqualTo(path)));
    verify(1, getRequestedFor(urlEqualTo(path)));
    verify(0, getRequestedFor(urlEqualTo(completePath)));
    assertEquals(2, findAll(headRequestedFor(urlMatching(".*"))).size());
    assertEquals(1, findAll(getRequestedFor(urlMatching(".*"))).size());
}
Also used : ResourceLoader(net.sourceforge.pmd.util.ResourceLoader) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 5 with ResourceLoader

use of net.sourceforge.pmd.util.ResourceLoader in project pmd by pmd.

the class PMDTaskImpl method setupResourceLoader.

private ResourceLoader setupResourceLoader() {
    if (classpath == null) {
        classpath = new Path(project);
    }
    /*
         * 'basedir' is added to the path to make sure that relative paths such
         * as "<ruleset>resources/custom_ruleset.xml</ruleset>" still work when
         * ant is invoked from a different directory using "-f"
         */
    classpath.add(new Path(null, project.getBaseDir().toString()));
    project.log("Using the AntClassLoader: " + classpath, Project.MSG_VERBOSE);
    // must be true, otherwise you'll get ClassCastExceptions as classes
    // are loaded twice
    // and exist in multiple class loaders
    final boolean parentFirst = true;
    return new ResourceLoader(new AntClassLoader(Thread.currentThread().getContextClassLoader(), project, classpath, parentFirst));
}
Also used : Path(org.apache.tools.ant.types.Path) ResourceLoader(net.sourceforge.pmd.util.ResourceLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader)

Aggregations

ResourceLoader (net.sourceforge.pmd.util.ResourceLoader)13 Test (org.junit.Test)9 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 JavaUtilLoggingRule (net.sourceforge.pmd.junit.JavaUtilLoggingRule)2 MockRule (net.sourceforge.pmd.lang.rule.MockRule)2 Renderer (net.sourceforge.pmd.renderers.Renderer)2 DataSource (net.sourceforge.pmd.util.datasource.DataSource)2 File (java.io.File)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 SQLException (java.sql.SQLException)1 LinkedList (java.util.LinkedList)1 StringTokenizer (java.util.StringTokenizer)1 Logger (java.util.logging.Logger)1 Report (net.sourceforge.pmd.Report)1 RuleContext (net.sourceforge.pmd.RuleContext)1