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"));
}
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);
}
}
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());
}
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());
}
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));
}
Aggregations